@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/partition.ts
CHANGED
|
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Get a partitioned array of items
|
|
8
|
+
*
|
|
8
9
|
* @param array Array to search in
|
|
9
10
|
* @param callback Callback to get an item's value for matching
|
|
10
11
|
* @param value Value to match against
|
|
11
12
|
* @returns Partitioned array of items
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* partition(
|
|
17
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
18
|
+
* item => item.id,
|
|
19
|
+
* 2,
|
|
20
|
+
* ); // => [[{id: 2}], [{id: 1}, {id: 3}]]
|
|
21
|
+
* ```
|
|
12
22
|
*/
|
|
13
23
|
export function partition<
|
|
14
24
|
Item,
|
|
@@ -17,10 +27,20 @@ export function partition<
|
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Get a partitioned array of items
|
|
30
|
+
*
|
|
20
31
|
* @param array Array to search in
|
|
21
32
|
* @param key Key to get an item's value for matching
|
|
22
33
|
* @param value Value to match against
|
|
23
34
|
* @returns Partitioned array of items
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* partition(
|
|
39
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
40
|
+
* 'id',
|
|
41
|
+
* 2,
|
|
42
|
+
* ); // => [[{id: 2}], [{id: 1}, {id: 3}]]
|
|
43
|
+
* ```
|
|
24
44
|
*/
|
|
25
45
|
export function partition<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
26
46
|
array: Item[],
|
|
@@ -30,9 +50,18 @@ export function partition<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
30
50
|
|
|
31
51
|
/**
|
|
32
52
|
* Get a partitioned array of items
|
|
53
|
+
*
|
|
33
54
|
* @param array Array to search in
|
|
34
55
|
* @param filter Filter callback to match items
|
|
35
56
|
* @returns Partitioned array of items
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* partition(
|
|
61
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
62
|
+
* item => item.id === 2,
|
|
63
|
+
* ); // => [[{id: 2}], [{id: 1}, {id: 3}]]
|
|
64
|
+
* ```
|
|
36
65
|
*/
|
|
37
66
|
export function partition<Item>(
|
|
38
67
|
array: Item[],
|
|
@@ -41,9 +70,15 @@ export function partition<Item>(
|
|
|
41
70
|
|
|
42
71
|
/**
|
|
43
72
|
* Get a partitioned array of items matching the given item
|
|
73
|
+
*
|
|
44
74
|
* @param array Array to search in
|
|
45
75
|
* @param item Item to match against
|
|
46
76
|
* @returns Partitioned array of items
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* partition([1, 2, 3, 4, 5], 3); // => [[3], [1, 2, 4, 5]]
|
|
81
|
+
* ```
|
|
47
82
|
*/
|
|
48
83
|
export function partition<Item>(array: Item[], item: Item): Item[][];
|
|
49
84
|
|
package/src/array/push.ts
CHANGED
|
@@ -2,13 +2,19 @@ import {INSERT_TYPE_PUSH, insertValues} from '../internal/array/insert';
|
|
|
2
2
|
|
|
3
3
|
// #region Functions
|
|
4
4
|
|
|
5
|
-
// Uses chunking to avoid call stack size being exceeded
|
|
6
|
-
|
|
7
5
|
/**
|
|
8
6
|
* Push items into an array _(at the end)_
|
|
7
|
+
*
|
|
8
|
+
* _(Uses chunking to avoid call stack size being exceeded)_
|
|
9
|
+
*
|
|
9
10
|
* @param array Original array
|
|
10
11
|
* @param pushed Pushed items
|
|
11
12
|
* @returns New length of the array
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* push([1, 2, 3], [4, 5]); // => 5 (new length); array becomes [1, 2, 3, 4, 5]
|
|
17
|
+
* ```
|
|
12
18
|
*/
|
|
13
19
|
export function push<Item>(array: Item[], pushed: Item[]): number {
|
|
14
20
|
return insertValues(INSERT_TYPE_PUSH, array, pushed, array.length, 0) as number;
|
package/src/array/reverse.ts
CHANGED
package/src/array/select.ts
CHANGED
|
@@ -5,11 +5,22 @@ import type {PlainObject} from '../models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Get a filtered and mapped array of items
|
|
8
|
+
*
|
|
8
9
|
* @param array Array to search in
|
|
9
10
|
* @param filterCallback Callback to get an item's value for matching
|
|
10
11
|
* @param filterValue Value to match against
|
|
11
12
|
* @param mapCallback Callback to map the matched items
|
|
12
13
|
* @returns Filtered and mapped array of items
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* select(
|
|
18
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
19
|
+
* item => item.id,
|
|
20
|
+
* 2,
|
|
21
|
+
* item => item.name,
|
|
22
|
+
* ); // => ['Bob']
|
|
23
|
+
* ```
|
|
13
24
|
*/
|
|
14
25
|
export function select<
|
|
15
26
|
Item,
|
|
@@ -24,11 +35,22 @@ export function select<
|
|
|
24
35
|
|
|
25
36
|
/**
|
|
26
37
|
* Get a filtered and mapped array of items
|
|
38
|
+
*
|
|
27
39
|
* @param array Array to search in
|
|
28
40
|
* @param filterCallback Callback to get an item's value for matching
|
|
29
41
|
* @param filterValue Value to match against
|
|
30
42
|
* @param mapKey Key to get an item's value for mapping
|
|
31
43
|
* @returns Filtered and mapped array of items
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* select(
|
|
48
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
49
|
+
* item => item.id,
|
|
50
|
+
* 2,
|
|
51
|
+
* 'name',
|
|
52
|
+
* ); // => ['Bob']
|
|
53
|
+
* ```
|
|
32
54
|
*/
|
|
33
55
|
export function select<
|
|
34
56
|
Item extends PlainObject,
|
|
@@ -43,11 +65,22 @@ export function select<
|
|
|
43
65
|
|
|
44
66
|
/**
|
|
45
67
|
* Get a filtered and mapped array of items
|
|
68
|
+
*
|
|
46
69
|
* @param array Array to search in
|
|
47
70
|
* @param filterKey Key to get an item's value for matching
|
|
48
71
|
* @param filterValue Value to match against
|
|
49
72
|
* @param mapCallback Callback to map the matched items
|
|
50
73
|
* @returns Filtered and mapped array of items
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* select(
|
|
78
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
79
|
+
* 'id',
|
|
80
|
+
* 2,
|
|
81
|
+
* item => item.name,
|
|
82
|
+
* ); // => ['Bob']
|
|
83
|
+
* ```
|
|
51
84
|
*/
|
|
52
85
|
export function select<
|
|
53
86
|
Item extends PlainObject,
|
|
@@ -62,11 +95,22 @@ export function select<
|
|
|
62
95
|
|
|
63
96
|
/**
|
|
64
97
|
* Get a filtered and mapped array of items
|
|
98
|
+
*
|
|
65
99
|
* @param array Array to search in
|
|
66
100
|
* @param filterKey Key to get an item's value for matching
|
|
67
101
|
* @param filterValue Value to match against
|
|
68
102
|
* @param mapKey Key to get an item's value for mapping
|
|
69
103
|
* @returns Filtered and mapped array of items
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* select(
|
|
108
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
109
|
+
* 'id',
|
|
110
|
+
* 2,
|
|
111
|
+
* 'name',
|
|
112
|
+
* ); // => ['Bob']
|
|
113
|
+
* ```
|
|
70
114
|
*/
|
|
71
115
|
export function select<
|
|
72
116
|
Item extends PlainObject,
|
|
@@ -81,10 +125,20 @@ export function select<
|
|
|
81
125
|
|
|
82
126
|
/**
|
|
83
127
|
* Get a filtered and mapped array of items
|
|
128
|
+
*
|
|
84
129
|
* @param array Array to search in
|
|
85
130
|
* @param filterCallback Filter callback to match items
|
|
86
131
|
* @param mapCallback Callback to map the matched items
|
|
87
132
|
* @returns Filtered and mapped array of items
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* select(
|
|
137
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
138
|
+
* item => item.id === 2,
|
|
139
|
+
* item => item.name,
|
|
140
|
+
* ); // => ['Bob']
|
|
141
|
+
* ```
|
|
88
142
|
*/
|
|
89
143
|
export function select<
|
|
90
144
|
Item,
|
|
@@ -103,6 +157,16 @@ export function select<
|
|
|
103
157
|
* @param filterCallback Filter callback to match items
|
|
104
158
|
* @param mapKey Key to get an item's value for mapping
|
|
105
159
|
* @returns Filtered and mapped array of items
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* select(
|
|
164
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
165
|
+
* item => item.id,
|
|
166
|
+
* 2,
|
|
167
|
+
* 'name'
|
|
168
|
+
* ); // => ['Bob']
|
|
169
|
+
* ```
|
|
106
170
|
*/
|
|
107
171
|
export function select<
|
|
108
172
|
Item extends PlainObject,
|
|
@@ -117,10 +181,20 @@ export function select<
|
|
|
117
181
|
|
|
118
182
|
/**
|
|
119
183
|
* Get a filtered and mapped array of items
|
|
184
|
+
*
|
|
120
185
|
* @param array Array to search in
|
|
121
186
|
* @param filter Filter callback to match items
|
|
122
187
|
* @param map Callback to map the matched items
|
|
123
188
|
* @returns Filtered and mapped array of items
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* select(
|
|
193
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
194
|
+
* item => item.id === 2,
|
|
195
|
+
* item => item.name,
|
|
196
|
+
* ); // => ['Bob']
|
|
197
|
+
* ```
|
|
124
198
|
*/
|
|
125
199
|
export function select<
|
|
126
200
|
Item,
|
|
@@ -133,10 +207,20 @@ export function select<
|
|
|
133
207
|
|
|
134
208
|
/**
|
|
135
209
|
* Get a filtered and mapped array of items
|
|
210
|
+
*
|
|
136
211
|
* @param array Array to search in
|
|
137
212
|
* @param filter Filter callback to match items
|
|
138
213
|
* @param map Key to get an item's value for mapping
|
|
139
214
|
* @returns Filtered and mapped array of items
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* select(
|
|
219
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
220
|
+
* item => item.id === 2,
|
|
221
|
+
* 'name'
|
|
222
|
+
* ); // => ['Bob']
|
|
223
|
+
* ```
|
|
140
224
|
*/
|
|
141
225
|
export function select<Item extends PlainObject, MapKey extends keyof Item>(
|
|
142
226
|
array: Item[],
|
|
@@ -146,29 +230,26 @@ export function select<Item extends PlainObject, MapKey extends keyof Item>(
|
|
|
146
230
|
|
|
147
231
|
/**
|
|
148
232
|
* Get a filtered and mapped array of items
|
|
233
|
+
*
|
|
149
234
|
* @param array Array to search in
|
|
150
235
|
* @param item Item to match against
|
|
151
236
|
* @param map Callback to map the matched items
|
|
152
237
|
* @returns Filtered and mapped array of items
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* select(
|
|
242
|
+
* [1, 2, 3, 2, 1],
|
|
243
|
+
* 3,
|
|
244
|
+
* value => value ** 2,
|
|
245
|
+
* ); // => [9]
|
|
246
|
+
* ```
|
|
153
247
|
*/
|
|
154
248
|
export function select<
|
|
155
249
|
Item,
|
|
156
250
|
MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
157
251
|
>(array: Item[], item: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
|
|
158
252
|
|
|
159
|
-
/**
|
|
160
|
-
* Get a filtered and mapped array of items
|
|
161
|
-
* @param array Array to search in
|
|
162
|
-
* @param item Item to match against
|
|
163
|
-
* @param map Key to get an item's value for mapping
|
|
164
|
-
* @returns Filtered and mapped array of items
|
|
165
|
-
*/
|
|
166
|
-
export function select<Item extends PlainObject, MapKey extends keyof Item>(
|
|
167
|
-
array: Item[],
|
|
168
|
-
item: Item,
|
|
169
|
-
map: MapKey,
|
|
170
|
-
): Array<Item[MapKey]>;
|
|
171
|
-
|
|
172
253
|
export function select(array: unknown[], ...parameters: unknown[]): unknown[] {
|
|
173
254
|
const mapper = parameters.pop();
|
|
174
255
|
|
package/src/array/single.ts
CHANGED
|
@@ -7,10 +7,20 @@ import type {PlainObject} from '../models';
|
|
|
7
7
|
* Get the _only_ item matching the given value
|
|
8
8
|
*
|
|
9
9
|
* Throws an error if multiple items match the value
|
|
10
|
+
*
|
|
10
11
|
* @param array Array to search in
|
|
11
12
|
* @param callback Callback to get an item's value for matching
|
|
12
13
|
* @param value Value to match against
|
|
13
14
|
* @returns Only item that matches the value, or `undefined` if no match is found
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* single(
|
|
19
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
20
|
+
* item => item.id,
|
|
21
|
+
* 2
|
|
22
|
+
* ); // => {id: 2}
|
|
23
|
+
* ```
|
|
14
24
|
*/
|
|
15
25
|
export function single<
|
|
16
26
|
Item,
|
|
@@ -21,10 +31,20 @@ export function single<
|
|
|
21
31
|
* Get the _only_ item matching the given value by key
|
|
22
32
|
*
|
|
23
33
|
* Throws an error if multiple items match the value
|
|
34
|
+
*
|
|
24
35
|
* @param array Array to search in
|
|
25
36
|
* @param key Key to get an item's value for matching
|
|
26
37
|
* @param value Value to match against
|
|
27
38
|
* @returns Only item that matches the value, or `undefined` if no match is found
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* single(
|
|
43
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
44
|
+
* 'id',
|
|
45
|
+
* 2
|
|
46
|
+
* ); // => {id: 2}
|
|
47
|
+
* ```
|
|
28
48
|
*/
|
|
29
49
|
export function single<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
30
50
|
array: Item[],
|
|
@@ -36,9 +56,18 @@ export function single<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
36
56
|
* Get the _only_ item matching the filter
|
|
37
57
|
*
|
|
38
58
|
* Throws an error if multiple items match the filter
|
|
59
|
+
*
|
|
39
60
|
* @param array Array to search in
|
|
40
61
|
* @param filter Filter callback to match items
|
|
41
62
|
* @returns Only item that matches the filter, or `undefined` if no match is found
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* single(
|
|
67
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
68
|
+
* item => item.id === 2
|
|
69
|
+
* ); // => {id: 2}
|
|
70
|
+
* ```
|
|
42
71
|
*/
|
|
43
72
|
export function single<Item>(
|
|
44
73
|
array: Item[],
|
package/src/array/slice.ts
CHANGED
|
@@ -11,35 +11,64 @@ type ExtractType = 'drop' | 'take';
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Drop items from the start of an array until they match a value
|
|
14
|
+
*
|
|
14
15
|
* @param array Original array
|
|
15
|
-
* @param
|
|
16
|
+
* @param callback Callback to get an item's value for matching
|
|
16
17
|
* @param value Value to match against
|
|
17
18
|
* @returns New array with items dropped
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* drop(
|
|
23
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
24
|
+
* item => item.id,
|
|
25
|
+
* 2,
|
|
26
|
+
* ); // => [{id: 2}, {id: 3}]
|
|
27
|
+
* ```
|
|
18
28
|
*/
|
|
19
|
-
export function drop<Item extends
|
|
29
|
+
export function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
|
|
20
30
|
array: Item[],
|
|
21
|
-
|
|
22
|
-
value:
|
|
31
|
+
callback: Callback,
|
|
32
|
+
value: ReturnType<Callback>,
|
|
23
33
|
): Item[];
|
|
24
34
|
|
|
25
35
|
/**
|
|
26
36
|
* Drop items from the start of an array until they match a value
|
|
37
|
+
*
|
|
27
38
|
* @param array Original array
|
|
28
|
-
* @param
|
|
39
|
+
* @param key Key to get an item's value for matching
|
|
29
40
|
* @param value Value to match against
|
|
30
|
-
* @
|
|
41
|
+
* @returns New array with items dropped
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* drop(
|
|
46
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
47
|
+
* 'id',
|
|
48
|
+
* 2,
|
|
49
|
+
* ); // => [{id: 2}, {id: 3}]
|
|
50
|
+
* ```
|
|
31
51
|
*/
|
|
32
|
-
export function drop<Item
|
|
52
|
+
export function drop<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
33
53
|
array: Item[],
|
|
34
|
-
|
|
35
|
-
value:
|
|
54
|
+
key: ItemKey,
|
|
55
|
+
value: Item[ItemKey],
|
|
36
56
|
): Item[];
|
|
37
57
|
|
|
38
58
|
/**
|
|
39
59
|
* Drop items from the start of an array while they match a filter
|
|
60
|
+
*
|
|
40
61
|
* @param array Original array
|
|
41
62
|
* @param callback Filter callback to match items
|
|
42
|
-
* @
|
|
63
|
+
* @returns New array with items dropped
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* drop(
|
|
68
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
69
|
+
* item => item.id === 2,
|
|
70
|
+
* ); // => [{id: 2}, {id: 3}]
|
|
71
|
+
* ```
|
|
43
72
|
*/
|
|
44
73
|
export function drop<Item extends PlainObject>(
|
|
45
74
|
array: Item[],
|
|
@@ -48,9 +77,16 @@ export function drop<Item extends PlainObject>(
|
|
|
48
77
|
|
|
49
78
|
/**
|
|
50
79
|
* Drop a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
80
|
+
*
|
|
51
81
|
* @param array Original array
|
|
52
82
|
* @param count Number of items to drop
|
|
53
83
|
* @returns New array with items dropped
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* drop([1, 2, 3, 4, 5], 2); // => [3, 4, 5]
|
|
88
|
+
* drop([1, 2, 3, 4, 5], -2); // => [1, 2, 3]
|
|
89
|
+
* ```
|
|
54
90
|
*/
|
|
55
91
|
export function drop(array: unknown[], count: number): unknown[];
|
|
56
92
|
|
|
@@ -138,25 +174,43 @@ function extract(
|
|
|
138
174
|
|
|
139
175
|
/**
|
|
140
176
|
* Slice an array, returning a new array with a specified range of items
|
|
177
|
+
*
|
|
141
178
|
* @param array Original array
|
|
142
179
|
* @param start Start index _(inclusive)_
|
|
143
180
|
* @param end End index _(exclusive)_
|
|
144
|
-
* @
|
|
181
|
+
* @returns New array with sliced items
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* slice([1, 2, 3, 4, 5], 1, 4); // => [2, 3, 4]
|
|
186
|
+
* ```
|
|
145
187
|
*/
|
|
146
188
|
export function slice<Item>(array: Item[], start: number, end: number): Item[];
|
|
147
189
|
|
|
148
190
|
/**
|
|
149
|
-
* Slice an array, returning a new array with a specified number of items
|
|
191
|
+
* Slice an array, returning a new array with a specified number of items _(from the start)_
|
|
192
|
+
*
|
|
150
193
|
* @param array Original array
|
|
151
|
-
* @param count Maximum
|
|
152
|
-
* @
|
|
194
|
+
* @param count Maximum size of the new array
|
|
195
|
+
* @returns New array with sliced items
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* slice([1, 2, 3, 4, 5], 3); // => [1, 2, 3]
|
|
200
|
+
* ```
|
|
153
201
|
*/
|
|
154
202
|
export function slice<Item>(array: Item[], count: number): Item[];
|
|
155
203
|
|
|
156
204
|
/**
|
|
157
205
|
* Slice an array
|
|
206
|
+
*
|
|
158
207
|
* @param array Array to slice
|
|
159
208
|
* @returns Sliced array
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* slice([1, 2, 3]); // => [1, 2, 3]
|
|
213
|
+
* ```
|
|
160
214
|
*/
|
|
161
215
|
export function slice<Item>(array: Item[]): Item[];
|
|
162
216
|
|
|
@@ -187,35 +241,64 @@ export function slice(array: unknown[], first?: number, second?: number): unknow
|
|
|
187
241
|
|
|
188
242
|
/**
|
|
189
243
|
* Take items from the start of an array until they match a value
|
|
244
|
+
*
|
|
190
245
|
* @param array Original array
|
|
191
|
-
* @param
|
|
246
|
+
* @param callback Callback to get an item's value for matching
|
|
192
247
|
* @param value Value to match against
|
|
193
248
|
* @returns New array with taken items
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* take(
|
|
253
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
254
|
+
* item => item.id,
|
|
255
|
+
* 2,
|
|
256
|
+
* ); // => [{id: 1}]
|
|
257
|
+
* ```
|
|
194
258
|
*/
|
|
195
|
-
export function take<Item extends
|
|
259
|
+
export function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
|
|
196
260
|
array: Item[],
|
|
197
|
-
|
|
198
|
-
value:
|
|
261
|
+
callback: Callback,
|
|
262
|
+
value: ReturnType<Callback>,
|
|
199
263
|
): Item[];
|
|
200
264
|
|
|
201
265
|
/**
|
|
202
266
|
* Take items from the start of an array until they match a value
|
|
267
|
+
*
|
|
203
268
|
* @param array Original array
|
|
204
|
-
* @param
|
|
269
|
+
* @param key Key to get an item's value for matching
|
|
205
270
|
* @param value Value to match against
|
|
206
|
-
* @
|
|
271
|
+
* @returns New array with taken items
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```typescript
|
|
275
|
+
* take(
|
|
276
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
277
|
+
* 'id',
|
|
278
|
+
* 2,
|
|
279
|
+
* ); // => [{id: 1}]
|
|
280
|
+
* ```
|
|
207
281
|
*/
|
|
208
|
-
export function take<Item
|
|
282
|
+
export function take<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
209
283
|
array: Item[],
|
|
210
|
-
|
|
211
|
-
value:
|
|
284
|
+
key: ItemKey,
|
|
285
|
+
value: Item[ItemKey],
|
|
212
286
|
): Item[];
|
|
213
287
|
|
|
214
288
|
/**
|
|
215
289
|
* Take items from the start of an array while they match a filter
|
|
290
|
+
*
|
|
216
291
|
* @param array Original array
|
|
217
292
|
* @param callback Filter callback to match items
|
|
218
|
-
* @
|
|
293
|
+
* @returns New array with taken items
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* take(
|
|
298
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
299
|
+
* item => item.id === 2,
|
|
300
|
+
* ); // => [{id: 1}]
|
|
301
|
+
* ```
|
|
219
302
|
*/
|
|
220
303
|
export function take<Item extends PlainObject>(
|
|
221
304
|
array: Item[],
|
|
@@ -224,9 +307,16 @@ export function take<Item extends PlainObject>(
|
|
|
224
307
|
|
|
225
308
|
/**
|
|
226
309
|
* Take a specified number of items, from the start if `>= 0`, or from the end if `< 0`
|
|
310
|
+
*
|
|
227
311
|
* @param array Original array
|
|
228
312
|
* @param count Number of items to take
|
|
229
313
|
* @returns New array with taken items
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* take([1, 2, 3, 4, 5], 2); // => [1, 2]
|
|
318
|
+
* take([1, 2, 3, 4, 5], -2); // => [4, 5]
|
|
319
|
+
* ```
|
|
230
320
|
*/
|
|
231
321
|
export function take(array: unknown[], count: number): unknown[];
|
|
232
322
|
|