@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
package/src/herald.ts DELETED
@@ -1,216 +0,0 @@
1
- import {noop} from './internal/function/misc';
2
- import type {GenericCallback, Unsubscriber} from './models';
3
-
4
- // #region Types
5
-
6
- export type Events<Map extends Record<string, GenericCallback>> = {
7
- /**
8
- * Subscribe to an event with a callback
9
- *
10
- * @param event Event name
11
- * @param callback Callback function
12
- * @returns Unsubscriber function
13
- */
14
- subscribe<Event extends keyof Map>(event: Event, callback: Map[Event]): Unsubscriber;
15
-
16
- /**
17
- * Unsubscribe from an event with a callback _(or all callbacks, if no callback is provided)_
18
- *
19
- * @param event Event name
20
- * @param callback Callback function
21
- * @returns Unsubscriber function
22
- */
23
- unsubscribe<Event extends keyof Map>(event: Event, callback?: Map[Event]): void;
24
- };
25
-
26
- export type Herald<Map extends Record<string, GenericCallback>> = {
27
- readonly events: Events<Map>;
28
-
29
- /**
30
- * Remove all event subscribers
31
- */
32
- clear(): void;
33
-
34
- /**
35
- * Emit an event with parameters
36
- *
37
- * @param event Event name
38
- * @param parameters Event parameters
39
- */
40
- emit<Event extends keyof Map>(event: Event, ...parameters: Parameters<Map[Event]>): void;
41
-
42
- /**
43
- * Subscribe to an event with a callback
44
- *
45
- * @param event Event name
46
- * @param callback Callback function
47
- * @returns Unsubscriber function
48
- */
49
- subscribe<Event extends keyof Map>(event: Event, callback: Map[Event]): Unsubscriber;
50
-
51
- /**
52
- * Unsubscribe from an event with a callback _(or all callbacks, if no callback is provided)_
53
- *
54
- * @param event Event name
55
- * @param callback Callback function
56
- */
57
- unsubscribe<Event extends keyof Map>(event: Event, callback?: Map[Event]): void;
58
- };
59
-
60
- // #endregion
61
-
62
- // #region Functions
63
-
64
- function getEvents<Map extends Record<string, GenericCallback>>(herald: Herald<Map>): Events<Map> {
65
- const events = {
66
- subscribe: (event: string, callback: GenericCallback) =>
67
- herald.subscribe(event, callback as Map[keyof Map]),
68
- unsubscribe: (event: string, callback?: GenericCallback) =>
69
- herald.unsubscribe(event, callback as Map[keyof Map]),
70
- };
71
-
72
- Object.defineProperty(events, KEY_HERALD, {
73
- enumerable: false,
74
- value: NAME_EVENTS,
75
- });
76
-
77
- return Object.freeze(events) as Events<Map>;
78
- }
79
-
80
- /**
81
- * Create a _Herald_ for announcing named events
82
- *
83
- * @param names Event names
84
- * @returns _Herald_ instance
85
- */
86
- export function herald<Events extends Record<string, GenericCallback>>(
87
- names: (keyof Events)[],
88
- ): Herald<Events> {
89
- if (
90
- !Array.isArray(names) ||
91
- names.length === 0 ||
92
- !names.every(name => typeof name === 'string')
93
- ) {
94
- throw new Error(MESSAGE);
95
- }
96
-
97
- const set = new Set<keyof Events>(names);
98
- const subscribers = new Map<keyof Events, Set<Events[keyof Events]>>();
99
-
100
- const instance = {
101
- clear() {
102
- subscribers.clear();
103
- },
104
- emit<Event extends keyof Events>(event: Event, ...parameters: Parameters<Events[Event]>) {
105
- const callbacks = subscribers.get(event);
106
-
107
- if (callbacks == null) {
108
- return;
109
- }
110
-
111
- for (const callback of callbacks) {
112
- callback(...parameters);
113
- }
114
- },
115
- subscribe<Event extends keyof Events>(event: Event, callback: Events[Event]): Unsubscriber {
116
- if (!set.has(event) || typeof callback !== 'function') {
117
- return noop;
118
- }
119
-
120
- let eventSubscribers = subscribers.get(event);
121
-
122
- if (eventSubscribers == null) {
123
- eventSubscribers = new Set();
124
-
125
- subscribers.set(event, eventSubscribers);
126
- }
127
-
128
- eventSubscribers.add(callback);
129
-
130
- return () => {
131
- eventSubscribers?.delete(callback);
132
- };
133
- },
134
- unsubscribe<Event extends keyof Events>(event: Event, callback?: Events[Event]): void {
135
- if (!set.has(event) || (callback != null ? typeof callback !== 'function' : false)) {
136
- return;
137
- }
138
-
139
- const eventSubscribers = subscribers.get(event);
140
-
141
- if (eventSubscribers == null) {
142
- return;
143
- }
144
-
145
- if (callback == null) {
146
- eventSubscribers.clear();
147
- } else {
148
- eventSubscribers.delete(callback);
149
- }
150
-
151
- if (callback == null || eventSubscribers.size === 0) {
152
- subscribers.delete(event);
153
- }
154
- },
155
- };
156
-
157
- Object.defineProperties(instance, {
158
- [KEY_HERALD]: {
159
- enumerable: false,
160
- value: NAME_HERALD,
161
- },
162
- events: {
163
- enumerable: true,
164
- value: getEvents<Events>(instance as Herald<Events>),
165
- },
166
- });
167
-
168
- return Object.freeze(instance) as Herald<Events>;
169
- }
170
-
171
- /**
172
- * Is the value events for a herald?
173
- *
174
- * @param value Value to check
175
- * @returns `true` if the value is events for a herald, otherwise `false`
176
- */
177
- export function isEvents<
178
- Map extends Record<string, GenericCallback> = Record<string, GenericCallback>,
179
- >(value: unknown): value is Events<Map> {
180
- return isHeraldInstance(NAME_EVENTS, value);
181
- }
182
-
183
- /**
184
- * Is the value a herald?
185
- *
186
- * @param value Value to check
187
- * @returns `true` if the value is a herald, otherwise `false`
188
- */
189
- export function isHerald<
190
- Events extends Record<string, GenericCallback> = Record<string, GenericCallback>,
191
- >(value: unknown): value is Herald<Events> {
192
- return isHeraldInstance(NAME_HERALD, value);
193
- }
194
-
195
- function isHeraldInstance<Instance>(name: string, value: unknown): value is Instance {
196
- return (
197
- typeof value === 'object' &&
198
- value != null &&
199
- KEY_HERALD in value &&
200
- (value as Record<string, unknown>)[KEY_HERALD] === name
201
- );
202
- }
203
-
204
- // #endregion
205
-
206
- // #region Variables
207
-
208
- const KEY_HERALD = '$herald';
209
-
210
- const MESSAGE = 'Herald requires an array of event names.';
211
-
212
- const NAME_EVENTS = 'events';
213
-
214
- const NAME_HERALD = 'herald';
215
-
216
- // #endregion
package/src/index.ts DELETED
@@ -1,68 +0,0 @@
1
- export * from './array/filter';
2
- export * from './array/first';
3
- export * from './array/group-by';
4
- export * from './array/index';
5
- export * from './array/last';
6
- export * from './array/move';
7
- export * from './array/sort';
8
- export * from './array/swap';
9
- export * from './array/to-map';
10
- export * from './array/to-record';
11
- export * from './array/unique';
12
-
13
- export * from './function/assert';
14
- export * from './function/index';
15
- export * from './function/limit';
16
- export * from './function/once';
17
- export * from './function/retry';
18
- export * from './function/work';
19
-
20
- export * from './herald';
21
-
22
- export * from './internal/function/misc';
23
- export * from './internal/string';
24
- export {isSubscription, type Subscription} from './internal/subscription';
25
- export * from './internal/value/compare';
26
- export * from './internal/value/equal';
27
- export * from './internal/value/get';
28
- export * from './internal/value/has';
29
- export * from './internal/value/set';
30
-
31
- export * from './string/case';
32
- export * from './string/fuzzy';
33
- export * from './string/index';
34
- export * from './string/match';
35
- export * from './string/normalize';
36
- export * from './string/template';
37
-
38
- export * from './value/clone';
39
- export * from './value/collection';
40
- export * from './value/diff';
41
- export * from './value/freeze';
42
- export * from './value/index';
43
- export * from './value/merge';
44
- export * from './value/transform';
45
-
46
- export * from './beacon';
47
- export * from './color';
48
- export * from './is';
49
- export * from './logger';
50
- export * from './math';
51
- export * from './models';
52
- export * from './number';
53
- export * from './promise/delay';
54
- export * from './promise/index';
55
- export * from './promise/misc';
56
- export * from './promise/models';
57
- export * from './promise/timed';
58
- export * from './query';
59
- export * from './queue';
60
- export * from './random';
61
- export * from './result/index';
62
- export * from './result/match';
63
- export * from './result/misc';
64
- export * from './result/models';
65
- export * from './result/work/flow';
66
- export * from './result/work/pipe';
67
- export * from './sized/map';
68
- export * from './sized/set';
@@ -1,46 +0,0 @@
1
- import type {GenericCallback, PlainObject} from '../../models';
2
-
3
- // #region Types
4
-
5
- export type Callbacks = {
6
- bool?: GenericCallback;
7
- keyed?: GenericCallback;
8
- value?: GenericCallback;
9
- };
10
-
11
- // #endregion
12
-
13
- // #region Functions
14
-
15
- export function getArrayCallback(value: unknown): GenericCallback | undefined {
16
- switch (typeof value) {
17
- case 'function':
18
- return value as GenericCallback;
19
-
20
- case 'number':
21
- case 'string':
22
- return typeof value === 'string' && value.includes('.')
23
- ? undefined
24
- : (obj: PlainObject) => obj[value];
25
-
26
- default:
27
- break;
28
- }
29
- }
30
-
31
- export function getArrayCallbacks(
32
- bool?: unknown,
33
- key?: unknown,
34
- value?: unknown,
35
- ): Callbacks | undefined {
36
- if (typeof bool === 'function') {
37
- return {bool: bool as GenericCallback};
38
- }
39
-
40
- return {
41
- keyed: getArrayCallback(key),
42
- value: getArrayCallback(value),
43
- };
44
- }
45
-
46
- // #endregion
@@ -1,51 +0,0 @@
1
- // #region Functions
2
-
3
- /**
4
- * Chunk an array into smaller arrays
5
- *
6
- * @param array Array to chunk
7
- * @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
8
- * @returns Array of arrays
9
- *
10
- * @example
11
- * ```typescript
12
- * chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
13
- * ```
14
- */
15
- export function chunk<Item>(array: Item[], size?: number): Item[][] {
16
- if (!Array.isArray(array)) {
17
- return [];
18
- }
19
-
20
- if (array.length === 0) {
21
- return [];
22
- }
23
-
24
- const {length} = array;
25
-
26
- const actualSize = typeof size === 'number' && size > 0 && size <= MAX_SIZE ? size : MAX_SIZE;
27
-
28
- if (length <= actualSize) {
29
- return [array];
30
- }
31
-
32
- const chunks: Item[][] = [];
33
-
34
- let index = 0;
35
-
36
- while (index < length) {
37
- chunks.push(array.slice(index, index + actualSize));
38
-
39
- index += actualSize;
40
- }
41
-
42
- return chunks;
43
- }
44
-
45
- // #endregion
46
-
47
- // #region Variables
48
-
49
- const MAX_SIZE = 5_000;
50
-
51
- // #endregion
@@ -1,56 +0,0 @@
1
- // #region Functions
2
-
3
- /**
4
- * Compact an array _(removing all false-y values)_
5
- *
6
- * @param array Array to compact
7
- * @param strict True to remove all false-y values
8
- * @returns Compacted array
9
- *
10
- * @example
11
- * ```typescript
12
- * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
13
- * ```
14
- */
15
- export function compact<Item>(
16
- array: Item[],
17
- strict: true,
18
- ): Exclude<Item, 0 | '' | false | null | undefined>[];
19
-
20
- /**
21
- * Compact an array _(removing all `null` and `undefined` values)_
22
- *
23
- * @param array Array to compact
24
- * @returns Compacted array
25
- *
26
- * @example
27
- * ```typescript
28
- * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
29
- * ```
30
- */
31
- export function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
32
-
33
- export function compact<Item>(array: Item[], strict?: unknown): Item[] {
34
- if (!Array.isArray(array)) {
35
- return [];
36
- }
37
-
38
- if (strict === true) {
39
- return array.filter(Boolean);
40
- }
41
-
42
- const {length} = array;
43
- const compacted: Item[] = [];
44
-
45
- for (let index = 0; index < length; index += 1) {
46
- const item = array[index];
47
-
48
- if (item != null) {
49
- compacted.push(item);
50
- }
51
- }
52
-
53
- return compacted;
54
- }
55
-
56
- // #endregion
@@ -1,200 +0,0 @@
1
- import {getArrayCallback, getArrayCallbacks} from './callbacks';
2
-
3
- // #region Types
4
-
5
- type FindValueType = 'index' | 'item';
6
-
7
- type FindValuesResult = {
8
- matched: unknown[];
9
- notMatched: unknown[];
10
- };
11
-
12
- type FindValuesType = 'all' | 'unique';
13
-
14
- type Parameters = {
15
- bool?: unknown;
16
- key?: unknown;
17
- value?: unknown;
18
- };
19
-
20
- // #endregion
21
-
22
- // #region Functions
23
-
24
- export function findValue(
25
- type: Exclude<FindValueType, 'item'>,
26
- array: unknown[],
27
- parameters: unknown[],
28
- reversed: boolean,
29
- ): number;
30
-
31
- export function findValue(
32
- type: Exclude<FindValueType, 'index'>,
33
- array: unknown[],
34
- parameters: unknown[],
35
- reversed: boolean,
36
- ): unknown;
37
-
38
- export function findValue(
39
- type: FindValueType,
40
- array: unknown[],
41
- parameters: unknown[],
42
- reversed: boolean,
43
- ): unknown {
44
- const findIndex = type === FIND_VALUE_INDEX;
45
-
46
- if (!Array.isArray(array) || array.length === 0) {
47
- return findIndex ? -1 : undefined;
48
- }
49
-
50
- const {bool, key, value} = getFindParameters(parameters);
51
-
52
- const callbacks = getArrayCallbacks(bool, key);
53
-
54
- if (callbacks?.bool == null && callbacks?.keyed == null) {
55
- if (findIndex) {
56
- return reversed ? array.lastIndexOf(value) : array.indexOf(value);
57
- }
58
-
59
- return reversed
60
- ? array.findLast(item => Object.is(item, value))
61
- : array.find(item => Object.is(item, value));
62
- }
63
-
64
- if (callbacks.bool != null) {
65
- const index = reversed ? array.findLastIndex(callbacks.bool) : array.findIndex(callbacks.bool);
66
-
67
- return findIndex ? index : array[index];
68
- }
69
-
70
- return findValueInArray(array, callbacks.keyed, value, findIndex, reversed);
71
- }
72
-
73
- function findValueInArray(
74
- array: unknown[],
75
- callback: ((item: unknown, index: number, array: unknown[]) => boolean) | undefined,
76
- value: unknown,
77
- findIndex: boolean,
78
- reversed: boolean,
79
- ): unknown {
80
- const {length} = array;
81
-
82
- for (let index = 0; index < length; index += 1) {
83
- const item = reversed ? array.at(-(index + 1)) : array[index];
84
-
85
- if (Object.is(callback?.(item, index, array), value)) {
86
- return findIndex ? index : item;
87
- }
88
- }
89
-
90
- return findIndex ? -1 : undefined;
91
- }
92
-
93
- export function findAbsoluteValueOrDefault(
94
- array: unknown[],
95
- parameters: unknown[],
96
- defaultValue: unknown,
97
- useDefaultValue: boolean,
98
- reversed: boolean,
99
- ): unknown {
100
- if (parameters.length === 0) {
101
- if (Array.isArray(array) && array.length > 0) {
102
- return reversed ? array.at(-1) : array[0];
103
- }
104
-
105
- return useDefaultValue ? defaultValue : undefined;
106
- }
107
-
108
- const index = findValue(FIND_VALUE_INDEX, array, parameters, reversed) as number;
109
-
110
- return index > -1 ? array[index] : useDefaultValue ? defaultValue : undefined;
111
- }
112
-
113
- export function findValues(
114
- type: FindValuesType,
115
- array: unknown[],
116
- parameters: unknown[],
117
- mapper?: unknown,
118
- ): FindValuesResult {
119
- const result: FindValuesResult = {
120
- matched: [],
121
- notMatched: [],
122
- };
123
-
124
- if (!Array.isArray(array) || array.length === 0) {
125
- return result;
126
- }
127
-
128
- const {length} = array;
129
- const {bool, key, value} = getFindParameters(parameters);
130
- const callbacks = getArrayCallbacks(bool, key);
131
-
132
- if (type === FIND_VALUES_UNIQUE && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
133
- result.matched = [...new Set(array)];
134
-
135
- return result;
136
- }
137
-
138
- const mapCallback = getArrayCallback(mapper);
139
-
140
- if (callbacks?.bool != null || (type === FIND_VALUES_ALL && key == null)) {
141
- const callback = callbacks?.bool ?? (item => Object.is(item, value));
142
-
143
- for (let index = 0; index < length; index += 1) {
144
- const item = array[index];
145
-
146
- if (callback(item, index, array)) {
147
- result.matched.push(mapCallback?.(item, index, array) ?? item);
148
- } else {
149
- result.notMatched.push(item);
150
- }
151
- }
152
-
153
- return result;
154
- }
155
-
156
- const keys = new Set();
157
-
158
- for (let index = 0; index < length; index += 1) {
159
- const item = array[index];
160
- const keyed = callbacks?.keyed?.(item, index, array) ?? item;
161
-
162
- if (
163
- (type === FIND_VALUES_ALL && Object.is(keyed, value)) ||
164
- (type === FIND_VALUES_UNIQUE && !keys.has(keyed))
165
- ) {
166
- keys.add(keyed);
167
- result.matched.push(mapCallback?.(item, index, array) ?? item);
168
- } else {
169
- result.notMatched.push(item);
170
- }
171
- }
172
-
173
- return result;
174
- }
175
-
176
- export function getFindParameters(original: unknown[]): Parameters {
177
- const {length} = original;
178
-
179
- return {
180
- bool: length === 1 && typeof original[0] === 'function' ? original[0] : undefined,
181
- key: length === 2 ? original[0] : undefined,
182
- value: length === 1 && typeof original[0] !== 'function' ? original[0] : original[1],
183
- };
184
- }
185
-
186
- // #endregion
187
-
188
- // #region Variables
189
-
190
- export const FIND_VALUE_INDEX = 'index';
191
-
192
- export const FIND_VALUE_ITEM = 'item';
193
-
194
- export const FIND_VALUES_ALL: FindValuesType = 'all';
195
-
196
- export const FIND_VALUES_UNIQUE: FindValuesType = 'unique';
197
-
198
- const UNIQUE_THRESHOLD = 100;
199
-
200
- // #endregion
@@ -1,42 +0,0 @@
1
- import type {Key} from '../../models';
2
- import {getArrayCallbacks} from './callbacks';
3
-
4
- // #region Functions
5
-
6
- export function groupValues(
7
- array: unknown[],
8
- key: unknown,
9
- value: unknown,
10
- arrays: boolean,
11
- ): Record<Key, unknown> {
12
- if (!Array.isArray(array) || array.length === 0) {
13
- return {};
14
- }
15
-
16
- const {length} = array;
17
- const callbacks = getArrayCallbacks(undefined, key, value);
18
- const record: Record<Key, unknown> = {};
19
-
20
- for (let index = 0; index < length; index += 1) {
21
- const item = array[index];
22
-
23
- const keyed = callbacks?.keyed?.(item, index, array) ?? index;
24
- const valued = callbacks?.value?.(item, index, array) ?? item;
25
-
26
- if (arrays) {
27
- const existing = record[keyed];
28
-
29
- if (existing == null) {
30
- record[keyed] = [valued];
31
- } else {
32
- (existing as unknown[]).push(valued);
33
- }
34
- } else {
35
- record[keyed] = valued;
36
- }
37
- }
38
-
39
- return record;
40
- }
41
-
42
- // #endregion