Skip to content

Commit

Permalink
now traverses over dates correctly and should work for other builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 10, 2011
1 parent a504425 commit bb8d1b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,26 @@ Traverse.prototype.clone = function () {
}

if (typeof src === 'object' && src !== null) {
var dst = Array.isArray(src) ? [] : Object.create(src.__proto__);
var dst;

if (Array.isArray(src)) {
dst = [];
}
else if (src instanceof Date) {
dst = new Date(src);
}
else if (src instanceof Boolean) {
dst = new Boolean(src);
}
else if (src instanceof Number) {
dst = new Number(src);
}
else if (src instanceof String) {
dst = new String(src);
}
else {
dst = Object.create(src.__proto__);
}

parents.push(src);
nodes.push(dst);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "traverse",
"version" : "0.3.1",
"version" : "0.3.2",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
7 changes: 6 additions & 1 deletion test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ exports.dateMap = function () {
if (typeof node === 'number') this.update(node + 100);
});

assert.eql(res, { x : obj.x, y : 110, z : 5 });
assert.ok(obj.x !== res.x);
assert.eql(res, {
x : obj.x,
y : 110,
z : 105,
});
};

0 comments on commit bb8d1b5

Please sign in to comment.