Skip to content

Commit

Permalink
fix for isRoot, check path.length, not node === root
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 19, 2011
1 parent 6a6cb49 commit 423066e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -149,6 +147,8 @@ function walk (root, cb) {

return state;
})(root);

return root;
}

Object.keys(Traverse.prototype).forEach(function (key) {
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.0",
"version" : "0.3.1",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
8 changes: 5 additions & 3 deletions test/circular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, '...' ] });
Expand Down

0 comments on commit 423066e

Please sign in to comment.