@oscarpalmer/atoms 0.192.0 → 0.193.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/dist/index.d.mts +25 -23
- package/dist/index.mjs +30 -31
- package/dist/queue.d.mts +25 -23
- package/dist/queue.mjs +27 -28
- package/package.json +3 -4
- package/src/array/difference.ts +0 -73
- package/src/array/exists.ts +0 -93
- package/src/array/filter.ts +0 -182
- package/src/array/find.ts +0 -183
- package/src/array/first.ts +0 -196
- package/src/array/flatten.ts +0 -20
- package/src/array/from.ts +0 -129
- package/src/array/get.ts +0 -131
- package/src/array/group-by.ts +0 -369
- package/src/array/index.ts +0 -26
- package/src/array/insert.ts +0 -52
- package/src/array/intersection.ts +0 -72
- package/src/array/last.ts +0 -194
- package/src/array/match.ts +0 -436
- package/src/array/move.ts +0 -315
- package/src/array/partition.ts +0 -91
- package/src/array/push.ts +0 -23
- package/src/array/reverse.ts +0 -33
- package/src/array/select.ts +0 -260
- package/src/array/single.ts +0 -93
- package/src/array/slice.ts +0 -335
- package/src/array/sort.ts +0 -607
- package/src/array/splice.ts +0 -106
- package/src/array/swap.ts +0 -322
- package/src/array/to-map.ts +0 -371
- package/src/array/to-record.ts +0 -351
- package/src/array/to-set.ts +0 -80
- package/src/array/toggle.ts +0 -79
- package/src/array/union.ts +0 -72
- package/src/array/unique.ts +0 -67
- package/src/array/update.ts +0 -75
- package/src/beacon.ts +0 -378
- package/src/color/constants.ts +0 -137
- package/src/color/index.ts +0 -83
- package/src/color/instance.ts +0 -162
- package/src/color/misc/alpha.ts +0 -56
- package/src/color/misc/format.ts +0 -29
- package/src/color/misc/get.ts +0 -210
- package/src/color/misc/is.ts +0 -221
- package/src/color/misc/state.ts +0 -243
- package/src/color/models.ts +0 -389
- package/src/color/space/hex.ts +0 -184
- package/src/color/space/hsl.ts +0 -174
- package/src/color/space/hwb.ts +0 -153
- package/src/color/space/rgb.ts +0 -240
- package/src/function/assert.ts +0 -174
- package/src/function/index.ts +0 -2
- package/src/function/limit.ts +0 -83
- package/src/function/memoize.ts +0 -202
- package/src/function/once.ts +0 -200
- package/src/function/retry.ts +0 -175
- package/src/function/work.ts +0 -1007
- package/src/herald.ts +0 -216
- package/src/index.ts +0 -68
- package/src/internal/array/callbacks.ts +0 -46
- package/src/internal/array/chunk.ts +0 -51
- package/src/internal/array/compact.ts +0 -56
- package/src/internal/array/find.ts +0 -200
- package/src/internal/array/group.ts +0 -42
- package/src/internal/array/index-of.ts +0 -182
- package/src/internal/array/insert.ts +0 -80
- package/src/internal/array/overlap.ts +0 -35
- package/src/internal/array/sets.ts +0 -74
- package/src/internal/array/shuffle.ts +0 -33
- package/src/internal/array/update.ts +0 -52
- package/src/internal/function/misc.ts +0 -8
- package/src/internal/function/timer.ts +0 -168
- package/src/internal/is.ts +0 -244
- package/src/internal/math/aggregate.ts +0 -164
- package/src/internal/math/misc.ts +0 -28
- package/src/internal/number.ts +0 -129
- package/src/internal/random.ts +0 -49
- package/src/internal/result.ts +0 -92
- package/src/internal/sized.ts +0 -25
- package/src/internal/string.ts +0 -128
- package/src/internal/subscription.ts +0 -264
- package/src/internal/value/compare.ts +0 -170
- package/src/internal/value/equal.ts +0 -465
- package/src/internal/value/get.ts +0 -57
- package/src/internal/value/handlers.ts +0 -95
- package/src/internal/value/has.ts +0 -118
- package/src/internal/value/misc.ts +0 -125
- package/src/internal/value/partial.ts +0 -46
- package/src/internal/value/set.ts +0 -132
- package/src/is.ts +0 -182
- package/src/logger.ts +0 -230
- package/src/math.ts +0 -281
- package/src/models.ts +0 -286
- package/src/number.ts +0 -1
- package/src/promise/delay.ts +0 -62
- package/src/promise/helpers.ts +0 -96
- package/src/promise/index.ts +0 -300
- package/src/promise/misc.ts +0 -96
- package/src/promise/models.ts +0 -195
- package/src/promise/timed.ts +0 -91
- package/src/query.ts +0 -147
- package/src/queue.ts +0 -814
- package/src/random.ts +0 -125
- package/src/result/index.ts +0 -105
- package/src/result/match.ts +0 -124
- package/src/result/misc.ts +0 -129
- package/src/result/models.ts +0 -83
- package/src/result/work/flow.ts +0 -488
- package/src/result/work/pipe.ts +0 -496
- package/src/sized/map.ts +0 -110
- package/src/sized/set.ts +0 -107
- package/src/string/case.ts +0 -236
- package/src/string/fuzzy.ts +0 -444
- package/src/string/index.ts +0 -199
- package/src/string/match.ts +0 -80
- package/src/string/normalize.ts +0 -205
- package/src/string/template.ts +0 -161
- package/src/value/clone.ts +0 -319
- package/src/value/collection.ts +0 -63
- package/src/value/diff.ts +0 -232
- package/src/value/freeze.ts +0 -322
- package/src/value/handle.ts +0 -3
- package/src/value/index.ts +0 -5
- package/src/value/merge.ts +0 -278
- package/src/value/omit.ts +0 -20
- package/src/value/pick.ts +0 -20
- package/src/value/shake.ts +0 -45
- package/src/value/smush.ts +0 -89
- package/src/value/transform.ts +0 -153
- package/src/value/unsmush.ts +0 -84
package/src/array/swap.ts
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import {getArrayCallback} from '../internal/array/callbacks';
|
|
2
|
-
import {indexOf} from '../internal/array/index-of';
|
|
3
|
-
import {arraysOverlap} from '../internal/array/overlap';
|
|
4
|
-
import type {PlainObject} from '../models';
|
|
5
|
-
import {indexOfArray} from './match';
|
|
6
|
-
|
|
7
|
-
// #region Functions
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Swap two smaller arrays within a larger array
|
|
11
|
-
*
|
|
12
|
-
* If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
|
|
13
|
-
*
|
|
14
|
-
* @param array Array of items to swap
|
|
15
|
-
* @param first First array
|
|
16
|
-
* @param second Second array
|
|
17
|
-
* @param callback Callback to get an item's value for matching
|
|
18
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* swap(
|
|
23
|
-
* [
|
|
24
|
-
* {id: 1, name: 'Alice'},
|
|
25
|
-
* {id: 2, name: 'Bob'},
|
|
26
|
-
* {id: 3, name: 'Charlie'},
|
|
27
|
-
* ],
|
|
28
|
-
* [
|
|
29
|
-
* {id: 2, name: 'Bob'},
|
|
30
|
-
* ],
|
|
31
|
-
* [
|
|
32
|
-
* {id: 3, name: 'Charlie'},
|
|
33
|
-
* ],
|
|
34
|
-
* item => item.id,
|
|
35
|
-
* ); // => [
|
|
36
|
-
* // {id: 1, name: 'Alice'},
|
|
37
|
-
* // {id: 3, name: 'Charlie'},
|
|
38
|
-
* // {id: 2, name: 'Bob'},
|
|
39
|
-
* // ]
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export function swap<Item>(
|
|
43
|
-
array: Item[],
|
|
44
|
-
first: Item[],
|
|
45
|
-
second: Item[],
|
|
46
|
-
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
47
|
-
): Item[];
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Swap two smaller arrays within a larger array
|
|
51
|
-
*
|
|
52
|
-
* If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
|
|
53
|
-
*
|
|
54
|
-
* @param array Array of items to swap
|
|
55
|
-
* @param first First array
|
|
56
|
-
* @param second Second array
|
|
57
|
-
* @param key Key to get an item's value for matching
|
|
58
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```typescript
|
|
62
|
-
* swap(
|
|
63
|
-
* [
|
|
64
|
-
* {id: 1, name: 'Alice'},
|
|
65
|
-
* {id: 2, name: 'Bob'},
|
|
66
|
-
* {id: 3, name: 'Charlie'},
|
|
67
|
-
* ],
|
|
68
|
-
* [
|
|
69
|
-
* {id: 2, name: 'Bob'},
|
|
70
|
-
* ],
|
|
71
|
-
* [
|
|
72
|
-
* {id: 3, name: 'Charlie'},
|
|
73
|
-
* ],
|
|
74
|
-
* 'id',
|
|
75
|
-
* ); // => [
|
|
76
|
-
* // {id: 1, name: 'Alice'},
|
|
77
|
-
* // {id: 3, name: 'Charlie'},
|
|
78
|
-
* // {id: 2, name: 'Bob'},
|
|
79
|
-
* // ]
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
83
|
-
array: Item[],
|
|
84
|
-
first: Item[],
|
|
85
|
-
second: Item[],
|
|
86
|
-
key: ItemKey,
|
|
87
|
-
): Item[];
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Swap two smaller arrays within a larger array
|
|
91
|
-
*
|
|
92
|
-
* If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
|
|
93
|
-
*
|
|
94
|
-
* @param array Array of items to swap
|
|
95
|
-
* @param first First array
|
|
96
|
-
* @param second Second array
|
|
97
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* swap(
|
|
102
|
-
* [1, 2, 3, 4, 5, 6],
|
|
103
|
-
* [1, 2],
|
|
104
|
-
* [5, 6],
|
|
105
|
-
* ); // => [5, 6, 3, 4, 1, 2]
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
export function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[];
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Swap two indiced items in an array
|
|
112
|
-
*
|
|
113
|
-
* If either of the items are not present in the array, the array will be returned unchanged
|
|
114
|
-
*
|
|
115
|
-
* @param array Array of items to swap
|
|
116
|
-
* @param first First item
|
|
117
|
-
* @param second Second item
|
|
118
|
-
* @param callback Callback to get an item's value for matching
|
|
119
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```typescript
|
|
123
|
-
* swap(
|
|
124
|
-
* [
|
|
125
|
-
* {id: 1, name: 'Alice'},
|
|
126
|
-
* {id: 2, name: 'Bob'},
|
|
127
|
-
* {id: 3, name: 'Charlie'},
|
|
128
|
-
* ],
|
|
129
|
-
* {id: 2, name: 'Bob'},
|
|
130
|
-
* {id: 3, name: 'Charlie'},
|
|
131
|
-
* item => item.id,
|
|
132
|
-
* ); // => [
|
|
133
|
-
* // {id: 1, name: 'Alice'},
|
|
134
|
-
* // {id: 3, name: 'Charlie'},
|
|
135
|
-
* // {id: 2, name: 'Bob'},
|
|
136
|
-
* // ]
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
export function swap<Item>(
|
|
140
|
-
array: Item[],
|
|
141
|
-
first: Item,
|
|
142
|
-
second: Item,
|
|
143
|
-
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
144
|
-
): Item[];
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Swap two indiced items in an array
|
|
148
|
-
*
|
|
149
|
-
* If either of the items are not present in the array, the array will be returned unchanged
|
|
150
|
-
*
|
|
151
|
-
* @param array Array of items to swap
|
|
152
|
-
* @param first First item
|
|
153
|
-
* @param second Second item
|
|
154
|
-
* @param key Key to get an item's value for matching
|
|
155
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* swap(
|
|
160
|
-
* [
|
|
161
|
-
* {id: 1, name: 'Alice'},
|
|
162
|
-
* {id: 2, name: 'Bob'},
|
|
163
|
-
* {id: 3, name: 'Charlie'},
|
|
164
|
-
* ],
|
|
165
|
-
* {id: 2, name: 'Bob'},
|
|
166
|
-
* {id: 3, name: 'Charlie'},
|
|
167
|
-
* 'id',
|
|
168
|
-
* ); // => [
|
|
169
|
-
* // {id: 1, name: 'Alice'},
|
|
170
|
-
* // {id: 3, name: 'Charlie'},
|
|
171
|
-
* // {id: 2, name: 'Bob'},
|
|
172
|
-
* // ]
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
|
-
export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
176
|
-
array: Item[],
|
|
177
|
-
first: Item,
|
|
178
|
-
second: Item,
|
|
179
|
-
key: ItemKey,
|
|
180
|
-
): Item[];
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Swap two indiced items in an array
|
|
184
|
-
*
|
|
185
|
-
* @param array Array of items to swap
|
|
186
|
-
* @param first First item
|
|
187
|
-
* @param second Second item
|
|
188
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
189
|
-
*
|
|
190
|
-
* @example
|
|
191
|
-
* ```typescript
|
|
192
|
-
* swap(
|
|
193
|
-
* [1, 2, 3, 4, 5, 6],
|
|
194
|
-
* 2,
|
|
195
|
-
* 5,
|
|
196
|
-
* ); // => [1, 5, 3, 4, 2, 6]
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
export function swap<Item>(array: Item[], first: Item, second: Item): Item[];
|
|
200
|
-
|
|
201
|
-
export function swap(
|
|
202
|
-
array: unknown[],
|
|
203
|
-
first: unknown,
|
|
204
|
-
second: unknown,
|
|
205
|
-
third?: unknown,
|
|
206
|
-
): unknown[] {
|
|
207
|
-
if (!Array.isArray(array)) {
|
|
208
|
-
return [];
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (array.length === 0) {
|
|
212
|
-
return array;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (Array.isArray(first) && Array.isArray(second)) {
|
|
216
|
-
return swapArrays(array, first, second, third);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return swapValues(array, first, second, third);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
swap.indices = swapIndices;
|
|
223
|
-
|
|
224
|
-
function swapArrays(array: unknown[], from: unknown[], to: unknown[], key: unknown): unknown[] {
|
|
225
|
-
if (from.length === 0 || to.length === 0) {
|
|
226
|
-
return array;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (from.length === 1 && to.length === 1) {
|
|
230
|
-
return swapValues(array, from[0], to[0], key);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const fromIndex = indexOfArray(array, from, key as never);
|
|
234
|
-
const toIndex = indexOfArray(array, to, key as never);
|
|
235
|
-
|
|
236
|
-
if (fromIndex === -1 || toIndex === -1) {
|
|
237
|
-
return array;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const {first, second, overlap} = arraysOverlap(
|
|
241
|
-
{
|
|
242
|
-
array: from,
|
|
243
|
-
index: fromIndex,
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
array: to,
|
|
247
|
-
index: toIndex,
|
|
248
|
-
},
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
if (overlap) {
|
|
252
|
-
// The arrays overlap, so we can't swap them without losing data
|
|
253
|
-
|
|
254
|
-
return array;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
array.splice(first.index, first.array.length, ...second.array);
|
|
258
|
-
|
|
259
|
-
array.splice(
|
|
260
|
-
second.index + (second.array.length - first.array.length),
|
|
261
|
-
second.array.length,
|
|
262
|
-
...first.array,
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
return array;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Swap two indiced items in an array
|
|
270
|
-
*
|
|
271
|
-
* If either index is out of bounds, the array will be returned unchanged
|
|
272
|
-
*
|
|
273
|
-
* _Available as `swapIndices` and `swap.indices`_
|
|
274
|
-
*
|
|
275
|
-
* @param array Array of items to swap
|
|
276
|
-
* @param first First index _(can be negative to count from the end)_
|
|
277
|
-
* @param second Second index _(can be negative to count from the end)_
|
|
278
|
-
* @returns Original array with items swapped _(or unchanged if unable to swap)_
|
|
279
|
-
*/
|
|
280
|
-
function swapIndices<Item>(array: Item[], first: number, second: number): Item[] {
|
|
281
|
-
if (!Array.isArray(array) || array.length === 0) {
|
|
282
|
-
return [];
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (typeof first !== 'number' || typeof second !== 'number') {
|
|
286
|
-
return array;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const firstIndex = first < 0 ? array.length + first : first;
|
|
290
|
-
const secondIndex = second < 0 ? array.length + second : second;
|
|
291
|
-
|
|
292
|
-
if (firstIndex === secondIndex || firstIndex >= array.length || secondIndex >= array.length) {
|
|
293
|
-
return array;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const temp = array[firstIndex];
|
|
297
|
-
|
|
298
|
-
array[firstIndex] = array[secondIndex];
|
|
299
|
-
array[secondIndex] = temp;
|
|
300
|
-
|
|
301
|
-
return array;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function swapValues(array: unknown[], from: unknown, to: unknown, key?: unknown): unknown[] {
|
|
305
|
-
const callback = getArrayCallback(key);
|
|
306
|
-
|
|
307
|
-
const first =
|
|
308
|
-
callback == null
|
|
309
|
-
? array.indexOf(from)
|
|
310
|
-
: indexOf(array, callback, callback(from, undefined, []));
|
|
311
|
-
|
|
312
|
-
const second =
|
|
313
|
-
callback == null ? array.indexOf(to) : indexOf(array, callback, callback(to, undefined, []));
|
|
314
|
-
|
|
315
|
-
if (first === second || first === -1 || second === -1) {
|
|
316
|
-
return array;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
return swapIndices(array, first, second);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// #endregion
|
package/src/array/to-map.ts
DELETED
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
import {getArrayCallbacks} from '../internal/array/callbacks';
|
|
2
|
-
import type {Key, PlainObject} from '../models';
|
|
3
|
-
|
|
4
|
-
// #region Functions
|
|
5
|
-
|
|
6
|
-
function getMapValues(
|
|
7
|
-
array: unknown[],
|
|
8
|
-
first: unknown,
|
|
9
|
-
second: unknown,
|
|
10
|
-
arrays: boolean,
|
|
11
|
-
): Map<unknown, unknown> {
|
|
12
|
-
if (!Array.isArray(array)) {
|
|
13
|
-
return new Map();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const {length} = array;
|
|
17
|
-
const callbacks = getArrayCallbacks(undefined, first, second);
|
|
18
|
-
const map = new Map<Key, unknown>();
|
|
19
|
-
|
|
20
|
-
for (let index = 0; index < length; index += 1) {
|
|
21
|
-
const item = array[index];
|
|
22
|
-
|
|
23
|
-
const key = callbacks?.keyed?.(item, index, array) ?? index;
|
|
24
|
-
const value = callbacks?.value?.(item, index, array) ?? item;
|
|
25
|
-
|
|
26
|
-
if (arrays) {
|
|
27
|
-
const existing = map.get(key);
|
|
28
|
-
|
|
29
|
-
if (existing == null) {
|
|
30
|
-
map.set(key, [value]);
|
|
31
|
-
} else {
|
|
32
|
-
(existing as unknown[]).push(value);
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
map.set(key, value);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return map;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Create a _Map_ from an array of items using callbacks
|
|
44
|
-
*
|
|
45
|
-
* If multiple items have the same key, the latest item's value will be used
|
|
46
|
-
*
|
|
47
|
-
* @param array Array to convert
|
|
48
|
-
* @param key Callback to get an item's grouping key
|
|
49
|
-
* @param value Callback to get an item's value
|
|
50
|
-
* @returns _Map_ of keyed values
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```typescript
|
|
54
|
-
* toMap(
|
|
55
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
56
|
-
* item => item.value,
|
|
57
|
-
* item => item.id,
|
|
58
|
-
* ); // => Map { 10 => 3, 20 => 2 }
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export function toMap<
|
|
62
|
-
Item,
|
|
63
|
-
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
64
|
-
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
65
|
-
>(
|
|
66
|
-
array: Item[],
|
|
67
|
-
key: KeyCallback,
|
|
68
|
-
value: ValueCallback,
|
|
69
|
-
): Map<ReturnType<KeyCallback>, ReturnType<ValueCallback>>;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Create a _Map_ from an array of items using a callback and value
|
|
73
|
-
*
|
|
74
|
-
* If multiple items have the same key, the latest item's value will be used
|
|
75
|
-
*
|
|
76
|
-
* @param array Array to convert
|
|
77
|
-
* @param key Callback to get an item's grouping key
|
|
78
|
-
* @param value Key to use for value
|
|
79
|
-
* @returns _Map_ of keyed values
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```typescript
|
|
83
|
-
* toMap(
|
|
84
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
85
|
-
* item => item.value,
|
|
86
|
-
* 'id',
|
|
87
|
-
* ); // => Map { 10 => 3, 20 => 2 }
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
export function toMap<
|
|
91
|
-
Item extends PlainObject,
|
|
92
|
-
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
93
|
-
ItemValue extends keyof Item,
|
|
94
|
-
>(array: Item[], key: KeyCallback, value: ItemValue): Map<ReturnType<KeyCallback>, Item[ItemValue]>;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Create a _Map_ from an array of items using a key and callback
|
|
98
|
-
*
|
|
99
|
-
* If multiple items have the same key, the latest item's value will be used
|
|
100
|
-
*
|
|
101
|
-
* @param array Array to convert
|
|
102
|
-
* @param key Key to use for grouping
|
|
103
|
-
* @param value Callback to get an item's value
|
|
104
|
-
* @returns _Map_ of keyed values
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* ```typescript
|
|
108
|
-
* toMap(
|
|
109
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
110
|
-
* 'value',
|
|
111
|
-
* item => item.id,
|
|
112
|
-
* ); // => Map { 10 => 3, 20 => 2 }
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export function toMap<
|
|
116
|
-
Item extends PlainObject,
|
|
117
|
-
ItemKey extends keyof Item,
|
|
118
|
-
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
119
|
-
>(array: Item[], key: ItemKey, value: ValueCallback): Map<Item[ItemKey], ReturnType<ValueCallback>>;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Create a _Map_ from an array of items using a key and value
|
|
123
|
-
*
|
|
124
|
-
* If multiple items have the same key, the latest item's value will be used
|
|
125
|
-
*
|
|
126
|
-
* @param array Array to convert
|
|
127
|
-
* @param key Key to use for grouping
|
|
128
|
-
* @param value Key to use for value
|
|
129
|
-
* @returns _Map_ of keyed values
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```typescript
|
|
133
|
-
* toMap(
|
|
134
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
135
|
-
* 'value',
|
|
136
|
-
* 'id',
|
|
137
|
-
* ); // => Map { 10 => 3, 20 => 2 }
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
export function toMap<
|
|
141
|
-
Item extends PlainObject,
|
|
142
|
-
ItemKey extends keyof Item,
|
|
143
|
-
ItemValue extends keyof Item,
|
|
144
|
-
>(array: Item[], key: ItemKey, value: ItemValue): Map<Item[ItemKey], Item[ItemValue]>;
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Create a _Map_ from an array of items using a callback
|
|
148
|
-
*
|
|
149
|
-
* If multiple items have the same key, the latest item will be used
|
|
150
|
-
*
|
|
151
|
-
* @param array Array to convert
|
|
152
|
-
* @param callback Callback to get an item's grouping key
|
|
153
|
-
* @returns _Map_ of keyed items
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```typescript
|
|
157
|
-
* toMap(
|
|
158
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
159
|
-
* item => item.value,
|
|
160
|
-
* ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
|
|
161
|
-
* ```
|
|
162
|
-
*/
|
|
163
|
-
export function toMap<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(
|
|
164
|
-
array: Item[],
|
|
165
|
-
callback: Callback,
|
|
166
|
-
): Map<ReturnType<Callback>, Item>;
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Create a _Map_ from an array of items using a key
|
|
170
|
-
*
|
|
171
|
-
* If multiple items have the same key, the latest item will be used
|
|
172
|
-
*
|
|
173
|
-
* @param array Array to convert
|
|
174
|
-
* @param key Key to use for grouping
|
|
175
|
-
* @returns _Map_ of keyed items
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```typescript
|
|
179
|
-
* toMap(
|
|
180
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
181
|
-
* 'value',
|
|
182
|
-
* ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
export function toMap<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
186
|
-
array: Item[],
|
|
187
|
-
key: ItemKey,
|
|
188
|
-
): Map<Item[ItemKey], Item>;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Create a _Map_ from an array of items _(using indices as keys)_
|
|
192
|
-
*
|
|
193
|
-
* @param array Array to convert
|
|
194
|
-
* @returns _Map_ of indiced items
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```typescript
|
|
198
|
-
* toMap(
|
|
199
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
200
|
-
* ); // => Map { 0 => {id: 1, value: 10}, 1 => {id: 2, value: 20}, 2 => {id: 3, value: 10} }
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
export function toMap<Item>(array: Item[]): Map<number, Item>;
|
|
204
|
-
|
|
205
|
-
export function toMap(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
206
|
-
return getMapValues(array, first, second, false);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
toMap.arrays = toMapArrays;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Create a _Map_ from an array of items using callbacks, grouping values into arrays
|
|
213
|
-
*
|
|
214
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
215
|
-
*
|
|
216
|
-
* @param array Array to convert
|
|
217
|
-
* @param key Callback to get an item's grouping key
|
|
218
|
-
* @param value Callback to get an item's value
|
|
219
|
-
* @returns _Map_ of keyed arrays of values
|
|
220
|
-
*
|
|
221
|
-
* @example
|
|
222
|
-
* ```typescript
|
|
223
|
-
* toMapArrays(
|
|
224
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
225
|
-
* item => item.value,
|
|
226
|
-
* item => item.id,
|
|
227
|
-
* ); // => Map { 10 => [1, 3], 20 => [2] }
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
export function toMapArrays<
|
|
231
|
-
Item,
|
|
232
|
-
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
233
|
-
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
234
|
-
>(
|
|
235
|
-
array: Item[],
|
|
236
|
-
key: KeyCallback,
|
|
237
|
-
value: ValueCallback,
|
|
238
|
-
): Map<ReturnType<KeyCallback>, ReturnType<ValueCallback>[]>;
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Create a _Map_ from an array of items using a callback and value, grouping values into arrays
|
|
242
|
-
*
|
|
243
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
244
|
-
*
|
|
245
|
-
* @param array Array to convert
|
|
246
|
-
* @param key Callback to get an item's grouping key
|
|
247
|
-
* @param value Key to use for value
|
|
248
|
-
* @returns _Map_ of keyed arrays of values
|
|
249
|
-
*
|
|
250
|
-
* @example
|
|
251
|
-
* ```typescript
|
|
252
|
-
* toMapArrays(
|
|
253
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
254
|
-
* item => item.value,
|
|
255
|
-
* 'id',
|
|
256
|
-
* ); // => Map { 10 => [1, 3], 20 => [2] }
|
|
257
|
-
* ```
|
|
258
|
-
*/
|
|
259
|
-
export function toMapArrays<
|
|
260
|
-
Item extends PlainObject,
|
|
261
|
-
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
262
|
-
ItemValue extends keyof Item,
|
|
263
|
-
>(
|
|
264
|
-
array: Item[],
|
|
265
|
-
key: KeyCallback,
|
|
266
|
-
value: ItemValue,
|
|
267
|
-
): Map<ReturnType<KeyCallback>, Item[ItemValue][]>;
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Create a _Map_ from an array of items using a key and callback, grouping values into arrays
|
|
271
|
-
*
|
|
272
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
273
|
-
*
|
|
274
|
-
* @param array Array to convert
|
|
275
|
-
* @param key Key to use for grouping
|
|
276
|
-
* @param value Callback to get an item's value
|
|
277
|
-
* @returns _Map_ of keyed arrays of values
|
|
278
|
-
*
|
|
279
|
-
* @example
|
|
280
|
-
* ```typescript
|
|
281
|
-
* toMapArrays(
|
|
282
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
283
|
-
* 'value',
|
|
284
|
-
* item => item.id,
|
|
285
|
-
* ); // => Map { 10 => [1, 3], 20 => [2] }
|
|
286
|
-
* ```
|
|
287
|
-
*/
|
|
288
|
-
export function toMapArrays<
|
|
289
|
-
Item extends PlainObject,
|
|
290
|
-
ItemKey extends keyof Item,
|
|
291
|
-
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
292
|
-
>(
|
|
293
|
-
array: Item[],
|
|
294
|
-
key: ItemKey,
|
|
295
|
-
value: ValueCallback,
|
|
296
|
-
): Map<Item[ItemKey], ReturnType<ValueCallback>[]>;
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Create a _Map_ from an array of items using a key and value, grouping values into arrays
|
|
300
|
-
*
|
|
301
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
302
|
-
*
|
|
303
|
-
* @param array Array to convert
|
|
304
|
-
* @param key Key to use for grouping
|
|
305
|
-
* @param value Key to use for value
|
|
306
|
-
* @returns _Map_ of keyed arrays of values
|
|
307
|
-
*
|
|
308
|
-
* @example
|
|
309
|
-
* ```typescript
|
|
310
|
-
* toMapArrays(
|
|
311
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
312
|
-
* 'value',
|
|
313
|
-
* 'id',
|
|
314
|
-
* ); // => Map { 10 => [1, 3], 20 => [2] }
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
export function toMapArrays<
|
|
318
|
-
Item extends PlainObject,
|
|
319
|
-
ItemKey extends keyof Item,
|
|
320
|
-
ItemValue extends keyof Item,
|
|
321
|
-
>(array: Item[], key: ItemKey, value: ItemValue): Map<Item[ItemKey], Item[ItemValue][]>;
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Create a _Map_ from an array of items using a callback, grouping items into arrays
|
|
325
|
-
*
|
|
326
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
327
|
-
*
|
|
328
|
-
* @param array Array to convert
|
|
329
|
-
* @param callback Callback to get an item's grouping key
|
|
330
|
-
* @returns _Map_ of keyed arrays of items
|
|
331
|
-
*
|
|
332
|
-
* @example
|
|
333
|
-
* ```typescript
|
|
334
|
-
* toMapArrays(
|
|
335
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
336
|
-
* item => item.value,
|
|
337
|
-
* ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
|
|
338
|
-
* ```
|
|
339
|
-
*/
|
|
340
|
-
export function toMapArrays<
|
|
341
|
-
Item,
|
|
342
|
-
Callback extends (item: Item, index: number, array: Item[]) => Key,
|
|
343
|
-
>(array: Item[], callback: Callback): Map<ReturnType<Callback>, Item[]>;
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Create a _Map_ from an array of items using a key, grouping items into arrays
|
|
347
|
-
*
|
|
348
|
-
* _Available as `toMapArrays` and `toMap.arrays`_
|
|
349
|
-
*
|
|
350
|
-
* @param array Array to convert
|
|
351
|
-
* @param key Key to use for grouping
|
|
352
|
-
* @returns _Map_ of keyed arrays of items
|
|
353
|
-
*
|
|
354
|
-
* @example
|
|
355
|
-
* ```typescript
|
|
356
|
-
* toMapArrays(
|
|
357
|
-
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
358
|
-
* 'value',
|
|
359
|
-
* ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
|
|
360
|
-
* ```
|
|
361
|
-
*/
|
|
362
|
-
export function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
363
|
-
array: Item[],
|
|
364
|
-
key: ItemKey,
|
|
365
|
-
): Map<Item[ItemKey], Item[]>;
|
|
366
|
-
|
|
367
|
-
export function toMapArrays(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
368
|
-
return getMapValues(array, first, second, true);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// #endregion
|