From bb8d1b567489a94507941f4a6bda2224ed2d9692 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Sun, 10 Apr 2011 02:20:57 -0700 Subject: [PATCH] now traverses over dates correctly and should work for other builtins --- index.js | 21 ++++++++++++++++++++- package.json | 2 +- test/date.js | 7 ++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d72265c..1084c0a 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/package.json b/package.json index af3c763..24d7104 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/date.js b/test/date.js index f48c973..72630e7 100644 --- a/test/date.js +++ b/test/date.js @@ -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, + }); };