diff --git a/index.js b/index.js index 7dac223..5cbb2cb 100644 --- a/index.js +++ b/index.js @@ -194,24 +194,23 @@ function walk (root, cb, immutable) { var node = immutable ? copy(node_) : node_; var modifiers = {}; - var updated = false; + var keepGoing = true; var state = { node : node, node_ : node_, path : [].concat(path), - parent : parents.slice(-1)[0], + parent : parents[parents.length - 1], key : path.slice(-1)[0], isRoot : path.length === 0, level : path.length, circular : null, - update : function (x) { - updated = true; - + update : function (x, stopHere) { if (!state.isRoot) { state.parent.node[state.key] = x; } state.node = x; + if (stopHere) keepGoing = false; }, 'delete' : function () { delete state.parent.node[state.key]; @@ -255,8 +254,10 @@ function walk (root, cb, immutable) { if (ret !== undefined && state.update) state.update(ret); if (modifiers.before) modifiers.before.call(state, state.node); + if (!keepGoing) return state; + if (typeof state.node == 'object' - && state.node !== null && !state.circular && !updated) { + && state.node !== null && !state.circular) { parents.push(state); var keys = Object.keys(state.node); diff --git a/package.json b/package.json index 4a13bd4..460e81b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "traverse", - "version" : "0.4.0", + "version" : "0.4.1", "description" : "Traverse and transform objects by visiting every node on a recursive walk", "author" : "James Halliday", "license" : "MIT/X11", diff --git a/test/subexpr.js b/test/subexpr.js index 39a422d..a4cc859 100644 --- a/test/subexpr.js +++ b/test/subexpr.js @@ -5,7 +5,7 @@ exports.subexpr = function () { var obj = [ 'a', 4, 'b', 5, 'c', 6 ]; var r = traverse(obj).map(function (x) { if (typeof x === 'number') { - this.update([ x - 0.1, x, x + 0.1 ]); + this.update([ x - 0.1, x, x + 0.1 ], true); } });