UNPKG

4.86 kBMarkdownView Raw
1traverse
2========
3
4Traverse and transform objects by visiting every node on a recursive walk.
5
6[![build status](https://secure.travis-ci.org/substack/js-traverse.png)](http://travis-ci.org/substack/js-traverse)
7
8examples
9========
10
11transform negative numbers in-place
12-----------------------------------
13
14negative.js
15
16````javascript
17var traverse = require('traverse');
18var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
19
20traverse(obj).forEach(function (x) {
21 if (x < 0) this.update(x + 128);
22});
23
24console.dir(obj);
25````
26
27Output:
28
29 [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
30
31collect leaf nodes
32------------------
33
34leaves.js
35
36````javascript
37var traverse = require('traverse');
38
39var obj = {
40 a : [1,2,3],
41 b : 4,
42 c : [5,6],
43 d : { e : [7,8], f : 9 },
44};
45
46var leaves = traverse(obj).reduce(function (acc, x) {
47 if (this.isLeaf) acc.push(x);
48 return acc;
49}, []);
50
51console.dir(leaves);
52````
53
54Output:
55
56 [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
57
58scrub circular references
59-------------------------
60
61scrub.js:
62
63````javascript
64var traverse = require('traverse');
65
66var obj = { a : 1, b : 2, c : [ 3, 4 ] };
67obj.c.push(obj);
68
69var scrubbed = traverse(obj).map(function (x) {
70 if (this.circular) this.remove()
71});
72console.dir(scrubbed);
73````
74
75output:
76
77 { a: 1, b: 2, c: [ 3, 4 ] }
78
79methods
80=======
81
82Each method that takes an `fn` uses the context documented below in the context
83section.
84
85.map(fn)
86--------
87
88Execute `fn` for each node in the object and return a new object with the
89results of the walk. To update nodes in the result use `this.update(value)`.
90
91.forEach(fn)
92------------
93
94Execute `fn` for each node in the object but unlike `.map()`, when
95`this.update()` is called it updates the object in-place.
96
97.reduce(fn, acc)
98----------------
99
100For each node in the object, perform a
101[left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function))
102with the return value of `fn(acc, node)`.
103
104If `acc` isn't specified, `acc` is set to the root object for the first step
105and the root element is skipped.
106
107.paths()
108--------
109
110Return an `Array` of every possible non-cyclic path in the object.
111Paths are `Array`s of string keys.
112
113.nodes()
114--------
115
116Return an `Array` of every node in the object.
117
118.clone()
119--------
120
121Create a deep clone of the object.
122
123.get(path)
124----------
125
126Get the element at the array `path`.
127
128.set(path, value)
129-----------------
130
131Set the element at the array `path` to `value`.
132
133.has(path)
134----------
135
136Return whether the element at the array `path` exists.
137
138context
139=======
140
141Each method that takes a callback has a context (its `this` object) with these
142attributes:
143
144this.node
145---------
146
147The present node on the recursive walk
148
149this.path
150---------
151
152An array of string keys from the root to the present node
153
154this.parent
155-----------
156
157The context of the node's parent.
158This is `undefined` for the root node.
159
160this.key
161--------
162
163The name of the key of the present node in its parent.
164This is `undefined` for the root node.
165
166this.isRoot, this.notRoot
167-------------------------
168
169Whether the present node is the root node
170
171this.isLeaf, this.notLeaf
172-------------------------
173
174Whether or not the present node is a leaf node (has no children)
175
176this.level
177----------
178
179Depth of the node within the traversal
180
181this.circular
182-------------
183
184If the node equals one of its parents, the `circular` attribute is set to the
185context of that parent and the traversal progresses no deeper.
186
187this.update(value, stopHere=false)
188----------------------------------
189
190Set a new value for the present node.
191
192All the elements in `value` will be recursively traversed unless `stopHere` is
193true.
194
195this.remove(stopHere=false)
196-------------
197
198Remove the current element from the output. If the node is in an Array it will
199be spliced off. Otherwise it will be deleted from its parent.
200
201this.delete(stopHere=false)
202-------------
203
204Delete the current element from its parent in the output. Calls `delete` even on
205Arrays.
206
207this.before(fn)
208---------------
209
210Call this function before any of the children are traversed.
211
212You can assign into `this.keys` here to traverse in a custom order.
213
214this.after(fn)
215--------------
216
217Call this function after any of the children are traversed.
218
219this.pre(fn)
220------------
221
222Call this function before each of the children are traversed.
223
224this.post(fn)
225-------------
226
227Call this function after each of the children are traversed.
228
229
230install
231=======
232
233Using [npm](http://npmjs.org) do:
234
235 $ npm install traverse
236
237test
238====
239
240Using [expresso](http://github.com/visionmedia/expresso) do:
241
242 $ expresso
243
244 100% wahoo, your stuff is not broken!
245
246in the browser
247==============
248
249Use [browserify](https://github.com/substack/node-browserify) to run traverse in
250the browser.
251
252traverse has been tested and works with:
253
254* Internet Explorer 5.5, 6.0, 7.0, 8.0, 9.0
255* Firefox 3.5
256* Chrome 6.0
257* Opera 10.6
258* Safari 5.0
259
\No newline at end of file