Skip to content

Commit

Permalink
moved stop behavior in updates into a second keepGoing argument
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 11, 2011
1 parent 44e731b commit 1d31897
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
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.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",
Expand Down
2 changes: 1 addition & 1 deletion test/subexpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down

0 comments on commit 1d31897

Please sign in to comment.