-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.d.ts
329 lines (309 loc) · 9.06 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import type { PathArray, Query } from 'wild-wild-parser'
export type Target = object | unknown[]
export type { Query }
export interface Options {
/**
* When using unions or deep wildcards, a query might match both a property
* and some of its children.
* This option decides whether the returned properties should be sorted from
* children to parents, or the reverse.
*
* @default false
*
* @example
* ```js
* const target = { user: { name: 'Alice' } }
* list(target, 'user.**') // [{ name: 'Alice' }, 'Alice']
* list(target, 'user.**', { childFirst: true }) // ['Alice', { name: 'Alice' }]
* ```
*/
readonly childFirst?: boolean
/**
* When using unions or deep wildcards, a query might match both a property
* and some of its children.
* When `true`, only roots are matched. In other words, a matching property is
* ignored if one of its parents also matches.
*
* @default false
*
* @example
* ```js
* const target = { user: { name: 'Alice' } }
* list(target, 'user.**') // [{ name: 'Alice' }, 'Alice']
* list(target, 'user.**', { roots: true }) // [{ name: 'Alice' }]
* ```
*/
readonly roots?: boolean
/**
* When using unions or deep wildcards, a query might match both a property
* and some of its children.
* When `true`, only leaves are matched. In other words, a matching property
* is ignored if one of its children also matches.
*
* @default false
*
* @example
* ```js
* const target = { user: { name: 'Alice' } }
* list(target, 'user.**') // [{ name: 'Alice' }, 'Alice']
* list(target, 'user.**', { leaves: true }) // ['Alice']
* ```
*/
readonly leaves?: boolean
/**
* When returning sibling object properties, sort them by the lexigographic
* order of their names (not values).
*
* @default false
*
* @example
* ```js
* const target = { lastName: 'Doe', firstName: 'John' }
* list(target, '*') // ['Doe', 'John']
* list(target, '*', { sort: true }) // ['John', 'Doe']
* ```
*/
readonly sort?: boolean
/**
* When `false`, properties not defined in the target are ignored.
*
* @default `false` with `list|iterate()`, `true` with `set()`
*
* @example
* ```js
* const target = {}
*
* set(target, 'name', 'Alice') // { name: 'Alice' }
* set(target, 'name', 'Alice', { missing: false }) // {}
*
* list(target, 'name') // []
* list(target, 'name', { missing: true, entries: true })
* // [{ value: undefined, path: ['name'], missing: true }]
* ```
*/
readonly missing?: boolean
/**
* By default, properties' values are returned.
* When `true`, objects with the following shape are returned instead:
* - `value` `any`: property's value
* - `path` `Path`: property's full path
* - `missing` `boolean`: whether the property is missing from the target
*
* @default false
*
* @example
* ```js
* const target = { firstName: 'Alice', lastName: 'Smith' }
* list(target, '*') // ['Alice', 'Smith']
* list(target, '*', { entries: true })
* // [
* // { value: 'Alice', path: ['firstName'], missing: false },
* // { value: 'Smith', path: ['lastName'], missing: false },
* // ]
* ```
*/
readonly entries?: boolean
/**
* If `true`, wildcards do not recurse on arrays.
* Array items can still be matched by using indices or slices.
*
* @default false
*
* @example
* ```js
* const target = [{ name: 'Alice' }, { name: 'Bob' }]
* list(target, '**')
* // [
* // [{ name: 'Alice' }, { name: 'Bob' }],
* // { name: 'Alice' },
* // 'Alice',
* // { name: 'Bob' },
* // 'Bob',
* // ]
* list(target, '**', { shallowArrays: true })
* // [
* // [{ name: 'Alice' }, { name: 'Bob' }],
* // ]
* ```
*/
readonly shallowArrays?: boolean
/**
* Unless `true`, wildcards and regexps ignore properties of objects that are
* not plain objects (like class instances, errors or functions).
* Those can still be matched by using their property name.
*
* @default false
*
* @example
* ```js
* const target = { user: new User({ name: 'Alice' }) }
* list(target, 'user.*') // []
* list(target, 'user.*', { classes: true }) // ['Alice']
* ```
*/
readonly classes?: boolean
/**
* By default, wildcards and regexps ignore properties that are either
* inherited or not enumerable. Those can still be matched by using their
* property name.
* When `true`, inherited properties are not ignored, but not enumerable ones
* still are.
*
* @default false
*/
readonly inherited?: boolean
/**
* By default, the target is deeply cloned. When `true`, it is directly
* mutated instead, which is faster but has side effects.
*
* @default false
*
* @example
* ```js
* const target = {}
* console.log(set(target, 'name', 'Alice')) // { name: 'Alice' }
* console.log(target) // {}
* console.log(set(target, 'name', 'Alice', { mutate: true })) // { name: 'Alice' }
* console.log(target) // { name: 'Alice' }
* ```
*/
readonly mutate?: boolean
}
type OptionsWithEntries = Options & { readonly entries: true }
type Value = unknown
/**
* Property entry
*/
export interface Entry {
/**
* Property's value
*/
value: Value
/**
* Property's full path
*/
path: PathArray
/**
* Whether the property is missing from the target
*/
missing: boolean
}
/**
* Return whether the `query` matches any property.
*
* @example
* ```js
* const target = { settings: { lastName: undefined, colors: ['red', 'blue'] } }
*
* has(target, 'settings.firstName') // false
* has(target, ['settings', 'firstName']) // false
* has(target, 'settings.lastName') // true
* ```
*/
export function has(target: Target, query: Query, options?: Options): boolean
/**
* Return the first property matching the `query`.
*
* @example
* ```js
* const target = { settings: { colors: ['red', 'blue'] } }
* get(target, 'settings.colors.0') // 'red'
* get(target, ['settings', 'colors', 0]) // 'red'
* ```
*/
export function get<T extends Options>(
target: Target,
query: Query,
options?: T,
): (T extends OptionsWithEntries ? Entry : Value) | undefined
/**
* Return all properties matching the `query`, as an array.
*
* @example
* ```js
* const target = {
* userOne: { firstName: 'John', lastName: 'Doe', age: 72 },
* userTwo: { firstName: 'Alice', colors: ['red', 'blue', 'yellow'] },
* }
*
* list(target, 'userOne.firstName userTwo.colors.0') // ['John', 'red']
* list(target, [
* ['userOne', 'firstName'],
* ['userTwo', 'colors', 0],
* ]) // ['John', 'red']
*
* list(target, 'userOne./Name/') // ['John', 'Doe']
* list(target, ['userOne', /Name/]) // ['John', 'Doe']
*
* list(target, 'userTwo.colors.*') // ['red', 'blue', 'yellow']
* list(target, 'userTwo.colors.0:2') // ['red', 'blue']
* list(target, '**.firstName') // ['John', 'Alice']
* list(target, 'userOne.*', { entries: true })
* // [
* // { value: 'John', path: ['userOne', 'firstName'], missing: false },
* // { value: 'Doe', path: ['userOne', 'lastName'], missing: false },
* // { value: 72, path: ['userOne', 'age'], missing: false },
* // ]
* ```
*/
export function list<T extends Options>(
target: Target,
query: Query,
options?: T,
): (T extends OptionsWithEntries ? Entry : Value)[]
/**
* Return all properties matching the `query`, as an iterable.
* This is slower than `list()` but uses less memory.
*
* @example
* ```js
* const target = { settings: { colors: ['red', 'blue'] } }
*
* for (const color of iterate(target, 'settings.colors.*')) {
* console.log(color) // 'red', 'blue'
* }
* ```
*/
export function iterate<T extends Options>(
target: Target,
query: Query,
options?: T,
): Generator<T extends OptionsWithEntries ? Entry : Value>
/**
* Delete all properties matching the `query`. The return value is a deep clone
* unless the `mutate` option is `true`.
*
* @example
* ```js
* const target = { user: { firstName: 'John', lastName: 'Doe', age: 72 } }
*
* remove(target, 'user.lastName') // { user: { firstName: 'John', age: 72 } }
* remove(target, 'user./Name/') // { user: { age: 72 } }
* remove(target, ['user', /Name/]) // { user: { age: 72 } }
* ```
*/
export function remove(target: Target, query: Query, options?: Options): Target
/**
* Sets all properties matching the `query`. The return value is a deep clone
* unless the `mutate` option is `true`.
*
* @example
* ```js
* const target = { colors: ['red', 'blue'] }
*
* set(target, 'colors.0', 'yellow') // ['yellow', 'blue']
* set(target, ['colors', 0], 'yellow') // ['yellow', 'blue']
* set(target, 'colors.-1', 'yellow') // ['red', 'yellow']
* set(target, 'colors.-0', 'yellow') // ['red', 'blue', 'yellow']
* set(target, 'colors.*', 'yellow') // ['yellow', 'yellow']
* set({}, 'user.0.color', 'red') // { user: [{ color: 'red' }] }
* set({}, 'user.0.color', 'red', { missing: false }) // {}
* ```
*/
export function set(
target: Target,
query: Query,
value: Value,
options?: Options,
): Target
export function isObject(value: unknown, classes: boolean): boolean