@ix-xs/node-comfort 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +190 -0
- package/README.md +136 -549
- package/index.js +80 -35
- package/package.json +160 -33
- package/src/Arr.js +887 -419
- package/src/Async.js +585 -0
- package/src/Cache.js +329 -0
- package/src/Checker.js +971 -539
- package/src/Cli.js +1034 -0
- package/src/Color.js +415 -0
- package/src/Crypto.js +707 -0
- package/src/Emitter.js +289 -0
- package/src/Env.js +555 -0
- package/src/FS.js +1483 -894
- package/src/Func.js +670 -353
- package/src/Http.js +538 -0
- package/src/Id.js +384 -201
- package/src/Logger.js +739 -378
- package/src/Num.js +844 -361
- package/src/Obj.js +855 -372
- package/src/SQLite.js +1308 -1005
- package/src/Schema.js +1586 -0
- package/src/Str.js +983 -436
- package/src/Sys.js +511 -0
- package/src/Time.js +1260 -422
- package/src/Utils.js +321 -325
- package/src/errors.js +292 -0
- package/types/index.d.ts +693 -526
- package/types/src/Arr.d.ts +455 -145
- package/types/src/Arr.d.ts.map +1 -1
- package/types/src/Async.d.ts +321 -0
- package/types/src/Async.d.ts.map +1 -0
- package/types/src/Cache.d.ts +195 -0
- package/types/src/Cache.d.ts.map +1 -0
- package/types/src/Checker.d.ts +501 -235
- package/types/src/Checker.d.ts.map +1 -1
- package/types/src/Cli.d.ts +582 -0
- package/types/src/Cli.d.ts.map +1 -0
- package/types/src/Color.d.ts +294 -0
- package/types/src/Color.d.ts.map +1 -0
- package/types/src/Crypto.d.ts +534 -0
- package/types/src/Crypto.d.ts.map +1 -0
- package/types/src/Emitter.d.ts +170 -0
- package/types/src/Emitter.d.ts.map +1 -0
- package/types/src/Env.d.ts +382 -0
- package/types/src/Env.d.ts.map +1 -0
- package/types/src/FS.d.ts +686 -238
- package/types/src/FS.d.ts.map +1 -1
- package/types/src/Func.d.ts +380 -135
- package/types/src/Func.d.ts.map +1 -1
- package/types/src/Http.d.ts +374 -0
- package/types/src/Http.d.ts.map +1 -0
- package/types/src/Id.d.ts +149 -68
- package/types/src/Id.d.ts.map +1 -1
- package/types/src/Logger.d.ts +363 -46
- package/types/src/Logger.d.ts.map +1 -1
- package/types/src/Num.d.ts +473 -137
- package/types/src/Num.d.ts.map +1 -1
- package/types/src/Obj.d.ts +390 -104
- package/types/src/Obj.d.ts.map +1 -1
- package/types/src/SQLite.d.ts +646 -309
- package/types/src/SQLite.d.ts.map +1 -1
- package/types/src/Schema.d.ts +940 -0
- package/types/src/Schema.d.ts.map +1 -0
- package/types/src/Str.d.ts +569 -166
- package/types/src/Str.d.ts.map +1 -1
- package/types/src/Sys.d.ts +269 -0
- package/types/src/Sys.d.ts.map +1 -0
- package/types/src/Time.d.ts +663 -204
- package/types/src/Time.d.ts.map +1 -1
- package/types/src/Utils.d.ts +154 -102
- package/types/src/Utils.d.ts.map +1 -1
- package/types/src/errors.d.ts +292 -0
- package/types/src/errors.d.ts.map +1 -0
- package/types/index.d.ts.map +0 -1
package/types/src/Arr.d.ts
CHANGED
|
@@ -1,252 +1,562 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* How to get a value out of an item: a property name, or a function
|
|
3
|
+
* `(item, index) => value`.
|
|
4
|
+
*/
|
|
5
|
+
export type Iteratee<T> = (T extends object ? keyof T & (string | number) : never) | ((item: T, index: number) => unknown);
|
|
6
|
+
/**
|
|
7
|
+
* A sort key for `sortBy()`: an iteratee, or `[iteratee, "asc" | "desc"]`
|
|
8
|
+
* to give that key its own direction.
|
|
9
|
+
*/
|
|
10
|
+
export type SortKey<T> = Iteratee<T> | [Iteratee<T>, "asc" | "desc"];
|
|
11
|
+
/**
|
|
12
|
+
* Options for `sortBy()`.
|
|
13
|
+
*/
|
|
14
|
+
export type SortByOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Direction for keys that don't set their own. Defaults to `"asc"`.
|
|
17
|
+
*/
|
|
18
|
+
order?: "asc" | "desc" | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Sort text like a person would: ignore case, respect accents, `"item2"` before `"item10"`.
|
|
21
|
+
*/
|
|
22
|
+
natural?: boolean | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Language used by `natural`. Defaults to the system's.
|
|
25
|
+
*/
|
|
26
|
+
locale?: string | undefined;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A page returned by `paginate()`.
|
|
30
|
+
*/
|
|
31
|
+
export type Page<T> = {
|
|
32
|
+
/**
|
|
33
|
+
* The items on this page.
|
|
34
|
+
*/
|
|
35
|
+
items: T[];
|
|
36
|
+
/**
|
|
37
|
+
* The page number, starting at 1.
|
|
38
|
+
*/
|
|
39
|
+
page: number;
|
|
40
|
+
perPage: number;
|
|
41
|
+
/**
|
|
42
|
+
* Total number of items.
|
|
43
|
+
*/
|
|
44
|
+
total: number;
|
|
45
|
+
/**
|
|
46
|
+
* Number of pages, at least 1.
|
|
47
|
+
*/
|
|
48
|
+
pages: number;
|
|
49
|
+
hasPrev: boolean;
|
|
50
|
+
hasNext: boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Splits an array into groups of `size` items. The last group can be shorter.
|
|
3
54
|
*
|
|
4
55
|
* @example
|
|
5
|
-
*
|
|
56
|
+
* arr.chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
57
|
+
* arr.chunk(emails, 100).forEach(sendBatch);
|
|
6
58
|
*
|
|
7
|
-
* @
|
|
8
|
-
* @param {
|
|
9
|
-
* @
|
|
59
|
+
* @template T
|
|
60
|
+
* @param {readonly T[]} array
|
|
61
|
+
* @param {number} size
|
|
62
|
+
* @returns {T[][]}
|
|
10
63
|
*/
|
|
11
|
-
export function chunk(array:
|
|
64
|
+
export function chunk<T>(array: readonly T[], size: number): T[][];
|
|
12
65
|
/**
|
|
13
|
-
*
|
|
66
|
+
* Sliding windows of `size` items, moving by `step`. Good for moving
|
|
67
|
+
* averages and comparing neighbours.
|
|
14
68
|
*
|
|
15
|
-
*
|
|
69
|
+
* @example
|
|
70
|
+
* arr.windows([1, 2, 3, 4], 2); // [[1, 2], [2, 3], [3, 4]]
|
|
71
|
+
* arr.windows([1, 2, 3, 4, 5], 3, 2); // [[1, 2, 3], [3, 4, 5]]
|
|
72
|
+
*
|
|
73
|
+
* @template T
|
|
74
|
+
* @param {readonly T[]} array
|
|
75
|
+
* @param {number} size
|
|
76
|
+
* @param {number} [step=1]
|
|
77
|
+
* @returns {T[][]}
|
|
78
|
+
*/
|
|
79
|
+
export function windows<T>(array: readonly T[], size: number, step?: number): T[][];
|
|
80
|
+
/**
|
|
81
|
+
* The first item, or the first `n` items.
|
|
16
82
|
*
|
|
17
83
|
* @example
|
|
18
|
-
*
|
|
19
|
-
*
|
|
84
|
+
* arr.first([1, 2, 3]); // 1
|
|
85
|
+
* arr.first([1, 2, 3], 2); // [1, 2]
|
|
86
|
+
*
|
|
87
|
+
* @template T
|
|
88
|
+
* @overload
|
|
89
|
+
* @param {readonly T[]} array
|
|
90
|
+
* @returns {T | undefined}
|
|
91
|
+
*/
|
|
92
|
+
export function first<T>(array: readonly T[]): T | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* The first `n` items.
|
|
20
95
|
*
|
|
21
|
-
* @
|
|
22
|
-
* @
|
|
23
|
-
* @
|
|
96
|
+
* @template T
|
|
97
|
+
* @overload
|
|
98
|
+
* @param {readonly T[]} array
|
|
99
|
+
* @param {number} n
|
|
100
|
+
* @returns {T[]}
|
|
24
101
|
*/
|
|
25
|
-
export function
|
|
102
|
+
export function first<T>(array: readonly T[], n: number): T[];
|
|
26
103
|
/**
|
|
27
|
-
*
|
|
104
|
+
* The last item, or the last `n` items.
|
|
28
105
|
*
|
|
29
106
|
* @example
|
|
30
|
-
*
|
|
31
|
-
*
|
|
107
|
+
* arr.last([1, 2, 3]); // 3
|
|
108
|
+
* arr.last([1, 2, 3], 2); // [2, 3]
|
|
109
|
+
*
|
|
110
|
+
* @template T
|
|
111
|
+
* @overload
|
|
112
|
+
* @param {readonly T[]} array
|
|
113
|
+
* @returns {T | undefined}
|
|
114
|
+
*/
|
|
115
|
+
export function last<T>(array: readonly T[]): T | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* The last `n` items.
|
|
32
118
|
*
|
|
33
|
-
* @
|
|
34
|
-
* @
|
|
35
|
-
* @
|
|
119
|
+
* @template T
|
|
120
|
+
* @overload
|
|
121
|
+
* @param {readonly T[]} array
|
|
122
|
+
* @param {number} n
|
|
123
|
+
* @returns {T[]}
|
|
36
124
|
*/
|
|
37
|
-
export function
|
|
125
|
+
export function last<T>(array: readonly T[], n: number): T[];
|
|
38
126
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
127
|
+
* One page of an array, with what you need to draw pagination controls.
|
|
128
|
+
* Pages start at 1; an out-of-range page is brought back into range.
|
|
41
129
|
*
|
|
42
130
|
* @example
|
|
43
|
-
*
|
|
44
|
-
* // {
|
|
131
|
+
* arr.paginate(products, 2, 20);
|
|
132
|
+
* // { items: [...], page: 2, perPage: 20, total: 95, pages: 5, hasPrev: true, hasNext: true }
|
|
133
|
+
*
|
|
134
|
+
* @template T
|
|
135
|
+
* @param {readonly T[]} array
|
|
136
|
+
* @param {number} [page=1]
|
|
137
|
+
* @param {number} [perPage=20]
|
|
138
|
+
* @returns {Page<T>}
|
|
139
|
+
*/
|
|
140
|
+
export function paginate<T>(array: readonly T[], page?: number, perPage?: number): Page<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Removes duplicates. With an iteratee, items that give the same value are
|
|
143
|
+
* duplicates; the first one is kept.
|
|
45
144
|
*
|
|
46
|
-
* @
|
|
47
|
-
*
|
|
48
|
-
*
|
|
145
|
+
* @example
|
|
146
|
+
* arr.unique([1, 1, 2, 3]); // [1, 2, 3]
|
|
147
|
+
* arr.unique(users, "email"); // one user per email
|
|
148
|
+
* arr.unique(tags, (t) => t.toLowerCase());
|
|
149
|
+
*
|
|
150
|
+
* @template T
|
|
151
|
+
* @param {readonly T[]} array
|
|
152
|
+
* @param {Iteratee<T>} [iteratee]
|
|
153
|
+
* @returns {T[]}
|
|
49
154
|
*/
|
|
50
|
-
export function
|
|
155
|
+
export function unique<T>(array: readonly T[], iteratee?: Iteratee<T>): T[];
|
|
51
156
|
/**
|
|
52
|
-
*
|
|
53
|
-
* `[matching, notMatching]`.
|
|
157
|
+
* Groups items by key.
|
|
54
158
|
*
|
|
55
159
|
* @example
|
|
56
|
-
*
|
|
57
|
-
*
|
|
160
|
+
* arr.groupBy(users, "role"); // { admin: [...], user: [...] }
|
|
161
|
+
* arr.groupBy([1, 2, 3, 4], (n) => (n % 2 ? "odd" : "even")); // { odd: [1, 3], even: [2, 4] }
|
|
58
162
|
*
|
|
59
|
-
* @
|
|
60
|
-
* @param {
|
|
61
|
-
* @
|
|
163
|
+
* @template T
|
|
164
|
+
* @param {readonly T[]} array
|
|
165
|
+
* @param {Iteratee<T>} iteratee
|
|
166
|
+
* @returns {Record<string, T[]>}
|
|
62
167
|
*/
|
|
63
|
-
export function
|
|
168
|
+
export function groupBy<T>(array: readonly T[], iteratee: Iteratee<T>): Record<string, T[]>;
|
|
64
169
|
/**
|
|
65
|
-
*
|
|
170
|
+
* Indexes items by key. If two items share a key, the last one wins.
|
|
66
171
|
*
|
|
67
172
|
* @example
|
|
68
|
-
*
|
|
69
|
-
*
|
|
173
|
+
* const byId = arr.keyBy(users, "id");
|
|
174
|
+
* byId[42]; // user 42
|
|
70
175
|
*
|
|
71
|
-
* @
|
|
72
|
-
* @param {
|
|
73
|
-
* @
|
|
176
|
+
* @template T
|
|
177
|
+
* @param {readonly T[]} array
|
|
178
|
+
* @param {Iteratee<T>} iteratee
|
|
179
|
+
* @returns {Record<string, T>}
|
|
74
180
|
*/
|
|
75
|
-
export function
|
|
181
|
+
export function keyBy<T>(array: readonly T[], iteratee: Iteratee<T>): Record<string, T>;
|
|
76
182
|
/**
|
|
77
|
-
*
|
|
183
|
+
* Builds a `Map` from an array. Unlike `keyBy()`, keys keep their type.
|
|
78
184
|
*
|
|
79
|
-
*
|
|
185
|
+
* @example
|
|
186
|
+
* arr.toMap(users, "id"); // Map<number, User>
|
|
187
|
+
* arr.toMap(users, "id", (u) => u.name); // Map<number, string>
|
|
188
|
+
*
|
|
189
|
+
* @template T
|
|
190
|
+
* @template [V=T]
|
|
191
|
+
* @param {readonly T[]} array
|
|
192
|
+
* @param {Iteratee<T>} key
|
|
193
|
+
* @param {(item: T, index: number) => V} [value] The value to store. Defaults to the item.
|
|
194
|
+
* @returns {Map<any, V>}
|
|
195
|
+
*/
|
|
196
|
+
export function toMap<T, V = T>(array: readonly T[], key: Iteratee<T>, value?: (item: T, index: number) => V): Map<any, V>;
|
|
197
|
+
/**
|
|
198
|
+
* Counts items per key.
|
|
80
199
|
*
|
|
81
|
-
* @
|
|
82
|
-
*
|
|
200
|
+
* @example
|
|
201
|
+
* arr.countBy(["a", "b", "a"]); // { a: 2, b: 1 }
|
|
202
|
+
* arr.countBy(users, "country"); // { FR: 12, US: 7 }
|
|
203
|
+
*
|
|
204
|
+
* @template T
|
|
205
|
+
* @param {readonly T[]} array
|
|
206
|
+
* @param {Iteratee<T>} [iteratee] Defaults to the item itself.
|
|
207
|
+
* @returns {Record<string, number>}
|
|
83
208
|
*/
|
|
84
|
-
export function
|
|
209
|
+
export function countBy<T>(array: readonly T[], iteratee?: Iteratee<T>): Record<string, number>;
|
|
85
210
|
/**
|
|
86
|
-
*
|
|
211
|
+
* Counts the items that pass a test.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* arr.count(users, (u) => u.active);
|
|
87
215
|
*
|
|
88
|
-
* @
|
|
89
|
-
* @
|
|
216
|
+
* @template T
|
|
217
|
+
* @param {readonly T[]} array
|
|
218
|
+
* @param {(item: T, index: number) => unknown} predicate
|
|
219
|
+
* @returns {number}
|
|
90
220
|
*/
|
|
91
|
-
export function
|
|
221
|
+
export function count<T>(array: readonly T[], predicate: (item: T, index: number) => unknown): number;
|
|
92
222
|
/**
|
|
93
|
-
*
|
|
223
|
+
* Splits items into those that pass a test and those that don't.
|
|
94
224
|
*
|
|
95
|
-
* @
|
|
96
|
-
*
|
|
97
|
-
*
|
|
225
|
+
* @example
|
|
226
|
+
* const [adults, minors] = arr.partition(people, (p) => p.age >= 18);
|
|
227
|
+
*
|
|
228
|
+
* @template T
|
|
229
|
+
* @param {readonly T[]} array
|
|
230
|
+
* @param {(item: T, index: number) => unknown} predicate
|
|
231
|
+
* @returns {[T[], T[]]}
|
|
98
232
|
*/
|
|
99
|
-
export function
|
|
233
|
+
export function partition<T>(array: readonly T[], predicate: (item: T, index: number) => unknown): [T[], T[]];
|
|
100
234
|
/**
|
|
101
|
-
*
|
|
235
|
+
* Takes one property from every item.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* arr.pluck(users, "email"); // ["ada@x.io", "bob@x.io"]
|
|
102
239
|
*
|
|
103
|
-
*
|
|
240
|
+
* @template T
|
|
241
|
+
* @template {keyof T} K
|
|
242
|
+
* @param {readonly T[]} array
|
|
243
|
+
* @param {K} key
|
|
244
|
+
* @returns {Array<T[K]>}
|
|
245
|
+
*/
|
|
246
|
+
export function pluck<T, K extends keyof T>(array: readonly T[], key: K): Array<T[K]>;
|
|
247
|
+
/**
|
|
248
|
+
* Sorts by one or more keys, each with its own direction if you like.
|
|
249
|
+
* The sort is stable and `null`/`undefined` always go last.
|
|
104
250
|
*
|
|
105
251
|
* @example
|
|
106
|
-
*
|
|
107
|
-
*
|
|
252
|
+
* arr.sortBy(users, "age"); // youngest first
|
|
253
|
+
* arr.sortBy(users, "age", "desc"); // oldest first
|
|
254
|
+
* arr.sortBy(users, [["age", "desc"], "lastName"]);
|
|
255
|
+
* arr.sortBy(files, "name", { natural: true }); // "file2" before "file10"
|
|
256
|
+
*
|
|
257
|
+
* @template T
|
|
258
|
+
* @param {readonly T[]} array
|
|
259
|
+
* @param {SortKey<T> | Array<SortKey<T>>} keys
|
|
260
|
+
* @param {"asc" | "desc" | SortByOptions} [order="asc"]
|
|
261
|
+
* @returns {T[]}
|
|
262
|
+
*/
|
|
263
|
+
export function sortBy<T>(array: readonly T[], keys: SortKey<T> | Array<SortKey<T>>, order?: "asc" | "desc" | SortByOptions): T[];
|
|
264
|
+
/**
|
|
265
|
+
* A shuffled copy (Fisher-Yates).
|
|
108
266
|
*
|
|
109
|
-
* @
|
|
110
|
-
* @param {
|
|
111
|
-
* @
|
|
112
|
-
* @returns {any[]} A new sorted array.
|
|
267
|
+
* @template T
|
|
268
|
+
* @param {readonly T[]} array
|
|
269
|
+
* @returns {T[]}
|
|
113
270
|
*/
|
|
114
|
-
export function
|
|
271
|
+
export function shuffle<T>(array: readonly T[]): T[];
|
|
115
272
|
/**
|
|
116
|
-
*
|
|
273
|
+
* One random item, or `undefined` if the array is empty.
|
|
274
|
+
*
|
|
275
|
+
* @template T
|
|
276
|
+
* @param {readonly T[]} array
|
|
277
|
+
* @returns {T | undefined}
|
|
278
|
+
*/
|
|
279
|
+
export function sample<T>(array: readonly T[]): T | undefined;
|
|
280
|
+
/**
|
|
281
|
+
* `n` random items, never the same one twice.
|
|
117
282
|
*
|
|
118
283
|
* @example
|
|
119
|
-
*
|
|
284
|
+
* arr.sampleSize(players, 3);
|
|
120
285
|
*
|
|
121
|
-
* @
|
|
122
|
-
* @param {
|
|
123
|
-
* @
|
|
286
|
+
* @template T
|
|
287
|
+
* @param {readonly T[]} array
|
|
288
|
+
* @param {number} n
|
|
289
|
+
* @returns {T[]}
|
|
124
290
|
*/
|
|
125
|
-
export function
|
|
291
|
+
export function sampleSize<T>(array: readonly T[], n: number): T[];
|
|
126
292
|
/**
|
|
127
|
-
*
|
|
293
|
+
* Rotates items. Positive `n` moves items from the start to the end,
|
|
294
|
+
* negative `n` the other way.
|
|
128
295
|
*
|
|
129
296
|
* @example
|
|
130
|
-
*
|
|
297
|
+
* arr.rotate([1, 2, 3, 4], 1); // [2, 3, 4, 1]
|
|
298
|
+
* arr.rotate([1, 2, 3, 4], -1); // [4, 1, 2, 3]
|
|
131
299
|
*
|
|
132
|
-
* @
|
|
133
|
-
* @
|
|
300
|
+
* @template T
|
|
301
|
+
* @param {readonly T[]} array
|
|
302
|
+
* @param {number} [n=1]
|
|
303
|
+
* @returns {T[]}
|
|
134
304
|
*/
|
|
135
|
-
export function
|
|
305
|
+
export function rotate<T>(array: readonly T[], n?: number): T[];
|
|
136
306
|
/**
|
|
137
|
-
*
|
|
307
|
+
* Moves one item to another position. Negative indexes count from the end.
|
|
138
308
|
*
|
|
139
309
|
* @example
|
|
140
|
-
*
|
|
310
|
+
* arr.move(["a", "b", "c"], 0, 2); // ["b", "c", "a"]
|
|
141
311
|
*
|
|
142
|
-
* @
|
|
143
|
-
* @
|
|
312
|
+
* @template T
|
|
313
|
+
* @param {readonly T[]} array
|
|
314
|
+
* @param {number} from
|
|
315
|
+
* @param {number} to
|
|
316
|
+
* @returns {T[]}
|
|
144
317
|
*/
|
|
145
|
-
export function
|
|
318
|
+
export function move<T>(array: readonly T[], from: number, to: number): T[];
|
|
146
319
|
/**
|
|
147
|
-
*
|
|
320
|
+
* Swaps two items.
|
|
148
321
|
*
|
|
149
322
|
* @example
|
|
150
|
-
*
|
|
323
|
+
* arr.swap(["a", "b", "c"], 0, 2); // ["c", "b", "a"]
|
|
151
324
|
*
|
|
152
|
-
* @
|
|
153
|
-
* @
|
|
325
|
+
* @template T
|
|
326
|
+
* @param {readonly T[]} array
|
|
327
|
+
* @param {number} i
|
|
328
|
+
* @param {number} j
|
|
329
|
+
* @returns {T[]}
|
|
154
330
|
*/
|
|
155
|
-
export function
|
|
331
|
+
export function swap<T>(array: readonly T[], i: number, j: number): T[];
|
|
156
332
|
/**
|
|
157
|
-
*
|
|
333
|
+
* Items of `array` that appear in none of the others.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* arr.difference([1, 2, 3, 4], [2, 4]); // [1, 3]
|
|
158
337
|
*
|
|
159
|
-
*
|
|
338
|
+
* @template T
|
|
339
|
+
* @param {readonly T[]} array
|
|
340
|
+
* @param {...ReadonlyArray<T>} others
|
|
341
|
+
* @returns {T[]}
|
|
342
|
+
*/
|
|
343
|
+
export function difference<T>(array: readonly T[], ...others: ReadonlyArray<T>[]): T[];
|
|
344
|
+
/**
|
|
345
|
+
* Items present in every array, without duplicates.
|
|
160
346
|
*
|
|
161
347
|
* @example
|
|
162
|
-
*
|
|
348
|
+
* arr.intersection([1, 2, 3], [2, 3, 4], [3, 2]); // [2, 3]
|
|
163
349
|
*
|
|
164
|
-
* @
|
|
165
|
-
* @
|
|
350
|
+
* @template T
|
|
351
|
+
* @param {...ReadonlyArray<T>} arrays
|
|
352
|
+
* @returns {T[]}
|
|
166
353
|
*/
|
|
167
|
-
export function
|
|
354
|
+
export function intersection<T>(...arrays: ReadonlyArray<T>[]): T[];
|
|
168
355
|
/**
|
|
169
|
-
*
|
|
356
|
+
* Items present in any array, without duplicates.
|
|
170
357
|
*
|
|
171
|
-
* @
|
|
172
|
-
*
|
|
173
|
-
*
|
|
358
|
+
* @example
|
|
359
|
+
* arr.union([1, 2], [2, 3], [3, 4]); // [1, 2, 3, 4]
|
|
360
|
+
*
|
|
361
|
+
* @template T
|
|
362
|
+
* @param {...ReadonlyArray<T>} arrays
|
|
363
|
+
* @returns {T[]}
|
|
174
364
|
*/
|
|
175
|
-
export function
|
|
365
|
+
export function union<T>(...arrays: ReadonlyArray<T>[]): T[];
|
|
176
366
|
/**
|
|
177
|
-
*
|
|
367
|
+
* A copy without the given values. `NaN` works too.
|
|
178
368
|
*
|
|
179
|
-
* @
|
|
180
|
-
*
|
|
181
|
-
*
|
|
369
|
+
* @example
|
|
370
|
+
* arr.without([1, 2, 3, 2], 2); // [1, 3]
|
|
371
|
+
*
|
|
372
|
+
* @template T
|
|
373
|
+
* @param {readonly T[]} array
|
|
374
|
+
* @param {...T} values
|
|
375
|
+
* @returns {T[]}
|
|
182
376
|
*/
|
|
183
|
-
export function
|
|
377
|
+
export function without<T>(array: readonly T[], ...values: T[]): T[];
|
|
184
378
|
/**
|
|
185
|
-
*
|
|
379
|
+
* A copy without the items that pass the test.
|
|
186
380
|
*
|
|
187
|
-
* @
|
|
188
|
-
*
|
|
189
|
-
*
|
|
381
|
+
* @example
|
|
382
|
+
* arr.remove([1, 2, 3, 4], (n) => n % 2 === 0); // [1, 3]
|
|
383
|
+
*
|
|
384
|
+
* @template T
|
|
385
|
+
* @param {readonly T[]} array
|
|
386
|
+
* @param {(item: T, index: number) => unknown} predicate
|
|
387
|
+
* @returns {T[]}
|
|
388
|
+
*/
|
|
389
|
+
export function remove<T>(array: readonly T[], predicate: (item: T, index: number) => unknown): T[];
|
|
390
|
+
/**
|
|
391
|
+
* Removes falsy values (`false`, `null`, `undefined`, `0`, `""`, `NaN`).
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* arr.compact([0, 1, false, 2, "", 3, null]); // [1, 2, 3]
|
|
395
|
+
*
|
|
396
|
+
* @template T
|
|
397
|
+
* @param {readonly T[]} array
|
|
398
|
+
* @returns {Array<Exclude<T, false | 0 | 0n | "" | null | undefined>>}
|
|
399
|
+
*/
|
|
400
|
+
export function compact<T>(array: readonly T[]): Array<Exclude<T, false | 0 | 0n | "" | null | undefined>>;
|
|
401
|
+
/**
|
|
402
|
+
* Adds the item if it's missing, removes it if it's there. Handy for
|
|
403
|
+
* multi-select state.
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* arr.toggle(["a", "b"], "b"); // ["a"]
|
|
407
|
+
* arr.toggle(["a"], "b"); // ["a", "b"]
|
|
408
|
+
*
|
|
409
|
+
* @template T
|
|
410
|
+
* @param {readonly T[]} array
|
|
411
|
+
* @param {T} item
|
|
412
|
+
* @returns {T[]}
|
|
413
|
+
*/
|
|
414
|
+
export function toggle<T>(array: readonly T[], item: T): T[];
|
|
415
|
+
/**
|
|
416
|
+
* Replaces the item with the same identity, or appends it if there's none.
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* arr.upsert(users, { id: 2, name: "Bob" }, "id");
|
|
420
|
+
*
|
|
421
|
+
* @template T
|
|
422
|
+
* @param {readonly T[]} array
|
|
423
|
+
* @param {T} item
|
|
424
|
+
* @param {Iteratee<T>} identity What makes two items "the same", like `"id"`.
|
|
425
|
+
* @returns {T[]}
|
|
426
|
+
*/
|
|
427
|
+
export function upsert<T>(array: readonly T[], item: T, identity: Iteratee<T>): T[];
|
|
428
|
+
/**
|
|
429
|
+
* Inserts items at an index. Negative indexes count from the end.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* arr.insert(["a", "d"], 1, "b", "c"); // ["a", "b", "c", "d"]
|
|
433
|
+
*
|
|
434
|
+
* @template T
|
|
435
|
+
* @param {readonly T[]} array
|
|
436
|
+
* @param {number} index
|
|
437
|
+
* @param {...T} items
|
|
438
|
+
* @returns {T[]}
|
|
439
|
+
*/
|
|
440
|
+
export function insert<T>(array: readonly T[], index: number, ...items: T[]): T[];
|
|
441
|
+
/**
|
|
442
|
+
* Flattens nested arrays, one level by default.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* arr.flatten([1, [2, [3, [4]]]]); // [1, 2, [3, [4]]]
|
|
446
|
+
* arr.flatten([1, [2, [3, [4]]]], Infinity); // [1, 2, 3, 4]
|
|
447
|
+
*
|
|
448
|
+
* @template T
|
|
449
|
+
* @template {number} [D=1]
|
|
450
|
+
* @param {readonly T[]} array
|
|
451
|
+
* @param {D} [depth]
|
|
452
|
+
* @returns {FlatArray<T[], D>[]}
|
|
453
|
+
*/
|
|
454
|
+
export function flatten<T, D extends number = 1>(array: readonly T[], depth?: D): FlatArray<T[], D>[];
|
|
455
|
+
/**
|
|
456
|
+
* Pairs items by position. Shorter arrays leave `undefined` holes.
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* arr.zip(["a", "b"], [1, 2]); // [["a", 1], ["b", 2]]
|
|
460
|
+
*
|
|
461
|
+
* @template T
|
|
462
|
+
* @param {...ReadonlyArray<T>} arrays
|
|
463
|
+
* @returns {Array<Array<T | undefined>>}
|
|
190
464
|
*/
|
|
191
|
-
export function
|
|
465
|
+
export function zip<T>(...arrays: ReadonlyArray<T>[]): Array<Array<T | undefined>>;
|
|
192
466
|
/**
|
|
193
|
-
*
|
|
467
|
+
* The opposite of `zip()`: rows become columns.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* arr.unzip([["a", 1], ["b", 2]]); // [["a", "b"], [1, 2]]
|
|
194
471
|
*
|
|
195
|
-
* @
|
|
196
|
-
* @param {
|
|
197
|
-
* @
|
|
198
|
-
* @returns {any[]} The reordered array.
|
|
472
|
+
* @template T
|
|
473
|
+
* @param {ReadonlyArray<ReadonlyArray<T>>} tuples
|
|
474
|
+
* @returns {Array<Array<T | undefined>>}
|
|
199
475
|
*/
|
|
200
|
-
export function
|
|
476
|
+
export function unzip<T>(tuples: ReadonlyArray<ReadonlyArray<T>>): Array<Array<T | undefined>>;
|
|
201
477
|
/**
|
|
202
|
-
*
|
|
478
|
+
* Takes items from each array in turn, then appends the rest.
|
|
203
479
|
*
|
|
204
480
|
* @example
|
|
205
|
-
*
|
|
206
|
-
* nodeComfort.arr.countBy([1.1, 2.3, 1.7], Math.floor); // { 1: 2, 2: 1 }
|
|
481
|
+
* arr.interleave([1, 3, 5], [2, 4]); // [1, 2, 3, 4, 5]
|
|
207
482
|
*
|
|
208
|
-
* @
|
|
209
|
-
* @param {
|
|
210
|
-
* @returns {
|
|
483
|
+
* @template T
|
|
484
|
+
* @param {...ReadonlyArray<T>} arrays
|
|
485
|
+
* @returns {T[]}
|
|
211
486
|
*/
|
|
212
|
-
export function
|
|
487
|
+
export function interleave<T>(...arrays: ReadonlyArray<T>[]): T[];
|
|
213
488
|
/**
|
|
214
|
-
*
|
|
489
|
+
* Every combination of one item from each array.
|
|
215
490
|
*
|
|
216
491
|
* @example
|
|
217
|
-
*
|
|
492
|
+
* arr.cartesian(["S", "M"], ["red", "blue"]);
|
|
493
|
+
* // [["S", "red"], ["S", "blue"], ["M", "red"], ["M", "blue"]]
|
|
218
494
|
*
|
|
219
|
-
* @
|
|
220
|
-
* @param {
|
|
221
|
-
* @returns {
|
|
495
|
+
* @template T
|
|
496
|
+
* @param {...ReadonlyArray<T>} arrays
|
|
497
|
+
* @returns {T[][]}
|
|
222
498
|
*/
|
|
223
|
-
export function
|
|
499
|
+
export function cartesian<T>(...arrays: ReadonlyArray<T>[]): T[][];
|
|
224
500
|
/**
|
|
225
|
-
*
|
|
501
|
+
* An array of `n` items, from a value or a function of the index.
|
|
226
502
|
*
|
|
227
|
-
* @
|
|
228
|
-
*
|
|
229
|
-
*
|
|
503
|
+
* @example
|
|
504
|
+
* arr.times(3, "x"); // ["x", "x", "x"]
|
|
505
|
+
* arr.times(3, (i) => i * 2); // [0, 2, 4]
|
|
506
|
+
*
|
|
507
|
+
* @template T
|
|
508
|
+
* @param {number} n
|
|
509
|
+
* @param {T | ((index: number) => T)} [value]
|
|
510
|
+
* @returns {T[]}
|
|
230
511
|
*/
|
|
231
|
-
export function
|
|
512
|
+
export function times<T>(n: number, value?: T | ((index: number) => T)): T[];
|
|
232
513
|
/**
|
|
233
|
-
*
|
|
514
|
+
* Adds up a number from each item. Non-numbers count as 0.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* arr.sumBy(cart, "price");
|
|
518
|
+
* arr.sumBy(cart, (item) => item.price * item.quantity);
|
|
519
|
+
*
|
|
520
|
+
* @template T
|
|
521
|
+
* @param {readonly T[]} array
|
|
522
|
+
* @param {Iteratee<T>} [iteratee]
|
|
523
|
+
* @returns {number}
|
|
524
|
+
*/
|
|
525
|
+
export function sumBy<T>(array: readonly T[], iteratee?: Iteratee<T>): number;
|
|
526
|
+
/**
|
|
527
|
+
* The average of a number from each item, or `0` for an empty array.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* arr.averageBy(reviews, "rating"); // 4.3
|
|
531
|
+
*
|
|
532
|
+
* @template T
|
|
533
|
+
* @param {readonly T[]} array
|
|
534
|
+
* @param {Iteratee<T>} [iteratee]
|
|
535
|
+
* @returns {number}
|
|
536
|
+
*/
|
|
537
|
+
export function averageBy<T>(array: readonly T[], iteratee?: Iteratee<T>): number;
|
|
538
|
+
/**
|
|
539
|
+
* The item with the highest value.
|
|
540
|
+
*
|
|
541
|
+
* @example
|
|
542
|
+
* arr.maxBy(players, "score");
|
|
234
543
|
*
|
|
235
|
-
* @
|
|
236
|
-
* @param {
|
|
237
|
-
* @
|
|
544
|
+
* @template T
|
|
545
|
+
* @param {readonly T[]} array
|
|
546
|
+
* @param {Iteratee<T>} [iteratee]
|
|
547
|
+
* @returns {T | undefined}
|
|
238
548
|
*/
|
|
239
|
-
export function
|
|
549
|
+
export function maxBy<T>(array: readonly T[], iteratee?: Iteratee<T>): T | undefined;
|
|
240
550
|
/**
|
|
241
|
-
*
|
|
551
|
+
* The item with the lowest value.
|
|
242
552
|
*
|
|
243
553
|
* @example
|
|
244
|
-
*
|
|
245
|
-
* nodeComfort.arr.times(3, (i) => i * 2); // [0, 2, 4]
|
|
554
|
+
* arr.minBy(products, "price");
|
|
246
555
|
*
|
|
247
|
-
* @
|
|
248
|
-
* @param {
|
|
249
|
-
* @
|
|
556
|
+
* @template T
|
|
557
|
+
* @param {readonly T[]} array
|
|
558
|
+
* @param {Iteratee<T>} [iteratee]
|
|
559
|
+
* @returns {T | undefined}
|
|
250
560
|
*/
|
|
251
|
-
export function
|
|
561
|
+
export function minBy<T>(array: readonly T[], iteratee?: Iteratee<T>): T | undefined;
|
|
252
562
|
//# sourceMappingURL=Arr.d.ts.map
|