UNPKG

6.95 kBTypeScriptView Raw
1declare function traverse<T>(obj: T, options?: traverse.TraverseOptions): traverse.Traverse<T>;
2
3declare namespace traverse {
4 /**
5 * Get the element at the array `path`.
6 */
7 function get(obj: any, path: string[]): any;
8
9 /**
10 * Return whether the element at the array `path` exists.
11 */
12 function has(obj: any, path: string[]): boolean;
13
14 /**
15 * Set the element at the array `path` to `value`.
16 */
17 function set(obj: any, path: string[], value: any): any;
18
19 /**
20 * Execute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `this.update(value)`.
21 */
22 function map(obj: any, cb: (this: TraverseContext, v: any) => void): any;
23
24 /**
25 * Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place.
26 */
27 function forEach(obj: any, cb: (this: TraverseContext, v: any) => void): any;
28
29 /**
30 * For each node in the object, perform a [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function)) with the return value of `fn(acc, node)`.
31 *
32 * If `init` isn't specified, `init` is set to the root object for the first step and the root element is skipped.
33 */
34 function reduce(obj: any, cb: (this: TraverseContext, acc: any, v: any) => void, init?: any): any;
35
36 /**
37 * Return an `Array` of every possible non-cyclic path in the object.
38 * Paths are `Array`s of string keys.
39 */
40 function paths(obj: any): string[][];
41
42 /**
43 * Return an `Array` of every node in the object.
44 */
45 function nodes(obj: any): any[];
46
47 /**
48 * Create a deep clone of the object.
49 */
50 function clone<T>(obj: T): T;
51
52 interface Traverse<T> {
53 /**
54 * Get the element at the array `path`.
55 */
56 get(path: string[]): any;
57
58 /**
59 * Return whether the element at the array `path` exists.
60 */
61 has(path: string[]): boolean;
62
63 /**
64 * Set the element at the array `path` to `value`.
65 */
66 set(path: string[], value: any): any;
67
68 /**
69 * Execute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `this.update(value)`.
70 */
71 map(cb: (this: TraverseContext, v: any) => void): any;
72
73 /**
74 * Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place.
75 */
76 forEach(cb: (this: TraverseContext, v: any) => void): any;
77
78 /**
79 * For each node in the object, perform a [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function)) with the return value of `fn(acc, node)`.
80 *
81 * If `init` isn't specified, `init` is set to the root object for the first step and the root element is skipped.
82 */
83 reduce(cb: (this: TraverseContext, acc: any, v: any) => void, init?: any): any;
84
85 /**
86 * Return an `Array` of every possible non-cyclic path in the object.
87 * Paths are `Array`s of string keys.
88 */
89 paths(): string[][];
90
91 /**
92 * Return an `Array` of every node in the object.
93 */
94 nodes(): any[];
95
96 /**
97 * Create a deep clone of the object.
98 */
99 clone(): T;
100 }
101
102 interface TraverseOptions {
103 /**
104 * If true, does not alter the original object
105 */
106 immutable?: boolean;
107
108 /**
109 * If false, removes all symbols from traversed objects
110 *
111 * @default false
112 */
113 includeSymbols?: boolean;
114 }
115
116 interface TraverseContext {
117 /**
118 * The present node on the recursive walk
119 */
120 node: any;
121
122 /**
123 * An array of string keys from the root to the present node
124 */
125 path: string[];
126
127 /**
128 * The context of the node's parent.
129 * This is `undefined` for the root node.
130 */
131 parent: TraverseContext | undefined;
132
133 /**
134 * The contexts of the node's parents.
135 */
136 parents: TraverseContext[];
137
138 /**
139 * The name of the key of the present node in its parent.
140 * This is `undefined` for the root node.
141 */
142 key: string | undefined;
143
144 /**
145 * Whether the present node is the root node
146 */
147 isRoot: boolean;
148 /**
149 * Whether the present node is not the root node
150 */
151 notRoot: boolean;
152
153 /**
154 * Whether or not the present node is a leaf node (has no children)
155 */
156 isLeaf: boolean;
157 /**
158 * Whether or not the present node is not a leaf node (has children)
159 */
160 notLeaf: boolean;
161
162 /**
163 * Depth of the node within the traversal
164 */
165 level: number;
166
167 /**
168 * If the node equals one of its parents, the `circular` attribute is set to the context of that parent and the traversal progresses no deeper.
169 */
170 circular: TraverseContext | undefined;
171
172 /**
173 * Set a new value for the present node.
174 *
175 * All the elements in `value` will be recursively traversed unless `stopHere` is true (false by default).
176 */
177 update(value: any, stopHere?: boolean): void;
178
179 /**
180 * Remove the current element from the output. If the node is in an Array it will be spliced off. Otherwise it will be deleted from its parent.
181 */
182 remove(stopHere?: boolean): void;
183
184 /**
185 * Delete the current element from its parent in the output. Calls `delete` even on Arrays.
186 */
187 delete(stopHere?: boolean): void;
188
189 /**
190 * Object keys of the node.
191 */
192 keys: string[] | null;
193
194 /**
195 * Call this function before all of the children are traversed.
196 * You can assign into `this.keys` here to traverse in a custom order.
197 */
198 before(callback: (this: TraverseContext, value: any) => void): void;
199
200 /**
201 * Call this function after all of the children are traversed.
202 */
203 after(callback: (this: TraverseContext, value: any) => void): void;
204
205 /**
206 * Call this function before each of the children are traversed.
207 */
208 pre(callback: (this: TraverseContext, child: any, key: any) => void): void;
209
210 /**
211 * Call this function after each of the children are traversed.
212 */
213 post(callback: (this: TraverseContext, child: any) => void): void;
214
215 /**
216 * Stops traversal entirely.
217 */
218 stop(): void;
219
220 /**
221 * Prevents traversing descendents of the current node.
222 */
223 block(): void;
224 }
225}
226
227export = traverse;