From 423066e821070ffb8f8a29022175b08f4bfc5d99 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Fri, 18 Feb 2011 16:38:15 -0800 Subject: [PATCH] fix for isRoot, check path.length, not node === root --- index.js | 10 +++++----- package.json | 2 +- test/circular.js | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 6bc4d97..d72265c 100644 --- a/index.js +++ b/index.js @@ -5,13 +5,11 @@ function Traverse (obj) { } Traverse.prototype.map = function (cb) { - var obj = Traverse.clone(this.value); - walk(obj, cb); - return obj; + return walk(this.clone(), cb); }; Traverse.prototype.forEach = function (cb) { - walk(this.value, cb); + this.value = walk(this.value, cb); return this.value; }; @@ -84,7 +82,7 @@ function walk (root, cb) { path : [].concat(path), parent : parents.slice(-1)[0], key : path.slice(-1)[0], - isRoot : node === root, + isRoot : path.length === 0, level : path.length, circular : null, update : function (x) { @@ -149,6 +147,8 @@ function walk (root, cb) { return state; })(root); + + return root; } Object.keys(Traverse.prototype).forEach(function (key) { diff --git a/package.json b/package.json index d9ef4b2..af3c763 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "traverse", - "version" : "0.3.0", + "version" : "0.3.1", "description" : "Traverse and transform objects by visiting every node on a recursive walk", "author" : "James Halliday", "license" : "MIT/X11", diff --git a/test/circular.js b/test/circular.js index c735d18..d0c7b44 100644 --- a/test/circular.js +++ b/test/circular.js @@ -55,7 +55,7 @@ exports.doubleCirc = function () { assert.eql(circs.length, 2); }; -exports.circDubUpForEach = function () { +exports.circDubForEach = function () { var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; obj.y[2] = obj; obj.x.push(obj.y); @@ -67,13 +67,15 @@ exports.circDubUpForEach = function () { assert.eql(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); }; -exports.circDubUpMap = function () { +exports.circDubMap = function () { var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; obj.y[2] = obj; obj.x.push(obj.y); var c = Traverse(obj).map(function (x) { - if (this.circular) this.update('...'); + if (this.circular) { + this.update('...'); + } }); assert.eql(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] });