@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.
Files changed (130) hide show
  1. package/dist/index.d.mts +25 -23
  2. package/dist/index.mjs +30 -31
  3. package/dist/queue.d.mts +25 -23
  4. package/dist/queue.mjs +27 -28
  5. package/package.json +3 -4
  6. package/src/array/difference.ts +0 -73
  7. package/src/array/exists.ts +0 -93
  8. package/src/array/filter.ts +0 -182
  9. package/src/array/find.ts +0 -183
  10. package/src/array/first.ts +0 -196
  11. package/src/array/flatten.ts +0 -20
  12. package/src/array/from.ts +0 -129
  13. package/src/array/get.ts +0 -131
  14. package/src/array/group-by.ts +0 -369
  15. package/src/array/index.ts +0 -26
  16. package/src/array/insert.ts +0 -52
  17. package/src/array/intersection.ts +0 -72
  18. package/src/array/last.ts +0 -194
  19. package/src/array/match.ts +0 -436
  20. package/src/array/move.ts +0 -315
  21. package/src/array/partition.ts +0 -91
  22. package/src/array/push.ts +0 -23
  23. package/src/array/reverse.ts +0 -33
  24. package/src/array/select.ts +0 -260
  25. package/src/array/single.ts +0 -93
  26. package/src/array/slice.ts +0 -335
  27. package/src/array/sort.ts +0 -607
  28. package/src/array/splice.ts +0 -106
  29. package/src/array/swap.ts +0 -322
  30. package/src/array/to-map.ts +0 -371
  31. package/src/array/to-record.ts +0 -351
  32. package/src/array/to-set.ts +0 -80
  33. package/src/array/toggle.ts +0 -79
  34. package/src/array/union.ts +0 -72
  35. package/src/array/unique.ts +0 -67
  36. package/src/array/update.ts +0 -75
  37. package/src/beacon.ts +0 -378
  38. package/src/color/constants.ts +0 -137
  39. package/src/color/index.ts +0 -83
  40. package/src/color/instance.ts +0 -162
  41. package/src/color/misc/alpha.ts +0 -56
  42. package/src/color/misc/format.ts +0 -29
  43. package/src/color/misc/get.ts +0 -210
  44. package/src/color/misc/is.ts +0 -221
  45. package/src/color/misc/state.ts +0 -243
  46. package/src/color/models.ts +0 -389
  47. package/src/color/space/hex.ts +0 -184
  48. package/src/color/space/hsl.ts +0 -174
  49. package/src/color/space/hwb.ts +0 -153
  50. package/src/color/space/rgb.ts +0 -240
  51. package/src/function/assert.ts +0 -174
  52. package/src/function/index.ts +0 -2
  53. package/src/function/limit.ts +0 -83
  54. package/src/function/memoize.ts +0 -202
  55. package/src/function/once.ts +0 -200
  56. package/src/function/retry.ts +0 -175
  57. package/src/function/work.ts +0 -1007
  58. package/src/herald.ts +0 -216
  59. package/src/index.ts +0 -68
  60. package/src/internal/array/callbacks.ts +0 -46
  61. package/src/internal/array/chunk.ts +0 -51
  62. package/src/internal/array/compact.ts +0 -56
  63. package/src/internal/array/find.ts +0 -200
  64. package/src/internal/array/group.ts +0 -42
  65. package/src/internal/array/index-of.ts +0 -182
  66. package/src/internal/array/insert.ts +0 -80
  67. package/src/internal/array/overlap.ts +0 -35
  68. package/src/internal/array/sets.ts +0 -74
  69. package/src/internal/array/shuffle.ts +0 -33
  70. package/src/internal/array/update.ts +0 -52
  71. package/src/internal/function/misc.ts +0 -8
  72. package/src/internal/function/timer.ts +0 -168
  73. package/src/internal/is.ts +0 -244
  74. package/src/internal/math/aggregate.ts +0 -164
  75. package/src/internal/math/misc.ts +0 -28
  76. package/src/internal/number.ts +0 -129
  77. package/src/internal/random.ts +0 -49
  78. package/src/internal/result.ts +0 -92
  79. package/src/internal/sized.ts +0 -25
  80. package/src/internal/string.ts +0 -128
  81. package/src/internal/subscription.ts +0 -264
  82. package/src/internal/value/compare.ts +0 -170
  83. package/src/internal/value/equal.ts +0 -465
  84. package/src/internal/value/get.ts +0 -57
  85. package/src/internal/value/handlers.ts +0 -95
  86. package/src/internal/value/has.ts +0 -118
  87. package/src/internal/value/misc.ts +0 -125
  88. package/src/internal/value/partial.ts +0 -46
  89. package/src/internal/value/set.ts +0 -132
  90. package/src/is.ts +0 -182
  91. package/src/logger.ts +0 -230
  92. package/src/math.ts +0 -281
  93. package/src/models.ts +0 -286
  94. package/src/number.ts +0 -1
  95. package/src/promise/delay.ts +0 -62
  96. package/src/promise/helpers.ts +0 -96
  97. package/src/promise/index.ts +0 -300
  98. package/src/promise/misc.ts +0 -96
  99. package/src/promise/models.ts +0 -195
  100. package/src/promise/timed.ts +0 -91
  101. package/src/query.ts +0 -147
  102. package/src/queue.ts +0 -814
  103. package/src/random.ts +0 -125
  104. package/src/result/index.ts +0 -105
  105. package/src/result/match.ts +0 -124
  106. package/src/result/misc.ts +0 -129
  107. package/src/result/models.ts +0 -83
  108. package/src/result/work/flow.ts +0 -488
  109. package/src/result/work/pipe.ts +0 -496
  110. package/src/sized/map.ts +0 -110
  111. package/src/sized/set.ts +0 -107
  112. package/src/string/case.ts +0 -236
  113. package/src/string/fuzzy.ts +0 -444
  114. package/src/string/index.ts +0 -199
  115. package/src/string/match.ts +0 -80
  116. package/src/string/normalize.ts +0 -205
  117. package/src/string/template.ts +0 -161
  118. package/src/value/clone.ts +0 -319
  119. package/src/value/collection.ts +0 -63
  120. package/src/value/diff.ts +0 -232
  121. package/src/value/freeze.ts +0 -322
  122. package/src/value/handle.ts +0 -3
  123. package/src/value/index.ts +0 -5
  124. package/src/value/merge.ts +0 -278
  125. package/src/value/omit.ts +0 -20
  126. package/src/value/pick.ts +0 -20
  127. package/src/value/shake.ts +0 -45
  128. package/src/value/smush.ts +0 -89
  129. package/src/value/transform.ts +0 -153
  130. 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