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