4 Traverse and transform objects by visiting every node on a recursive walk.
6 [![build status](https://secure.travis-ci.org/substack/js-traverse.png)](http://travis-ci.org/substack/js-traverse)
11 transform negative numbers in-place
12 -----------------------------------
17 var traverse = require('traverse');
18 var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
20 traverse(obj).forEach(function (x) {
21 if (x < 0) this.update(x + 128);
29 [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
37 var traverse = require('traverse');
43 d : { e : [7,8], f : 9 },
46 var leaves = traverse(obj).reduce(function (acc, x) {
47 if (this.isLeaf) acc.push(x);
56 [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
58 scrub circular references
59 -------------------------
64 var traverse = require('traverse');
66 var obj = { a : 1, b : 2, c : [ 3, 4 ] };
69 var scrubbed = traverse(obj).map(function (x) {
70 if (this.circular) this.remove()
72 console.dir(scrubbed);
77 { a: 1, b: 2, c: [ 3, 4 ] }
82 Each method that takes an `fn` uses the context documented below in the context
88 Execute `fn` for each node in the object and return a new object with the
89 results of the walk. To update nodes in the result use `this.update(value)`.
94 Execute `fn` for each node in the object but unlike `.map()`, when
95 `this.update()` is called it updates the object in-place.
100 For each node in the object, perform a
101 [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function))
102 with the return value of `fn(acc, node)`.
104 If `acc` isn't specified, `acc` is set to the root object for the first step
105 and the root element is skipped.
110 Return an `Array` of every possible non-cyclic path in the object.
111 Paths are `Array`s of string keys.
116 Return an `Array` of every node in the object.
121 Create a deep clone of the object.
126 Get the element at the array `path`.
131 Set the element at the array `path` to `value`.
136 Return whether the element at the array `path` exists.
141 Each method that takes a callback has a context (its `this` object) with these
147 The present node on the recursive walk
152 An array of string keys from the root to the present node
157 The context of the node's parent.
158 This is `undefined` for the root node.
163 The name of the key of the present node in its parent.
164 This is `undefined` for the root node.
166 this.isRoot, this.notRoot
167 -------------------------
169 Whether the present node is the root node
171 this.isLeaf, this.notLeaf
172 -------------------------
174 Whether or not the present node is a leaf node (has no children)
179 Depth of the node within the traversal
184 If the node equals one of its parents, the `circular` attribute is set to the
185 context of that parent and the traversal progresses no deeper.
187 this.update(value, stopHere=false)
188 ----------------------------------
190 Set a new value for the present node.
192 All the elements in `value` will be recursively traversed unless `stopHere` is
195 this.remove(stopHere=false)
198 Remove the current element from the output. If the node is in an Array it will
199 be spliced off. Otherwise it will be deleted from its parent.
201 this.delete(stopHere=false)
204 Delete the current element from its parent in the output. Calls `delete` even on
210 Call this function before any of the children are traversed.
212 You can assign into `this.keys` here to traverse in a custom order.
217 Call this function after any of the children are traversed.
222 Call this function before each of the children are traversed.
227 Call this function after each of the children are traversed.
233 Using [npm](http://npmjs.org) do:
235 $ npm install traverse
240 Using [expresso](http://github.com/visionmedia/expresso) do:
244 100% wahoo, your stuff is not broken!
249 Use [browserify](https://github.com/substack/node-browserify) to run traverse in
252 traverse has been tested and works with:
254 * Internet Explorer 5.5, 6.0, 7.0, 8.0, 9.0