-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only update when this.update is still around, tests for Traverse.func…
…tions
- Loading branch information
James Halliday
committed
Sep 4, 2010
1 parent
bc68fa5
commit cc59d56
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|