Skip to content

Commit

Permalink
Partially merge '833-paglias-toJS-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Apr 6, 2013
2 parents 7fbb70c + 873dcc8 commit 6ccad62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
24 changes: 22 additions & 2 deletions spec/mappingHelperBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,31 @@ describe('Mapping helpers', function() {
expect(result.someProp.owner).toEqual(result);
});

it('ko.toJS should treat Date instances as primitives (and not walk their subproperties)', function () {
it('ko.toJS should treat Date, Number, String and Boolean instances as primitives (and not walk their subproperties)', function () {
var date = new Date();
var result = ko.toJS({ due: ko.observable(date) });
var string = new String();
var number = new Number();
var booleanValue = new Boolean(); // 'boolean' is a resever word in Javascript

var result = ko.toJS({
due: ko.observable(date),
string: ko.observable(string),
number: ko.observable(number),
booleanValue: ko.observable(booleanValue)
});

expect(result.due instanceof Date).toEqual(true);
expect(result.due).toEqual(date);

console.log(string instanceof String, result.string instanceof String);
expect(result.string instanceof String).toEqual(true);
expect(result.string).toEqual(string);

expect(result.number instanceof Number).toEqual(true);
expect(result.number).toEqual(number);

expect(result.booleanValue instanceof Boolean).toEqual(true);
expect(result.booleanValue).toEqual(booleanValue);
});

it('ko.toJSON should unwrap everything and then stringify', function() {
Expand Down
30 changes: 17 additions & 13 deletions src/subscribables/mappingHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
visitedObjects = visitedObjects || new objectLookup();

rootObject = mapInputCallback(rootObject);
var canHaveProperties = (typeof rootObject == "object") && (rootObject !== null) && (rootObject !== undefined) && (!(rootObject instanceof Date));
var canHaveProperties = (typeof rootObject == "object") && (rootObject !== null) && (rootObject !== undefined) && (!(rootObject instanceof Date)) && (!(rootObject instanceof String)) && (!(rootObject instanceof Number)) && (!(rootObject instanceof Boolean));
if (!canHaveProperties)
return rootObject;

Expand Down Expand Up @@ -70,21 +70,25 @@
};

function objectLookup() {
var keys = [];
var values = [];
this.save = function(key, value) {
var existingIndex = ko.utils.arrayIndexOf(keys, key);
this.keys = [];
this.values = [];
};

objectLookup.prototype = {
constructor: objectLookup,
save: function(key, value) {
var existingIndex = ko.utils.arrayIndexOf(this.keys, key);
if (existingIndex >= 0)
values[existingIndex] = value;
this.values[existingIndex] = value;
else {
keys.push(key);
values.push(value);
this.keys.push(key);
this.values.push(value);
}
};
this.get = function(key) {
var existingIndex = ko.utils.arrayIndexOf(keys, key);
return (existingIndex >= 0) ? values[existingIndex] : undefined;
};
},
get: function(key) {
var existingIndex = ko.utils.arrayIndexOf(this.keys, key);
return (existingIndex >= 0) ? this.values[existingIndex] : undefined;
}
};
})();

Expand Down

0 comments on commit 6ccad62

Please sign in to comment.