Skip to content

Commit

Permalink
subexpr test passes by checking if update() happened
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 11, 2011
1 parent be2b574 commit 44e731b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ function walk (root, cb, immutable) {
var node = immutable ? copy(node_) : node_;
var modifiers = {};

var updated = false;

var state = {
node : node,
node_ : node_,
Expand All @@ -204,6 +206,8 @@ function walk (root, cb, immutable) {
level : path.length,
circular : null,
update : function (x) {
updated = true;

if (!state.isRoot) {
state.parent.node[state.key] = x;
}
Expand Down Expand Up @@ -252,7 +256,7 @@ function walk (root, cb, immutable) {
if (modifiers.before) modifiers.before.call(state, state.node);

if (typeof state.node == 'object'
&& state.node !== null && !state.circular) {
&& state.node !== null && !state.circular && !updated) {
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.3.8",
"version" : "0.4.0",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
7 changes: 7 additions & 0 deletions test/subexpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ exports.subexpr = function () {
this.update([ x - 0.1, x, x + 0.1 ]);
}
});

assert.deepEqual(obj, [ 'a', 4, 'b', 5, 'c', 6 ]);
assert.deepEqual(r, [
'a', [ 3.9, 4, 4.1 ],
'b', [ 4.9, 5, 5.1 ],
'c', [ 5.9, 6, 6.1 ],
]);
};

0 comments on commit 44e731b

Please sign in to comment.