Skip to content

Commit

Permalink
only update when this.update is still around, tests for Traverse.func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
James Halliday committed Sep 4, 2010
1 parent bc68fa5 commit cc59d56
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function Traverse (refObj) {

// use return values to update if defined
var ret = f.call(state, node);
if (ret !== undefined) state.update(ret);
if (ret !== undefined && state.update) state.update(ret);

if (typeof state.node == 'object'
&& state.node !== null && !state.circular) {
Expand Down
42 changes: 42 additions & 0 deletions test/interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node
var sys = require('sys');
var Traverse = require('traverse');

exports['interface map'] = function (assert) {
var obj = { a : [ 5,6,7 ], b : { c : [8] } };

assert.equal(
Traverse.paths(obj)
.sort()
.map(function (path) { return path.join('/') })
.slice(1)
.join(' ')
,
'a a/0 a/1 a/2 b b/c b/c/0'
);

assert.equal(
Traverse.nodes(obj)
.map(function (node) { return sys.inspect(node) })
.join(' ')
,
'{ a: [ 5, 6, 7 ], b: { c: [ 8 ] } } [ 5, 6, 7 ] 5 6 7 { c: [ 8 ] } [ 8 ] 8'
);

assert.equal(
sys.inspect(Traverse.map(obj, function (node) {
if (typeof node == 'number') {
return node + 1000;
}
else if (Array.isArray(node)) {
return node.join(' ');
}
})),
"{ a: '5 6 7', b: { c: '8' } }"
);

var nodes = 0;
Traverse.forEach(obj, function (node) { nodes ++ });
assert.equal(nodes, 8);
};

0 comments on commit cc59d56

Please sign in to comment.