@oscarpalmer/atoms 0.184.2 → 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/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- 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/{position.d.mts → match.d.mts} +168 -36
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.d.mts +78 -8
- package/dist/array/move.mjs +11 -1
- 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 +30 -4
- package/dist/array/sort.mjs +1 -1
- 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 +2 -1
- 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/beacon.d.mts +12 -0
- package/dist/beacon.mjs +9 -0
- package/dist/color/instance.d.mts +8 -0
- package/dist/color/instance.mjs +3 -0
- package/dist/color/models.d.mts +30 -0
- package/dist/function/assert.d.mts +29 -8
- package/dist/function/assert.mjs +29 -8
- package/dist/function/memoize.d.mts +3 -0
- package/dist/function/memoize.mjs +3 -0
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +3 -0
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +2158 -288
- package/dist/index.mjs +294 -181
- 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/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/get.d.mts +27 -5
- package/dist/internal/value/has.d.mts +7 -7
- package/dist/internal/value/has.mjs +1 -1
- package/dist/internal/value/misc.d.mts +2 -2
- package/dist/internal/value/misc.mjs +10 -4
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +11 -0
- package/dist/models.d.mts +14 -1
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.d.mts +0 -6
- package/dist/promise/models.d.mts +36 -0
- package/dist/promise/models.mjs +6 -0
- package/dist/queue.d.mts +13 -1
- package/dist/queue.mjs +9 -0
- package/dist/result/index.d.mts +0 -8
- package/dist/result/index.mjs +0 -8
- package/dist/result/match.d.mts +4 -4
- package/dist/result/work/flow.d.mts +12 -36
- package/dist/result/work/pipe.d.mts +11 -33
- package/dist/sized/set.d.mts +3 -2
- package/dist/sized/set.mjs +3 -2
- package/dist/value/collection.d.mts +1 -1
- package/dist/value/handle.mjs +1 -1
- package/dist/value/merge.d.mts +28 -25
- package/dist/value/merge.mjs +29 -18
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +10 -1
- package/dist/value/unsmush.d.mts +2 -3
- package/package.json +5 -5
- package/src/array/difference.ts +33 -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 +40 -0
- package/src/array/get.ts +21 -15
- package/src/array/group-by.ts +142 -0
- package/src/array/index.ts +1 -1
- package/src/array/insert.ts +16 -2
- package/src/array/intersection.ts +33 -0
- package/src/array/last.ts +75 -2
- package/src/array/{position.ts → match.ts} +197 -65
- package/src/array/move.ts +87 -13
- package/src/array/partition.ts +35 -0
- package/src/array/push.ts +8 -2
- package/src/array/reverse.ts +5 -0
- package/src/array/select.ts +96 -13
- package/src/array/single.ts +29 -0
- package/src/array/slice.ts +114 -24
- package/src/array/sort.ts +30 -4
- package/src/array/splice.ts +52 -4
- package/src/array/swap.ts +122 -13
- 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 +42 -3
- package/src/array/union.ts +33 -0
- package/src/array/unique.ts +24 -0
- package/src/array/update.ts +38 -3
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +9 -1
- package/src/color/models.ts +30 -0
- package/src/function/assert.ts +66 -7
- package/src/function/memoize.ts +3 -0
- package/src/function/once.ts +5 -1
- package/src/function/retry.ts +3 -0
- 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/compare.ts +2 -1
- package/src/internal/value/equal.ts +5 -0
- package/src/internal/value/get.ts +27 -5
- package/src/internal/value/has.ts +10 -10
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/models.ts +18 -0
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +13 -1
- package/src/result/index.ts +0 -8
- package/src/result/match.ts +4 -4
- package/src/result/work/flow.ts +12 -36
- package/src/result/work/pipe.ts +11 -33
- package/src/sized/set.ts +4 -3
- package/src/value/collection.ts +1 -1
- package/src/value/merge.ts +88 -66
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +10 -1
- package/src/value/unsmush.ts +2 -8
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
// #region Types
|
|
2
|
-
|
|
3
1
|
import {getArrayCallback} from '../internal/array/callbacks';
|
|
4
2
|
import type {PlainObject} from '../models';
|
|
5
3
|
|
|
6
|
-
|
|
4
|
+
// #region Types
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Comparison of an array within another array
|
|
8
|
+
*/
|
|
9
|
+
export type ArrayComparison = 'end' | 'inside' | 'invalid' | 'outside' | 'same' | 'start';
|
|
7
10
|
|
|
8
11
|
// #endregion
|
|
9
12
|
|
|
@@ -13,33 +16,58 @@ export type ArrayPosition = 'end' | 'inside' | 'invalid' | 'outside' | 'same' |
|
|
|
13
16
|
* Does the needle array end the haystack array?
|
|
14
17
|
* @param haystack Haystack array
|
|
15
18
|
* @param needle Needle array
|
|
16
|
-
* @param
|
|
17
|
-
* @
|
|
19
|
+
* @param callback Callback to get an item's value for matching
|
|
20
|
+
* @returns `true` if the haystack ends with the needle, otherwise `false`
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* endsWithArray(
|
|
25
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
26
|
+
* [{id: 2}, {id: 3}],
|
|
27
|
+
* item => item.id,
|
|
28
|
+
* ); // => true
|
|
29
|
+
* ```
|
|
18
30
|
*/
|
|
19
|
-
export function endsWithArray<Item
|
|
31
|
+
export function endsWithArray<Item>(
|
|
20
32
|
haystack: Item[],
|
|
21
33
|
needle: Item[],
|
|
22
|
-
|
|
34
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
23
35
|
): boolean;
|
|
24
36
|
|
|
25
37
|
/**
|
|
26
38
|
* Does the needle array end the haystack array?
|
|
39
|
+
*
|
|
27
40
|
* @param haystack Haystack array
|
|
28
41
|
* @param needle Needle array
|
|
29
|
-
* @param
|
|
30
|
-
* @
|
|
42
|
+
* @param key Key to get an item's value for matching
|
|
43
|
+
* @returns `true` if the haystack ends with the needle, otherwise `false`
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* endsWithArray(
|
|
48
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
49
|
+
* [{id: 2}, {id: 3}],
|
|
50
|
+
* 'id',
|
|
51
|
+
* ); // => true
|
|
52
|
+
* ```
|
|
31
53
|
*/
|
|
32
|
-
export function endsWithArray<Item>(
|
|
54
|
+
export function endsWithArray<Item extends PlainObject>(
|
|
33
55
|
haystack: Item[],
|
|
34
56
|
needle: Item[],
|
|
35
|
-
|
|
57
|
+
key: keyof Item,
|
|
36
58
|
): boolean;
|
|
37
59
|
|
|
38
60
|
/**
|
|
39
61
|
* Does the needle array end the haystack array?
|
|
62
|
+
*
|
|
40
63
|
* @param haystack Haystack array
|
|
41
64
|
* @param needle Needle array
|
|
42
|
-
* @
|
|
65
|
+
* @returns `true` if the haystack ends with the needle, otherwise `false`
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* endsWithArray([1, 2, 3], [2, 3]); // => true
|
|
70
|
+
* ```
|
|
43
71
|
*/
|
|
44
72
|
export function endsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
|
|
45
73
|
|
|
@@ -49,59 +77,85 @@ export function endsWithArray(haystack: unknown[], needle: unknown[], key?: unkn
|
|
|
49
77
|
|
|
50
78
|
/**
|
|
51
79
|
* Get the position of an array within another array
|
|
80
|
+
*
|
|
52
81
|
* @param haystack Haystack array
|
|
53
82
|
* @param needle Needle array
|
|
54
|
-
* @param
|
|
83
|
+
* @param callback Callback to get an item's value for matching
|
|
55
84
|
* @returns Position of the needle within the haystack
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* getArrayComparison(
|
|
89
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
90
|
+
* [{id: 2}, {id: 3}],
|
|
91
|
+
* item => item.id,
|
|
92
|
+
* ); // => 'end'
|
|
93
|
+
* ```
|
|
56
94
|
*/
|
|
57
|
-
export function
|
|
95
|
+
export function getArrayComparison<Item>(
|
|
58
96
|
haystack: Item[],
|
|
59
97
|
needle: Item[],
|
|
60
|
-
|
|
61
|
-
):
|
|
98
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
99
|
+
): ArrayComparison;
|
|
62
100
|
|
|
63
101
|
/**
|
|
64
102
|
* Get the position of an array within another array
|
|
103
|
+
*
|
|
65
104
|
* @param haystack Haystack array
|
|
66
105
|
* @param needle Needle array
|
|
67
|
-
* @param
|
|
106
|
+
* @param key Key to get an item's value for matching
|
|
68
107
|
* @returns Position of the needle within the haystack
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* getArrayComparison(
|
|
112
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
113
|
+
* [{id: 2}, {id: 3}],
|
|
114
|
+
* 'id',
|
|
115
|
+
* ); // => 'end'
|
|
116
|
+
* ```
|
|
69
117
|
*/
|
|
70
|
-
export function
|
|
118
|
+
export function getArrayComparison<Item extends PlainObject>(
|
|
71
119
|
haystack: Item[],
|
|
72
120
|
needle: Item[],
|
|
73
|
-
|
|
74
|
-
):
|
|
121
|
+
key: keyof Item,
|
|
122
|
+
): ArrayComparison;
|
|
75
123
|
|
|
76
124
|
/**
|
|
77
125
|
* Get the position of an array within another array
|
|
126
|
+
*
|
|
78
127
|
* @param haystack Haystack array
|
|
79
128
|
* @param needle Needle array
|
|
80
129
|
* @returns Position of the needle within the haystack
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* getArrayComparison([1, 2, 3], [2, 3]); // => 'end'
|
|
134
|
+
* ```
|
|
81
135
|
*/
|
|
82
|
-
export function
|
|
136
|
+
export function getArrayComparison<Item>(haystack: Item[], needle: Item[]): ArrayComparison;
|
|
83
137
|
|
|
84
|
-
export function
|
|
138
|
+
export function getArrayComparison(
|
|
85
139
|
haystack: unknown[],
|
|
86
140
|
needle: unknown[],
|
|
87
141
|
key?: unknown,
|
|
88
|
-
):
|
|
142
|
+
): ArrayComparison {
|
|
89
143
|
return getPosition(haystack, needle, key)[1];
|
|
90
144
|
}
|
|
91
145
|
|
|
92
|
-
function getName(start: number, haystack: number, needle: number):
|
|
146
|
+
function getName(start: number, haystack: number, needle: number): ArrayComparison {
|
|
93
147
|
if (start === 0) {
|
|
94
|
-
return haystack === needle ?
|
|
148
|
+
return haystack === needle ? COMPARISON_SAME : COMPARISON_START;
|
|
95
149
|
}
|
|
96
150
|
|
|
97
|
-
return start + needle === haystack ?
|
|
151
|
+
return start + needle === haystack ? COMPARISON_END : COMPARISON_INSIDE;
|
|
98
152
|
}
|
|
99
153
|
|
|
100
154
|
function getPosition(
|
|
101
155
|
haystack: unknown[],
|
|
102
156
|
needle: unknown[],
|
|
103
157
|
key?: unknown,
|
|
104
|
-
): readonly [number,
|
|
158
|
+
): readonly [number, ArrayComparison] {
|
|
105
159
|
if (!Array.isArray(haystack) || !Array.isArray(needle)) {
|
|
106
160
|
return invalid;
|
|
107
161
|
}
|
|
@@ -162,35 +216,61 @@ function getPosition(
|
|
|
162
216
|
|
|
163
217
|
/**
|
|
164
218
|
* Does the needle array exist within the haystack array?
|
|
219
|
+
*
|
|
165
220
|
* @param haystack Haystack array
|
|
166
221
|
* @param needle Needle array
|
|
167
|
-
* @param
|
|
168
|
-
* @
|
|
222
|
+
* @param callback Callback to get an item's value for matching
|
|
223
|
+
* @returns `true` if the haystack includes the needle, otherwise `false`
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* includesArray(
|
|
228
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
229
|
+
* [{id: 2}, {id: 3}],
|
|
230
|
+
* item => item.id,
|
|
231
|
+
* ); // => true
|
|
232
|
+
* ```
|
|
169
233
|
*/
|
|
170
|
-
export function includesArray<Item
|
|
234
|
+
export function includesArray<Item>(
|
|
171
235
|
haystack: Item[],
|
|
172
236
|
needle: Item[],
|
|
173
|
-
|
|
237
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
174
238
|
): boolean;
|
|
175
239
|
|
|
176
240
|
/**
|
|
177
241
|
* Does the needle array exist within the haystack array?
|
|
242
|
+
*
|
|
178
243
|
* @param haystack Haystack array
|
|
179
244
|
* @param needle Needle array
|
|
180
|
-
* @param
|
|
181
|
-
* @
|
|
245
|
+
* @param key Key to get an item's value for matching
|
|
246
|
+
* @returns `true` if the haystack includes the needle, otherwise `false`
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* includesArray(
|
|
251
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
252
|
+
* [{id: 2}, {id: 3}],
|
|
253
|
+
* 'id',
|
|
254
|
+
* ); // => true
|
|
255
|
+
* ```
|
|
182
256
|
*/
|
|
183
|
-
export function includesArray<Item>(
|
|
257
|
+
export function includesArray<Item extends PlainObject>(
|
|
184
258
|
haystack: Item[],
|
|
185
259
|
needle: Item[],
|
|
186
|
-
|
|
260
|
+
key: keyof Item,
|
|
187
261
|
): boolean;
|
|
188
262
|
|
|
189
263
|
/**
|
|
190
264
|
* Does the needle array exist within the haystack array?
|
|
265
|
+
*
|
|
191
266
|
* @param haystack Haystack array
|
|
192
267
|
* @param needle Needle array
|
|
193
|
-
* @
|
|
268
|
+
* @returns `true` if the haystack includes the needle, otherwise `false`
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* includesArray([1, 2, 3], [2, 3]); // => true
|
|
273
|
+
* ```
|
|
194
274
|
*/
|
|
195
275
|
export function includesArray<Item>(haystack: Item[], needle: Item[]): boolean;
|
|
196
276
|
|
|
@@ -200,35 +280,61 @@ export function includesArray(haystack: unknown[], needle: unknown[], key?: unkn
|
|
|
200
280
|
|
|
201
281
|
/**
|
|
202
282
|
* Get the index of an array within another array
|
|
283
|
+
*
|
|
203
284
|
* @param haystack Haystack array
|
|
204
285
|
* @param needle Needle array
|
|
205
|
-
* @param
|
|
206
|
-
* @
|
|
286
|
+
* @param callback Callback to get an item's value for matching
|
|
287
|
+
* @returns Index of the needle's start within the haystack, or `-1` if it is not found
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* indexOfArray(
|
|
292
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
293
|
+
* [{id: 2}, {id: 3}],
|
|
294
|
+
* item => item.id,
|
|
295
|
+
* ); // => 1
|
|
296
|
+
* ```
|
|
207
297
|
*/
|
|
208
|
-
export function indexOfArray<Item
|
|
298
|
+
export function indexOfArray<Item>(
|
|
209
299
|
haystack: Item[],
|
|
210
300
|
needle: Item[],
|
|
211
|
-
|
|
301
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
212
302
|
): number;
|
|
213
303
|
|
|
214
304
|
/**
|
|
215
305
|
* Get the index of an array within another array
|
|
306
|
+
*
|
|
216
307
|
* @param haystack Haystack array
|
|
217
308
|
* @param needle Needle array
|
|
218
|
-
* @param
|
|
219
|
-
* @
|
|
309
|
+
* @param key Key to get an item's value for matching
|
|
310
|
+
* @returns Index of the needle's start within the haystack, or `-1` if it is not found
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* indexOfArray(
|
|
315
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
316
|
+
* [{id: 2}, {id: 3}],
|
|
317
|
+
* 'id',
|
|
318
|
+
* ); // => 1
|
|
319
|
+
* ```
|
|
220
320
|
*/
|
|
221
|
-
export function indexOfArray<Item>(
|
|
321
|
+
export function indexOfArray<Item extends PlainObject>(
|
|
222
322
|
haystack: Item[],
|
|
223
323
|
needle: Item[],
|
|
224
|
-
|
|
324
|
+
key: keyof Item,
|
|
225
325
|
): number;
|
|
226
326
|
|
|
227
327
|
/**
|
|
228
328
|
* Get the index of an array within another array
|
|
329
|
+
*
|
|
229
330
|
* @param haystack Haystack array
|
|
230
331
|
* @param needle Needle array
|
|
231
|
-
* @
|
|
332
|
+
* @returns Index of the needle's start within the haystack, or `-1` if it is not found
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```typescript
|
|
336
|
+
* indexOfArray([1, 2, 3], [2, 3]); // => 1
|
|
337
|
+
* ```
|
|
232
338
|
*/
|
|
233
339
|
export function indexOfArray<Item>(haystack: Item[], needle: Item[]): number;
|
|
234
340
|
|
|
@@ -238,35 +344,61 @@ export function indexOfArray(haystack: unknown[], needle: unknown[], key?: unkno
|
|
|
238
344
|
|
|
239
345
|
/**
|
|
240
346
|
* Does the needle array start the haystack array?
|
|
347
|
+
*
|
|
241
348
|
* @param haystack Haystack array
|
|
242
349
|
* @param needle Needle array
|
|
243
|
-
* @param
|
|
244
|
-
* @
|
|
350
|
+
* @param callback Callback to get an item's value for matching
|
|
351
|
+
* @returns `true` if the haystack starts with the needle, otherwise `false`
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* startsWithArray(
|
|
356
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
357
|
+
* [{id: 1}, {id: 2}],
|
|
358
|
+
* item => item.id,
|
|
359
|
+
* ); // => true
|
|
360
|
+
* ```
|
|
245
361
|
*/
|
|
246
|
-
export function startsWithArray<Item
|
|
362
|
+
export function startsWithArray<Item>(
|
|
247
363
|
haystack: Item[],
|
|
248
364
|
needle: Item[],
|
|
249
|
-
|
|
365
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
250
366
|
): boolean;
|
|
251
367
|
|
|
252
368
|
/**
|
|
253
369
|
* Does the needle array start the haystack array?
|
|
370
|
+
*
|
|
254
371
|
* @param haystack Haystack array
|
|
255
372
|
* @param needle Needle array
|
|
256
|
-
* @param
|
|
257
|
-
* @
|
|
373
|
+
* @param key Key to get an item's value for matching
|
|
374
|
+
* @returns `true` if the haystack starts with the needle, otherwise `false`
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* startsWithArray(
|
|
379
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
380
|
+
* [{id: 1}, {id: 2}],
|
|
381
|
+
* 'id',
|
|
382
|
+
* ); // => true
|
|
383
|
+
* ```
|
|
258
384
|
*/
|
|
259
|
-
export function startsWithArray<Item>(
|
|
385
|
+
export function startsWithArray<Item extends PlainObject>(
|
|
260
386
|
haystack: Item[],
|
|
261
387
|
needle: Item[],
|
|
262
|
-
|
|
388
|
+
key: keyof Item,
|
|
263
389
|
): boolean;
|
|
264
390
|
|
|
265
391
|
/**
|
|
266
392
|
* Does the needle array start the haystack array?
|
|
393
|
+
*
|
|
267
394
|
* @param haystack Haystack array
|
|
268
395
|
* @param needle Needle array
|
|
269
|
-
* @
|
|
396
|
+
* @returns `true` if the haystack starts with the needle, otherwise `false`
|
|
397
|
+
*
|
|
398
|
+
* @example
|
|
399
|
+
* ```typescript
|
|
400
|
+
* startsWithArray([1, 2, 3], [1, 2]); // => true
|
|
401
|
+
* ```
|
|
270
402
|
*/
|
|
271
403
|
export function startsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
|
|
272
404
|
|
|
@@ -278,26 +410,26 @@ export function startsWithArray(haystack: unknown[], needle: unknown[], key?: un
|
|
|
278
410
|
|
|
279
411
|
// #region Variables
|
|
280
412
|
|
|
281
|
-
const
|
|
413
|
+
const COMPARISON_END: ArrayComparison = 'end';
|
|
282
414
|
|
|
283
|
-
const
|
|
415
|
+
const COMPARISON_INSIDE: ArrayComparison = 'inside';
|
|
284
416
|
|
|
285
|
-
const
|
|
417
|
+
const COMPARISON_INVALID: ArrayComparison = 'invalid';
|
|
286
418
|
|
|
287
|
-
const
|
|
419
|
+
const COMPARISON_OUTSIDE: ArrayComparison = 'outside';
|
|
288
420
|
|
|
289
|
-
const
|
|
421
|
+
const COMPARISON_SAME: ArrayComparison = 'same';
|
|
290
422
|
|
|
291
|
-
const
|
|
423
|
+
const COMPARISON_START: ArrayComparison = 'start';
|
|
292
424
|
|
|
293
|
-
const endings = new Set<
|
|
425
|
+
const endings = new Set<ArrayComparison>([COMPARISON_END, COMPARISON_SAME]);
|
|
294
426
|
|
|
295
|
-
const invalid = [-1,
|
|
427
|
+
const invalid = [-1, COMPARISON_INVALID] as const;
|
|
296
428
|
|
|
297
|
-
const outside = [-1,
|
|
429
|
+
const outside = [-1, COMPARISON_OUTSIDE] as const;
|
|
298
430
|
|
|
299
|
-
const outsides = new Set<
|
|
431
|
+
const outsides = new Set<ArrayComparison>([COMPARISON_INVALID, COMPARISON_OUTSIDE]);
|
|
300
432
|
|
|
301
|
-
const starts = new Set<
|
|
433
|
+
const starts = new Set<ArrayComparison>([COMPARISON_START, COMPARISON_SAME]);
|
|
302
434
|
|
|
303
435
|
// #endregion
|
package/src/array/move.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {arraysOverlap} from '../internal/array/overlap';
|
|
2
2
|
import type {PlainObject} from '../models';
|
|
3
|
-
import {indexOfArray} from './
|
|
3
|
+
import {indexOfArray} from './match';
|
|
4
|
+
|
|
5
|
+
// #region Functions
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Move an item _(or array of items)_ to the position of another item _(or array of items)_ within an array
|
|
@@ -8,17 +10,27 @@ import {indexOfArray} from './position';
|
|
|
8
10
|
* When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
|
|
9
11
|
*
|
|
10
12
|
* If either of values are not present in the array, or if they overlap, the array will be returned unchanged
|
|
13
|
+
*
|
|
11
14
|
* @param array Array to move within
|
|
12
15
|
* @param from Item or items to move
|
|
13
16
|
* @param to Item or items to move to
|
|
14
|
-
* @param
|
|
17
|
+
* @param callback Callback to get an item's value for matching
|
|
15
18
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
|
|
23
|
+
*
|
|
24
|
+
* move(array, {id: 1}, {id: 3}, item => item.id); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
|
|
25
|
+
* move(array, [{id: 2}, {id: 3}], {id: 4}, item => item.id); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
|
|
26
|
+
* move(array, {id: 5}, {id: 1}, item => item.id); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
|
|
27
|
+
* ```
|
|
16
28
|
*/
|
|
17
|
-
export function move<Item
|
|
29
|
+
export function move<Item>(
|
|
18
30
|
array: Item[],
|
|
19
31
|
from: Item | Item[],
|
|
20
32
|
to: Item | Item[],
|
|
21
|
-
|
|
33
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
22
34
|
): Item[];
|
|
23
35
|
|
|
24
36
|
/**
|
|
@@ -27,17 +39,27 @@ export function move<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
27
39
|
* When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
|
|
28
40
|
*
|
|
29
41
|
* If either of values are not present in the array, or if they overlap, the array will be returned unchanged
|
|
42
|
+
*
|
|
30
43
|
* @param array Array to move within
|
|
31
44
|
* @param from Item or items to move
|
|
32
45
|
* @param to Item or items to move to
|
|
33
|
-
* @param
|
|
46
|
+
* @param key Key to get an item's value for matching
|
|
34
47
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
|
|
52
|
+
*
|
|
53
|
+
* move(array, {id: 1}, {id: 3}, 'id'); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
|
|
54
|
+
* move(array, [{id: 2}, {id: 3}], {id: 4}, 'id'); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
|
|
55
|
+
* move(array, {id: 5}, {id: 1}, 'id'); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
|
|
56
|
+
* ```
|
|
35
57
|
*/
|
|
36
|
-
export function move<Item>(
|
|
58
|
+
export function move<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
37
59
|
array: Item[],
|
|
38
60
|
from: Item | Item[],
|
|
39
61
|
to: Item | Item[],
|
|
40
|
-
|
|
62
|
+
key: ItemKey,
|
|
41
63
|
): Item[];
|
|
42
64
|
|
|
43
65
|
/**
|
|
@@ -46,10 +68,20 @@ export function move<Item>(
|
|
|
46
68
|
* When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
|
|
47
69
|
*
|
|
48
70
|
* If either of values are not present in the array, or if they overlap, the array will be returned unchanged
|
|
71
|
+
*
|
|
49
72
|
* @param array Array to move within
|
|
50
73
|
* @param from Item or items to move
|
|
51
74
|
* @param to Item or items to move to
|
|
52
75
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const array = [1, 2, 3, 4];
|
|
80
|
+
*
|
|
81
|
+
* move(array, 1, 3); // => [2, 3, 1, 4]
|
|
82
|
+
* move(array, [2, 3], 4); // => [1, 4, 2, 3]
|
|
83
|
+
* move(array, 5, 1); // => [1, 2, 3, 4] (unchanged)
|
|
84
|
+
* ```
|
|
53
85
|
*/
|
|
54
86
|
export function move<Item>(array: Item[], from: Item | Item[], to: Item | Item[]): Item[];
|
|
55
87
|
|
|
@@ -114,10 +146,20 @@ move.toIndex = moveToIndex;
|
|
|
114
146
|
* If the from index is out of bounds, the array will be returned unchanged
|
|
115
147
|
*
|
|
116
148
|
* Available as `moveIndices` and `move.indices`
|
|
149
|
+
*
|
|
117
150
|
* @param array Array to move within
|
|
118
151
|
* @param from Index to move from
|
|
119
152
|
* @param to Index to move to
|
|
120
153
|
* @returns Original array with item moved _(or unchanged if unable to move)_
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const array = [1, 2, 3, 4];
|
|
158
|
+
*
|
|
159
|
+
* moveIndices(array, 0, 2); // => [2, 3, 1, 4]
|
|
160
|
+
* moveIndices(array, -1, 0); // => [4, 2, 3, 1]
|
|
161
|
+
* moveIndices(array, 5, 1); // => [4, 2, 3, 1] (unchanged)
|
|
162
|
+
* ```
|
|
121
163
|
*/
|
|
122
164
|
export function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
|
|
123
165
|
if (!Array.isArray(array)) {
|
|
@@ -154,17 +196,27 @@ export function moveIndices<Item>(array: Item[], from: number, to: number): Item
|
|
|
154
196
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
155
197
|
*
|
|
156
198
|
* Available as `moveToIndex` and `move.toIndex`
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
|
|
203
|
+
*
|
|
204
|
+
* moveToIndex(array, {id: 1}, 2, item => item.id); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
|
|
205
|
+
* moveToIndex(array, [{id: 2}, {id: 3}], 3, item => item.id); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
|
|
206
|
+
* moveToIndex(array, {id: 5}, 1, item => item.id); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
157
209
|
* @param array Array to move within
|
|
158
210
|
* @param value Item or items to move
|
|
159
211
|
* @param index Index to move to
|
|
160
|
-
* @param
|
|
212
|
+
* @param callback Callback to get an item's value for matching
|
|
161
213
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
162
214
|
*/
|
|
163
|
-
export function moveToIndex<Item
|
|
215
|
+
export function moveToIndex<Item>(
|
|
164
216
|
array: Item[],
|
|
165
217
|
value: Item | Item[],
|
|
166
218
|
index: number,
|
|
167
|
-
|
|
219
|
+
callback: (item: Item, index: number, array: Item[]) => unknown,
|
|
168
220
|
): Item[];
|
|
169
221
|
|
|
170
222
|
/**
|
|
@@ -173,17 +225,27 @@ export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item
|
|
|
173
225
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
174
226
|
*
|
|
175
227
|
* Available as `moveToIndex` and `move.toIndex`
|
|
228
|
+
*
|
|
176
229
|
* @param array Array to move within
|
|
177
230
|
* @param value Item or items to move
|
|
178
231
|
* @param index Index to move to
|
|
179
|
-
* @param
|
|
232
|
+
* @param key Key to get an item's value for matching
|
|
180
233
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
|
|
238
|
+
*
|
|
239
|
+
* moveToIndex(array, {id: 1}, 2, 'id'); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
|
|
240
|
+
* moveToIndex(array, [{id: 2}, {id: 3}], 3, 'id'); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
|
|
241
|
+
* moveToIndex(array, {id: 5}, 1, 'id'); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
|
|
242
|
+
* ```
|
|
181
243
|
*/
|
|
182
|
-
export function moveToIndex<Item>(
|
|
244
|
+
export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
183
245
|
array: Item[],
|
|
184
246
|
value: Item | Item[],
|
|
185
247
|
index: number,
|
|
186
|
-
|
|
248
|
+
key: ItemKey,
|
|
187
249
|
): Item[];
|
|
188
250
|
|
|
189
251
|
/**
|
|
@@ -192,10 +254,20 @@ export function moveToIndex<Item>(
|
|
|
192
254
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
193
255
|
*
|
|
194
256
|
* Available as `moveToIndex` and `move.toIndex`
|
|
257
|
+
*
|
|
195
258
|
* @param array Array to move within
|
|
196
259
|
* @param value Item or items to move
|
|
197
260
|
* @param index Index to move to
|
|
198
261
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const array = [1, 2, 3, 4];
|
|
266
|
+
*
|
|
267
|
+
* moveToIndex(array, 1, 2); // => [2, 3, 1, 4]
|
|
268
|
+
* moveToIndex(array, [2, 3], 3); // => [1, 4, 2, 3]
|
|
269
|
+
* moveToIndex(array, 5, 1); // => [1, 2, 3, 4] (unchanged)
|
|
270
|
+
* ```
|
|
199
271
|
*/
|
|
200
272
|
export function moveToIndex<Item>(array: Item[], value: Item | Item[], index: number): Item[];
|
|
201
273
|
|
|
@@ -239,3 +311,5 @@ export function moveToIndex(
|
|
|
239
311
|
|
|
240
312
|
return array;
|
|
241
313
|
}
|
|
314
|
+
|
|
315
|
+
// #endregion
|