@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
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import {FIND_VALUE_INDEX, findValue} from './find';
|
|
2
|
-
import type {PlainObject} from '../../models';
|
|
3
|
-
|
|
4
|
-
// #region Functions
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Get the index of the first matching item by callback
|
|
8
|
-
*
|
|
9
|
-
* @param array Array to search in
|
|
10
|
-
* @param callback Callback to get an item's value
|
|
11
|
-
* @param value Value to match against
|
|
12
|
-
* @returns Index of the first matching item, or `-1` if no match is found
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* indexOf(
|
|
17
|
-
* [{id: 1}, {id: 2}, {id: 3}],
|
|
18
|
-
* item => item.id,
|
|
19
|
-
* 2,
|
|
20
|
-
* ); // => 1
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export function indexOf<
|
|
24
|
-
Item,
|
|
25
|
-
Callback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
26
|
-
>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get the index of the first matching item by key
|
|
30
|
-
*
|
|
31
|
-
* @param array Array to search in
|
|
32
|
-
* @param key Key to match items by
|
|
33
|
-
* @param value Value to match against
|
|
34
|
-
* @returns Index of the first matching item, or `-1` if no match is found
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```typescript
|
|
38
|
-
* indexOf(
|
|
39
|
-
* [{id: 1}, {id: 2}, {id: 3}],
|
|
40
|
-
* 'id',
|
|
41
|
-
* 2,
|
|
42
|
-
* ); // => 1
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
46
|
-
array: Item[],
|
|
47
|
-
key: ItemKey,
|
|
48
|
-
value: Item[ItemKey],
|
|
49
|
-
): number;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Get the index of the first item matching the filter
|
|
53
|
-
*
|
|
54
|
-
* @param array Array to search in
|
|
55
|
-
* @param filter Filter callback to match items
|
|
56
|
-
* @returns Index of the first matching item, or `-1` if no match is found
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* indexOf(
|
|
61
|
-
* [{id: 1}, {id: 2}, {id: 3}],
|
|
62
|
-
* item => item.id === 2,
|
|
63
|
-
* ); // => 1
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export function indexOf<Item>(
|
|
67
|
-
array: Item[],
|
|
68
|
-
filter: (item: Item, index: number, array: Item[]) => boolean,
|
|
69
|
-
): number;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Get the index of the first item matching the given item
|
|
73
|
-
*
|
|
74
|
-
* @param array Array to search in
|
|
75
|
-
* @param item Item to match against
|
|
76
|
-
* @returns Index of the first matching item, or `-1` if no match is found
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```typescript
|
|
80
|
-
* indexOf([1, 2, 3, 4, 5], 3); // => 2
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export function indexOf<Item>(array: Item[], item: Item): number;
|
|
84
|
-
|
|
85
|
-
export function indexOf(array: unknown[], ...parameters: unknown[]): number {
|
|
86
|
-
return findValue(FIND_VALUE_INDEX, array, parameters, false);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
indexOf.last = lastIndexOf;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Get the index of the last matching item by callback
|
|
93
|
-
*
|
|
94
|
-
* _Available as `lastIndexOf` and `indexOf.last`_
|
|
95
|
-
*
|
|
96
|
-
* @param array Array to search in
|
|
97
|
-
* @param callback Callback to get an item's value
|
|
98
|
-
* @param value Value to match against
|
|
99
|
-
* @returns Index of the last matching item, or `-1` if no match is found
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```typescript
|
|
103
|
-
* lastIndexOf(
|
|
104
|
-
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
105
|
-
* item => item.id,
|
|
106
|
-
* 2,
|
|
107
|
-
* ); // => 3
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export function lastIndexOf<
|
|
111
|
-
Item,
|
|
112
|
-
Callback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
113
|
-
>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get the index of the last matching item by key
|
|
117
|
-
*
|
|
118
|
-
* _Available as `lastIndexOf` and `indexOf.last`_
|
|
119
|
-
*
|
|
120
|
-
* @param array Array to search in
|
|
121
|
-
* @param key Key to match items by
|
|
122
|
-
* @param value Value to match against
|
|
123
|
-
* @returns Index of the last matching item, or `-1` if no match is found
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```typescript
|
|
127
|
-
* lastIndexOf(
|
|
128
|
-
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
129
|
-
* 'id',
|
|
130
|
-
* 2,
|
|
131
|
-
* ); // => 3
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
export function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
135
|
-
array: Item[],
|
|
136
|
-
key: ItemKey,
|
|
137
|
-
value: Item[ItemKey],
|
|
138
|
-
): number;
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Get the index of the last item matching the filter
|
|
142
|
-
*
|
|
143
|
-
* _Available as `lastIndexOf` and `indexOf.last`_
|
|
144
|
-
*
|
|
145
|
-
* @param array Array to search in
|
|
146
|
-
* @param filter Filter callback to match items
|
|
147
|
-
* @returns Index of the last matching item, or `-1` if no match is found
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```typescript
|
|
151
|
-
* lastIndexOf(
|
|
152
|
-
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
153
|
-
* item => item.id === 2,
|
|
154
|
-
* ); // => 3
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
export function lastIndexOf<Item>(
|
|
158
|
-
array: Item[],
|
|
159
|
-
filter: (item: Item, index: number, array: Item[]) => boolean,
|
|
160
|
-
): number;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Get the index of the last item matching the given item
|
|
164
|
-
*
|
|
165
|
-
* _Available as `lastIndexOf` and `indexOf.last`_
|
|
166
|
-
*
|
|
167
|
-
* @param array Array to search in
|
|
168
|
-
* @param item Item to match against
|
|
169
|
-
* @returns Index of the last matching item, or `-1` if no match is found
|
|
170
|
-
*
|
|
171
|
-
* @example
|
|
172
|
-
* ```typescript
|
|
173
|
-
* lastIndexOf([1, 2, 3, 2, 1], 2); // => 3
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
export function lastIndexOf<Item>(array: Item[], item: Item): number;
|
|
177
|
-
|
|
178
|
-
export function lastIndexOf(array: unknown[], ...parameters: unknown[]): number {
|
|
179
|
-
return findValue(FIND_VALUE_INDEX, array, parameters, true);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// #endregion
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import {chunk} from './chunk';
|
|
2
|
-
|
|
3
|
-
// #region Types
|
|
4
|
-
|
|
5
|
-
type InsertType = 'insert' | 'push' | 'splice';
|
|
6
|
-
|
|
7
|
-
// #endregion
|
|
8
|
-
|
|
9
|
-
// #region Functions
|
|
10
|
-
|
|
11
|
-
function insertChunkedValues(
|
|
12
|
-
type: InsertType,
|
|
13
|
-
array: unknown[],
|
|
14
|
-
items: unknown[],
|
|
15
|
-
start: number,
|
|
16
|
-
deleteCount: number,
|
|
17
|
-
): unknown {
|
|
18
|
-
const actualDeleteCount = deleteCount < 0 ? 0 : deleteCount;
|
|
19
|
-
const actualStart = Math.min(Math.max(0, start), array.length);
|
|
20
|
-
const chunked = chunk(items);
|
|
21
|
-
const lastIndex = chunked.length - 1;
|
|
22
|
-
|
|
23
|
-
let index = chunked.length;
|
|
24
|
-
let returned: unknown[] | undefined;
|
|
25
|
-
|
|
26
|
-
while (index > 0) {
|
|
27
|
-
index -= 1;
|
|
28
|
-
|
|
29
|
-
const spliced = array.splice(
|
|
30
|
-
actualStart,
|
|
31
|
-
index === lastIndex ? actualDeleteCount : 0,
|
|
32
|
-
...chunked[index],
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
if (returned == null) {
|
|
36
|
-
returned = spliced;
|
|
37
|
-
} else {
|
|
38
|
-
returned.push(...spliced);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (type === INSERT_TYPE_INSERT) {
|
|
43
|
-
return array;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return type === INSERT_TYPE_SPLICE ? returned : array.length;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function insertValues(
|
|
50
|
-
type: InsertType,
|
|
51
|
-
array: unknown,
|
|
52
|
-
items: unknown,
|
|
53
|
-
start: unknown,
|
|
54
|
-
deleteCount: number,
|
|
55
|
-
): unknown {
|
|
56
|
-
const spliceArray = type === INSERT_TYPE_INSERT || type === INSERT_TYPE_SPLICE;
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
!Array.isArray(array) ||
|
|
60
|
-
typeof start !== 'number' ||
|
|
61
|
-
!Array.isArray(items) ||
|
|
62
|
-
items.length === 0
|
|
63
|
-
) {
|
|
64
|
-
return spliceArray ? [] : 0;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return insertChunkedValues(type, array, items, start, spliceArray ? deleteCount : 0);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// #endregion
|
|
71
|
-
|
|
72
|
-
// #region Variables
|
|
73
|
-
|
|
74
|
-
export const INSERT_TYPE_INSERT: InsertType = 'insert';
|
|
75
|
-
|
|
76
|
-
export const INSERT_TYPE_PUSH: InsertType = 'push';
|
|
77
|
-
|
|
78
|
-
export const INSERT_TYPE_SPLICE: InsertType = 'splice';
|
|
79
|
-
|
|
80
|
-
// #endregion
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
type OverlapItem = {
|
|
2
|
-
array: unknown[];
|
|
3
|
-
index: number;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
type OverlapResult = {
|
|
7
|
-
first: OverlapItem;
|
|
8
|
-
second: OverlapItem;
|
|
9
|
-
overlap: boolean;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function arraysOverlap(first: OverlapItem, second: OverlapItem): OverlapResult {
|
|
13
|
-
const firstArray = first.index < second.index ? first.array : second.array;
|
|
14
|
-
const secondArray = first.index < second.index ? second.array : first.array;
|
|
15
|
-
|
|
16
|
-
const firstIndex = firstArray === first.array ? first.index : second.index;
|
|
17
|
-
const secondIndex = firstArray === first.array ? second.index : first.index;
|
|
18
|
-
|
|
19
|
-
const firstEnd = firstIndex + firstArray.length - 1;
|
|
20
|
-
const secondEnd = secondIndex + secondArray.length - 1;
|
|
21
|
-
|
|
22
|
-
const overlap = firstIndex <= secondEnd && firstEnd >= secondIndex;
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
overlap,
|
|
26
|
-
first: {
|
|
27
|
-
array: firstArray,
|
|
28
|
-
index: firstIndex,
|
|
29
|
-
},
|
|
30
|
-
second: {
|
|
31
|
-
array: secondArray,
|
|
32
|
-
index: secondIndex,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import {getArrayCallback} from './callbacks';
|
|
2
|
-
|
|
3
|
-
// #region Types
|
|
4
|
-
|
|
5
|
-
type CompareSetsType = 'difference' | 'intersection' | 'union';
|
|
6
|
-
|
|
7
|
-
// #endregion
|
|
8
|
-
|
|
9
|
-
// #region Functions
|
|
10
|
-
|
|
11
|
-
export function compareSets(
|
|
12
|
-
type: CompareSetsType,
|
|
13
|
-
first: unknown[],
|
|
14
|
-
second: unknown[],
|
|
15
|
-
key?: unknown,
|
|
16
|
-
): unknown[] {
|
|
17
|
-
if (!Array.isArray(first)) {
|
|
18
|
-
return [];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const isDifference = type === COMPARE_SETS_DIFFERENCE;
|
|
22
|
-
const isIntersection = type === COMPARE_SETS_INTERSECTION;
|
|
23
|
-
const isUnion = type === COMPARE_SETS_UNION;
|
|
24
|
-
|
|
25
|
-
if (first.length === 0) {
|
|
26
|
-
return isDifference ? first.slice() : isIntersection ? [] : second.slice();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!Array.isArray(second) || second.length === 0) {
|
|
30
|
-
return isIntersection ? [] : first.slice();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const callback = getArrayCallback(key);
|
|
34
|
-
|
|
35
|
-
const values = isUnion ? first : second;
|
|
36
|
-
let {length} = values;
|
|
37
|
-
|
|
38
|
-
const set = new Set<unknown>([]);
|
|
39
|
-
|
|
40
|
-
for (let index = 0; index < length; index += 1) {
|
|
41
|
-
const item = values[index];
|
|
42
|
-
|
|
43
|
-
set.add(callback?.(item, index, values) ?? item);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const source = isUnion ? second : first;
|
|
47
|
-
|
|
48
|
-
length = source.length;
|
|
49
|
-
|
|
50
|
-
const result: unknown[] = isUnion ? first.slice() : [];
|
|
51
|
-
|
|
52
|
-
for (let index = 0; index < length; index += 1) {
|
|
53
|
-
const item = source[index];
|
|
54
|
-
const value = callback?.(item, index, source) ?? item;
|
|
55
|
-
|
|
56
|
-
if (isIntersection ? set.has(value) : !set.has(value)) {
|
|
57
|
-
result.push(item);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// #endregion
|
|
65
|
-
|
|
66
|
-
// #region Variables
|
|
67
|
-
|
|
68
|
-
export const COMPARE_SETS_DIFFERENCE: CompareSetsType = 'difference';
|
|
69
|
-
|
|
70
|
-
export const COMPARE_SETS_INTERSECTION: CompareSetsType = 'intersection';
|
|
71
|
-
|
|
72
|
-
export const COMPARE_SETS_UNION: CompareSetsType = 'union';
|
|
73
|
-
|
|
74
|
-
// #endregion
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {getRandomInteger} from '../random';
|
|
2
|
-
|
|
3
|
-
// #region Functions
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Shuffle items in array
|
|
7
|
-
*
|
|
8
|
-
* @param array Original array
|
|
9
|
-
* @returns Shuffled array
|
|
10
|
-
*/
|
|
11
|
-
export function shuffle<Item>(array: Item[]): Item[] {
|
|
12
|
-
if (!Array.isArray(array)) {
|
|
13
|
-
return [];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const shuffled = array.slice();
|
|
17
|
-
|
|
18
|
-
if (shuffled.length < 2) {
|
|
19
|
-
return shuffled;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let index = shuffled.length;
|
|
23
|
-
|
|
24
|
-
while (--index >= 0) {
|
|
25
|
-
const random = getRandomInteger(0, index);
|
|
26
|
-
|
|
27
|
-
[shuffled[index], shuffled[random]] = [shuffled[random], shuffled[index]];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return shuffled;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// #endregion
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {getArrayCallback} from './callbacks';
|
|
2
|
-
|
|
3
|
-
export function updateInArray(
|
|
4
|
-
array: unknown[],
|
|
5
|
-
items: unknown[],
|
|
6
|
-
key: unknown,
|
|
7
|
-
replace: boolean,
|
|
8
|
-
): unknown[] {
|
|
9
|
-
if (!Array.isArray(array)) {
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const itemsIsArray = Array.isArray(items);
|
|
14
|
-
|
|
15
|
-
if (array.length === 0 || !itemsIsArray) {
|
|
16
|
-
if (itemsIsArray) {
|
|
17
|
-
array.push(...items);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return array;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const {length} = items;
|
|
24
|
-
|
|
25
|
-
if (length === 0) {
|
|
26
|
-
return array;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const callback = getArrayCallback(key);
|
|
30
|
-
|
|
31
|
-
for (let valuesIndex = 0; valuesIndex < length; valuesIndex += 1) {
|
|
32
|
-
const item = items[valuesIndex];
|
|
33
|
-
const value = callback?.(item) ?? item;
|
|
34
|
-
|
|
35
|
-
const arrayIndex =
|
|
36
|
-
callback == null
|
|
37
|
-
? array.indexOf(value)
|
|
38
|
-
: array.findIndex(
|
|
39
|
-
(arrayItem, arrayIndex) => callback(arrayItem, arrayIndex, array) === value,
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
if (arrayIndex === -1) {
|
|
43
|
-
array.push(item);
|
|
44
|
-
} else if (replace) {
|
|
45
|
-
array[arrayIndex] = item;
|
|
46
|
-
} else {
|
|
47
|
-
array.splice(arrayIndex, 1);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return array;
|
|
52
|
-
}
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AsyncCancelableCallback,
|
|
3
|
-
CancelableCallback,
|
|
4
|
-
GenericAsyncCallback,
|
|
5
|
-
GenericCallback,
|
|
6
|
-
} from '../../models';
|
|
7
|
-
|
|
8
|
-
// #region Types
|
|
9
|
-
|
|
10
|
-
type AsyncItem = {
|
|
11
|
-
parameters: unknown[];
|
|
12
|
-
promise: Promise<unknown>;
|
|
13
|
-
reject: (reason?: unknown) => void;
|
|
14
|
-
resolve: (value?: unknown) => void;
|
|
15
|
-
running: boolean;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type TimerType = 'debounce' | 'throttle' | 'wait';
|
|
19
|
-
|
|
20
|
-
// #endregion
|
|
21
|
-
|
|
22
|
-
// #region Functions
|
|
23
|
-
|
|
24
|
-
export function getAsyncTimer<Callback extends GenericAsyncCallback | GenericCallback>(
|
|
25
|
-
type: TimerType,
|
|
26
|
-
callback: Callback,
|
|
27
|
-
time?: number,
|
|
28
|
-
): AsyncCancelableCallback<Callback> {
|
|
29
|
-
async function run(item: AsyncItem): Promise<void> {
|
|
30
|
-
const now = performance.now();
|
|
31
|
-
|
|
32
|
-
start ??= now;
|
|
33
|
-
|
|
34
|
-
if (interval === 0 || now - start >= interval - OFFSET) {
|
|
35
|
-
start = throttle ? now : undefined;
|
|
36
|
-
|
|
37
|
-
item.running = true;
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
let result = callback(...item.parameters);
|
|
41
|
-
|
|
42
|
-
if (result instanceof Promise) {
|
|
43
|
-
result = await result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
item.resolve(result);
|
|
47
|
-
} catch (error) {
|
|
48
|
-
item.reject(error);
|
|
49
|
-
} finally {
|
|
50
|
-
item.running = false;
|
|
51
|
-
}
|
|
52
|
-
} else {
|
|
53
|
-
id = startTimer(() => run(item));
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const interval = getInterval(time);
|
|
58
|
-
const throttle = type === TIMER_THROTTLE;
|
|
59
|
-
|
|
60
|
-
let id: number;
|
|
61
|
-
let last: AsyncItem;
|
|
62
|
-
let start: number | undefined;
|
|
63
|
-
|
|
64
|
-
const timer = (...parameters: Parameters<Callback>): Promise<unknown> => {
|
|
65
|
-
timer.cancel();
|
|
66
|
-
|
|
67
|
-
const next: AsyncItem = {
|
|
68
|
-
parameters,
|
|
69
|
-
running: false,
|
|
70
|
-
} as never;
|
|
71
|
-
|
|
72
|
-
next.promise = new Promise<unknown>((resolve, reject) => {
|
|
73
|
-
next.reject = reject;
|
|
74
|
-
next.resolve = resolve;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
last = next;
|
|
78
|
-
|
|
79
|
-
if (throttle) {
|
|
80
|
-
void run(next);
|
|
81
|
-
} else {
|
|
82
|
-
id = startTimer(() => {
|
|
83
|
-
void run(next);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return next.promise;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
timer.cancel = (): void => {
|
|
91
|
-
clearTimer(id);
|
|
92
|
-
|
|
93
|
-
if (last != null && !last.running) {
|
|
94
|
-
last.reject();
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
return timer as AsyncCancelableCallback<Callback>;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function getInterval(value: unknown): number {
|
|
102
|
-
return typeof value === 'number' && value > 0 ? value : 0;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function getTimer<Callback extends GenericCallback>(
|
|
106
|
-
type: TimerType,
|
|
107
|
-
callback: Callback,
|
|
108
|
-
time?: number,
|
|
109
|
-
): CancelableCallback<Callback> {
|
|
110
|
-
function run(): void {
|
|
111
|
-
const now = performance.now();
|
|
112
|
-
|
|
113
|
-
start ??= now;
|
|
114
|
-
|
|
115
|
-
if (interval === 0 || now - start >= interval - OFFSET) {
|
|
116
|
-
start = throttle ? now : undefined;
|
|
117
|
-
|
|
118
|
-
callback(...args);
|
|
119
|
-
} else {
|
|
120
|
-
id = startTimer(run);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const interval = getInterval(time);
|
|
125
|
-
const throttle = type === TIMER_THROTTLE;
|
|
126
|
-
|
|
127
|
-
let args: Parameters<Callback>;
|
|
128
|
-
let id: number;
|
|
129
|
-
let start: number | undefined;
|
|
130
|
-
|
|
131
|
-
const timer = (...parameters: Parameters<Callback>): void => {
|
|
132
|
-
timer.cancel();
|
|
133
|
-
|
|
134
|
-
args = parameters;
|
|
135
|
-
|
|
136
|
-
if (throttle) {
|
|
137
|
-
run();
|
|
138
|
-
} else {
|
|
139
|
-
id = startTimer(run);
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
timer.cancel = (): void => {
|
|
144
|
-
clearTimer(id);
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
return timer as CancelableCallback<Callback>;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// #endregion
|
|
151
|
-
|
|
152
|
-
// #region Variables
|
|
153
|
-
|
|
154
|
-
const OFFSET = 5;
|
|
155
|
-
|
|
156
|
-
export const TIMER_DEBOUNCE: TimerType = 'debounce';
|
|
157
|
-
|
|
158
|
-
export const TIMER_THROTTLE: TimerType = 'throttle';
|
|
159
|
-
|
|
160
|
-
export const TIMER_WAIT: TimerType = 'wait';
|
|
161
|
-
|
|
162
|
-
// istanbul ignore next
|
|
163
|
-
const clearTimer = typeof cancelAnimationFrame === 'function' ? cancelAnimationFrame : clearTimeout;
|
|
164
|
-
|
|
165
|
-
// istanbul ignore next
|
|
166
|
-
const startTimer = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : setTimeout;
|
|
167
|
-
|
|
168
|
-
// #endregion
|