tinkit 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +135 -2
- package/dist/arrays/index.d.ts +1168 -0
- package/dist/arrays/index.js +1 -0
- package/dist/async/index.d.ts +729 -0
- package/dist/async/index.js +1 -0
- package/dist/cache/index.d.ts +165 -0
- package/dist/cache/index.js +1 -0
- package/dist/color/index.d.ts +328 -0
- package/dist/color/index.js +1 -0
- package/dist/date/index.d.ts +1152 -0
- package/dist/date/index.js +1 -0
- package/dist/decorators/index.d.ts +313 -0
- package/dist/decorators/index.js +1 -0
- package/dist/easing/index.d.ts +452 -0
- package/dist/easing/index.js +1 -0
- package/dist/emitter/index.d.ts +104 -0
- package/dist/emitter/index.js +1 -0
- package/dist/format/index.d.ts +317 -0
- package/dist/format/index.js +1 -0
- package/dist/functions/index.d.ts +648 -0
- package/dist/functions/index.js +1 -0
- package/dist/guards/index.d.ts +1009 -0
- package/dist/guards/index.js +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +1 -0
- package/dist/iterables/index.d.ts +501 -0
- package/dist/iterables/index.js +1 -0
- package/dist/math/index.d.ts +670 -0
- package/dist/math/index.js +1 -0
- package/dist/objects/index.d.ts +1307 -0
- package/dist/objects/index.js +1 -0
- package/dist/random/index.d.ts +184 -0
- package/dist/random/index.js +1 -0
- package/dist/search/index.d.ts +151 -0
- package/dist/search/index.js +1 -0
- package/dist/store/index.d.ts +71 -0
- package/dist/store/index.js +1 -0
- package/dist/strings/index.d.ts +751 -0
- package/dist/strings/index.js +3 -0
- package/dist/url/index.d.ts +357 -0
- package/dist/url/index.js +1 -0
- package/dist/utils/assert.d.ts +81 -0
- package/dist/utils/assert.js +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +0 -0
- package/dist/utils/types.d.ts +64 -0
- package/dist/utils/types.js +0 -0
- package/dist/validate/index.d.ts +409 -0
- package/dist/validate/index.js +1 -0
- package/llms-full.txt +708 -0
- package/llms.txt +42 -0
- package/package.json +144 -5
|
@@ -0,0 +1,1168 @@
|
|
|
1
|
+
import type { RandomGenerator } from '../utils/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Values that are falsy in JavaScript and representable as types.
|
|
4
|
+
* At runtime {@link compact} also removes `-0` and `NaN`, which TypeScript cannot express as literal types.
|
|
5
|
+
*/
|
|
6
|
+
export type Falsy = false | null | undefined | 0 | 0n | '';
|
|
7
|
+
/**
|
|
8
|
+
* Recursively unwraps (possibly readonly) nested arrays down to their non-array element type.
|
|
9
|
+
* @example
|
|
10
|
+
* type R = FlatDeep<number | (number | string[])[]>; // number | string
|
|
11
|
+
*/
|
|
12
|
+
export type FlatDeep<T> = T extends ReadonlyArray<infer U> ? FlatDeep<U> : T;
|
|
13
|
+
/**
|
|
14
|
+
* The tuple of element types produced by {@link zip} for a tuple of input arrays.
|
|
15
|
+
* @example
|
|
16
|
+
* type R = Zipped<[number[], string[]]>; // [number, string]
|
|
17
|
+
*/
|
|
18
|
+
export type Zipped<T extends ReadonlyArray<ReadonlyArray<unknown>>> = {
|
|
19
|
+
-readonly [K in keyof T]: T[K] extends ReadonlyArray<infer U> ? U : never;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Values accepted as sort keys by {@link sortBy}. Selectors may also return `null` or `undefined` (a missing key): such keys,
|
|
23
|
+
* like `NaN` and invalid `Date`s, rank after every other key in both directions.
|
|
24
|
+
*/
|
|
25
|
+
export type SortKey = number | string | bigint | Date;
|
|
26
|
+
/**
|
|
27
|
+
* Sort direction accepted by {@link sortBy}.
|
|
28
|
+
*/
|
|
29
|
+
export type SortOrder = 'asc' | 'desc';
|
|
30
|
+
/**
|
|
31
|
+
* Check if an array contains a value, using SameValueZero equality (so `NaN` is found and `0` matches `-0`).
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {ReadonlyArray<T>} array - The array to be checked
|
|
34
|
+
* @param {T} value - The value to look for
|
|
35
|
+
* @returns {boolean} Whether the value is in the array
|
|
36
|
+
* @example
|
|
37
|
+
* contains([1, 2, 3], 2); // => true
|
|
38
|
+
* contains([NaN], NaN); // => true
|
|
39
|
+
*/
|
|
40
|
+
export declare function contains<T>(array: ReadonlyArray<T>, value: T): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Get the first item in an array.
|
|
43
|
+
* @template T
|
|
44
|
+
* @param {ReadonlyArray<T>} array - The array to be processed
|
|
45
|
+
* @returns {T | undefined} The first item, or `undefined` when the array is empty
|
|
46
|
+
* @example
|
|
47
|
+
* first([1, 2, 3]); // => 1
|
|
48
|
+
* first([]); // => undefined
|
|
49
|
+
*/
|
|
50
|
+
export declare function first<T>(array: ReadonlyArray<T>): T | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Get the last item in an array.
|
|
53
|
+
* @template T
|
|
54
|
+
* @param {ReadonlyArray<T>} array - The array to be processed
|
|
55
|
+
* @returns {T | undefined} The last item, or `undefined` when the array is empty
|
|
56
|
+
* @example
|
|
57
|
+
* last([1, 2, 3]); // => 3
|
|
58
|
+
* last([]); // => undefined
|
|
59
|
+
*/
|
|
60
|
+
export declare function last<T>(array: ReadonlyArray<T>): T | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Flatten an array by exactly one level, like `Array.prototype.flat()` (holes in sparse arrays are skipped).
|
|
63
|
+
* @template T
|
|
64
|
+
* @param {ReadonlyArray<T | ReadonlyArray<T>>} array - The array to be flattened
|
|
65
|
+
* @returns {T[]} A new, one-level-flattened array
|
|
66
|
+
* @example
|
|
67
|
+
* flatten([[1, 2], 3, [4]]); // => [1, 2, 3, 4]
|
|
68
|
+
* flatten([[1, [2]]]); // => [1, [2]]
|
|
69
|
+
*/
|
|
70
|
+
export declare function flatten<T>(array: ReadonlyArray<T | ReadonlyArray<T>>): T[];
|
|
71
|
+
/**
|
|
72
|
+
* Recursively flatten nested arrays of any depth, like `Array.prototype.flat(Infinity)` (holes are skipped).
|
|
73
|
+
* The implementation is iterative, so very deep nesting cannot overflow the call stack.
|
|
74
|
+
* @template T
|
|
75
|
+
* @param {ReadonlyArray<T>} array - The array to be flattened
|
|
76
|
+
* @returns {Array<FlatDeep<T>>} A new array containing every non-array leaf, in depth-first order
|
|
77
|
+
* @throws {TypeError} when the array contains itself (directly or through nested arrays)
|
|
78
|
+
* @example
|
|
79
|
+
* flattenDeep([1, [2, [3, [4]]]]); // => [1, 2, 3, 4]
|
|
80
|
+
*/
|
|
81
|
+
export declare function flattenDeep<T>(array: ReadonlyArray<T>): Array<FlatDeep<T>>;
|
|
82
|
+
/**
|
|
83
|
+
* Remove duplicates from an iterable (SameValueZero equality), keeping the first occurrence of each value.
|
|
84
|
+
* @template T
|
|
85
|
+
* @param {Iterable<T>} iterable - The iterable to be processed
|
|
86
|
+
* @returns {T[]} A new array with duplicates removed, in first-appearance order
|
|
87
|
+
* @example
|
|
88
|
+
* unique([1, 2, 2, 3, 1]); // => [1, 2, 3]
|
|
89
|
+
*/
|
|
90
|
+
export declare function unique<T>(iterable: Iterable<T>): T[];
|
|
91
|
+
/**
|
|
92
|
+
* Get the union of two iterables: every distinct value from `a`, then every distinct value of `b` not already in `a`.
|
|
93
|
+
* Uses SameValueZero equality.
|
|
94
|
+
* @template T
|
|
95
|
+
* @param {Iterable<T>} a - The first iterable
|
|
96
|
+
* @param {Iterable<T>} b - The second iterable
|
|
97
|
+
* @returns {T[]} The unique values of both iterables, in first-appearance order
|
|
98
|
+
* @example
|
|
99
|
+
* union([1, 2, 3], [3, 4, 5]); // => [1, 2, 3, 4, 5]
|
|
100
|
+
*/
|
|
101
|
+
export declare function union<T>(a: Iterable<T>, b: Iterable<T>): T[];
|
|
102
|
+
/**
|
|
103
|
+
* Get the intersection of two iterables: the distinct values of `a` that also appear in `b`.
|
|
104
|
+
* Uses SameValueZero equality.
|
|
105
|
+
* @template T
|
|
106
|
+
* @param {Iterable<T>} a - The first iterable (defines the output order)
|
|
107
|
+
* @param {Iterable<T>} b - The second iterable
|
|
108
|
+
* @returns {T[]} The unique common values, in order of first appearance in `a`
|
|
109
|
+
* @example
|
|
110
|
+
* intersection([1, 2, 3], [2, 3, 4]); // => [2, 3]
|
|
111
|
+
*/
|
|
112
|
+
export declare function intersection<T>(a: Iterable<T>, b: Iterable<T>): T[];
|
|
113
|
+
/**
|
|
114
|
+
* Get the difference of two iterables: the distinct values of `a` that do not appear in `b`.
|
|
115
|
+
* Uses SameValueZero equality.
|
|
116
|
+
* @template T
|
|
117
|
+
* @param {Iterable<T>} a - The iterable to take values from (defines the output order)
|
|
118
|
+
* @param {Iterable<T>} b - The iterable of values to exclude
|
|
119
|
+
* @returns {T[]} The unique values of `a` missing from `b`, in first-appearance order
|
|
120
|
+
* @example
|
|
121
|
+
* difference([1, 2, 3, 1], [2, 3, 4]); // => [1]
|
|
122
|
+
*/
|
|
123
|
+
export declare function difference<T>(a: Iterable<T>, b: Iterable<T>): T[];
|
|
124
|
+
/**
|
|
125
|
+
* Get the symmetric difference of two iterables: the distinct values that appear in exactly one of them.
|
|
126
|
+
* Uses SameValueZero equality.
|
|
127
|
+
* @template T
|
|
128
|
+
* @param {Iterable<T>} a - The first iterable
|
|
129
|
+
* @param {Iterable<T>} b - The second iterable
|
|
130
|
+
* @returns {T[]} The values only in `a` (first-appearance order), followed by the values only in `b`
|
|
131
|
+
* @example
|
|
132
|
+
* symmetricDifference([1, 2, 3], [3, 4]); // => [1, 2, 4]
|
|
133
|
+
*/
|
|
134
|
+
export declare function symmetricDifference<T>(a: Iterable<T>, b: Iterable<T>): T[];
|
|
135
|
+
/**
|
|
136
|
+
* Check whether every value of `subset` also appears in `superset` (SameValueZero equality).
|
|
137
|
+
* An empty `subset` is a subset of anything.
|
|
138
|
+
* @template T
|
|
139
|
+
* @param {Iterable<T>} subset - The candidate subset
|
|
140
|
+
* @param {Iterable<T>} superset - The candidate superset
|
|
141
|
+
* @returns {boolean} Whether `subset` ⊆ `superset`
|
|
142
|
+
* @example
|
|
143
|
+
* isSubset([1, 2], [3, 2, 1]); // => true
|
|
144
|
+
* isSubset([1, 4], [1, 2, 3]); // => false
|
|
145
|
+
*/
|
|
146
|
+
export declare function isSubset<T>(subset: Iterable<T>, superset: Iterable<T>): boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Split an array into consecutive chunks of `size` items; only the last chunk may be shorter.
|
|
149
|
+
* @template T
|
|
150
|
+
* @param {ReadonlyArray<T>} array - The array to split
|
|
151
|
+
* @param {number} size - The maximum length of each chunk (a positive integer)
|
|
152
|
+
* @returns {T[][]} A new array of chunks (empty when the input is empty)
|
|
153
|
+
* @throws {RangeError} when `size` is not a positive integer
|
|
154
|
+
* @throws {TypeError} when `size` is not a number
|
|
155
|
+
* @example
|
|
156
|
+
* chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
|
|
157
|
+
*/
|
|
158
|
+
export declare function chunk<T>(array: ReadonlyArray<T>, size: number): T[][];
|
|
159
|
+
/**
|
|
160
|
+
* Remove every falsy value (`false`, `null`, `undefined`, `0`, `-0`, `NaN`, `''`, `0n`) from an array.
|
|
161
|
+
* @template T
|
|
162
|
+
* @param {ReadonlyArray<T>} array - The array to compact
|
|
163
|
+
* @returns {Array<Exclude<T, Falsy>>} A new array containing only the truthy values
|
|
164
|
+
* @example
|
|
165
|
+
* compact([0, 1, false, 2, '', 3, null, NaN]); // => [1, 2, 3]
|
|
166
|
+
*/
|
|
167
|
+
export declare function compact<T>(array: ReadonlyArray<T>): Array<Exclude<T, Falsy>>;
|
|
168
|
+
/**
|
|
169
|
+
* Create an array of numbers progressing from `start` up to, but not including, `end`, with Python `range` semantics.
|
|
170
|
+
* With a single argument, it is the (exclusive) end and the range starts at 0.
|
|
171
|
+
* The result is empty when `step` points away from `end`. Each value is computed as `start + i * step`,
|
|
172
|
+
* so fractional steps do not accumulate rounding errors (but are still subject to floating-point precision).
|
|
173
|
+
* When `(end - start) / step` is an integer up to floating-point rounding (e.g. `range(0, 2.1, 0.3)`), the range has exactly
|
|
174
|
+
* that many items, and no item ever reaches `end` (it is always exclusive, even when rounding errors would land on it).
|
|
175
|
+
* Only lengths above `2 ** 32 - 1` (the array limit) are rejected, far more than a heap can hold: callers must bound untrusted sizes
|
|
176
|
+
* (e.g. `range(Number(query.pages))`), since a result that does not fit in memory aborts the whole process with a fatal
|
|
177
|
+
* "heap out of memory" error rather than throwing.
|
|
178
|
+
* @param {number} start - The first value, or the exclusive end when `end` is omitted
|
|
179
|
+
* @param {number} [end] - The exclusive end of the range
|
|
180
|
+
* @param {number} [step=1] - The increment between values (may be negative, never 0)
|
|
181
|
+
* @returns {number[]} The generated numbers
|
|
182
|
+
* @throws {TypeError} when `start`, `end` or `step` is not a number
|
|
183
|
+
* @throws {RangeError} when an argument is not finite, `step` is 0, or the result would exceed the maximum array length
|
|
184
|
+
* @example
|
|
185
|
+
* range(5); // => [0, 1, 2, 3, 4]
|
|
186
|
+
* range(2, 8, 2); // => [2, 4, 6]
|
|
187
|
+
* range(5, 0, -2); // => [5, 3, 1]
|
|
188
|
+
* range(0, 5, -1); // => []
|
|
189
|
+
* range(0, 2.1, 0.3); // => [0, 0.3, 0.6, 0.8999999999999999, 1.2, 1.5, 1.7999999999999998] // 7 items, 2.1 excluded
|
|
190
|
+
*/
|
|
191
|
+
export declare function range(start: number, end?: number, step?: number): number[];
|
|
192
|
+
/**
|
|
193
|
+
* Combine arrays element-wise into tuples, truncating to the length of the shortest array.
|
|
194
|
+
* @template T
|
|
195
|
+
* @param {...ReadonlyArray<unknown>} arrays - The arrays to zip
|
|
196
|
+
* @returns {Array<Zipped<T>>} A new array of tuples; empty when called without arrays
|
|
197
|
+
* @example
|
|
198
|
+
* zip([1, 2, 3], ['a', 'b']); // => [[1, 'a'], [2, 'b']] // typed as [number, string][]
|
|
199
|
+
*/
|
|
200
|
+
export declare function zip<T extends Array<ReadonlyArray<unknown>>>(...arrays: T): Array<Zipped<T>>;
|
|
201
|
+
/**
|
|
202
|
+
* Group the items of an array by the key returned by `keyFn`, preserving input order within each group.
|
|
203
|
+
* The result is a null-prototype object, so keys such as `'__proto__'` or `'constructor'` are stored safely
|
|
204
|
+
* (no prototype pollution), but it has no `Object.prototype` methods. Number keys are converted to strings, as in any object.
|
|
205
|
+
* @template T
|
|
206
|
+
* @template K
|
|
207
|
+
* @param {ReadonlyArray<T>} array - The items to group
|
|
208
|
+
* @param {(item: T, index: number) => K} keyFn - Returns the group key of an item
|
|
209
|
+
* @returns {Partial<Record<K, T[]>>} An object mapping each key to the items that produced it
|
|
210
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
211
|
+
* @example
|
|
212
|
+
* groupBy([1, 2, 3, 4], function (n) { return n % 2 === 0 ? 'even' : 'odd'; }); // => { odd: [1, 3], even: [2, 4] }
|
|
213
|
+
*/
|
|
214
|
+
export declare function groupBy<T, K extends PropertyKey>(array: ReadonlyArray<T>, keyFn: (item: T, index: number) => K): Partial<Record<K, T[]>>;
|
|
215
|
+
/**
|
|
216
|
+
* Index the items of an array by the key returned by `keyFn`; when several items share a key, the last one wins.
|
|
217
|
+
* The result is a null-prototype object (safe for keys such as `'__proto__'`).
|
|
218
|
+
* @template T
|
|
219
|
+
* @template K
|
|
220
|
+
* @param {ReadonlyArray<T>} array - The items to index
|
|
221
|
+
* @param {(item: T, index: number) => K} keyFn - Returns the key of an item
|
|
222
|
+
* @returns {Record<K, T>} An object mapping each key to the last item that produced it
|
|
223
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
224
|
+
* @example
|
|
225
|
+
* keyBy([{ id: 'a', v: 1 }, { id: 'a', v: 2 }], function (o) { return o.id; }); // => { a: { id: 'a', v: 2 } }
|
|
226
|
+
*/
|
|
227
|
+
export declare function keyBy<T, K extends PropertyKey>(array: ReadonlyArray<T>, keyFn: (item: T, index: number) => K): Record<K, T>;
|
|
228
|
+
/**
|
|
229
|
+
* Count the items of an array per key returned by `keyFn`.
|
|
230
|
+
* The result is a null-prototype object (safe for keys such as `'__proto__'`).
|
|
231
|
+
* @template T
|
|
232
|
+
* @template K
|
|
233
|
+
* @param {ReadonlyArray<T>} array - The items to count
|
|
234
|
+
* @param {(item: T, index: number) => K} keyFn - Returns the key of an item
|
|
235
|
+
* @returns {Record<K, number>} An object mapping each key to the number of items that produced it
|
|
236
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
237
|
+
* @example
|
|
238
|
+
* countBy(['one', 'two', 'three'], function (s) { return s.length; }); // => { 3: 2, 5: 1 }
|
|
239
|
+
*/
|
|
240
|
+
export declare function countBy<T, K extends PropertyKey>(array: ReadonlyArray<T>, keyFn: (item: T, index: number) => K): Record<K, number>;
|
|
241
|
+
/**
|
|
242
|
+
* Split an array in two: the items matching a type-guard `predicate`, and the others. Relative order is preserved.
|
|
243
|
+
* @template T
|
|
244
|
+
* @template S
|
|
245
|
+
* @param {ReadonlyArray<T>} array - The items to split
|
|
246
|
+
* @param {(item: T, index: number) => item is S} predicate - Type guard deciding which side an item goes to
|
|
247
|
+
* @returns {[S[], Exclude<T, S>[]]} `[matching, nonMatching]`
|
|
248
|
+
* @throws {TypeError} when `predicate` is not a function
|
|
249
|
+
* @example
|
|
250
|
+
* partition([1, 'a', 2], function (v): v is number { return typeof v === 'number'; }); // => [[1, 2], ['a']]
|
|
251
|
+
*/
|
|
252
|
+
export declare function partition<T, S extends T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => item is S): [S[], Exclude<T, S>[]];
|
|
253
|
+
/**
|
|
254
|
+
* Split an array in two: the items for which `predicate` returns true, and the others. Relative order is preserved.
|
|
255
|
+
* @template T
|
|
256
|
+
* @param {ReadonlyArray<T>} array - The items to split
|
|
257
|
+
* @param {(item: T, index: number) => boolean} predicate - Decides which side an item goes to
|
|
258
|
+
* @returns {[T[], T[]]} `[matching, nonMatching]`
|
|
259
|
+
* @throws {TypeError} when `predicate` is not a function
|
|
260
|
+
* @example
|
|
261
|
+
* partition([1, 2, 3, 4], function (n) { return n % 2 === 0; }); // => [[2, 4], [1, 3]]
|
|
262
|
+
*/
|
|
263
|
+
export declare function partition<T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): [T[], T[]];
|
|
264
|
+
/**
|
|
265
|
+
* Remove items whose key (as returned by `keyFn`, compared with SameValueZero) was already seen, keeping the first occurrence.
|
|
266
|
+
* @template T
|
|
267
|
+
* @param {ReadonlyArray<T>} array - The items to deduplicate
|
|
268
|
+
* @param {(item: T, index: number) => unknown} keyFn - Returns the identity key of an item
|
|
269
|
+
* @returns {T[]} A new array with one item per distinct key, in first-appearance order
|
|
270
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
271
|
+
* @example
|
|
272
|
+
* uniqueBy([{ id: 1, v: 'a' }, { id: 1, v: 'b' }, { id: 2, v: 'c' }], function (o) { return o.id; }); // => [{ id: 1, v: 'a' }, { id: 2, v: 'c' }]
|
|
273
|
+
*/
|
|
274
|
+
export declare function uniqueBy<T>(array: ReadonlyArray<T>, keyFn: (item: T, index: number) => unknown): T[];
|
|
275
|
+
/**
|
|
276
|
+
* Return a sorted copy of an array, ordered by the key `selector` returns for each item. The sort is stable
|
|
277
|
+
* (items with equal keys keep their relative order, in both directions) and the selector is called once per item.
|
|
278
|
+
*
|
|
279
|
+
* Ordering rules, chosen to be deterministic and locale-independent:
|
|
280
|
+
* - numbers and bigints compare numerically (and may be mixed); `Date`s compare by timestamp;
|
|
281
|
+
* - strings compare by UTF-16 code units with plain `<` (not `localeCompare`), so `'B' < 'a'`;
|
|
282
|
+
* - numeric keys sort before string keys when both kinds are mixed;
|
|
283
|
+
* - `NaN` keys, invalid `Date`s and missing (`null`/`undefined`) keys always go last, whatever the order, in input order.
|
|
284
|
+
*
|
|
285
|
+
* Use {@link orderBy} to sort by several keys.
|
|
286
|
+
* @template T
|
|
287
|
+
* @param {ReadonlyArray<T>} array - The items to sort (not mutated)
|
|
288
|
+
* @param {(item: T, index: number) => SortKey | null | undefined} selector - Returns the sort key of an item
|
|
289
|
+
* @param {SortOrder} [order='asc'] - `'asc'` for ascending or `'desc'` for descending keys
|
|
290
|
+
* @returns {T[]} A new sorted array
|
|
291
|
+
* @throws {TypeError} when `selector` is not a function
|
|
292
|
+
* @throws {RangeError} when `order` is neither `'asc'` nor `'desc'`
|
|
293
|
+
* @example
|
|
294
|
+
* sortBy([{ n: 'b' }, { n: 'a' }], function (o) { return o.n; }); // => [{ n: 'a' }, { n: 'b' }]
|
|
295
|
+
* sortBy([1, 3, 2], function (n) { return n; }, 'desc'); // => [3, 2, 1]
|
|
296
|
+
* sortBy([{ n: 2 }, {}, { n: 1 }], function (o: { n?: number }) { return o.n; }); // => [{ n: 1 }, { n: 2 }, {}]
|
|
297
|
+
*/
|
|
298
|
+
export declare function sortBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => SortKey | null | undefined, order?: SortOrder): T[];
|
|
299
|
+
/**
|
|
300
|
+
* Return a shuffled copy of an array using the Fisher–Yates algorithm (the input is not mutated).
|
|
301
|
+
* Not cryptographically secure by default: pass `secureRandom` (random module) for tokens, OTPs, lotteries…
|
|
302
|
+
* @template T
|
|
303
|
+
* @param {ReadonlyArray<T>} array - The items to shuffle
|
|
304
|
+
* @param {RandomGenerator} [random=Math.random] - Generator of floats in `[0, 1)`, injectable for deterministic results
|
|
305
|
+
* @returns {T[]} A new array containing the same items in random order
|
|
306
|
+
* @throws {RangeError} when `random()` returns a value outside `[0, 1)`
|
|
307
|
+
* @throws {TypeError} when `random` is not a function
|
|
308
|
+
* @example
|
|
309
|
+
* shuffle([1, 2, 3, 4]); // e.g. [3, 1, 4, 2]
|
|
310
|
+
*/
|
|
311
|
+
export declare function shuffle<T>(array: ReadonlyArray<T>, random?: RandomGenerator): T[];
|
|
312
|
+
/**
|
|
313
|
+
* Pick a random item from an array.
|
|
314
|
+
* Not cryptographically secure by default: pass `secureRandom` (random module) for tokens, OTPs, lotteries…
|
|
315
|
+
* @template T
|
|
316
|
+
* @param {ReadonlyArray<T>} array - The items to pick from
|
|
317
|
+
* @param {RandomGenerator} [random=Math.random] - Generator of floats in `[0, 1)`, injectable for deterministic results
|
|
318
|
+
* @returns {T | undefined} A random item, or `undefined` when the array is empty (`random` is then not called)
|
|
319
|
+
* @throws {RangeError} when `random()` returns a value outside `[0, 1)`
|
|
320
|
+
* @throws {TypeError} when `random` is not a function
|
|
321
|
+
* @example
|
|
322
|
+
* sample(['a', 'b', 'c']); // e.g. 'b'
|
|
323
|
+
* sample([]); // => undefined
|
|
324
|
+
*/
|
|
325
|
+
export declare function sample<T>(array: ReadonlyArray<T>, random?: RandomGenerator): T | undefined;
|
|
326
|
+
/**
|
|
327
|
+
* Return the first `n` items of an array (or all of them when it is shorter).
|
|
328
|
+
* @template T
|
|
329
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
330
|
+
* @param {number} n - How many items to take (a non-negative integer)
|
|
331
|
+
* @returns {T[]} A new array with at most `n` items
|
|
332
|
+
* @throws {RangeError} when `n` is negative or not an integer
|
|
333
|
+
* @throws {TypeError} when `n` is not a number
|
|
334
|
+
* @example
|
|
335
|
+
* take([1, 2, 3], 2); // => [1, 2]
|
|
336
|
+
*/
|
|
337
|
+
export declare function take<T>(array: ReadonlyArray<T>, n: number): T[];
|
|
338
|
+
/**
|
|
339
|
+
* Return an array without its first `n` items.
|
|
340
|
+
* @template T
|
|
341
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
342
|
+
* @param {number} n - How many items to skip (a non-negative integer)
|
|
343
|
+
* @returns {T[]} A new array with the remaining items
|
|
344
|
+
* @throws {RangeError} when `n` is negative or not an integer
|
|
345
|
+
* @throws {TypeError} when `n` is not a number
|
|
346
|
+
* @example
|
|
347
|
+
* drop([1, 2, 3], 2); // => [3]
|
|
348
|
+
*/
|
|
349
|
+
export declare function drop<T>(array: ReadonlyArray<T>, n: number): T[];
|
|
350
|
+
/**
|
|
351
|
+
* The tuple of arrays produced by {@link unzip} for an array of tuples of type `T`.
|
|
352
|
+
* @example
|
|
353
|
+
* type R = Unzipped<[number, string]>; // [number[], string[]]
|
|
354
|
+
*/
|
|
355
|
+
export type Unzipped<T extends ReadonlyArray<unknown>> = {
|
|
356
|
+
-readonly [K in keyof T]: Array<T[K]>;
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* A comparator, as accepted by `Array.prototype.sort`: negative when `a` sorts before `b`, positive when after, `0` when equivalent.
|
|
360
|
+
*/
|
|
361
|
+
export type Comparator<T> = (a: T, b: T) => number;
|
|
362
|
+
/**
|
|
363
|
+
* Split an array of tuples into one array per tuple position: the inverse of {@link zip}.
|
|
364
|
+
* Like `zip`, the result is truncated to the SHORTEST tuple, so `unzip(zip(a, b))` gives back `[a, b]` for equal-length, non-empty arrays.
|
|
365
|
+
* The number of positions is read from the tuples themselves, so for an EMPTY input it cannot be known and `[]` is returned;
|
|
366
|
+
* the return type says so (`Unzipped<T> | []`) — use destructuring defaults: `const [xs = [], ys = []] = unzip(pairs);`.
|
|
367
|
+
* @template T - The tuple type
|
|
368
|
+
* @param {ReadonlyArray<T>} tuples - The tuples to split
|
|
369
|
+
* @returns {Unzipped<T> | []} One fresh array per tuple position, or `[]` when there are no tuples
|
|
370
|
+
* @example
|
|
371
|
+
* unzip([[1, 'a'], [2, 'b']] as [number, string][]); // => [[1, 2], ['a', 'b']] // typed as [number[], string[]] | []
|
|
372
|
+
* unzip([]); // => []
|
|
373
|
+
*/
|
|
374
|
+
export declare function unzip<T extends ReadonlyArray<unknown>>(tuples: ReadonlyArray<T>): Unzipped<T> | [];
|
|
375
|
+
/**
|
|
376
|
+
* Return the sliding windows of `size` consecutive items, starting every `step` items. Only FULL windows are returned,
|
|
377
|
+
* so an array shorter than `size` gives `[]`, and trailing items that do not fill a window are dropped.
|
|
378
|
+
* The number of windows is `max(0, floor((length - size) / step) + 1)`, and the result holds `size` times as many items in all, which
|
|
379
|
+
* must not exceed `2 ** 32 - 1`. That limit is far more than a heap can hold: callers must bound untrusted sizes, since a result
|
|
380
|
+
* that does not fit in memory aborts the whole process with a fatal "heap out of memory" error rather than throwing.
|
|
381
|
+
* @template T
|
|
382
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
383
|
+
* @param {number} size - The length of each window (a positive integer)
|
|
384
|
+
* @param {number} [step=1] - The distance between the starts of two consecutive windows (a positive integer)
|
|
385
|
+
* @returns {T[][]} A new array of windows (each window is a fresh array)
|
|
386
|
+
* @throws {RangeError} when `size` or `step` is not a positive integer, or the windows would hold more than `2 ** 32 - 1` items in all
|
|
387
|
+
* @throws {TypeError} when `size` or `step` is not a number
|
|
388
|
+
* @example
|
|
389
|
+
* windowed([1, 2, 3, 4], 2); // => [[1, 2], [2, 3], [3, 4]]
|
|
390
|
+
* windowed([1, 2, 3, 4, 5], 2, 2); // => [[1, 2], [3, 4]]
|
|
391
|
+
* windowed([1, 2], 3); // => []
|
|
392
|
+
*/
|
|
393
|
+
export declare function windowed<T>(array: ReadonlyArray<T>, size: number, step?: number): T[][];
|
|
394
|
+
/**
|
|
395
|
+
* Return every pair of adjacent items: `[a[0], a[1]], [a[1], a[2]], ...` (empty for arrays with fewer than 2 items).
|
|
396
|
+
* @template T
|
|
397
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
398
|
+
* @returns {Array<[T, T]>} A new array of `length - 1` pairs
|
|
399
|
+
* @example
|
|
400
|
+
* pairwise([1, 2, 3]); // => [[1, 2], [2, 3]]
|
|
401
|
+
* pairwise([1]); // => []
|
|
402
|
+
*/
|
|
403
|
+
export declare function pairwise<T>(array: ReadonlyArray<T>): Array<[T, T]>;
|
|
404
|
+
/**
|
|
405
|
+
* Return the item with the SMALLEST key, using the same ordering rules as {@link sortBy}
|
|
406
|
+
* (numbers/bigints numerically and before strings, strings by UTF-16 code units, `Date`s by timestamp).
|
|
407
|
+
* On ties the FIRST such item wins. Items whose key is `NaN`, an invalid `Date`, `null` or `undefined` are ignored.
|
|
408
|
+
* The selector is called exactly once per item.
|
|
409
|
+
* @template T
|
|
410
|
+
* @param {ReadonlyArray<T>} array - The items to inspect
|
|
411
|
+
* @param {(item: T, index: number) => SortKey | null | undefined} selector - Returns the key of an item
|
|
412
|
+
* @returns {T | undefined} The first item with the smallest key, or `undefined` when the array is empty or every key is `NaN`
|
|
413
|
+
* @throws {TypeError} when `selector` is not a function
|
|
414
|
+
* @example
|
|
415
|
+
* const items = [{ n: 3 }, { n: 1 }, { n: 1 }];
|
|
416
|
+
* minBy(items, function (o) { return o.n; }) === items[1]; // => true // ties keep the first
|
|
417
|
+
* minBy([NaN, 2], function (n) { return n; }); // => 2
|
|
418
|
+
*/
|
|
419
|
+
export declare function minBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => SortKey | null | undefined): T | undefined;
|
|
420
|
+
/**
|
|
421
|
+
* Return the item with the LARGEST key, using the same ordering rules as {@link sortBy}
|
|
422
|
+
* (numbers/bigints numerically and before strings, strings by UTF-16 code units, `Date`s by timestamp).
|
|
423
|
+
* On ties the FIRST such item wins. Items whose key is `NaN`, an invalid `Date`, `null` or `undefined` are ignored.
|
|
424
|
+
* The selector is called exactly once per item.
|
|
425
|
+
* @template T
|
|
426
|
+
* @param {ReadonlyArray<T>} array - The items to inspect
|
|
427
|
+
* @param {(item: T, index: number) => SortKey | null | undefined} selector - Returns the key of an item
|
|
428
|
+
* @returns {T | undefined} The first item with the largest key, or `undefined` when the array is empty or every key is `NaN`
|
|
429
|
+
* @throws {TypeError} when `selector` is not a function
|
|
430
|
+
* @example
|
|
431
|
+
* maxBy([{ n: 3 }, { n: 7 }], function (o) { return o.n; }); // => { n: 7 }
|
|
432
|
+
* maxBy(['b', 'a', 'B'], function (s) { return s; }); // => 'b'
|
|
433
|
+
*/
|
|
434
|
+
export declare function maxBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => SortKey | null | undefined): T | undefined;
|
|
435
|
+
/**
|
|
436
|
+
* Add up the numbers returned by `selector` for each item, using compensated (Neumaier) summation like `sum` from the math module (same algorithm, same result),
|
|
437
|
+
* so the rounding error does not grow with the number of items.
|
|
438
|
+
* `NaN`/`Infinity` keys propagate as in plain addition. Like the aggregates of the math module, each key is converted once with unary `+`,
|
|
439
|
+
* so numeric strings count as numbers instead of being concatenated.
|
|
440
|
+
* @template T
|
|
441
|
+
* @param {ReadonlyArray<T>} array - The items to add up
|
|
442
|
+
* @param {(item: T, index: number) => number} selector - Returns the number contributed by an item
|
|
443
|
+
* @returns {number} The total (`0` for an empty array)
|
|
444
|
+
* @throws {TypeError} when `selector` is not a function
|
|
445
|
+
* @example
|
|
446
|
+
* sumBy([{ price: 0.1 }, { price: 0.2 }, { price: 0.3 }], function (o) { return o.price; }); // => 0.6
|
|
447
|
+
*/
|
|
448
|
+
export declare function sumBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => number): number;
|
|
449
|
+
/**
|
|
450
|
+
* Return a copy of an array with the item at `from` moved to position `to` (the input is not mutated).
|
|
451
|
+
* `to` is the index the item has in the RESULT. Negative indices count from the end, like `Array.prototype.at`.
|
|
452
|
+
* Equivalent to `copy.splice(to, 0, ...copy.splice(from, 1))` on a copy. Holes of sparse arrays become `undefined`.
|
|
453
|
+
* @template T
|
|
454
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
455
|
+
* @param {number} from - Index of the item to move, in `[-length, length - 1]`
|
|
456
|
+
* @param {number} to - Index of the item in the result, in `[-length, length - 1]`
|
|
457
|
+
* @returns {T[]} A new array, a permutation of the input
|
|
458
|
+
* @throws {RangeError} when `from` or `to` is not an integer or is out of range (any index is out of range for an empty array)
|
|
459
|
+
* @throws {TypeError} when `from` or `to` is not a number
|
|
460
|
+
* @example
|
|
461
|
+
* move(['a', 'b', 'c', 'd'], 0, 2); // => ['b', 'c', 'a', 'd']
|
|
462
|
+
* move(['a', 'b', 'c'], -1, 0); // => ['c', 'a', 'b']
|
|
463
|
+
*/
|
|
464
|
+
export declare function move<T>(array: ReadonlyArray<T>, from: number, to: number): T[];
|
|
465
|
+
/**
|
|
466
|
+
* Return a copy of an array with `items` inserted before position `index` (the input is not mutated).
|
|
467
|
+
* Negative indices count from the end, like `Array.prototype.at` / `splice`: `-1` inserts before the last item;
|
|
468
|
+
* `index === length` appends. Holes of sparse arrays become `undefined`.
|
|
469
|
+
* @template T
|
|
470
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
471
|
+
* @param {number} index - Insertion position, in `[-length, length]`
|
|
472
|
+
* @param {...T} items - The items to insert (in order)
|
|
473
|
+
* @returns {T[]} A new array of length `array.length + items.length`
|
|
474
|
+
* @throws {RangeError} when `index` is not an integer or is out of range
|
|
475
|
+
* @throws {TypeError} when `index` is not a number
|
|
476
|
+
* @example
|
|
477
|
+
* insertAt([1, 2, 3], 1, 9, 8); // => [1, 9, 8, 2, 3]
|
|
478
|
+
* insertAt([1, 2, 3], -1, 9); // => [1, 2, 9, 3]
|
|
479
|
+
* insertAt([1, 2, 3], 3, 9); // => [1, 2, 3, 9]
|
|
480
|
+
*/
|
|
481
|
+
export declare function insertAt<T>(array: ReadonlyArray<T>, index: number, ...items: T[]): T[];
|
|
482
|
+
/**
|
|
483
|
+
* Return a copy of an array without the `count` items starting at `index` (the input is not mutated).
|
|
484
|
+
* Negative indices count from the end, like `Array.prototype.at`. Like `splice`, `count` is clamped to the items available
|
|
485
|
+
* (pass `array.length` to remove everything from `index` on). Holes of sparse arrays become `undefined`.
|
|
486
|
+
* @template T
|
|
487
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
488
|
+
* @param {number} index - Index of the first item to remove, in `[-length, length - 1]`
|
|
489
|
+
* @param {number} [count=1] - How many items to remove (a non-negative integer)
|
|
490
|
+
* @returns {T[]} A new array without the removed items
|
|
491
|
+
* @throws {RangeError} when `index` is not an integer or is out of range (any index is out of range for an empty array)
|
|
492
|
+
* @throws {RangeError} when `count` is not a non-negative integer
|
|
493
|
+
* @throws {TypeError} when `index` or `count` is not a number
|
|
494
|
+
* @example
|
|
495
|
+
* removeAt([1, 2, 3, 4], 1); // => [1, 3, 4]
|
|
496
|
+
* removeAt([1, 2, 3, 4], -2, 2); // => [1, 2]
|
|
497
|
+
*/
|
|
498
|
+
export declare function removeAt<T>(array: ReadonlyArray<T>, index: number, count?: number): T[];
|
|
499
|
+
/**
|
|
500
|
+
* Find, by binary search (O(log n)), the LOWEST index at which `value` can be inserted into `sortedArray` keeping it sorted
|
|
501
|
+
* (before any equal items). Without a comparator, numbers, strings or bigints are ordered with `<` (strings by UTF-16 code units).
|
|
502
|
+
* The array must already be sorted in that order (and contain no `NaN`); otherwise the result is unspecified.
|
|
503
|
+
* @template T - A number, string or bigint type
|
|
504
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted in ascending `<` order
|
|
505
|
+
* @param {T} value - The value to locate
|
|
506
|
+
* @returns {number} An index in `[0, length]`
|
|
507
|
+
* @throws {RangeError} when `value` is `NaN`
|
|
508
|
+
* @example
|
|
509
|
+
* sortedIndex([10, 20, 20, 30], 20); // => 1
|
|
510
|
+
* sortedIndex([10, 20, 30], 99); // => 3
|
|
511
|
+
*/
|
|
512
|
+
export declare function sortedIndex<T extends number | string | bigint>(sortedArray: ReadonlyArray<T>, value: T): number;
|
|
513
|
+
/**
|
|
514
|
+
* Find, by binary search (O(log n)), the LOWEST index at which `value` can be inserted into `sortedArray` keeping it sorted
|
|
515
|
+
* according to `compare` (before any items comparing equal). The array must already be sorted by `compare`.
|
|
516
|
+
* @template T
|
|
517
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted according to `compare`
|
|
518
|
+
* @param {T} value - The value to locate
|
|
519
|
+
* @param {Comparator<T>} compare - The comparator the array is sorted by
|
|
520
|
+
* @returns {number} An index in `[0, length]`
|
|
521
|
+
* @throws {TypeError} when `compare` is given but is not a function
|
|
522
|
+
* @example
|
|
523
|
+
* sortedIndex([{ n: 1 }, { n: 5 }], { n: 3 }, function (a, b) { return a.n - b.n; }); // => 1
|
|
524
|
+
*/
|
|
525
|
+
export declare function sortedIndex<T>(sortedArray: ReadonlyArray<T>, value: T, compare: Comparator<T>): number;
|
|
526
|
+
/**
|
|
527
|
+
* Find the index of `value` in `sortedArray` by binary search (O(log n)); with duplicates, the LOWEST matching index is returned.
|
|
528
|
+
* Without a comparator, numbers, strings or bigints are ordered with `<`. The array must already be sorted in that order.
|
|
529
|
+
* @template T - A number, string or bigint type
|
|
530
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted in ascending `<` order
|
|
531
|
+
* @param {T} value - The value to find
|
|
532
|
+
* @returns {number} The index of `value`, or `-1` when it is absent
|
|
533
|
+
* @throws {RangeError} when `value` is `NaN`
|
|
534
|
+
* @example
|
|
535
|
+
* binarySearch([1, 3, 5, 7], 5); // => 2
|
|
536
|
+
* binarySearch([1, 3, 5, 7], 4); // => -1
|
|
537
|
+
*/
|
|
538
|
+
export declare function binarySearch<T extends number | string | bigint>(sortedArray: ReadonlyArray<T>, value: T): number;
|
|
539
|
+
/**
|
|
540
|
+
* Find the index of an item comparing equal (`compare(item, value) === 0`) to `value` in `sortedArray` by binary search;
|
|
541
|
+
* with several equal items, the LOWEST index is returned. The array must already be sorted by `compare`.
|
|
542
|
+
* @template T
|
|
543
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted according to `compare`
|
|
544
|
+
* @param {T} value - The value to find
|
|
545
|
+
* @param {Comparator<T>} compare - The comparator the array is sorted by
|
|
546
|
+
* @returns {number} The index of a matching item, or `-1`
|
|
547
|
+
* @throws {TypeError} when `compare` is given but is not a function
|
|
548
|
+
* @example
|
|
549
|
+
* binarySearch(['b', 'c'], 'B', function (a, b) { return a.toLowerCase() < b.toLowerCase() ? -1 : a.toLowerCase() > b.toLowerCase() ? 1 : 0; }); // => 0
|
|
550
|
+
*/
|
|
551
|
+
export declare function binarySearch<T>(sortedArray: ReadonlyArray<T>, value: T, compare: Comparator<T>): number;
|
|
552
|
+
/**
|
|
553
|
+
* Return the cartesian product of the given arrays: every tuple taking one item from each array, in lexicographic order
|
|
554
|
+
* (the LAST array varies fastest). The result has `product of the lengths` tuples: it is empty when any array is empty,
|
|
555
|
+
* and is `[[]]` (one empty tuple) when called without arrays. The tuples must not hold more than `2 ** 32 - 1` items in all (checked
|
|
556
|
+
* before anything is allocated). That limit is far more than a heap can hold: callers must bound untrusted inputs (e.g. option lists
|
|
557
|
+
* of a product configurator), since a result that does not fit in memory aborts the whole process with a fatal "heap out of memory"
|
|
558
|
+
* error rather than throwing.
|
|
559
|
+
* @template T - The tuple of input array types
|
|
560
|
+
* @param {...ReadonlyArray<unknown>} arrays - The arrays to combine
|
|
561
|
+
* @returns {Array<Zipped<T>>} A new array of fresh tuples
|
|
562
|
+
* @throws {RangeError} when the tuples would hold more than `2 ** 32 - 1` items in all
|
|
563
|
+
* @example
|
|
564
|
+
* cartesianProduct([1, 2], ['a', 'b']); // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']] // typed as [number, string][]
|
|
565
|
+
*/
|
|
566
|
+
export declare function cartesianProduct<T extends Array<ReadonlyArray<unknown>>>(...arrays: T): Array<Zipped<T>>;
|
|
567
|
+
/**
|
|
568
|
+
* Return a copy of an array with `separator` inserted between every two consecutive items.
|
|
569
|
+
* @template T - The item type
|
|
570
|
+
* @template S - The separator type
|
|
571
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
572
|
+
* @param {S} separator - The value to insert
|
|
573
|
+
* @returns {Array<T | S>} A new array of length `max(0, 2 * length - 1)`
|
|
574
|
+
* @example
|
|
575
|
+
* intersperse([1, 2, 3], 0); // => [1, 0, 2, 0, 3]
|
|
576
|
+
* intersperse(['a'], ','); // => ['a']
|
|
577
|
+
*/
|
|
578
|
+
export declare function intersperse<T, S>(array: ReadonlyArray<T>, separator: S): Array<T | S>;
|
|
579
|
+
/**
|
|
580
|
+
* Like {@link difference}, but items are compared by the key `keyFn` returns (SameValueZero): the items of `a` whose key is
|
|
581
|
+
* not the key of any item of `b`, keeping only the FIRST item of `a` per key. `differenceBy(a, b, identity)` equals `difference(a, b)`.
|
|
582
|
+
* `keyFn` is called once per item of both iterables.
|
|
583
|
+
* @template T - The item type of `a`
|
|
584
|
+
* @template U - The item type of `b`
|
|
585
|
+
* @param {Iterable<T>} a - The items to take from (defines the output order)
|
|
586
|
+
* @param {Iterable<U>} b - The items whose keys are excluded
|
|
587
|
+
* @param {(item: T | U) => unknown} keyFn - Returns the identity key of an item of either iterable
|
|
588
|
+
* @returns {T[]} The items of `a` with a distinct key missing from `b`, in first-appearance order
|
|
589
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
590
|
+
* @example
|
|
591
|
+
* differenceBy([{ id: 1 }, { id: 2 }], [{ id: 2 }], function (o) { return o.id; }); // => [{ id: 1 }]
|
|
592
|
+
* differenceBy([1.2, 2.5, 3.1], [2.1], Math.floor); // => [1.2, 3.1]
|
|
593
|
+
*/
|
|
594
|
+
export declare function differenceBy<T, U = T>(a: Iterable<T>, b: Iterable<U>, keyFn: (item: T | U) => unknown): T[];
|
|
595
|
+
/**
|
|
596
|
+
* Like {@link intersection}, but items are compared by the key `keyFn` returns (SameValueZero): the items of `a` whose key is
|
|
597
|
+
* also the key of an item of `b`, keeping only the FIRST item of `a` per key. `intersectionBy(a, b, identity)` equals `intersection(a, b)`.
|
|
598
|
+
* `keyFn` is called once per item of both iterables.
|
|
599
|
+
* @template T - The item type of `a`
|
|
600
|
+
* @template U - The item type of `b`
|
|
601
|
+
* @param {Iterable<T>} a - The items to take from (defines the output order)
|
|
602
|
+
* @param {Iterable<U>} b - The items whose keys are kept
|
|
603
|
+
* @param {(item: T | U) => unknown} keyFn - Returns the identity key of an item of either iterable
|
|
604
|
+
* @returns {T[]} The items of `a` with a distinct key also present in `b`, in first-appearance order
|
|
605
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
606
|
+
* @example
|
|
607
|
+
* intersectionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }], function (o) { return o.id; }); // => [{ id: 2 }]
|
|
608
|
+
*/
|
|
609
|
+
export declare function intersectionBy<T, U = T>(a: Iterable<T>, b: Iterable<U>, keyFn: (item: T | U) => unknown): T[];
|
|
610
|
+
/**
|
|
611
|
+
* One step of the edit script returned by {@link diffSequences}:
|
|
612
|
+
* - `'equal'`: `value` is `a[indexA]`, kept (it matches `b[indexB]`);
|
|
613
|
+
* - `'delete'`: `value` is `a[indexA]`, removed;
|
|
614
|
+
* - `'insert'`: `value` is `b[indexB]`, added.
|
|
615
|
+
* @template T - The element type
|
|
616
|
+
*/
|
|
617
|
+
export type DiffOperation<T> = {
|
|
618
|
+
type: 'equal';
|
|
619
|
+
value: T;
|
|
620
|
+
indexA: number;
|
|
621
|
+
indexB: number;
|
|
622
|
+
} | {
|
|
623
|
+
type: 'delete';
|
|
624
|
+
value: T;
|
|
625
|
+
indexA: number;
|
|
626
|
+
} | {
|
|
627
|
+
type: 'insert';
|
|
628
|
+
value: T;
|
|
629
|
+
indexB: number;
|
|
630
|
+
};
|
|
631
|
+
/**
|
|
632
|
+
* Options accepted by {@link diffSequences} and {@link longestCommonSubsequence}.
|
|
633
|
+
*/
|
|
634
|
+
export interface DiffOptions {
|
|
635
|
+
/**
|
|
636
|
+
* Largest number of edits (deletions plus insertions) accepted: when the sequences differ by more, a `RangeError` is thrown instead,
|
|
637
|
+
* after O((N + M) · maxEdits) work. An integer >= 0 or `Infinity`.
|
|
638
|
+
* @default Infinity
|
|
639
|
+
*/
|
|
640
|
+
maxEdits?: number;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Computes a minimal edit script turning sequence `a` into sequence `b`: a list of `'equal'`, `'delete'` and `'insert'` operations
|
|
644
|
+
* (see {@link DiffOperation}) with the smallest possible number of deletions plus insertions. Reading the script in order,
|
|
645
|
+
* the `'equal'` and `'delete'` values spell out `a`, and the `'equal'` and `'insert'` values spell out `b`. Within each run of
|
|
646
|
+
* changes, deletions come before insertions. When several minimal scripts exist, which one is returned is unspecified but
|
|
647
|
+
* deterministic.
|
|
648
|
+
*
|
|
649
|
+
* Uses Myers' O((N + M)·D) algorithm, in its linear-space divide-and-conquer form, after stripping the common prefix and suffix:
|
|
650
|
+
* time is O((N + M)·D) where D is the size of the edit script (so near-identical inputs are fast, and completely different ones
|
|
651
|
+
* cost O((N + M)²)), memory is O(N + M), and recursion depth is O(log D). Non-array iterables are copied first (strings are split
|
|
652
|
+
* into code points).
|
|
653
|
+
*
|
|
654
|
+
* **Untrusted input:** two unrelated texts of 16,000 characters already take seconds of CPU. Cap the input sizes, or pass
|
|
655
|
+
* `{ maxEdits }`, which bounds the work to O((N + M) · maxEdits) and throws a `RangeError` when the sequences differ by more.
|
|
656
|
+
* @template T - The element type
|
|
657
|
+
* @param {Iterable<T>} a - The original sequence
|
|
658
|
+
* @param {Iterable<T>} b - The target sequence
|
|
659
|
+
* @param {(x: T, y: T) => boolean} [equals=SameValueZero] - Element equality, always called as `equals(itemOfA, itemOfB)`;
|
|
660
|
+
* defaults to SameValueZero (`===`, except that `NaN` equals `NaN`). `'equal'` operations carry the element of `a`.
|
|
661
|
+
* @param {DiffOptions} [options={}] - `maxEdits` (default `Infinity`): the largest number of deletions plus insertions accepted
|
|
662
|
+
* @returns {Array<DiffOperation<T>>} The edit script, in order
|
|
663
|
+
* @throws {TypeError} when `a` or `b` is not iterable, `equals` is not a function, or `maxEdits` is not a number
|
|
664
|
+
* @throws {RangeError} when `maxEdits` is not an integer >= 0 or `Infinity`, or the sequences differ by more than `maxEdits` edits
|
|
665
|
+
* @example
|
|
666
|
+
* diffSequences([1, 2, 3], [1, 3, 4]); // => [{ type: 'equal', value: 1, indexA: 0, indexB: 0 }, { type: 'delete', value: 2, indexA: 1 }, { type: 'equal', value: 3, indexA: 2, indexB: 1 }, { type: 'insert', value: 4, indexB: 2 }]
|
|
667
|
+
* diffSequences('cat', 'cut').map(function (op) { return op.type; }); // => ['equal', 'delete', 'insert', 'equal']
|
|
668
|
+
* @example
|
|
669
|
+
* const before = ['Alice', 'bob'];
|
|
670
|
+
* const after = ['alice', 'Bob', 'Carol'];
|
|
671
|
+
* diffSequences(before, after, function (x, y) { return x.toLowerCase() === y.toLowerCase(); }).map(function (op) { return op.type; }); // => ['equal', 'equal', 'insert']
|
|
672
|
+
* @example
|
|
673
|
+
* diffSequences('kitten', 'sitting', undefined, { maxEdits: 5 }).length; // => 9
|
|
674
|
+
* diffSequences('a'.repeat(10000), 'b'.repeat(10000), undefined, { maxEdits: 100 }); // throws RangeError
|
|
675
|
+
*/
|
|
676
|
+
export declare function diffSequences<T>(a: Iterable<T>, b: Iterable<T>, equals?: (x: T, y: T) => boolean, options?: DiffOptions): Array<DiffOperation<T>>;
|
|
677
|
+
/**
|
|
678
|
+
* Computes a longest common subsequence of `a` and `b`: the longest list of elements that appear in both, in the same relative
|
|
679
|
+
* order (not necessarily contiguously). It is the `'equal'` part of {@link diffSequences}, with the same complexity
|
|
680
|
+
* (O((N + M)·D) time, O(N + M) memory). When several longest subsequences exist, which one is returned is unspecified but deterministic.
|
|
681
|
+
* **Untrusted input:** cap the input sizes or pass `{ maxEdits }`, as for {@link diffSequences}.
|
|
682
|
+
* @template T - The element type
|
|
683
|
+
* @param {Iterable<T>} a - The first sequence
|
|
684
|
+
* @param {Iterable<T>} b - The second sequence
|
|
685
|
+
* @param {(x: T, y: T) => boolean} [equals=SameValueZero] - Element equality, always called as `equals(itemOfA, itemOfB)`;
|
|
686
|
+
* defaults to SameValueZero. The returned elements are taken from `a`.
|
|
687
|
+
* @param {DiffOptions} [options={}] - `maxEdits` (default `Infinity`): the largest number of deletions plus insertions accepted
|
|
688
|
+
* @returns {T[]} The common subsequence, as a new array of elements of `a`
|
|
689
|
+
* @throws {TypeError} when `a` or `b` is not iterable, `equals` is not a function, or `maxEdits` is not a number
|
|
690
|
+
* @throws {RangeError} when `maxEdits` is not an integer >= 0 or `Infinity`, or the sequences differ by more than `maxEdits` edits
|
|
691
|
+
* @example
|
|
692
|
+
* longestCommonSubsequence([1, 2, 3, 4, 5], [2, 4, 6, 5]); // => [2, 4, 5]
|
|
693
|
+
* longestCommonSubsequence('ABCBDAB', 'BDCABA').length; // => 4
|
|
694
|
+
* longestCommonSubsequence([NaN, 0], [-0, NaN]).length; // => 1
|
|
695
|
+
*/
|
|
696
|
+
export declare function longestCommonSubsequence<T>(a: Iterable<T>, b: Iterable<T>, equals?: (x: T, y: T) => boolean, options?: DiffOptions): T[];
|
|
697
|
+
/**
|
|
698
|
+
* Like {@link union}, but items are compared by the key `keyFn` returns (SameValueZero): every item of `a`, then of `b`, whose key
|
|
699
|
+
* was not seen before, so only the FIRST item per key is kept. `unionBy(a, b, identity)` equals `union(a, b)`.
|
|
700
|
+
* `keyFn` is called once per item of both iterables.
|
|
701
|
+
* @template T - The item type of `a`
|
|
702
|
+
* @template U - The item type of `b`
|
|
703
|
+
* @param {Iterable<T>} a - The first items
|
|
704
|
+
* @param {Iterable<U>} b - The second items
|
|
705
|
+
* @param {(item: T | U) => unknown} keyFn - Returns the identity key of an item of either iterable
|
|
706
|
+
* @returns {Array<T | U>} One item per distinct key, in first-appearance order
|
|
707
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
708
|
+
* @example
|
|
709
|
+
* unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], function (o) { return o.id; }); // => [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
710
|
+
* unionBy([1.2, 2.5], [2.1, 3.4], Math.floor); // => [1.2, 2.5, 3.4]
|
|
711
|
+
*/
|
|
712
|
+
export declare function unionBy<T, U = T>(a: Iterable<T>, b: Iterable<U>, keyFn: (item: T | U) => unknown): Array<T | U>;
|
|
713
|
+
/**
|
|
714
|
+
* Like {@link symmetricDifference}, but items are compared by the key `keyFn` returns (SameValueZero): the items of `a` whose key
|
|
715
|
+
* is not the key of any item of `b`, followed by the items of `b` whose key is not the key of any item of `a`, keeping only the
|
|
716
|
+
* FIRST item per key. `keyFn` is called once per item of both iterables.
|
|
717
|
+
* @template T - The item type of `a`
|
|
718
|
+
* @template U - The item type of `b`
|
|
719
|
+
* @param {Iterable<T>} a - The first items
|
|
720
|
+
* @param {Iterable<U>} b - The second items
|
|
721
|
+
* @param {(item: T | U) => unknown} keyFn - Returns the identity key of an item of either iterable
|
|
722
|
+
* @returns {Array<T | U>} The items whose key appears in exactly one of the iterables, in first-appearance order
|
|
723
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
724
|
+
* @example
|
|
725
|
+
* symmetricDifferenceBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], function (o) { return o.id; }); // => [{ id: 1 }, { id: 3 }]
|
|
726
|
+
*/
|
|
727
|
+
export declare function symmetricDifferenceBy<T, U = T>(a: Iterable<T>, b: Iterable<U>, keyFn: (item: T | U) => unknown): Array<T | U>;
|
|
728
|
+
/**
|
|
729
|
+
* Remove the items `equals` to an earlier item, keeping the first occurrence: {@link unique} with a custom equality, for
|
|
730
|
+
* values that have no identity key (e.g. points within a tolerance). It compares every pair, in O(n²): when a key exists, prefer
|
|
731
|
+
* {@link uniqueBy}, which is O(n).
|
|
732
|
+
* @template T
|
|
733
|
+
* @param {Iterable<T>} iterable - The items to deduplicate
|
|
734
|
+
* @param {(x: T, y: T) => boolean} equals - Whether two items are duplicates (`x` is the candidate, `y` an item already kept)
|
|
735
|
+
* @returns {T[]} A new array of distinct items, in first-appearance order
|
|
736
|
+
* @throws {TypeError} when `equals` is not a function
|
|
737
|
+
* @example
|
|
738
|
+
* uniqueWith([1, 1.05, 2, 1.9], function (x, y) { return Math.abs(x - y) < 0.2; }); // => [1, 2]
|
|
739
|
+
*/
|
|
740
|
+
export declare function uniqueWith<T>(iterable: Iterable<T>, equals: (x: T, y: T) => boolean): T[];
|
|
741
|
+
/**
|
|
742
|
+
* {@link union} with a custom equality: every item of `a`, then of `b`, not `equals` to an item already kept (first occurrence
|
|
743
|
+
* wins). O(n²) in the total number of items: when a key exists, prefer {@link unionBy}, which is linear.
|
|
744
|
+
* @template T - The item type of `a`
|
|
745
|
+
* @template U - The item type of `b`
|
|
746
|
+
* @param {Iterable<T>} a - The first items
|
|
747
|
+
* @param {Iterable<U>} b - The second items
|
|
748
|
+
* @param {(x: T | U, y: T | U) => boolean} equals - Whether two items are the same (`x` is the candidate, `y` an item already kept)
|
|
749
|
+
* @returns {Array<T | U>} The distinct items of both iterables, in first-appearance order
|
|
750
|
+
* @throws {TypeError} when `equals` is not a function
|
|
751
|
+
* @example
|
|
752
|
+
* unionWith([{ x: 1, y: 2 }], [{ x: 1, y: 2 }, { x: 3, y: 4 }], function (p, q) { return p.x === q.x && p.y === q.y; }); // => [{ x: 1, y: 2 }, { x: 3, y: 4 }]
|
|
753
|
+
*/
|
|
754
|
+
export declare function unionWith<T, U = T>(a: Iterable<T>, b: Iterable<U>, equals: (x: T | U, y: T | U) => boolean): Array<T | U>;
|
|
755
|
+
/**
|
|
756
|
+
* {@link difference} with a custom equality: the items of `a` not `equals` to any item of `b`, keeping only the first of several
|
|
757
|
+
* items of `a` that are `equals` to each other. O(n·m): when a key exists, prefer {@link differenceBy}, which is linear.
|
|
758
|
+
* @template T - The item type of `a`
|
|
759
|
+
* @template U - The item type of `b`
|
|
760
|
+
* @param {Iterable<T>} a - The items to take from (defines the output order)
|
|
761
|
+
* @param {Iterable<U>} b - The items to exclude
|
|
762
|
+
* @param {(x: T, y: T | U) => boolean} equals - Whether two items are the same (`x` is always an item of `a`; `y` is an item of `b`, or an item of `a` already kept)
|
|
763
|
+
* @returns {T[]} The distinct items of `a` with no equal in `b`, in first-appearance order
|
|
764
|
+
* @throws {TypeError} when `equals` is not a function
|
|
765
|
+
* @example
|
|
766
|
+
* differenceWith([{ x: 1 }, { x: 2 }], [{ x: 2 }], function (p, q) { return p.x === q.x; }); // => [{ x: 1 }]
|
|
767
|
+
*/
|
|
768
|
+
export declare function differenceWith<T, U = T>(a: Iterable<T>, b: Iterable<U>, equals: (x: T, y: T | U) => boolean): T[];
|
|
769
|
+
/**
|
|
770
|
+
* {@link intersection} with a custom equality: the items of `a` `equals` to some item of `b`, keeping only the first of several
|
|
771
|
+
* items of `a` that are `equals` to each other. O(n·m): when a key exists, prefer {@link intersectionBy}, which is linear.
|
|
772
|
+
* @template T - The item type of `a`
|
|
773
|
+
* @template U - The item type of `b`
|
|
774
|
+
* @param {Iterable<T>} a - The items to take from (defines the output order)
|
|
775
|
+
* @param {Iterable<U>} b - The items to match
|
|
776
|
+
* @param {(x: T, y: T | U) => boolean} equals - Whether two items are the same (`x` is always an item of `a`; `y` is an item of `b`, or an item of `a` already kept)
|
|
777
|
+
* @returns {T[]} The distinct items of `a` with an equal in `b`, in first-appearance order
|
|
778
|
+
* @throws {TypeError} when `equals` is not a function
|
|
779
|
+
* @example
|
|
780
|
+
* intersectionWith([{ x: 1 }, { x: 2 }], [{ x: 2 }], function (p, q) { return p.x === q.x; }); // => [{ x: 2 }]
|
|
781
|
+
*/
|
|
782
|
+
export declare function intersectionWith<T, U = T>(a: Iterable<T>, b: Iterable<U>, equals: (x: T, y: T | U) => boolean): T[];
|
|
783
|
+
/**
|
|
784
|
+
* Return a sorted copy of an array, ordered by several keys: by the first selector's key, then items with equal first keys by the
|
|
785
|
+
* second selector's key, and so on, each key in its own direction. The sort is stable (items with equal keys in all selectors keep
|
|
786
|
+
* their input order) and each selector is called exactly once per item, not once per comparison.
|
|
787
|
+
*
|
|
788
|
+
* Every key follows the ordering rules of {@link sortBy}, which make the order TOTAL and deterministic: numbers/bigints numerically
|
|
789
|
+
* and before strings, strings by UTF-16 code units, `Date`s by timestamp, and `NaN`, invalid `Date`s, `null` and `undefined` keys
|
|
790
|
+
* after every other key in BOTH directions (equal to each other, so the next selector decides between them).
|
|
791
|
+
* @template T
|
|
792
|
+
* @param {ReadonlyArray<T>} array - The items to sort (not mutated)
|
|
793
|
+
* @param {ReadonlyArray<(item: T, index: number) => SortKey | null | undefined>} selectors - Return the keys of an item, most significant first (none: a plain copy)
|
|
794
|
+
* @param {SortOrder | ReadonlyArray<SortOrder>} [orders='asc'] - One direction for all keys, or one per selector (missing trailing ones are `'asc'`)
|
|
795
|
+
* @returns {T[]} A new sorted array
|
|
796
|
+
* @throws {TypeError} when `selectors` is not an array of functions, or `orders` is neither a direction nor an array
|
|
797
|
+
* @throws {RangeError} when a direction is neither `'asc'` nor `'desc'`, or there are more directions than selectors
|
|
798
|
+
* @example
|
|
799
|
+
* const users = [{ name: 'bo', age: 30 }, { name: 'al', age: 25 }, { name: 'cy', age: 30 }];
|
|
800
|
+
* orderBy(users, [function (u) { return u.age; }, function (u) { return u.name; }], ['desc', 'asc']); // => [{ name: 'bo', age: 30 }, { name: 'cy', age: 30 }, { name: 'al', age: 25 }]
|
|
801
|
+
* orderBy([3, NaN, 1], [function (n) { return n; }]); // => [1, 3, NaN]
|
|
802
|
+
*/
|
|
803
|
+
export declare function orderBy<T>(array: ReadonlyArray<T>, selectors: ReadonlyArray<(item: T, index: number) => SortKey | null | undefined>, orders?: SortOrder | ReadonlyArray<SortOrder>): T[];
|
|
804
|
+
/**
|
|
805
|
+
* Return the `n` first items of `sortBy(array, selector, order)` without sorting the whole array: a bounded heap selects them in
|
|
806
|
+
* O(length · log n), then only they are sorted. Same ordering rules as {@link sortBy} (stable on ties, `NaN`/invalid `Date`/nullish
|
|
807
|
+
* keys last in both directions), so the result always equals `sortBy(array, selector, order).slice(0, n)`.
|
|
808
|
+
* The selector is called exactly once per item. Use `'desc'` for the `n` largest ("top N").
|
|
809
|
+
* @template T
|
|
810
|
+
* @param {ReadonlyArray<T>} array - The items to rank (not mutated)
|
|
811
|
+
* @param {number} n - How many items to return (a non-negative integer; all of them when larger than the array)
|
|
812
|
+
* @param {(item: T, index: number) => SortKey | null | undefined} selector - Returns the ranking key of an item
|
|
813
|
+
* @param {SortOrder} [order='asc'] - `'asc'` for the smallest keys first, `'desc'` for the largest first
|
|
814
|
+
* @returns {T[]} A new array of at most `n` items, in rank order
|
|
815
|
+
* @throws {TypeError} when `n` is not a number or `selector` is not a function
|
|
816
|
+
* @throws {RangeError} when `n` is not a non-negative integer, or `order` is neither `'asc'` nor `'desc'`
|
|
817
|
+
* @example
|
|
818
|
+
* const scores = [{ id: 'a', score: 7 }, { id: 'b', score: 9 }, { id: 'c', score: 8 }];
|
|
819
|
+
* takeFirstBy(scores, 2, function (s) { return s.score; }, 'desc'); // => [{ id: 'b', score: 9 }, { id: 'c', score: 8 }]
|
|
820
|
+
* takeFirstBy([5, NaN, 1, 3], 3, function (n) { return n; }); // => [1, 3, 5]
|
|
821
|
+
*/
|
|
822
|
+
export declare function takeFirstBy<T>(array: ReadonlyArray<T>, n: number, selector: (item: T, index: number) => SortKey | null | undefined, order?: SortOrder): T[];
|
|
823
|
+
/**
|
|
824
|
+
* Return the item at position `index` of `sortBy(array, selector, order)` without sorting the whole array (a bounded heap of
|
|
825
|
+
* `position + 1` items, O(length · log position)): `0` is the first ranked item, `1` the second... Negative indices count from the end
|
|
826
|
+
* like `Array.prototype.at` (`-1` is the last ranked item). Same ordering rules as {@link sortBy}: stable on ties, and `NaN`/invalid
|
|
827
|
+
* `Date`/nullish keys ranked last in both directions. The selector is called exactly once per item.
|
|
828
|
+
* @template T
|
|
829
|
+
* @param {ReadonlyArray<T>} array - The items to rank (not mutated)
|
|
830
|
+
* @param {number} index - The rank to return, an integer (negative counts from the end)
|
|
831
|
+
* @param {(item: T, index: number) => SortKey | null | undefined} selector - Returns the ranking key of an item
|
|
832
|
+
* @param {SortOrder} [order='asc'] - `'asc'` to rank the smallest keys first, `'desc'` the largest first
|
|
833
|
+
* @returns {T | undefined} The item at that rank, or `undefined` when `index` is out of range
|
|
834
|
+
* @throws {TypeError} when `index` is not a number or `selector` is not a function
|
|
835
|
+
* @throws {RangeError} when `index` is not an integer, or `order` is neither `'asc'` nor `'desc'`
|
|
836
|
+
* @example
|
|
837
|
+
* nthBy([40, 10, 30, 20], 1, function (n) { return n; }); // => 20
|
|
838
|
+
* nthBy([40, 10, 30, 20], 0, function (n) { return n; }, 'desc'); // => 40
|
|
839
|
+
* nthBy([40, 10, 30, 20], -1, function (n) { return n; }); // => 40
|
|
840
|
+
* nthBy([1, 2], 5, function (n) { return n; }); // => undefined
|
|
841
|
+
*/
|
|
842
|
+
export declare function nthBy<T>(array: ReadonlyArray<T>, index: number, selector: (item: T, index: number) => SortKey | null | undefined, order?: SortOrder): T | undefined;
|
|
843
|
+
/**
|
|
844
|
+
* Find, by binary search (O(log n) selector calls), the LOWEST index at which `value` can be inserted into `sortedArray` keeping it
|
|
845
|
+
* sorted by the key `selector` returns (before any items with an equal key). Keys are compared with the ordering rules of
|
|
846
|
+
* {@link sortBy}, so any array sorted by `sortBy(items, selector, order)` qualifies, including its trailing `NaN`/invalid
|
|
847
|
+
* `Date`/nullish keys (an unorderable `value` key is located among them). For another order, use {@link sortedIndex} with a comparator.
|
|
848
|
+
* @template T
|
|
849
|
+
* @param {ReadonlyArray<T>} sortedArray - Items sorted as `sortBy(sortedArray, selector, order)` would
|
|
850
|
+
* @param {T} value - The item to locate (its key is computed once)
|
|
851
|
+
* @param {(item: T) => SortKey | null | undefined} selector - Returns the sort key of an item
|
|
852
|
+
* @param {SortOrder} [order='asc'] - The direction the array is sorted in
|
|
853
|
+
* @returns {number} An index in `[0, length]`
|
|
854
|
+
* @throws {TypeError} when `selector` is not a function
|
|
855
|
+
* @throws {RangeError} when `order` is neither `'asc'` nor `'desc'`
|
|
856
|
+
* @example
|
|
857
|
+
* const byAge = [{ age: 20 }, { age: 30 }, { age: 30 }, { age: 40 }];
|
|
858
|
+
* sortedIndexBy(byAge, { age: 30 }, function (p) { return p.age; }); // => 1
|
|
859
|
+
* sortedIndexBy([3, 2, 1], 2, function (n) { return n; }, 'desc'); // => 1
|
|
860
|
+
*/
|
|
861
|
+
export declare function sortedIndexBy<T>(sortedArray: ReadonlyArray<T>, value: T, selector: (item: T) => SortKey | null | undefined, order?: SortOrder): number;
|
|
862
|
+
/**
|
|
863
|
+
* Like {@link sortedIndexBy}, but returns the HIGHEST insertion index (after any items with an equal key), which keeps insertion
|
|
864
|
+
* order among equal keys when used to insert.
|
|
865
|
+
* @template T
|
|
866
|
+
* @param {ReadonlyArray<T>} sortedArray - Items sorted as `sortBy(sortedArray, selector, order)` would
|
|
867
|
+
* @param {T} value - The item to locate (its key is computed once)
|
|
868
|
+
* @param {(item: T) => SortKey | null | undefined} selector - Returns the sort key of an item
|
|
869
|
+
* @param {SortOrder} [order='asc'] - The direction the array is sorted in
|
|
870
|
+
* @returns {number} An index in `[0, length]`
|
|
871
|
+
* @throws {TypeError} when `selector` is not a function
|
|
872
|
+
* @throws {RangeError} when `order` is neither `'asc'` nor `'desc'`
|
|
873
|
+
* @example
|
|
874
|
+
* sortedLastIndexBy([{ age: 20 }, { age: 30 }, { age: 30 }, { age: 40 }], { age: 30 }, function (p) { return p.age; }); // => 3
|
|
875
|
+
*/
|
|
876
|
+
export declare function sortedLastIndexBy<T>(sortedArray: ReadonlyArray<T>, value: T, selector: (item: T) => SortKey | null | undefined, order?: SortOrder): number;
|
|
877
|
+
/**
|
|
878
|
+
* Find, by binary search (O(log n)), the HIGHEST index at which `value` can be inserted into `sortedArray` keeping it sorted (after
|
|
879
|
+
* any equal items): the upper-bound counterpart of {@link sortedIndex}. Without a comparator, numbers, strings or bigints are
|
|
880
|
+
* ordered with `<`; the array must already be sorted in that order (and contain no `NaN`), otherwise the result is unspecified.
|
|
881
|
+
* @template T - A number, string or bigint type
|
|
882
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted in ascending `<` order
|
|
883
|
+
* @param {T} value - The value to locate
|
|
884
|
+
* @returns {number} An index in `[0, length]`
|
|
885
|
+
* @throws {RangeError} when `value` is `NaN`
|
|
886
|
+
* @example
|
|
887
|
+
* sortedLastIndex([10, 20, 20, 30], 20); // => 3
|
|
888
|
+
* sortedLastIndex([10, 20, 30], 5); // => 0
|
|
889
|
+
*/
|
|
890
|
+
export declare function sortedLastIndex<T extends number | string | bigint>(sortedArray: ReadonlyArray<T>, value: T): number;
|
|
891
|
+
/**
|
|
892
|
+
* Find, by binary search (O(log n)), the HIGHEST index at which `value` can be inserted into `sortedArray` keeping it sorted
|
|
893
|
+
* according to `compare` (after any items comparing equal). The array must already be sorted by `compare`.
|
|
894
|
+
* @template T
|
|
895
|
+
* @param {ReadonlyArray<T>} sortedArray - An array sorted according to `compare`
|
|
896
|
+
* @param {T} value - The value to locate
|
|
897
|
+
* @param {Comparator<T>} compare - The comparator the array is sorted by
|
|
898
|
+
* @returns {number} An index in `[0, length]`
|
|
899
|
+
* @throws {TypeError} when `compare` is given but is not a function
|
|
900
|
+
* @example
|
|
901
|
+
* sortedLastIndex([{ n: 1 }, { n: 3 }, { n: 3 }], { n: 3 }, function (a, b) { return a.n - b.n; }); // => 3
|
|
902
|
+
*/
|
|
903
|
+
export declare function sortedLastIndex<T>(sortedArray: ReadonlyArray<T>, value: T, compare: Comparator<T>): number;
|
|
904
|
+
/**
|
|
905
|
+
* Return the last `n` items of an array (or all of them when it is shorter). Unlike `array.slice(-n)`, `n = 0` gives `[]`.
|
|
906
|
+
* @template T
|
|
907
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
908
|
+
* @param {number} n - How many items to take (a non-negative integer)
|
|
909
|
+
* @returns {T[]} A new array with at most `n` items
|
|
910
|
+
* @throws {RangeError} when `n` is negative or not an integer
|
|
911
|
+
* @throws {TypeError} when `n` is not a number
|
|
912
|
+
* @example
|
|
913
|
+
* takeLast([1, 2, 3], 2); // => [2, 3]
|
|
914
|
+
* takeLast([1, 2, 3], 0); // => []
|
|
915
|
+
*/
|
|
916
|
+
export declare function takeLast<T>(array: ReadonlyArray<T>, n: number): T[];
|
|
917
|
+
/**
|
|
918
|
+
* Return an array without its last `n` items.
|
|
919
|
+
* @template T
|
|
920
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
921
|
+
* @param {number} n - How many items to drop (a non-negative integer)
|
|
922
|
+
* @returns {T[]} A new array with the remaining items
|
|
923
|
+
* @throws {RangeError} when `n` is negative or not an integer
|
|
924
|
+
* @throws {TypeError} when `n` is not a number
|
|
925
|
+
* @example
|
|
926
|
+
* dropLast([1, 2, 3], 2); // => [1]
|
|
927
|
+
* dropLast([1, 2, 3], 0); // => [1, 2, 3]
|
|
928
|
+
*/
|
|
929
|
+
export declare function dropLast<T>(array: ReadonlyArray<T>, n: number): T[];
|
|
930
|
+
/**
|
|
931
|
+
* Return the longest suffix of an array whose items all match `predicate`, scanning from the end and stopping at the first item
|
|
932
|
+
* that does not match. The predicate receives each item with its index in the input.
|
|
933
|
+
* @template T
|
|
934
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
935
|
+
* @param {(item: T, index: number) => boolean} predicate - The condition the kept items meet
|
|
936
|
+
* @returns {T[]} A new array with the matching trailing items, in input order
|
|
937
|
+
* @throws {TypeError} when `predicate` is not a function
|
|
938
|
+
* @example
|
|
939
|
+
* takeLastWhile([1, 5, 2, 3], function (n) { return n < 4; }); // => [2, 3]
|
|
940
|
+
*/
|
|
941
|
+
export declare function takeLastWhile<T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): T[];
|
|
942
|
+
/**
|
|
943
|
+
* Return an array without its longest suffix whose items all match `predicate` (scanning from the end, stopping at the first item
|
|
944
|
+
* that does not match). The predicate receives each item with its index in the input.
|
|
945
|
+
* @template T
|
|
946
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
947
|
+
* @param {(item: T, index: number) => boolean} predicate - The condition the dropped items meet
|
|
948
|
+
* @returns {T[]} A new array with the remaining items
|
|
949
|
+
* @throws {TypeError} when `predicate` is not a function
|
|
950
|
+
* @example
|
|
951
|
+
* dropLastWhile(['a', 'b', '', ''], function (s) { return s === ''; }); // => ['a', 'b']
|
|
952
|
+
*/
|
|
953
|
+
export declare function dropLastWhile<T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): T[];
|
|
954
|
+
/**
|
|
955
|
+
* Split an array into runs of ADJACENT items with the same key (compared with SameValueZero, so `NaN` keys group together), like
|
|
956
|
+
* Python's `itertools.groupby`: unlike {@link groupBy}, a key that comes back after another one starts a new run.
|
|
957
|
+
* @template T
|
|
958
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
959
|
+
* @param {(item: T, index: number) => unknown} keyFn - Returns the key of an item (called once per item)
|
|
960
|
+
* @returns {T[][]} The runs, in input order (each a fresh, non-empty array); `[]` for an empty array
|
|
961
|
+
* @throws {TypeError} when `keyFn` is not a function
|
|
962
|
+
* @example
|
|
963
|
+
* chunkBy([1, 3, 2, 4, 5], function (n) { return n % 2; }); // => [[1, 3], [2, 4], [5]]
|
|
964
|
+
* chunkBy(['a', 'b', 'a'], function (s) { return s; }); // => [['a'], ['b'], ['a']]
|
|
965
|
+
*/
|
|
966
|
+
export declare function chunkBy<T>(array: ReadonlyArray<T>, keyFn: (item: T, index: number) => unknown): T[][];
|
|
967
|
+
/**
|
|
968
|
+
* Split an array in two at the first item matching `predicate`: the items before it, and the rest (starting with that item).
|
|
969
|
+
* When no item matches, the second part is empty.
|
|
970
|
+
* @template T
|
|
971
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
972
|
+
* @param {(item: T, index: number) => boolean} predicate - The condition that starts the second part
|
|
973
|
+
* @returns {[T[], T[]]} `[before, fromMatch]`, two new arrays
|
|
974
|
+
* @throws {TypeError} when `predicate` is not a function
|
|
975
|
+
* @example
|
|
976
|
+
* splitWhen([1, 2, 3, 2], function (n) { return n > 2; }); // => [[1, 2], [3, 2]]
|
|
977
|
+
* splitWhen([1, 2], function (n) { return n > 5; }); // => [[1, 2], []]
|
|
978
|
+
*/
|
|
979
|
+
export declare function splitWhen<T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): [T[], T[]];
|
|
980
|
+
/**
|
|
981
|
+
* Return a copy of an array with the items at positions `i` and `j` exchanged (the input is not mutated). Negative indices count
|
|
982
|
+
* from the end, like `Array.prototype.at`. Holes of sparse arrays become `undefined`.
|
|
983
|
+
* @template T
|
|
984
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
985
|
+
* @param {number} i - Index of the first item, in `[-length, length - 1]`
|
|
986
|
+
* @param {number} j - Index of the second item, in `[-length, length - 1]`
|
|
987
|
+
* @returns {T[]} A new array, a permutation of the input
|
|
988
|
+
* @throws {RangeError} when `i` or `j` is not an integer or is out of range (any index is out of range for an empty array)
|
|
989
|
+
* @throws {TypeError} when `i` or `j` is not a number
|
|
990
|
+
* @example
|
|
991
|
+
* swap(['a', 'b', 'c'], 0, 2); // => ['c', 'b', 'a']
|
|
992
|
+
* swap(['a', 'b', 'c'], 0, -2); // => ['b', 'a', 'c']
|
|
993
|
+
*/
|
|
994
|
+
export declare function swap<T>(array: ReadonlyArray<T>, i: number, j: number): T[];
|
|
995
|
+
/**
|
|
996
|
+
* Return a copy of an array rotated by `k` positions (the input is not mutated): a positive `k` moves every item `k` places towards
|
|
997
|
+
* the END, the last items wrapping around to the front (like Python's `deque.rotate`); a negative `k` rotates towards the start.
|
|
998
|
+
* `k` may exceed the length (it is taken modulo the length). Holes of sparse arrays become `undefined`.
|
|
999
|
+
* @template T
|
|
1000
|
+
* @param {ReadonlyArray<T>} array - The source array
|
|
1001
|
+
* @param {number} k - The number of positions, an integer (any sign or magnitude)
|
|
1002
|
+
* @returns {T[]} A new array, a permutation of the input
|
|
1003
|
+
* @throws {RangeError} when `k` is not an integer
|
|
1004
|
+
* @throws {TypeError} when `k` is not a number
|
|
1005
|
+
* @example
|
|
1006
|
+
* rotate([1, 2, 3, 4], 1); // => [4, 1, 2, 3]
|
|
1007
|
+
* rotate([1, 2, 3, 4], -1); // => [2, 3, 4, 1]
|
|
1008
|
+
* rotate([1, 2, 3], 7); // => [3, 1, 2]
|
|
1009
|
+
*/
|
|
1010
|
+
export declare function rotate<T>(array: ReadonlyArray<T>, k: number): T[];
|
|
1011
|
+
/**
|
|
1012
|
+
* Return a copy of an array where `item` is removed if present, or appended otherwise: the state change of a checkbox list or tag
|
|
1013
|
+
* picker. Presence is decided by SameValueZero (so `NaN` is found), or by comparing the keys `keyFn` returns. Every matching item
|
|
1014
|
+
* is removed (a list with duplicates loses all of them).
|
|
1015
|
+
* @template T
|
|
1016
|
+
* @param {ReadonlyArray<T>} array - The current items (not mutated)
|
|
1017
|
+
* @param {T} item - The item to toggle
|
|
1018
|
+
* @param {(item: T) => unknown} [keyFn] - Returns the identity key of an item (compared with SameValueZero)
|
|
1019
|
+
* @returns {T[]} A new array without the matching items, or with `item` appended when none matched
|
|
1020
|
+
* @throws {TypeError} when `keyFn` is given but is not a function
|
|
1021
|
+
* @example
|
|
1022
|
+
* toggle(['a', 'b'], 'b'); // => ['a']
|
|
1023
|
+
* toggle(['a', 'b'], 'c'); // => ['a', 'b', 'c']
|
|
1024
|
+
* toggle([{ id: 1 }, { id: 2 }], { id: 1 }, function (o) { return o.id; }); // => [{ id: 2 }]
|
|
1025
|
+
*/
|
|
1026
|
+
export declare function toggle<T>(array: ReadonlyArray<T>, item: T, keyFn?: (item: T) => unknown): T[];
|
|
1027
|
+
/**
|
|
1028
|
+
* Normalize a value that may be a single item, an array of items, or nothing, into a fresh array (lodash's `castArray`):
|
|
1029
|
+
* - an array is COPIED (shallowly), so the result can be mutated without touching the input (lodash returns the same array);
|
|
1030
|
+
* - `null` and `undefined` mean "nothing" and give `[]` (lodash gives `[undefined]` for `castArray(undefined)` but `[]` for
|
|
1031
|
+
* `castArray()`); to keep a nullish item, wrap it yourself: `[value]`;
|
|
1032
|
+
* - anything else, including strings, `Set`s and other iterables, is wrapped as ONE item (use `Array.from` to spread an iterable).
|
|
1033
|
+
* @template T
|
|
1034
|
+
* @param {T | ReadonlyArray<T> | null | undefined} value - An item, an array of items, or nothing
|
|
1035
|
+
* @returns {T[]} A new array
|
|
1036
|
+
* @example
|
|
1037
|
+
* toArray(1); // => [1]
|
|
1038
|
+
* toArray([1, 2]); // => [1, 2]
|
|
1039
|
+
* toArray(null); // => []
|
|
1040
|
+
* toArray('ab'); // => ['ab']
|
|
1041
|
+
*/
|
|
1042
|
+
export declare function toArray<T>(value: T | ReadonlyArray<T> | null | undefined): T[];
|
|
1043
|
+
/**
|
|
1044
|
+
* Build an object from a list of keys and a list of values: `{ [keys[i]]: values[i] }`, like `Object.fromEntries(zip(keys, values))`.
|
|
1045
|
+
* Keys are DEFINED as own data properties, never assigned: a `'__proto__'` key becomes an ordinary own property instead of
|
|
1046
|
+
* replacing the prototype (as it does in es-toolkit and radash), and no inherited setter or read-only property interferes.
|
|
1047
|
+
* The result is a plain object (`Object.prototype`). When a key repeats, the last value wins (its position stays the first one's).
|
|
1048
|
+
* @template K - The key type
|
|
1049
|
+
* @template V - The value type
|
|
1050
|
+
* @param {ReadonlyArray<K>} keys - The property keys (strings, numbers or symbols)
|
|
1051
|
+
* @param {ReadonlyArray<V>} values - The values, one per key
|
|
1052
|
+
* @returns {Record<K, V>} A new object
|
|
1053
|
+
* @throws {RangeError} when the arrays have different lengths (no key or value is silently dropped)
|
|
1054
|
+
* @example
|
|
1055
|
+
* zipObject(['a', 'b'], [1, 2]); // => { a: 1, b: 2 }
|
|
1056
|
+
* Object.keys(zipObject(['__proto__'], [1])); // => ['__proto__']
|
|
1057
|
+
* zipObject(['a', 'b'], [1]); // throws RangeError
|
|
1058
|
+
*/
|
|
1059
|
+
export declare function zipObject<const K extends PropertyKey, V>(keys: ReadonlyArray<K>, values: ReadonlyArray<V>): Record<K, V>;
|
|
1060
|
+
/**
|
|
1061
|
+
* Combine arrays element-wise with `fn`: `fn(a[i], b[i], ...)` for every index, truncating to the length of the SHORTEST array like
|
|
1062
|
+
* {@link zip} (lodash pads the shorter arrays with `undefined` instead, which `fn`'s parameter types would not reveal). The
|
|
1063
|
+
* arguments of `fn` are typed from the arrays, one per array.
|
|
1064
|
+
* @template T - The tuple of input array types
|
|
1065
|
+
* @template R - The result type of `fn`
|
|
1066
|
+
* @param {(...values: Zipped<T>) => R} fn - Combines the items at one index
|
|
1067
|
+
* @param {...ReadonlyArray<unknown>} arrays - The arrays to combine
|
|
1068
|
+
* @returns {R[]} A new array of `fn` results; empty when called without arrays
|
|
1069
|
+
* @throws {TypeError} when `fn` is not a function
|
|
1070
|
+
* @example
|
|
1071
|
+
* zipWith(function (a, b) { return a + b; }, [1, 2, 3], [10, 20]); // => [11, 22]
|
|
1072
|
+
* zipWith(function (name, age) { return `${name} (${age})`; }, ['al', 'bo'], [30, 25]); // => ['al (30)', 'bo (25)']
|
|
1073
|
+
*/
|
|
1074
|
+
export declare function zipWith<T extends Array<ReadonlyArray<unknown>>, R>(fn: (...values: Zipped<T>) => R, ...arrays: T): R[];
|
|
1075
|
+
/**
|
|
1076
|
+
* Return `n` distinct random items of an array (sampling WITHOUT replacement: positions are distinct, so equal values may repeat only
|
|
1077
|
+
* if the input repeats them). The result is in random order: a uniformly random arrangement of a uniformly random subset, so
|
|
1078
|
+
* `sampleSize(array, array.length)` is a shuffle. When `n` exceeds the length, every item is returned (like {@link take}).
|
|
1079
|
+
* Partial Fisher–Yates, on a copy when `n` is a large part of the array, otherwise over a virtual copy (O(n) time and memory
|
|
1080
|
+
* whatever the array length); both make exactly `min(n, length)` calls to `random` and give the same result for the same draws.
|
|
1081
|
+
* Not cryptographically secure by default: pass `secureRandom` (random module) for raffles, lotteries, audits...
|
|
1082
|
+
* @template T
|
|
1083
|
+
* @param {ReadonlyArray<T>} array - The items to pick from (not mutated)
|
|
1084
|
+
* @param {number} n - How many items to pick (a non-negative integer)
|
|
1085
|
+
* @param {RandomGenerator} [random=Math.random] - Generator of floats in `[0, 1)`, injectable for deterministic results
|
|
1086
|
+
* @returns {T[]} A new array of `min(n, length)` items
|
|
1087
|
+
* @throws {RangeError} when `n` is not a non-negative integer, or `random()` returns a value outside `[0, 1)`
|
|
1088
|
+
* @throws {TypeError} when `n` is not a number or `random` is not a function
|
|
1089
|
+
* @example
|
|
1090
|
+
* sampleSize([1, 2, 3, 4, 5], 2); // e.g. [4, 1]
|
|
1091
|
+
* sampleSize([1, 2, 3], 3, function () { return 0; }); // => [1, 2, 3]
|
|
1092
|
+
* sampleSize([1, 2], 5).length; // => 2
|
|
1093
|
+
*/
|
|
1094
|
+
export declare function sampleSize<T>(array: ReadonlyArray<T>, n: number, random?: RandomGenerator): T[];
|
|
1095
|
+
/**
|
|
1096
|
+
* Return the mean of the numbers `selector` returns for each item, added up with compensated summation like {@link sumBy} (so
|
|
1097
|
+
* `0.1`, `0.2`, `0.3` average to exactly `0.2`). Each key is converted once with unary `+`; `NaN` keys propagate.
|
|
1098
|
+
* @template T
|
|
1099
|
+
* @param {ReadonlyArray<T>} array - The items
|
|
1100
|
+
* @param {(item: T, index: number) => number} selector - Returns the number of an item
|
|
1101
|
+
* @returns {number} The mean, or `NaN` for an empty array (like `average` from the math module)
|
|
1102
|
+
* @throws {TypeError} when `selector` is not a function
|
|
1103
|
+
* @example
|
|
1104
|
+
* averageBy([{ n: 0.1 }, { n: 0.2 }, { n: 0.3 }], function (o) { return o.n; }); // => 0.2
|
|
1105
|
+
* averageBy([], function (n: number) { return n; }); // => NaN
|
|
1106
|
+
*/
|
|
1107
|
+
export declare function averageBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => number): number;
|
|
1108
|
+
/**
|
|
1109
|
+
* Return the median of the numbers `selector` returns for each item (the mean of the two middle values for an even count), with
|
|
1110
|
+
* the semantics of `median` from the math module: keys are converted once with unary `+`, and the result does not depend on the
|
|
1111
|
+
* item order.
|
|
1112
|
+
* @template T
|
|
1113
|
+
* @param {ReadonlyArray<T>} array - The items
|
|
1114
|
+
* @param {(item: T, index: number) => number} selector - Returns the number of an item
|
|
1115
|
+
* @returns {number} The median, or `NaN` when the array is empty or a key is `NaN`
|
|
1116
|
+
* @throws {TypeError} when `selector` is not a function
|
|
1117
|
+
* @example
|
|
1118
|
+
* medianBy([{ age: 30 }, { age: 20 }, { age: 50 }], function (p) { return p.age; }); // => 30
|
|
1119
|
+
* medianBy([4, 1, 3, 2], function (n) { return n; }); // => 2.5
|
|
1120
|
+
*/
|
|
1121
|
+
export declare function medianBy<T>(array: ReadonlyArray<T>, selector: (item: T, index: number) => number): number;
|
|
1122
|
+
/**
|
|
1123
|
+
* Return every combination of `k` items of an array (subsets of `k` POSITIONS, items in input order), in lexicographic order of
|
|
1124
|
+
* positions, like Python's `itertools.combinations`. There are `C(length, k)` of them: `[[]]` for `k = 0`, `[]` when `k > length`.
|
|
1125
|
+
* Positions are distinct, not values: duplicate inputs give duplicate-looking combinations (deduplicate the input first if needed).
|
|
1126
|
+
* The size is checked before anything is allocated: more than `2 ** 32 - 1` tuples or items in all throws, and since a result
|
|
1127
|
+
* that does not fit in memory aborts the process instead of throwing, callers must still bound untrusted sizes.
|
|
1128
|
+
* @template T
|
|
1129
|
+
* @param {ReadonlyArray<T>} array - The items to choose from
|
|
1130
|
+
* @param {number} k - The size of each combination (a non-negative integer)
|
|
1131
|
+
* @returns {T[][]} A new array of fresh tuples
|
|
1132
|
+
* @throws {TypeError} when `k` is not a number
|
|
1133
|
+
* @throws {RangeError} when `k` is not a non-negative integer, or the result would exceed `2 ** 32 - 1` tuples or items
|
|
1134
|
+
* @example
|
|
1135
|
+
* combinations(['a', 'b', 'c'], 2); // => [['a', 'b'], ['a', 'c'], ['b', 'c']]
|
|
1136
|
+
* combinations([1, 2], 3); // => []
|
|
1137
|
+
*/
|
|
1138
|
+
export declare function combinations<T>(array: ReadonlyArray<T>, k: number): T[][];
|
|
1139
|
+
/**
|
|
1140
|
+
* Return every arrangement of `k` items of an array (ordered selections of `k` distinct POSITIONS), in lexicographic order of
|
|
1141
|
+
* positions, like Python's `itertools.permutations`. There are `length! / (length - k)!` of them: `[[]]` for `k = 0`, `[]` when
|
|
1142
|
+
* `k > length`. Positions are distinct, not values: duplicate inputs give duplicate-looking permutations.
|
|
1143
|
+
* The size is checked before anything is allocated: more than `2 ** 32 - 1` tuples or items in all throws, and since a result
|
|
1144
|
+
* that does not fit in memory aborts the process instead of throwing, callers must still bound untrusted sizes.
|
|
1145
|
+
* @template T
|
|
1146
|
+
* @param {ReadonlyArray<T>} array - The items to arrange
|
|
1147
|
+
* @param {number} [k=array.length] - The size of each permutation (a non-negative integer)
|
|
1148
|
+
* @returns {T[][]} A new array of fresh tuples
|
|
1149
|
+
* @throws {TypeError} when `k` is not a number
|
|
1150
|
+
* @throws {RangeError} when `k` is not a non-negative integer, or the result would exceed `2 ** 32 - 1` tuples or items
|
|
1151
|
+
* @example
|
|
1152
|
+
* permutations([1, 2, 3]); // => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
|
|
1153
|
+
* permutations(['a', 'b', 'c'], 2); // => [['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b']]
|
|
1154
|
+
*/
|
|
1155
|
+
export declare function permutations<T>(array: ReadonlyArray<T>, k?: number): T[][];
|
|
1156
|
+
/**
|
|
1157
|
+
* Return every subset of an array (`2 ** length` of them, items in input order), in binary-counting order: item `i` is in subset
|
|
1158
|
+
* `m` when bit `i` of `m` is set, so `[]` comes first and the whole array last. Positions are distinct, not values.
|
|
1159
|
+
* The size is checked before anything is allocated (more than `2 ** 32 - 1` subsets or items in all throws, i.e. inputs longer
|
|
1160
|
+
* than 28 items); since a result that does not fit in memory aborts the process instead of throwing, keep inputs short.
|
|
1161
|
+
* @template T
|
|
1162
|
+
* @param {ReadonlyArray<T>} array - The items
|
|
1163
|
+
* @returns {T[][]} A new array of fresh subsets
|
|
1164
|
+
* @throws {RangeError} when the result would exceed `2 ** 32 - 1` subsets or items
|
|
1165
|
+
* @example
|
|
1166
|
+
* powerSet(['a', 'b', 'c']); // => [[], ['a'], ['b'], ['a', 'b'], ['c'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
|
|
1167
|
+
*/
|
|
1168
|
+
export declare function powerSet<T>(array: ReadonlyArray<T>): T[][];
|