@oscarpalmer/atoms 0.185.0 → 0.186.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array/difference.d.mts +29 -0
- package/dist/array/exists.d.mts +35 -0
- package/dist/array/filter.d.mts +72 -2
- package/dist/array/find.d.mts +70 -0
- package/dist/array/first.d.mts +77 -2
- package/dist/array/flatten.d.mts +6 -0
- package/dist/array/flatten.mjs +6 -0
- package/dist/array/from.d.mts +36 -0
- package/dist/array/get.d.mts +21 -13
- package/dist/array/group-by.d.mts +142 -0
- package/dist/array/insert.d.mts +16 -0
- package/dist/array/intersection.d.mts +29 -0
- package/dist/array/last.d.mts +75 -2
- package/dist/array/match.d.mts +161 -32
- package/dist/array/move.d.mts +78 -8
- package/dist/array/move.mjs +10 -0
- package/dist/array/partition.d.mts +35 -0
- package/dist/array/push.d.mts +8 -0
- package/dist/array/push.mjs +8 -0
- package/dist/array/reverse.d.mts +1 -0
- package/dist/array/reverse.mjs +1 -0
- package/dist/array/select.d.mts +94 -8
- package/dist/array/single.d.mts +29 -0
- package/dist/array/slice.d.mts +106 -16
- package/dist/array/sort.d.mts +21 -0
- package/dist/array/splice.d.mts +48 -0
- package/dist/array/splice.mjs +2 -1
- package/dist/array/swap.d.mts +113 -8
- package/dist/array/swap.mjs +1 -0
- package/dist/array/to-map.d.mts +124 -0
- package/dist/array/to-record.d.mts +124 -0
- package/dist/array/to-set.d.mts +24 -0
- package/dist/array/toggle.d.mts +38 -3
- package/dist/array/union.d.mts +29 -0
- package/dist/array/unique.d.mts +24 -0
- package/dist/array/update.d.mts +38 -3
- package/dist/index.d.mts +1892 -135
- package/dist/index.mjs +64 -18
- package/dist/internal/array/chunk.d.mts +6 -0
- package/dist/internal/array/chunk.mjs +6 -0
- package/dist/internal/array/compact.d.mts +12 -0
- package/dist/internal/array/index-of.d.mts +70 -0
- package/dist/internal/math/aggregate.d.mts +29 -0
- package/dist/internal/value/get.d.mts +25 -3
- package/dist/internal/value/has.d.mts +4 -4
- package/dist/models.d.mts +14 -1
- package/dist/value/collection.d.mts +1 -1
- package/dist/value/merge.d.mts +28 -25
- package/dist/value/merge.mjs +29 -18
- package/dist/value/transform.d.mts +1 -1
- package/dist/value/unsmush.d.mts +1 -5
- package/package.json +5 -5
- package/src/array/difference.ts +29 -0
- package/src/array/exists.ts +35 -0
- package/src/array/filter.ts +72 -2
- package/src/array/find.ts +70 -0
- package/src/array/first.ts +77 -3
- package/src/array/flatten.ts +6 -0
- package/src/array/from.ts +36 -0
- package/src/array/get.ts +21 -15
- package/src/array/group-by.ts +142 -0
- package/src/array/insert.ts +16 -2
- package/src/array/intersection.ts +29 -0
- package/src/array/last.ts +75 -2
- package/src/array/match.ts +171 -42
- package/src/array/move.ts +82 -12
- package/src/array/partition.ts +35 -0
- package/src/array/push.ts +8 -2
- package/src/array/reverse.ts +1 -0
- package/src/array/select.ts +94 -13
- package/src/array/single.ts +29 -0
- package/src/array/slice.ts +114 -24
- package/src/array/sort.ts +21 -0
- package/src/array/splice.ts +52 -4
- package/src/array/swap.ts +117 -12
- package/src/array/to-map.ts +124 -0
- package/src/array/to-record.ts +124 -0
- package/src/array/to-set.ts +24 -0
- package/src/array/toggle.ts +38 -3
- package/src/array/union.ts +29 -0
- package/src/array/unique.ts +24 -0
- package/src/array/update.ts +38 -3
- package/src/internal/array/chunk.ts +6 -0
- package/src/internal/array/compact.ts +12 -0
- package/src/internal/array/index-of.ts +70 -0
- package/src/internal/math/aggregate.ts +29 -0
- package/src/internal/string.ts +0 -2
- package/src/internal/value/get.ts +25 -3
- package/src/internal/value/has.ts +4 -4
- package/src/models.ts +18 -0
- package/src/value/collection.ts +1 -1
- package/src/value/merge.ts +88 -66
- package/src/value/transform.ts +1 -1
- package/src/value/unsmush.ts +1 -10
package/src/array/to-record.ts
CHANGED
|
@@ -7,10 +7,20 @@ import type {Key, KeyedValue, PlainObject, Simplify} from '../models';
|
|
|
7
7
|
* Create a record from an array of items using callbacks
|
|
8
8
|
*
|
|
9
9
|
* If multiple items have the same key, the latest item will be used
|
|
10
|
+
*
|
|
10
11
|
* @param array Array to convert
|
|
11
12
|
* @param key Callback to get an item's grouping key
|
|
12
13
|
* @param value Callback to get an item's value
|
|
13
14
|
* @returns Record of keyed values
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* toRecord(
|
|
19
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
20
|
+
* item => item.value,
|
|
21
|
+
* item => item.id,
|
|
22
|
+
* ); // => { 10: 3, 20: 2 }
|
|
23
|
+
* ```
|
|
14
24
|
*/
|
|
15
25
|
export function toRecord<
|
|
16
26
|
Item,
|
|
@@ -26,10 +36,20 @@ export function toRecord<
|
|
|
26
36
|
* Create a record from an array of items using a callback and value
|
|
27
37
|
*
|
|
28
38
|
* If multiple items have the same key, the latest item will be used
|
|
39
|
+
*
|
|
29
40
|
* @param array Array to convert
|
|
30
41
|
* @param callback Callback to get an item's grouping key
|
|
31
42
|
* @param value Key to use for value
|
|
32
43
|
* @returns Record with keys
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* toRecord(
|
|
48
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
49
|
+
* item => item.value,
|
|
50
|
+
* 'id',
|
|
51
|
+
* ); // => { 10: 3, 20: 2 }
|
|
52
|
+
* ```
|
|
33
53
|
*/
|
|
34
54
|
export function toRecord<
|
|
35
55
|
Item extends PlainObject,
|
|
@@ -45,10 +65,20 @@ export function toRecord<
|
|
|
45
65
|
* Create a record from an array of items using a key and callback
|
|
46
66
|
*
|
|
47
67
|
* If multiple items have the same key, the latest item will be used
|
|
68
|
+
*
|
|
48
69
|
* @param array Array to convert
|
|
49
70
|
* @param key Key to use for grouping
|
|
50
71
|
* @param callback Callback to get an item's value
|
|
51
72
|
* @returns Record with keys
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* toRecord(
|
|
77
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
78
|
+
* 'value',
|
|
79
|
+
* item => item.id,
|
|
80
|
+
* ); // => { 10: 3, 20: 2 }
|
|
81
|
+
* ```
|
|
52
82
|
*/
|
|
53
83
|
export function toRecord<
|
|
54
84
|
Item extends PlainObject,
|
|
@@ -64,10 +94,20 @@ export function toRecord<
|
|
|
64
94
|
* Create a record from an array of items using a key and value
|
|
65
95
|
*
|
|
66
96
|
* If multiple items have the same key, the latest item will be used
|
|
97
|
+
*
|
|
67
98
|
* @param array Array to convert
|
|
68
99
|
* @param key Key to use for grouping
|
|
69
100
|
* @param value Key to use for value
|
|
70
101
|
* @returns Record of keyed values
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* toRecord(
|
|
106
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
107
|
+
* 'value',
|
|
108
|
+
* 'id',
|
|
109
|
+
* ); // => { 10: 3, 20: 2 }
|
|
110
|
+
* ```
|
|
71
111
|
*/
|
|
72
112
|
export function toRecord<
|
|
73
113
|
Item extends PlainObject,
|
|
@@ -83,9 +123,18 @@ export function toRecord<
|
|
|
83
123
|
* Create a record from an array of items using a callback
|
|
84
124
|
*
|
|
85
125
|
* If multiple items have the same key, the latest item will be used
|
|
126
|
+
*
|
|
86
127
|
* @param array Array to convert
|
|
87
128
|
* @param callback Callback to get an item's grouping key
|
|
88
129
|
* @returns Record of keyed values
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* toRecord(
|
|
134
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
135
|
+
* item => item.value,
|
|
136
|
+
* ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} }
|
|
137
|
+
* ```
|
|
89
138
|
*/
|
|
90
139
|
export function toRecord<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(
|
|
91
140
|
array: Item[],
|
|
@@ -96,9 +145,18 @@ export function toRecord<Item, Callback extends (item: Item, index: number, arra
|
|
|
96
145
|
* Create a record from an array of items using a key
|
|
97
146
|
*
|
|
98
147
|
* If multiple items have the same key, the latest item will be used
|
|
148
|
+
*
|
|
99
149
|
* @param array Array to convert
|
|
100
150
|
* @param key Key to use for grouping
|
|
101
151
|
* @returns Record of keyed values
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* toRecord(
|
|
156
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
157
|
+
* 'value',
|
|
158
|
+
* ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} }
|
|
159
|
+
* ```
|
|
102
160
|
*/
|
|
103
161
|
export function toRecord<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
104
162
|
array: Item[],
|
|
@@ -107,8 +165,16 @@ export function toRecord<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
107
165
|
|
|
108
166
|
/**
|
|
109
167
|
* Create a record from an array of items _(using indices as keys)_
|
|
168
|
+
*
|
|
110
169
|
* @param array Array to convert
|
|
111
170
|
* @returns Record of indiced values
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* toRecord(
|
|
175
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
176
|
+
* ); // => { 0: {id: 1, value: 10}, 1: {id: 2, value: 20}, 2: {id: 3, value: 10} }
|
|
177
|
+
* ```
|
|
112
178
|
*/
|
|
113
179
|
export function toRecord<Item>(array: Item[]): Record<number, Item>;
|
|
114
180
|
|
|
@@ -122,10 +188,20 @@ toRecord.arrays = toRecordArrays;
|
|
|
122
188
|
* Create a record from an array of items using callbacks, grouping values into arrays
|
|
123
189
|
*
|
|
124
190
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
191
|
+
*
|
|
125
192
|
* @param array Array to convert
|
|
126
193
|
* @param key Callback to get an item's grouping key
|
|
127
194
|
* @param value Callback to get an item's value
|
|
128
195
|
* @returns Record of keyed arrays of values
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* toRecordArrays(
|
|
200
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
201
|
+
* item => item.value,
|
|
202
|
+
* item => item.id,
|
|
203
|
+
* ); // => { 10: [1, 3], 20: [2] }
|
|
204
|
+
* ```
|
|
129
205
|
*/
|
|
130
206
|
export function toRecordArrays<
|
|
131
207
|
Item,
|
|
@@ -141,10 +217,20 @@ export function toRecordArrays<
|
|
|
141
217
|
* Create a record from an array of items using a callback and value, grouping values into arrays
|
|
142
218
|
*
|
|
143
219
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
220
|
+
*
|
|
144
221
|
* @param array Array to convert
|
|
145
222
|
* @param callback Callback to get an item's grouping key
|
|
146
223
|
* @param value Key to use for value
|
|
147
224
|
* @returns Record of keyed arrays of values
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* toRecordArrays(
|
|
229
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
230
|
+
* item => item.value,
|
|
231
|
+
* 'id',
|
|
232
|
+
* ); // => { 10: [1, 3], 20: [2] }
|
|
233
|
+
* ```
|
|
148
234
|
*/
|
|
149
235
|
export function toRecordArrays<
|
|
150
236
|
Item extends PlainObject,
|
|
@@ -160,10 +246,20 @@ export function toRecordArrays<
|
|
|
160
246
|
* Create a record from an array of items using a key and callback, grouping values into arrays
|
|
161
247
|
*
|
|
162
248
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
249
|
+
*
|
|
163
250
|
* @param array Array to convert
|
|
164
251
|
* @param key Key to use for grouping
|
|
165
252
|
* @param callback Callback to get an item's value
|
|
166
253
|
* @returns Record of keyed arrays of values
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* toRecordArrays(
|
|
258
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
259
|
+
* 'value',
|
|
260
|
+
* item => item.id,
|
|
261
|
+
* ); // => { 10: [1, 3], 20: [2] }
|
|
262
|
+
* ```
|
|
167
263
|
*/
|
|
168
264
|
export function toRecordArrays<
|
|
169
265
|
Item extends PlainObject,
|
|
@@ -179,10 +275,20 @@ export function toRecordArrays<
|
|
|
179
275
|
* Create a record from an array of items using a key and value, grouping values into arrays
|
|
180
276
|
*
|
|
181
277
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
278
|
+
*
|
|
182
279
|
* @param array Array to convert
|
|
183
280
|
* @param key Key to use for grouping
|
|
184
281
|
* @param value Key to use for value
|
|
185
282
|
* @returns Record of keyed arrays of values
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* toRecordArrays(
|
|
287
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
288
|
+
* 'value',
|
|
289
|
+
* 'id',
|
|
290
|
+
* ); // => { 10: [1, 3], 20: [2] }
|
|
291
|
+
* ```
|
|
186
292
|
*/
|
|
187
293
|
export function toRecordArrays<
|
|
188
294
|
Item extends PlainObject,
|
|
@@ -198,9 +304,18 @@ export function toRecordArrays<
|
|
|
198
304
|
* Create a record from an array of items using a callback, grouping items into arrays
|
|
199
305
|
*
|
|
200
306
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
307
|
+
*
|
|
201
308
|
* @param array Array to convert
|
|
202
309
|
* @param callback Callback to get an item's grouping key
|
|
203
310
|
* @returns Record of keyed arrays of items
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* toRecordArrays(
|
|
315
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
316
|
+
* item => item.value,
|
|
317
|
+
* ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] }
|
|
318
|
+
* ```
|
|
204
319
|
*/
|
|
205
320
|
export function toRecordArrays<
|
|
206
321
|
Item,
|
|
@@ -211,9 +326,18 @@ export function toRecordArrays<
|
|
|
211
326
|
* Create a record from an array of items using a key, grouping items into arrays
|
|
212
327
|
*
|
|
213
328
|
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
329
|
+
*
|
|
214
330
|
* @param array Array to convert
|
|
215
331
|
* @param key Key to use for grouping
|
|
216
332
|
* @returns Record of keyed arrays of items
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```typescript
|
|
336
|
+
* toRecordArrays(
|
|
337
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
|
|
338
|
+
* 'value',
|
|
339
|
+
* ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] }
|
|
340
|
+
* ```
|
|
217
341
|
*/
|
|
218
342
|
export function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
219
343
|
array: Item[],
|
package/src/array/to-set.ts
CHANGED
|
@@ -5,9 +5,18 @@ import type {PlainObject} from '../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Create a Set from an array of items using a callback
|
|
8
|
+
*
|
|
8
9
|
* @param array Array to convert
|
|
9
10
|
* @param callback Callback to get an item's value
|
|
10
11
|
* @returns Set of values
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* toSet(
|
|
16
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
17
|
+
* item => item.id,
|
|
18
|
+
* ); // => Set { 1, 2, 3 }
|
|
19
|
+
* ```
|
|
11
20
|
*/
|
|
12
21
|
export function toSet<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
|
|
13
22
|
array: Item[],
|
|
@@ -16,9 +25,18 @@ export function toSet<Item, Callback extends (item: Item, index: number, array:
|
|
|
16
25
|
|
|
17
26
|
/**
|
|
18
27
|
* Create a Set from an array of items using a key
|
|
28
|
+
*
|
|
19
29
|
* @param array Array to convert
|
|
20
30
|
* @param key Key to use for value
|
|
21
31
|
* @returns Set of values
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* toSet(
|
|
36
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
37
|
+
* 'id',
|
|
38
|
+
* ); // => Set { 1, 2, 3 }
|
|
39
|
+
* ```
|
|
22
40
|
*/
|
|
23
41
|
export function toSet<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
24
42
|
array: Item[],
|
|
@@ -27,8 +45,14 @@ export function toSet<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
27
45
|
|
|
28
46
|
/**
|
|
29
47
|
* Create a Set from an array of items
|
|
48
|
+
*
|
|
30
49
|
* @param array Array to convert
|
|
31
50
|
* @returns Set of items
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* toSet([1, 2, 3]); // => Set { 1, 2, 3 }
|
|
55
|
+
* ```
|
|
32
56
|
*/
|
|
33
57
|
export function toSet<Item>(array: Item[]): Set<Item>;
|
|
34
58
|
|
package/src/array/toggle.ts
CHANGED
|
@@ -4,11 +4,23 @@ import type {PlainObject} from '../models';
|
|
|
4
4
|
// #region Functions
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Toggle an item in an array
|
|
7
|
+
* Toggle an item in an array
|
|
8
|
+
*
|
|
9
|
+
* If the item exists, it will be removed; if it doesn't, it will be added
|
|
10
|
+
*
|
|
8
11
|
* @param destination Array to toggle within
|
|
9
12
|
* @param toggled Toggled items
|
|
10
13
|
* @param callback Callback to find existing item
|
|
11
14
|
* @returns Original array
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* toggle(
|
|
19
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
20
|
+
* [{id: 2}, {id: 4}],
|
|
21
|
+
* item => item.id,
|
|
22
|
+
* ); // => [{id: 1}, {id: 3}, {id: 4}]
|
|
23
|
+
* ```
|
|
12
24
|
*/
|
|
13
25
|
export function toggle<Item>(
|
|
14
26
|
destination: Item[],
|
|
@@ -17,11 +29,23 @@ export function toggle<Item>(
|
|
|
17
29
|
): Item[];
|
|
18
30
|
|
|
19
31
|
/**
|
|
20
|
-
* Toggle an item in an array
|
|
32
|
+
* Toggle an item in an array
|
|
33
|
+
*
|
|
34
|
+
* If the item exists, it will be removed; if it doesn't, it will be added
|
|
35
|
+
*
|
|
21
36
|
* @param destination Array to toggle within
|
|
22
37
|
* @param toggled Toggled items
|
|
23
38
|
* @param key Key to find existing item
|
|
24
39
|
* @returns Original array
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* toggle(
|
|
44
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
45
|
+
* [{id: 2}, {id: 4}],
|
|
46
|
+
* 'id',
|
|
47
|
+
* ); // => [{id: 1}, {id: 3}, {id: 4}]
|
|
48
|
+
* ```
|
|
25
49
|
*/
|
|
26
50
|
export function toggle<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
27
51
|
destination: Item[],
|
|
@@ -30,10 +54,21 @@ export function toggle<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
30
54
|
): Item[];
|
|
31
55
|
|
|
32
56
|
/**
|
|
33
|
-
* Toggle an item in an array
|
|
57
|
+
* Toggle an item in an array
|
|
58
|
+
*
|
|
59
|
+
* If the item exists, it will be removed; if it doesn't, it will be added
|
|
60
|
+
*
|
|
34
61
|
* @param destination Array to toggle within
|
|
35
62
|
* @param toggled Toggled items
|
|
36
63
|
* @returns Original array
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* toggle(
|
|
68
|
+
* [1, 2, 3],
|
|
69
|
+
* [2, 4],
|
|
70
|
+
* ); // => [1, 3, 4]
|
|
71
|
+
* ```
|
|
37
72
|
*/
|
|
38
73
|
export function toggle<Item>(destination: Item[], toggled: Item[]): Item[];
|
|
39
74
|
|
package/src/array/union.ts
CHANGED
|
@@ -4,10 +4,20 @@ import {COMPARE_SETS_UNION, compareSets} from '../internal/array/sets';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Get the combined, unique values from two arrays
|
|
7
|
+
*
|
|
7
8
|
* @param first First array
|
|
8
9
|
* @param second Second array
|
|
9
10
|
* @param callback Callback to get an item's value for comparison
|
|
10
11
|
* @returns Combined, unique values from both arrays
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* union(
|
|
16
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
17
|
+
* [{id: 2}, {id: 4}],
|
|
18
|
+
* item => item.id,
|
|
19
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
|
|
20
|
+
* ```
|
|
11
21
|
*/
|
|
12
22
|
export function union<First, Second>(
|
|
13
23
|
first: First[],
|
|
@@ -17,10 +27,20 @@ export function union<First, Second>(
|
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Get the combined, unique values from two arrays
|
|
30
|
+
*
|
|
20
31
|
* @param first First array
|
|
21
32
|
* @param second Second array
|
|
22
33
|
* @param key Key to get an item's value for comparison
|
|
23
34
|
* @returns Combined, unique values from both arrays
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* union(
|
|
39
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
40
|
+
* [{id: 2}, {id: 4}],
|
|
41
|
+
* 'id',
|
|
42
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
|
|
43
|
+
* ```
|
|
24
44
|
*/
|
|
25
45
|
export function union<
|
|
26
46
|
First extends Record<string, unknown>,
|
|
@@ -30,9 +50,18 @@ export function union<
|
|
|
30
50
|
|
|
31
51
|
/**
|
|
32
52
|
* Get the combined, unique values from two arrays
|
|
53
|
+
*
|
|
33
54
|
* @param first First array
|
|
34
55
|
* @param second Second array
|
|
35
56
|
* @returns Combined, unique values from both arrays
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* union(
|
|
61
|
+
* [1, 2, 3],
|
|
62
|
+
* [2, 4],
|
|
63
|
+
* ); // => [1, 2, 3, 4]
|
|
64
|
+
* ```
|
|
36
65
|
*/
|
|
37
66
|
export function union<First, Second>(first: First[], second: Second[]): (First | Second)[];
|
|
38
67
|
|
package/src/array/unique.ts
CHANGED
|
@@ -5,9 +5,18 @@ import type {PlainObject} from '../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Get an array of unique items
|
|
8
|
+
*
|
|
8
9
|
* @param array Original array
|
|
9
10
|
* @param callback Callback to get an item's value
|
|
10
11
|
* @returns Array of unique items
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* unique(
|
|
16
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
17
|
+
* item => item.id,
|
|
18
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
19
|
+
* ```
|
|
11
20
|
*/
|
|
12
21
|
export function unique<Item>(
|
|
13
22
|
array: Item[],
|
|
@@ -16,9 +25,18 @@ export function unique<Item>(
|
|
|
16
25
|
|
|
17
26
|
/**
|
|
18
27
|
* Get an array of unique items
|
|
28
|
+
*
|
|
19
29
|
* @param array Original array
|
|
20
30
|
* @param key Key to use for unique value
|
|
21
31
|
* @returns Array of unique items
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* unique(
|
|
36
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
37
|
+
* 'id',
|
|
38
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
39
|
+
* ```
|
|
22
40
|
*/
|
|
23
41
|
export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
24
42
|
array: Item[],
|
|
@@ -27,8 +45,14 @@ export function unique<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
27
45
|
|
|
28
46
|
/**
|
|
29
47
|
* Get an array of unique items
|
|
48
|
+
*
|
|
30
49
|
* @param array Original array
|
|
31
50
|
* @returns Array of unique items
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* unique([1, 2, 3, 2]); // => [1, 2, 3]
|
|
55
|
+
* ```
|
|
32
56
|
*/
|
|
33
57
|
export function unique<Item>(array: Item[]): Item[];
|
|
34
58
|
|
package/src/array/update.ts
CHANGED
|
@@ -2,11 +2,23 @@ import {updateInArray} from '../internal/array/update';
|
|
|
2
2
|
import type {PlainObject} from '../models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Update an item in an array
|
|
5
|
+
* Update an item in an array
|
|
6
|
+
*
|
|
7
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
8
|
+
*
|
|
6
9
|
* @param destination Array to update within
|
|
7
10
|
* @param updated Updated items
|
|
8
11
|
* @param callback Callback to find existing item
|
|
9
12
|
* @returns Original array
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* update(
|
|
17
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
18
|
+
* [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
|
|
19
|
+
* item => item.id,
|
|
20
|
+
* ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
|
|
21
|
+
* ```
|
|
10
22
|
*/
|
|
11
23
|
export function update<Item>(
|
|
12
24
|
destination: Item[],
|
|
@@ -15,11 +27,23 @@ export function update<Item>(
|
|
|
15
27
|
): Item[];
|
|
16
28
|
|
|
17
29
|
/**
|
|
18
|
-
* Update an item in an array
|
|
30
|
+
* Update an item in an array
|
|
31
|
+
*
|
|
32
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
33
|
+
*
|
|
19
34
|
* @param destination Array to update within
|
|
20
35
|
* @param updated Updated items
|
|
21
36
|
* @param key Key to find existing item
|
|
22
37
|
* @returns Original array
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* update(
|
|
42
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
43
|
+
* [{id: 2, name: 'Updated'}, {id: 4, name: 'New'}],
|
|
44
|
+
* 'id',
|
|
45
|
+
* ); // => [{id: 1}, {id: 2, name: 'Updated'}, {id: 3}, {id: 4, name: 'New'}]
|
|
46
|
+
* ```
|
|
23
47
|
*/
|
|
24
48
|
export function update<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
25
49
|
destination: Item[],
|
|
@@ -28,10 +52,21 @@ export function update<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
28
52
|
): Item[];
|
|
29
53
|
|
|
30
54
|
/**
|
|
31
|
-
* Update an item in an array
|
|
55
|
+
* Update an item in an array
|
|
56
|
+
*
|
|
57
|
+
* If the item exists, it will be updated; if it doesn't, it will be added
|
|
58
|
+
*
|
|
32
59
|
* @param destination Array to update within
|
|
33
60
|
* @param updated Updated items
|
|
34
61
|
* @returns Original array
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* update(
|
|
66
|
+
* [1, 2, 3],
|
|
67
|
+
* [2, 4],
|
|
68
|
+
* ); // => [1, 2, 3, 4]
|
|
69
|
+
* ```
|
|
35
70
|
*/
|
|
36
71
|
export function update<Item>(destination: Item[], updated: Item[]): Item[];
|
|
37
72
|
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Chunk an array into smaller arrays
|
|
5
|
+
*
|
|
5
6
|
* @param array Array to chunk
|
|
6
7
|
* @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
|
|
7
8
|
* @returns Array of arrays
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
|
|
13
|
+
* ```
|
|
8
14
|
*/
|
|
9
15
|
export function chunk<Item>(array: Item[], size?: number): Item[][] {
|
|
10
16
|
if (!Array.isArray(array)) {
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Compact an array _(removing all false-y values)_
|
|
5
|
+
*
|
|
5
6
|
* @param array Array to compact
|
|
6
7
|
* @param strict True to remove all false-y values
|
|
7
8
|
* @returns Compacted array
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
|
|
13
|
+
* ```
|
|
8
14
|
*/
|
|
9
15
|
export function compact<Item>(
|
|
10
16
|
array: Item[],
|
|
@@ -13,8 +19,14 @@ export function compact<Item>(
|
|
|
13
19
|
|
|
14
20
|
/**
|
|
15
21
|
* Compact an array _(removing all `null` and `undefined` values)_
|
|
22
|
+
*
|
|
16
23
|
* @param array Array to compact
|
|
17
24
|
* @returns Compacted array
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
|
|
29
|
+
* ```
|
|
18
30
|
*/
|
|
19
31
|
export function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
|
|
20
32
|
|