@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/array/sort.ts DELETED
@@ -1,607 +0,0 @@
1
- import {isPlainObject} from '../internal/is';
2
- import {compare} from '../internal/value/compare';
3
- import type {PlainObject, Primitive} from '../models';
4
-
5
- // #region Types
6
-
7
- /**
8
- * Sorting information for arrays _(using a comparison callback)_
9
- */
10
- export type ArrayComparisonSorter<Item> = {
11
- /**
12
- * Callback to use when comparing items and values
13
- */
14
- comparison: ComparisonSorter<Item>;
15
- /**
16
- * Direction to sort by
17
- */
18
- direction?: SortDirection;
19
- };
20
-
21
- /**
22
- * Sorting information for arrays _(using a key)_
23
- */
24
- export type ArrayKeySorter<Item extends PlainObject, ItemKey extends keyof Item> = {
25
- /**
26
- * Comparator to use when comparing items and values
27
- */
28
- compare?: CompareCallback<Item, Item[ItemKey]>;
29
- /**
30
- * Direction to sort by
31
- */
32
- direction?: SortDirection;
33
- /**
34
- * Key to sort by
35
- */
36
- key: ItemKey;
37
- };
38
-
39
- /**
40
- * Sorters based on keys in an object
41
- */
42
- type ArrayKeySorters<Item extends PlainObject> = {
43
- [ItemKey in keyof Item]: ArrayKeySorter<Item, ItemKey>;
44
- }[keyof Item];
45
-
46
- /**
47
- * Sorter to use for sorting
48
- */
49
- type ArraySorter<Item> = Item extends PlainObject
50
- ?
51
- | keyof Item
52
- | ArrayComparisonSorter<Item>
53
- | ArrayKeySorters<Item>
54
- | ArrayValueSorter<Item>
55
- | ComparisonSorter<Item>
56
- : ArrayComparisonSorter<Item> | ArrayValueSorter<Item> | ComparisonSorter<Item>;
57
-
58
- /**
59
- * Sorting information for arrays _(using a value callback and built-in comparison)_
60
- */
61
- export type ArrayValueSorter<Item> = {
62
- /**
63
- * Direction to sort by
64
- */
65
- direction?: SortDirection;
66
- /**
67
- * Value to sort by
68
- */
69
- value: (item: Item) => unknown;
70
- };
71
-
72
- /**
73
- * Comparator to use when comparing items and values
74
- */
75
- type CompareCallback<Item, Value = CompareCallbackValue<Item>> = (
76
- first: Item,
77
- firstValue: Value,
78
- second: Item,
79
- secondValue: Value,
80
- ) => number;
81
-
82
- type CompareCallbackValue<Item> = Item extends Primitive ? Item : unknown;
83
-
84
- /**
85
- * Callback to use when comparing items and values
86
- */
87
- type ComparisonSorter<Item> = (first: Item, second: Item) => number;
88
-
89
- type InternalSorter = {
90
- compare?: InternalSorterCompare;
91
- get: boolean;
92
- identifier: string;
93
- modifier: number;
94
- value?: Function;
95
- };
96
-
97
- type InternalSorterCompare = {
98
- complex?: Function;
99
- simple?: Function;
100
- };
101
-
102
- /**
103
- * Direction to sort by
104
- */
105
- export type SortDirection = 'ascending' | 'descending';
106
-
107
- /**
108
- * Sorter for an array with predefined sorters
109
- *
110
- * Can be used to sort an array, get the predicted index for an item, and check if an array is sorted
111
- */
112
- export type Sorter<Item> = {
113
- /**
114
- * Sort an array of items
115
- *
116
- * @param array Array to sort
117
- * @returns Sorted array
118
- */
119
- (array: Item[]): Item[];
120
-
121
- /**
122
- * Get the index for an item _(to be inserted into an array of items)_
123
- *
124
- * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
125
- *
126
- * @param array Array to get the index from
127
- * @param item Item to get the index for
128
- * @returns Index for item
129
- */
130
- index(array: Item[], item: Item): number;
131
-
132
- /**
133
- * Is the array sorted?
134
- *
135
- * @param array Array to check
136
- * @returns `true` if sorted, otherwise `false`
137
- */
138
- is(array: Item[]): boolean;
139
- };
140
-
141
- // #endregion
142
-
143
- // #region Functions
144
-
145
- function getComparisonSorter(callback: Function, modifier: number): InternalSorter {
146
- return {
147
- modifier,
148
- compare: {
149
- simple: callback,
150
- },
151
- get: false,
152
- identifier: callback.toString(),
153
- };
154
- }
155
-
156
- function getComparisonValue(
157
- first: unknown,
158
- second: unknown,
159
- sorters: InternalSorter[],
160
- length: number,
161
- ): number {
162
- for (let index = 0; index < length; index += 1) {
163
- const sorter = sorters[index];
164
-
165
- const values = [
166
- sorter.get ? sorter.value!(first as PlainObject) : first,
167
- sorter.get ? sorter.value!(second as PlainObject) : second,
168
- ];
169
-
170
- const comparison =
171
- (sorter.compare?.complex?.(first, values[0], second, values[1]) ??
172
- sorter.compare?.simple?.(values[0], values[1]) ??
173
- compare(values[0], values[1])) * sorter.modifier;
174
-
175
- if (comparison !== 0) {
176
- return comparison;
177
- }
178
- }
179
-
180
- return 0;
181
- }
182
-
183
- function getIndex(array: unknown[], item: unknown, sorters: InternalSorter[]): number {
184
- if (!Array.isArray(array)) {
185
- return -1;
186
- }
187
-
188
- const {length} = array;
189
-
190
- if (length === 0) {
191
- return 0;
192
- }
193
-
194
- const sortersLength = sorters.length;
195
-
196
- if (getComparisonValue(item, array[0], sorters, sortersLength) < 0) {
197
- return 0;
198
- }
199
-
200
- if (getComparisonValue(item, array[length - 1], sorters, sortersLength) >= 0) {
201
- return length;
202
- }
203
-
204
- let low = 0;
205
- let high = length - 1;
206
-
207
- while (low <= high) {
208
- const mid = Math.floor((low + high) / 2);
209
-
210
- if (getComparisonValue(item, array[mid], sorters, sortersLength) < 0) {
211
- high = mid - 1;
212
- } else {
213
- low = mid + 1;
214
- }
215
- }
216
-
217
- return low;
218
- }
219
-
220
- function getModifier(first: unknown, second: unknown): number {
221
- const direction =
222
- first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING;
223
-
224
- return modifiers[direction];
225
- }
226
-
227
- function getObjectSorter(obj: PlainObject, modifier: number): InternalSorter | undefined {
228
- let sorter: InternalSorter | undefined;
229
-
230
- if (typeof obj.comparison === 'function') {
231
- sorter = getComparisonSorter(obj.comparison, modifier);
232
- } else if (typeof obj.key === 'string') {
233
- sorter = getValueSorter(obj.key, modifier);
234
-
235
- if (typeof obj.compare === 'function') {
236
- sorter.compare = {
237
- complex: obj.compare,
238
- };
239
- }
240
- } else if (typeof obj.value === 'function') {
241
- sorter = getValueSorter(obj.value, modifier);
242
- }
243
-
244
- if (sorter != null && typeof obj.direction === 'string') {
245
- sorter.modifier = modifiers[obj.direction as SortDirection] ?? modifier;
246
- }
247
-
248
- return sorter;
249
- }
250
-
251
- /**
252
- * Get the index for an item _(to be inserted into an array of items)_ based on sorters _(and an optional default direction)_
253
- *
254
- * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
255
- *
256
- * _Available as `getSortedIndex` and `sort.getIndex`_
257
- *
258
- * @param array Array to get the index from
259
- * @param item Item to get the index for
260
- * @param sorters Sorters to use to determine sorting
261
- * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
262
- * @returns Index for item
263
- */
264
- export function getSortedIndex<Item>(
265
- array: Item[],
266
- item: Item,
267
- sorters: Array<ArraySorter<Item>>,
268
- descending?: boolean,
269
- ): number;
270
-
271
- /**
272
- * Get the index for an item _(to be inserted into an array of items)_ based on a sorter _(and an optional default direction)_
273
- *
274
- * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
275
- *
276
- * _Available as `getSortedIndex` and `sort.getIndex`_
277
- *
278
- * @param array Array to get the index from
279
- * @param item Item to get the index for
280
- * @param sorter Sorter to use to determine sorting
281
- * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
282
- * @returns Index for item
283
- */
284
- export function getSortedIndex<Item>(
285
- array: Item[],
286
- item: Item,
287
- sorter: ArraySorter<Item>,
288
- descending?: boolean,
289
- ): number;
290
-
291
- /**
292
- * Get the index for an item _(to be inserted into an array of items)_ based on an optional default direction_
293
- *
294
- * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
295
- *
296
- * _Available as `getSortedIndex` and `sort.getIndex`_
297
- *
298
- * @param array Array to get the index from
299
- * @param item Item to get the index for
300
- * @param descending Sorted in descending order? _(defaults to `false`)_
301
- * @returns Index for item
302
- */
303
- export function getSortedIndex<Item>(array: Item[], item: Item, descending?: boolean): number;
304
-
305
- export function getSortedIndex(
306
- array: unknown[],
307
- item: unknown,
308
- first?: unknown,
309
- second?: unknown,
310
- ): number {
311
- return getIndex(array, item, getSorters(first, getModifier(first, second)));
312
- }
313
-
314
- function getSorter(value: unknown, modifier: number): InternalSorter | undefined {
315
- switch (true) {
316
- case typeof value === 'function':
317
- return getComparisonSorter(value, modifier);
318
-
319
- case typeof value === 'string':
320
- return getValueSorter(value, modifier);
321
-
322
- case isPlainObject(value):
323
- return getObjectSorter(value, modifier);
324
-
325
- default:
326
- break;
327
- }
328
- }
329
-
330
- function getSorters(value: unknown, modifier: number): InternalSorter[] {
331
- const array = Array.isArray(value) ? value : [value];
332
- const {length} = array;
333
-
334
- const sorters: InternalSorter[] = [];
335
-
336
- for (let index = 0; index < length; index += 1) {
337
- const item = array[index];
338
-
339
- const sorter = getSorter(item, modifier);
340
-
341
- if (sorter != null) {
342
- sorters.push(sorter);
343
- }
344
- }
345
-
346
- if (sorters.length === 0) {
347
- return [
348
- {
349
- modifier,
350
- get: false,
351
- identifier: 'default',
352
- },
353
- ];
354
- }
355
-
356
- return sorters.filter(
357
- (value, index, array) =>
358
- array.findIndex(next => next.identifier === value.identifier) === index,
359
- );
360
- }
361
-
362
- function getValueSorter(value: string | Function, modifier: number): InternalSorter {
363
- const isFunction = typeof value === 'function';
364
-
365
- return {
366
- modifier,
367
- get: true,
368
- identifier: isFunction ? value.toString() : value,
369
- value: isFunction ? value : (item: unknown) => (item as PlainObject)[value],
370
- };
371
- }
372
-
373
- /**
374
- * Initialize a sort handler with sorters _(and an optional default direction)_
375
- *
376
- * _Available as `initializeSorter` and `sort.initialize`_
377
- *
378
- * @param sorters Sorters to use for sorting
379
- * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
380
- * @returns Sort handler
381
- */
382
- export function initializeSorter<Item>(
383
- sorters: Array<ArraySorter<Item>>,
384
- descending?: boolean,
385
- ): Sorter<Item>;
386
-
387
- /**
388
- * Initialize a sort handler with a sorter _(and an optional default direction)_
389
- *
390
- * _Available as `initializeSorter` and `sort.initialize`_
391
- *
392
- * @param sorter Sorter to use for sorting
393
- * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
394
- * @returns Sort handler
395
- */
396
- export function initializeSorter<Item>(
397
- sorter: ArraySorter<Item>,
398
- descending?: boolean,
399
- ): Sorter<Item>;
400
-
401
- /**
402
- * Initialize a sort handler _(with an optional default direction)_
403
- *
404
- * _Available as `initializeSorter` and `sort.initialize`_
405
- *
406
- * @param descending Sort in descending order? _(defaults to `false`)_
407
- * @returns Sort handler
408
- */
409
- export function initializeSorter<Item>(descending?: boolean): Sorter<Item>;
410
-
411
- export function initializeSorter(first?: unknown, second?: unknown): Sorter<unknown> {
412
- const sorters = getSorters(first, getModifier(first, second));
413
-
414
- const sorter = (array: unknown[]) => sortArray(array, sorters);
415
-
416
- sorter.index = (array: unknown[], item: unknown) => getIndex(array, item, sorters);
417
- sorter.is = (array: unknown[]) => isSortedArray(array, sorters);
418
-
419
- return sorter as unknown as Sorter<unknown>;
420
- }
421
-
422
- /**
423
- * Is the array sorted according to the sorters _(and the optional default direction)_?
424
- *
425
- * @param array Array to check
426
- * @param sorters Sorters to determine sorting
427
- * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
428
- * @returns `true` if sorted, otherwise `false`
429
- */
430
- export function isSorted<Item>(
431
- array: Item[],
432
- sorters: Array<ArraySorter<Item>>,
433
- descending?: boolean,
434
- ): boolean;
435
-
436
- /**
437
- * Is the array sorted according to the sorter _(and the optional default direction)_?
438
- *
439
- * @param array Array to check
440
- * @param sorter Sorter to determine sorting
441
- * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
442
- * @returns `true` if sorted, otherwise `false`
443
- */
444
- export function isSorted<Item>(
445
- array: Item[],
446
- sorter: ArraySorter<Item>,
447
- descending?: boolean,
448
- ): boolean;
449
-
450
- /**
451
- * Is the array sorted?
452
- *
453
- * _Available as `isSorted` and `sort.is`_
454
- *
455
- * @param array Array to check
456
- * @param descending Sorted in descending order? _(defaults to `false`)_
457
- * @returns `true` if sorted, otherwise `false`
458
- */
459
- export function isSorted<Item>(array: Item[], descending?: boolean): boolean;
460
-
461
- export function isSorted(array: unknown[], first?: unknown, second?: unknown): boolean {
462
- return isSortedArray(array, getSorters(first, getModifier(first, second)));
463
- }
464
-
465
- function isSortedArray(array: unknown[], sorters: InternalSorter[]): boolean {
466
- if (!Array.isArray(array)) {
467
- return false;
468
- }
469
-
470
- const {length} = array;
471
-
472
- if (length < 2) {
473
- return true;
474
- }
475
-
476
- const sortersLength = sorters.length;
477
-
478
- let offset = 0;
479
-
480
- if (length >= SORT_THRESHOLD) {
481
- offset = Math.round(length / SORT_PEEK_PERCENTAGE);
482
- offset = offset > SORT_THRESHOLD ? SORT_THRESHOLD : offset;
483
-
484
- for (let index = 0; index < offset; index += 1) {
485
- const [firstItem, firstOffset] = [array[index], array[index + 1]];
486
- const [secondItem, secondOffset] = [array[length - index - 2], array[length - index - 1]];
487
-
488
- const [firstComparison, secondComparison] = [
489
- getComparisonValue(firstItem, firstOffset, sorters, sortersLength),
490
- getComparisonValue(secondItem, secondOffset, sorters, sortersLength),
491
- ];
492
-
493
- if (firstComparison > 0 || secondComparison > 0) {
494
- return false;
495
- }
496
- }
497
- }
498
-
499
- const end = length - offset - 1;
500
-
501
- for (let index = offset; index < end; index += 1) {
502
- const first = array[index];
503
- const second = array[index + 1];
504
-
505
- const comparison = getComparisonValue(first, second, sorters, sortersLength);
506
-
507
- if (comparison > 0) {
508
- return false;
509
- }
510
- }
511
-
512
- return true;
513
- }
514
-
515
- /**
516
- * Sort an array of items using a comparison callback
517
- *
518
- * @param array Array to sort
519
- * @param comparator Comparator to use for sorting
520
- * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
521
- * @returns Sorted array
522
- *
523
- * @example
524
- * ```typescript
525
- * sort(
526
- * [{id: 3}, {id: 1}, {id: 2}],
527
- * (first, second) => first.id - second.id,
528
- * ); // => [{id: 1}, {id: 2}, {id: 3}]
529
- * ```
530
- */
531
- export function sort<Item>(
532
- array: Item[],
533
- comparator: (first: Item, second: Item) => number,
534
- descending?: boolean,
535
- ): Item[];
536
-
537
- /**
538
- * Sort an array of items, using multiple sorters to sort by specific values
539
- *
540
- * @param array Array to sort
541
- * @param sorters Sorters to use for sorting
542
- * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
543
- * @returns Sorted array
544
- */
545
- export function sort<Item>(
546
- array: Item[],
547
- sorters: Array<ArraySorter<Item>>,
548
- descending?: boolean,
549
- ): Item[];
550
-
551
- /**
552
- * Sort an array of items, using a single sorter to sort by a specific value
553
- *
554
- * @param array Array to sort
555
- * @param sorter Sorter to use for sorting
556
- * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
557
- * @returns Sorted array
558
- */
559
- export function sort<Item>(array: Item[], sorter: ArraySorter<Item>, descending?: boolean): Item[];
560
-
561
- /**
562
- * Sort an array of items
563
- *
564
- * @param array Array to sort
565
- * @param descending Sort in descending order? _(defaults to `false`)_
566
- * @returns Sorted array
567
- */
568
- export function sort<Item>(array: Item[], descending?: boolean): Item[];
569
-
570
- export function sort(array: unknown[], first?: unknown, second?: unknown): unknown[] {
571
- return sortArray(array, getSorters(first, getModifier(first, second)));
572
- }
573
-
574
- function sortArray(array: unknown[], sorters: InternalSorter[]): unknown[] {
575
- if (!Array.isArray(array)) {
576
- return [];
577
- }
578
-
579
- const {length} = sorters;
580
-
581
- return array.length > 1
582
- ? array.sort((first, second) => getComparisonValue(first, second, sorters, length))
583
- : array;
584
- }
585
-
586
- sort.getIndex = getSortedIndex;
587
- sort.initialize = initializeSorter;
588
- sort.is = isSorted;
589
-
590
- // #endregion
591
-
592
- // #region Variables
593
-
594
- const SORT_PEEK_PERCENTAGE = 10;
595
-
596
- const SORT_THRESHOLD = 100;
597
-
598
- export const SORT_DIRECTION_ASCENDING: SortDirection = 'ascending';
599
-
600
- export const SORT_DIRECTION_DESCENDING: SortDirection = 'descending';
601
-
602
- const modifiers: Record<SortDirection, number> = {
603
- [SORT_DIRECTION_ASCENDING]: 1,
604
- [SORT_DIRECTION_DESCENDING]: -1,
605
- };
606
-
607
- // #endregion
@@ -1,106 +0,0 @@
1
- import {INSERT_TYPE_SPLICE, insertValues} from '../internal/array/insert';
2
-
3
- // #region Functions
4
-
5
- /**
6
- * Adds items into an array at a specific index and removes a specific amount of items
7
- *
8
- * _(Uses chunking to avoid call stack size being exceeded)_
9
- *
10
- * @param array Original array
11
- * @param start Index to start splicing from
12
- * @param amount Number of items to remove
13
- * @param added Added items
14
- * @returns Removed items
15
- *
16
- * @example
17
- * ```typescript
18
- * splice(
19
- * [1, 2, 3, 4, 5],
20
- * 2,
21
- * 2,
22
- * [6, 7]
23
- * ); // => [3, 4], array becomes [1, 2, 6, 7, 5]
24
- * ```
25
- */
26
- export function splice<Item>(array: Item[], start: number, amount: number, added: Item[]): Item[];
27
-
28
- /**
29
- * Adds items into an array at a specific index
30
- *
31
- * _(Uses chunking to avoid call stack size being exceeded)_
32
- *
33
- * @param array Original array
34
- * @param start Index to start splicing from
35
- * @param added Added items
36
- * @returns Removed items
37
- *
38
- * @example
39
- * ```typescript
40
- * splice(
41
- * [1, 2, 3, 4, 5],
42
- * 2,
43
- * [6, 7]
44
- * ); // => [], array becomes [1, 2, 6, 7, 3, 4, 5]
45
- * ```
46
- */
47
- export function splice<Item>(array: Item[], start: number, added: Item[]): Item[];
48
-
49
- /**
50
- * Removes and returns _(up to)_ a specific amount of items from an array, starting from a specific index
51
- *
52
- * _(Uses chunking to avoid call stack size being exceeded)_
53
- *
54
- * @param array Original array
55
- * @param start Index to start splicing from
56
- * @param amount Number of items to remove
57
- * @returns Removed items
58
- *
59
- * @example
60
- * ```typescript
61
- * splice(
62
- * [1, 2, 3, 4, 5],
63
- * 2,
64
- * 2,
65
- * ); // => [3, 4], array becomes [1, 2, 5]
66
- * ```
67
- */
68
- export function splice<Item>(array: Item[], start: number, amount: number): Item[];
69
-
70
- /**
71
- * Removes and returns all items from an array starting from a specific index
72
- *
73
- * _(Uses chunking to avoid call stack size being exceeded)_
74
- *
75
- * @param array Original array
76
- * @param start Index to start splicing from
77
- * @returns Removed items
78
- *
79
- * @example
80
- * ```typescript
81
- * splice(
82
- * [1, 2, 3, 4, 5],
83
- * 2,
84
- * ); // => [3, 4, 5], array becomes [1, 2]
85
- * ```
86
- */
87
- export function splice<Item>(array: Item[], start: number): Item[];
88
-
89
- export function splice(
90
- array: unknown,
91
- start: unknown,
92
- deleteCountOrItems?: unknown,
93
- items?: unknown,
94
- ): unknown[] {
95
- const deleteCountIsNumber = typeof deleteCountOrItems === 'number';
96
-
97
- return insertValues(
98
- INSERT_TYPE_SPLICE,
99
- array,
100
- deleteCountIsNumber ? items : deleteCountOrItems,
101
- start,
102
- deleteCountIsNumber ? deleteCountOrItems : 0,
103
- ) as unknown[];
104
- }
105
-
106
- // #endregion