@oscarpalmer/atoms 0.193.0 → 0.194.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.
Files changed (204) hide show
  1. package/dist/array/difference.mjs +2 -2
  2. package/dist/array/intersection.mjs +2 -2
  3. package/dist/array/match.mjs +13 -13
  4. package/dist/array/slice.mjs +5 -5
  5. package/dist/array/sort.d.mts +5 -1
  6. package/dist/array/sort.mjs +4 -2
  7. package/dist/array/union.mjs +2 -2
  8. package/dist/beacon.d.mts +8 -1
  9. package/dist/beacon.mjs +52 -60
  10. package/dist/color/constants.d.mts +1 -1
  11. package/dist/color/constants.mjs +3 -5
  12. package/dist/color/instance.mjs +34 -41
  13. package/dist/color/misc/format.d.mts +3 -2
  14. package/dist/color/misc/format.mjs +4 -1
  15. package/dist/color/misc/is.d.mts +9 -1
  16. package/dist/color/misc/is.mjs +11 -1
  17. package/dist/color/misc/state.d.mts +6 -6
  18. package/dist/color/misc/state.mjs +11 -14
  19. package/dist/color/models.d.mts +23 -68
  20. package/dist/function/assert.mjs +2 -2
  21. package/dist/function/limit.d.mts +0 -6
  22. package/dist/function/limit.mjs +2 -2
  23. package/dist/function/memoize.mjs +19 -32
  24. package/dist/function/once.d.mts +0 -3
  25. package/dist/function/once.mjs +16 -13
  26. package/dist/function/retry.d.mts +1 -24
  27. package/dist/function/retry.mjs +1 -1
  28. package/dist/function/work.mjs +13 -11
  29. package/dist/herald.d.mts +30 -31
  30. package/dist/herald.mjs +74 -56
  31. package/dist/index.d.mts +100 -185
  32. package/dist/index.mjs +680 -702
  33. package/dist/internal/abort.d.mts +10 -0
  34. package/dist/internal/abort.mjs +14 -0
  35. package/dist/internal/array/chunk.mjs +2 -2
  36. package/dist/internal/array/find.mjs +2 -2
  37. package/dist/internal/array/sets.d.mts +4 -4
  38. package/dist/internal/array/sets.mjs +7 -7
  39. package/dist/internal/function/timer.mjs +19 -15
  40. package/dist/internal/number.mjs +7 -7
  41. package/dist/internal/result.mjs +3 -3
  42. package/dist/internal/sized.mjs +4 -4
  43. package/dist/internal/string.mjs +4 -4
  44. package/dist/internal/subscription.d.mts +24 -8
  45. package/dist/internal/subscription.mjs +49 -36
  46. package/dist/internal/value/equal.mjs +6 -6
  47. package/dist/internal/value/misc.mjs +14 -14
  48. package/dist/internal/value/set.mjs +2 -2
  49. package/dist/logger.mjs +15 -39
  50. package/dist/promise/constants.d.mts +17 -0
  51. package/dist/promise/constants.mjs +14 -0
  52. package/dist/promise/delay.mjs +7 -10
  53. package/dist/promise/helpers.mjs +1 -1
  54. package/dist/promise/index.mjs +14 -17
  55. package/dist/promise/misc.d.mts +2 -1
  56. package/dist/promise/misc.mjs +8 -7
  57. package/dist/promise/models.d.mts +4 -16
  58. package/dist/promise/models.mjs +4 -13
  59. package/dist/promise/timed.mjs +9 -10
  60. package/dist/query.mjs +15 -15
  61. package/dist/queue.mjs +103 -116
  62. package/dist/random.mjs +7 -7
  63. package/dist/result/index.d.mts +0 -3
  64. package/dist/result/index.mjs +1 -1
  65. package/dist/result/match.d.mts +0 -3
  66. package/dist/result/match.mjs +4 -4
  67. package/dist/result/misc.mjs +2 -2
  68. package/dist/result/work/flow.d.mts +0 -3
  69. package/dist/result/work/flow.mjs +5 -5
  70. package/dist/result/work/pipe.d.mts +0 -3
  71. package/dist/result/work/pipe.mjs +3 -3
  72. package/dist/string/case.mjs +20 -20
  73. package/dist/string/fuzzy.mjs +23 -18
  74. package/dist/string/index.mjs +7 -7
  75. package/dist/string/normalize.mjs +14 -14
  76. package/dist/string/template.mjs +2 -2
  77. package/dist/value/clone.mjs +10 -10
  78. package/dist/value/smush.mjs +2 -2
  79. package/package.json +13 -5
  80. package/src/array/difference.ts +0 -73
  81. package/src/array/exists.ts +0 -93
  82. package/src/array/filter.ts +0 -182
  83. package/src/array/find.ts +0 -183
  84. package/src/array/first.ts +0 -196
  85. package/src/array/flatten.ts +0 -20
  86. package/src/array/from.ts +0 -129
  87. package/src/array/get.ts +0 -131
  88. package/src/array/group-by.ts +0 -369
  89. package/src/array/index.ts +0 -26
  90. package/src/array/insert.ts +0 -52
  91. package/src/array/intersection.ts +0 -72
  92. package/src/array/last.ts +0 -194
  93. package/src/array/match.ts +0 -436
  94. package/src/array/move.ts +0 -315
  95. package/src/array/partition.ts +0 -91
  96. package/src/array/push.ts +0 -23
  97. package/src/array/reverse.ts +0 -33
  98. package/src/array/select.ts +0 -260
  99. package/src/array/single.ts +0 -93
  100. package/src/array/slice.ts +0 -335
  101. package/src/array/sort.ts +0 -607
  102. package/src/array/splice.ts +0 -106
  103. package/src/array/swap.ts +0 -322
  104. package/src/array/to-map.ts +0 -371
  105. package/src/array/to-record.ts +0 -351
  106. package/src/array/to-set.ts +0 -80
  107. package/src/array/toggle.ts +0 -79
  108. package/src/array/union.ts +0 -72
  109. package/src/array/unique.ts +0 -67
  110. package/src/array/update.ts +0 -75
  111. package/src/beacon.ts +0 -378
  112. package/src/color/constants.ts +0 -137
  113. package/src/color/index.ts +0 -83
  114. package/src/color/instance.ts +0 -162
  115. package/src/color/misc/alpha.ts +0 -56
  116. package/src/color/misc/format.ts +0 -29
  117. package/src/color/misc/get.ts +0 -210
  118. package/src/color/misc/is.ts +0 -221
  119. package/src/color/misc/state.ts +0 -243
  120. package/src/color/models.ts +0 -389
  121. package/src/color/space/hex.ts +0 -184
  122. package/src/color/space/hsl.ts +0 -174
  123. package/src/color/space/hwb.ts +0 -153
  124. package/src/color/space/rgb.ts +0 -240
  125. package/src/function/assert.ts +0 -174
  126. package/src/function/index.ts +0 -2
  127. package/src/function/limit.ts +0 -83
  128. package/src/function/memoize.ts +0 -202
  129. package/src/function/once.ts +0 -200
  130. package/src/function/retry.ts +0 -175
  131. package/src/function/work.ts +0 -1007
  132. package/src/herald.ts +0 -216
  133. package/src/index.ts +0 -68
  134. package/src/internal/array/callbacks.ts +0 -46
  135. package/src/internal/array/chunk.ts +0 -51
  136. package/src/internal/array/compact.ts +0 -56
  137. package/src/internal/array/find.ts +0 -200
  138. package/src/internal/array/group.ts +0 -42
  139. package/src/internal/array/index-of.ts +0 -182
  140. package/src/internal/array/insert.ts +0 -80
  141. package/src/internal/array/overlap.ts +0 -35
  142. package/src/internal/array/sets.ts +0 -74
  143. package/src/internal/array/shuffle.ts +0 -33
  144. package/src/internal/array/update.ts +0 -52
  145. package/src/internal/function/misc.ts +0 -8
  146. package/src/internal/function/timer.ts +0 -168
  147. package/src/internal/is.ts +0 -244
  148. package/src/internal/math/aggregate.ts +0 -164
  149. package/src/internal/math/misc.ts +0 -28
  150. package/src/internal/number.ts +0 -129
  151. package/src/internal/random.ts +0 -49
  152. package/src/internal/result.ts +0 -92
  153. package/src/internal/sized.ts +0 -25
  154. package/src/internal/string.ts +0 -128
  155. package/src/internal/subscription.ts +0 -264
  156. package/src/internal/value/compare.ts +0 -170
  157. package/src/internal/value/equal.ts +0 -465
  158. package/src/internal/value/get.ts +0 -57
  159. package/src/internal/value/handlers.ts +0 -95
  160. package/src/internal/value/has.ts +0 -118
  161. package/src/internal/value/misc.ts +0 -125
  162. package/src/internal/value/partial.ts +0 -46
  163. package/src/internal/value/set.ts +0 -132
  164. package/src/is.ts +0 -182
  165. package/src/logger.ts +0 -230
  166. package/src/math.ts +0 -281
  167. package/src/models.ts +0 -286
  168. package/src/number.ts +0 -1
  169. package/src/promise/delay.ts +0 -62
  170. package/src/promise/helpers.ts +0 -96
  171. package/src/promise/index.ts +0 -300
  172. package/src/promise/misc.ts +0 -96
  173. package/src/promise/models.ts +0 -195
  174. package/src/promise/timed.ts +0 -91
  175. package/src/query.ts +0 -147
  176. package/src/queue.ts +0 -814
  177. package/src/random.ts +0 -125
  178. package/src/result/index.ts +0 -105
  179. package/src/result/match.ts +0 -124
  180. package/src/result/misc.ts +0 -129
  181. package/src/result/models.ts +0 -83
  182. package/src/result/work/flow.ts +0 -488
  183. package/src/result/work/pipe.ts +0 -496
  184. package/src/sized/map.ts +0 -110
  185. package/src/sized/set.ts +0 -107
  186. package/src/string/case.ts +0 -236
  187. package/src/string/fuzzy.ts +0 -444
  188. package/src/string/index.ts +0 -199
  189. package/src/string/match.ts +0 -80
  190. package/src/string/normalize.ts +0 -205
  191. package/src/string/template.ts +0 -161
  192. package/src/value/clone.ts +0 -319
  193. package/src/value/collection.ts +0 -63
  194. package/src/value/diff.ts +0 -232
  195. package/src/value/freeze.ts +0 -322
  196. package/src/value/handle.ts +0 -3
  197. package/src/value/index.ts +0 -5
  198. package/src/value/merge.ts +0 -278
  199. package/src/value/omit.ts +0 -20
  200. package/src/value/pick.ts +0 -20
  201. package/src/value/shake.ts +0 -45
  202. package/src/value/smush.ts +0 -89
  203. package/src/value/transform.ts +0 -153
  204. 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,8 +0,0 @@
1
- // #region Functions
2
-
3
- /**
4
- * A function that does nothing, which can be useful, I guess…
5
- */
6
- export function noop(): void {}
7
-
8
- // #endregion
@@ -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