@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.
Files changed (75) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/README.md +136 -549
  3. package/index.js +80 -35
  4. package/package.json +160 -33
  5. package/src/Arr.js +887 -419
  6. package/src/Async.js +585 -0
  7. package/src/Cache.js +329 -0
  8. package/src/Checker.js +971 -539
  9. package/src/Cli.js +1034 -0
  10. package/src/Color.js +415 -0
  11. package/src/Crypto.js +707 -0
  12. package/src/Emitter.js +289 -0
  13. package/src/Env.js +555 -0
  14. package/src/FS.js +1483 -894
  15. package/src/Func.js +670 -353
  16. package/src/Http.js +538 -0
  17. package/src/Id.js +384 -201
  18. package/src/Logger.js +739 -378
  19. package/src/Num.js +844 -361
  20. package/src/Obj.js +855 -372
  21. package/src/SQLite.js +1308 -1005
  22. package/src/Schema.js +1586 -0
  23. package/src/Str.js +983 -436
  24. package/src/Sys.js +511 -0
  25. package/src/Time.js +1260 -422
  26. package/src/Utils.js +321 -325
  27. package/src/errors.js +292 -0
  28. package/types/index.d.ts +693 -526
  29. package/types/src/Arr.d.ts +455 -145
  30. package/types/src/Arr.d.ts.map +1 -1
  31. package/types/src/Async.d.ts +321 -0
  32. package/types/src/Async.d.ts.map +1 -0
  33. package/types/src/Cache.d.ts +195 -0
  34. package/types/src/Cache.d.ts.map +1 -0
  35. package/types/src/Checker.d.ts +501 -235
  36. package/types/src/Checker.d.ts.map +1 -1
  37. package/types/src/Cli.d.ts +582 -0
  38. package/types/src/Cli.d.ts.map +1 -0
  39. package/types/src/Color.d.ts +294 -0
  40. package/types/src/Color.d.ts.map +1 -0
  41. package/types/src/Crypto.d.ts +534 -0
  42. package/types/src/Crypto.d.ts.map +1 -0
  43. package/types/src/Emitter.d.ts +170 -0
  44. package/types/src/Emitter.d.ts.map +1 -0
  45. package/types/src/Env.d.ts +382 -0
  46. package/types/src/Env.d.ts.map +1 -0
  47. package/types/src/FS.d.ts +686 -238
  48. package/types/src/FS.d.ts.map +1 -1
  49. package/types/src/Func.d.ts +380 -135
  50. package/types/src/Func.d.ts.map +1 -1
  51. package/types/src/Http.d.ts +374 -0
  52. package/types/src/Http.d.ts.map +1 -0
  53. package/types/src/Id.d.ts +149 -68
  54. package/types/src/Id.d.ts.map +1 -1
  55. package/types/src/Logger.d.ts +363 -46
  56. package/types/src/Logger.d.ts.map +1 -1
  57. package/types/src/Num.d.ts +473 -137
  58. package/types/src/Num.d.ts.map +1 -1
  59. package/types/src/Obj.d.ts +390 -104
  60. package/types/src/Obj.d.ts.map +1 -1
  61. package/types/src/SQLite.d.ts +646 -309
  62. package/types/src/SQLite.d.ts.map +1 -1
  63. package/types/src/Schema.d.ts +940 -0
  64. package/types/src/Schema.d.ts.map +1 -0
  65. package/types/src/Str.d.ts +569 -166
  66. package/types/src/Str.d.ts.map +1 -1
  67. package/types/src/Sys.d.ts +269 -0
  68. package/types/src/Sys.d.ts.map +1 -0
  69. package/types/src/Time.d.ts +663 -204
  70. package/types/src/Time.d.ts.map +1 -1
  71. package/types/src/Utils.d.ts +154 -102
  72. package/types/src/Utils.d.ts.map +1 -1
  73. package/types/src/errors.d.ts +292 -0
  74. package/types/src/errors.d.ts.map +1 -0
  75. package/types/index.d.ts.map +0 -1
@@ -1,252 +1,562 @@
1
1
  /**
2
- * Splits an array into chunks of a given size.
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
- * nodeComfort.arr.chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [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
- * @param {any[]} array - Source array.
8
- * @param {number} size - Chunk size (must be >= 1).
9
- * @returns {any[][]} The chunked array.
59
+ * @template T
60
+ * @param {readonly T[]} array
61
+ * @param {number} size
62
+ * @returns {T[][]}
10
63
  */
11
- export function chunk(array: any[], size: number): any[][];
64
+ export function chunk<T>(array: readonly T[], size: number): T[][];
12
65
  /**
13
- * Returns a new array with duplicate values removed.
66
+ * Sliding windows of `size` items, moving by `step`. Good for moving
67
+ * averages and comparing neighbours.
14
68
  *
15
- * Pass an `iteratee` to de-duplicate by a derived key.
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
- * nodeComfort.arr.unique([1, 1, 2, 3]); // [1, 2, 3]
19
- * nodeComfort.arr.unique([{ id: 1 }, { id: 1 }], "id"); // [{ id: 1 }]
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
- * @param {any[]} array - Source array.
22
- * @param {string|((item: any) => any)} [iteratee] - Key or function to compare by.
23
- * @returns {any[]} The de-duplicated array.
96
+ * @template T
97
+ * @overload
98
+ * @param {readonly T[]} array
99
+ * @param {number} n
100
+ * @returns {T[]}
24
101
  */
25
- export function unique(array: any[], iteratee?: string | ((item: any) => any)): any[];
102
+ export function first<T>(array: readonly T[], n: number): T[];
26
103
  /**
27
- * Groups the items of an array by a key or function.
104
+ * The last item, or the last `n` items.
28
105
  *
29
106
  * @example
30
- * nodeComfort.arr.groupBy([1, 2, 3, 4], (n) => (n % 2 ? "odd" : "even"));
31
- * // { odd: [1, 3], even: [2, 4] }
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
- * @param {any[]} array - Source array.
34
- * @param {string|((item: any) => any)} iteratee - Key or function to group by.
35
- * @returns {Record<string, any[]>} The grouped object.
119
+ * @template T
120
+ * @overload
121
+ * @param {readonly T[]} array
122
+ * @param {number} n
123
+ * @returns {T[]}
36
124
  */
37
- export function groupBy(array: any[], iteratee: string | ((item: any) => any)): Record<string, any[]>;
125
+ export function last<T>(array: readonly T[], n: number): T[];
38
126
  /**
39
- * Builds an object keyed by a derived value, mapping each key to the
40
- * (last) matching item.
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
- * nodeComfort.arr.keyBy([{ id: "a" }, { id: "b" }], "id");
44
- * // { a: { id: "a" }, b: { id: "b" } }
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
- * @param {any[]} array - Source array.
47
- * @param {string|((item: any) => any)} iteratee - Key or function.
48
- * @returns {Record<string, any>} The keyed object.
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 keyBy(array: any[], iteratee: string | ((item: any) => any)): Record<string, any>;
155
+ export function unique<T>(array: readonly T[], iteratee?: Iteratee<T>): T[];
51
156
  /**
52
- * Partitions an array into two groups based on a predicate:
53
- * `[matching, notMatching]`.
157
+ * Groups items by key.
54
158
  *
55
159
  * @example
56
- * nodeComfort.arr.partition([1, 2, 3, 4], (n) => n % 2 === 0);
57
- * // [[2, 4], [1, 3]]
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
- * @param {any[]} array - Source array.
60
- * @param {(item: any, index: number) => boolean} predicate - Test function.
61
- * @returns {[any[], any[]]} The two partitions.
163
+ * @template T
164
+ * @param {readonly T[]} array
165
+ * @param {Iteratee<T>} iteratee
166
+ * @returns {Record<string, T[]>}
62
167
  */
63
- export function partition(array: any[], predicate: (item: any, index: number) => boolean): [any[], any[]];
168
+ export function groupBy<T>(array: readonly T[], iteratee: Iteratee<T>): Record<string, T[]>;
64
169
  /**
65
- * Recursively flattens a nested array up to a given depth.
170
+ * Indexes items by key. If two items share a key, the last one wins.
66
171
  *
67
172
  * @example
68
- * nodeComfort.arr.flatten([1, [2, [3, [4]]]]); // [1, 2, 3, [4]]
69
- * nodeComfort.arr.flatten([1, [2, [3]]], Infinity); // [1, 2, 3]
173
+ * const byId = arr.keyBy(users, "id");
174
+ * byId[42]; // user 42
70
175
  *
71
- * @param {any[]} array - Source array.
72
- * @param {number} [depth=1] - Maximum depth to flatten.
73
- * @returns {any[]} The flattened array.
176
+ * @template T
177
+ * @param {readonly T[]} array
178
+ * @param {Iteratee<T>} iteratee
179
+ * @returns {Record<string, T>}
74
180
  */
75
- export function flatten(array: any[], depth?: number): any[];
181
+ export function keyBy<T>(array: readonly T[], iteratee: Iteratee<T>): Record<string, T>;
76
182
  /**
77
- * Returns a shuffled copy of an array (Fisher–Yates).
183
+ * Builds a `Map` from an array. Unlike `keyBy()`, keys keep their type.
78
184
  *
79
- * Uses `Math.random` • not cryptographically secure.
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
- * @param {any[]} array - Source array.
82
- * @returns {any[]} A new shuffled array.
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 shuffle(array: any[]): any[];
209
+ export function countBy<T>(array: readonly T[], iteratee?: Iteratee<T>): Record<string, number>;
85
210
  /**
86
- * Returns a random element from an array.
211
+ * Counts the items that pass a test.
212
+ *
213
+ * @example
214
+ * arr.count(users, (u) => u.active);
87
215
  *
88
- * @param {any[]} array - Source array.
89
- * @returns {any} A random element, or undefined if empty.
216
+ * @template T
217
+ * @param {readonly T[]} array
218
+ * @param {(item: T, index: number) => unknown} predicate
219
+ * @returns {number}
90
220
  */
91
- export function sample(array: any[]): any;
221
+ export function count<T>(array: readonly T[], predicate: (item: T, index: number) => unknown): number;
92
222
  /**
93
- * Returns `n` random elements from an array (without repetition).
223
+ * Splits items into those that pass a test and those that don't.
94
224
  *
95
- * @param {any[]} array - Source array.
96
- * @param {number} n - Number of elements to pick.
97
- * @returns {any[]} The sampled elements.
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 sampleSize(array: any[], n: number): any[];
233
+ export function partition<T>(array: readonly T[], predicate: (item: T, index: number) => unknown): [T[], T[]];
100
234
  /**
101
- * Sorts an array by one or more iteratees, ascending by default.
235
+ * Takes one property from every item.
236
+ *
237
+ * @example
238
+ * arr.pluck(users, "email"); // ["ada@x.io", "bob@x.io"]
102
239
  *
103
- * Does not mutate the input.
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
- * nodeComfort.arr.sortBy(users, "age");
107
- * nodeComfort.arr.sortBy(users, [(u) => u.age, "name"]);
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
- * @param {any[]} array - Source array.
110
- * @param {string|((item: any) => any)|Array<string|((item: any) => any)>} iteratees - Sort key(s).
111
- * @param {"asc"|"desc"} [direction="asc"] - Sort direction.
112
- * @returns {any[]} A new sorted array.
267
+ * @template T
268
+ * @param {readonly T[]} array
269
+ * @returns {T[]}
113
270
  */
114
- export function sortBy(array: any[], iteratees: string | ((item: any) => any) | Array<string | ((item: any) => any)>, direction?: "asc" | "desc"): any[];
271
+ export function shuffle<T>(array: readonly T[]): T[];
115
272
  /**
116
- * Returns the elements present in the first array but not in the others.
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
- * nodeComfort.arr.difference([1, 2, 3, 4], [2, 4]); // [1, 3]
284
+ * arr.sampleSize(players, 3);
120
285
  *
121
- * @param {any[]} array - Source array.
122
- * @param {...any[]} others - Arrays to subtract.
123
- * @returns {any[]} The difference.
286
+ * @template T
287
+ * @param {readonly T[]} array
288
+ * @param {number} n
289
+ * @returns {T[]}
124
290
  */
125
- export function difference(array: any[], ...others: any[][]): any[];
291
+ export function sampleSize<T>(array: readonly T[], n: number): T[];
126
292
  /**
127
- * Returns the elements common to all provided arrays.
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
- * nodeComfort.arr.intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
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
- * @param {...any[]} arrays - Arrays to intersect.
133
- * @returns {any[]} The intersection.
300
+ * @template T
301
+ * @param {readonly T[]} array
302
+ * @param {number} [n=1]
303
+ * @returns {T[]}
134
304
  */
135
- export function intersection(...arrays: any[][]): any[];
305
+ export function rotate<T>(array: readonly T[], n?: number): T[];
136
306
  /**
137
- * Returns the union (unique values) of all provided arrays.
307
+ * Moves one item to another position. Negative indexes count from the end.
138
308
  *
139
309
  * @example
140
- * nodeComfort.arr.union([1, 2], [2, 3], [3, 4]); // [1, 2, 3, 4]
310
+ * arr.move(["a", "b", "c"], 0, 2); // ["b", "c", "a"]
141
311
  *
142
- * @param {...any[]} arrays - Arrays to unite.
143
- * @returns {any[]} The union.
312
+ * @template T
313
+ * @param {readonly T[]} array
314
+ * @param {number} from
315
+ * @param {number} to
316
+ * @returns {T[]}
144
317
  */
145
- export function union(...arrays: any[][]): any[];
318
+ export function move<T>(array: readonly T[], from: number, to: number): T[];
146
319
  /**
147
- * Zips multiple arrays together into an array of tuples.
320
+ * Swaps two items.
148
321
  *
149
322
  * @example
150
- * nodeComfort.arr.zip(["a", "b"], [1, 2]); // [["a", 1], ["b", 2]]
323
+ * arr.swap(["a", "b", "c"], 0, 2); // ["c", "b", "a"]
151
324
  *
152
- * @param {...any[]} arrays - Arrays to zip.
153
- * @returns {any[][]} The zipped tuples.
325
+ * @template T
326
+ * @param {readonly T[]} array
327
+ * @param {number} i
328
+ * @param {number} j
329
+ * @returns {T[]}
154
330
  */
155
- export function zip(...arrays: any[][]): any[][];
331
+ export function swap<T>(array: readonly T[], i: number, j: number): T[];
156
332
  /**
157
- * Splits an array into `matching` and removes falsy values.
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
- * Removes `false`, `null`, `0`, `""`, `undefined` and `NaN`.
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
- * nodeComfort.arr.compact([0, 1, false, 2, "", 3, null]); // [1, 2, 3]
348
+ * arr.intersection([1, 2, 3], [2, 3, 4], [3, 2]); // [2, 3]
163
349
  *
164
- * @param {any[]} array - Source array.
165
- * @returns {any[]} The compacted array.
350
+ * @template T
351
+ * @param {...ReadonlyArray<T>} arrays
352
+ * @returns {T[]}
166
353
  */
167
- export function compact(array: any[]): any[];
354
+ export function intersection<T>(...arrays: ReadonlyArray<T>[]): T[];
168
355
  /**
169
- * Returns the first element (or the first `n` elements) of an array.
356
+ * Items present in any array, without duplicates.
170
357
  *
171
- * @param {any[]} array - Source array.
172
- * @param {number} [n] - How many elements to take.
173
- * @returns {any} The first element, or the first `n` elements when `n` is given.
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 first(array: any[], n?: number): any;
365
+ export function union<T>(...arrays: ReadonlyArray<T>[]): T[];
176
366
  /**
177
- * Returns the last element (or the last `n` elements) of an array.
367
+ * A copy without the given values. `NaN` works too.
178
368
  *
179
- * @param {any[]} array - Source array.
180
- * @param {number} [n] - How many elements to take.
181
- * @returns {any} The last element, or the last `n` elements when `n` is given.
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 last(array: any[], n?: number): any;
377
+ export function without<T>(array: readonly T[], ...values: T[]): T[];
184
378
  /**
185
- * Removes items from an array where the predicate is true, returning a new array.
379
+ * A copy without the items that pass the test.
186
380
  *
187
- * @param {any[]} array - Source array.
188
- * @param {(item: any, index: number) => boolean} predicate - Removal test.
189
- * @returns {any[]} A new array without the removed items.
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 remove(array: any[], predicate: (item: any, index: number) => boolean): any[];
465
+ export function zip<T>(...arrays: ReadonlyArray<T>[]): Array<Array<T | undefined>>;
192
466
  /**
193
- * Moves an element from one index to another (returns a new array).
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
- * @param {any[]} array - Source array.
196
- * @param {number} from - Source index.
197
- * @param {number} to - Destination index.
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 move(array: any[], from: number, to: number): any[];
476
+ export function unzip<T>(tuples: ReadonlyArray<ReadonlyArray<T>>): Array<Array<T | undefined>>;
201
477
  /**
202
- * Counts occurrences of each derived key in an array.
478
+ * Takes items from each array in turn, then appends the rest.
203
479
  *
204
480
  * @example
205
- * nodeComfort.arr.countBy(["a", "b", "a"]); // { a: 2, b: 1 }
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
- * @param {any[]} array - Source array.
209
- * @param {string|((item: any) => any)} [iteratee] - Key or function.
210
- * @returns {Record<string, number>} The counts.
483
+ * @template T
484
+ * @param {...ReadonlyArray<T>} arrays
485
+ * @returns {T[]}
211
486
  */
212
- export function countBy(array: any[], iteratee?: string | ((item: any) => any)): Record<string, number>;
487
+ export function interleave<T>(...arrays: ReadonlyArray<T>[]): T[];
213
488
  /**
214
- * Sums an array by a numeric iteratee (or the values themselves).
489
+ * Every combination of one item from each array.
215
490
  *
216
491
  * @example
217
- * nodeComfort.arr.sumBy([{ n: 1 }, { n: 2 }], "n"); // 3
492
+ * arr.cartesian(["S", "M"], ["red", "blue"]);
493
+ * // [["S", "red"], ["S", "blue"], ["M", "red"], ["M", "blue"]]
218
494
  *
219
- * @param {any[]} array - Source array.
220
- * @param {string|((item: any) => number)} [iteratee] - Key or function returning a number.
221
- * @returns {number} The sum.
495
+ * @template T
496
+ * @param {...ReadonlyArray<T>} arrays
497
+ * @returns {T[][]}
222
498
  */
223
- export function sumBy(array: any[], iteratee?: string | ((item: any) => number)): number;
499
+ export function cartesian<T>(...arrays: ReadonlyArray<T>[]): T[][];
224
500
  /**
225
- * Returns the item with the maximum derived value.
501
+ * An array of `n` items, from a value or a function of the index.
226
502
  *
227
- * @param {any[]} array - Source array.
228
- * @param {string|((item: any) => number)} [iteratee] - Key or function.
229
- * @returns {any} The item, or undefined if the array is empty.
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 maxBy(array: any[], iteratee?: string | ((item: any) => number)): any;
512
+ export function times<T>(n: number, value?: T | ((index: number) => T)): T[];
232
513
  /**
233
- * Returns the item with the minimum derived value.
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
- * @param {any[]} array - Source array.
236
- * @param {string|((item: any) => number)} [iteratee] - Key or function.
237
- * @returns {any} The item, or undefined if the array is empty.
544
+ * @template T
545
+ * @param {readonly T[]} array
546
+ * @param {Iteratee<T>} [iteratee]
547
+ * @returns {T | undefined}
238
548
  */
239
- export function minBy(array: any[], iteratee?: string | ((item: any) => number)): any;
549
+ export function maxBy<T>(array: readonly T[], iteratee?: Iteratee<T>): T | undefined;
240
550
  /**
241
- * Creates an array of a given length, filled by a value or generator function.
551
+ * The item with the lowest value.
242
552
  *
243
553
  * @example
244
- * nodeComfort.arr.times(3, "x"); // ["x", "x", "x"]
245
- * nodeComfort.arr.times(3, (i) => i * 2); // [0, 2, 4]
554
+ * arr.minBy(products, "price");
246
555
  *
247
- * @param {number} n - Number of items.
248
- * @param {any|((index: number) => any)} [value] - Fill value or generator.
249
- * @returns {any[]} The generated array.
556
+ * @template T
557
+ * @param {readonly T[]} array
558
+ * @param {Iteratee<T>} [iteratee]
559
+ * @returns {T | undefined}
250
560
  */
251
- export function times(n: number, value?: any | ((index: number) => any)): any[];
561
+ export function minBy<T>(array: readonly T[], iteratee?: Iteratee<T>): T | undefined;
252
562
  //# sourceMappingURL=Arr.d.ts.map