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