@oscarpalmer/atoms 0.192.0 → 0.193.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +25 -23
- package/dist/index.mjs +30 -31
- package/dist/queue.d.mts +25 -23
- package/dist/queue.mjs +27 -28
- package/package.json +3 -4
- package/src/array/difference.ts +0 -73
- package/src/array/exists.ts +0 -93
- package/src/array/filter.ts +0 -182
- package/src/array/find.ts +0 -183
- package/src/array/first.ts +0 -196
- package/src/array/flatten.ts +0 -20
- package/src/array/from.ts +0 -129
- package/src/array/get.ts +0 -131
- package/src/array/group-by.ts +0 -369
- package/src/array/index.ts +0 -26
- package/src/array/insert.ts +0 -52
- package/src/array/intersection.ts +0 -72
- package/src/array/last.ts +0 -194
- package/src/array/match.ts +0 -436
- package/src/array/move.ts +0 -315
- package/src/array/partition.ts +0 -91
- package/src/array/push.ts +0 -23
- package/src/array/reverse.ts +0 -33
- package/src/array/select.ts +0 -260
- package/src/array/single.ts +0 -93
- package/src/array/slice.ts +0 -335
- package/src/array/sort.ts +0 -607
- package/src/array/splice.ts +0 -106
- package/src/array/swap.ts +0 -322
- package/src/array/to-map.ts +0 -371
- package/src/array/to-record.ts +0 -351
- package/src/array/to-set.ts +0 -80
- package/src/array/toggle.ts +0 -79
- package/src/array/union.ts +0 -72
- package/src/array/unique.ts +0 -67
- package/src/array/update.ts +0 -75
- package/src/beacon.ts +0 -378
- package/src/color/constants.ts +0 -137
- package/src/color/index.ts +0 -83
- package/src/color/instance.ts +0 -162
- package/src/color/misc/alpha.ts +0 -56
- package/src/color/misc/format.ts +0 -29
- package/src/color/misc/get.ts +0 -210
- package/src/color/misc/is.ts +0 -221
- package/src/color/misc/state.ts +0 -243
- package/src/color/models.ts +0 -389
- package/src/color/space/hex.ts +0 -184
- package/src/color/space/hsl.ts +0 -174
- package/src/color/space/hwb.ts +0 -153
- package/src/color/space/rgb.ts +0 -240
- package/src/function/assert.ts +0 -174
- package/src/function/index.ts +0 -2
- package/src/function/limit.ts +0 -83
- package/src/function/memoize.ts +0 -202
- package/src/function/once.ts +0 -200
- package/src/function/retry.ts +0 -175
- package/src/function/work.ts +0 -1007
- package/src/herald.ts +0 -216
- package/src/index.ts +0 -68
- package/src/internal/array/callbacks.ts +0 -46
- package/src/internal/array/chunk.ts +0 -51
- package/src/internal/array/compact.ts +0 -56
- package/src/internal/array/find.ts +0 -200
- package/src/internal/array/group.ts +0 -42
- package/src/internal/array/index-of.ts +0 -182
- package/src/internal/array/insert.ts +0 -80
- package/src/internal/array/overlap.ts +0 -35
- package/src/internal/array/sets.ts +0 -74
- package/src/internal/array/shuffle.ts +0 -33
- package/src/internal/array/update.ts +0 -52
- package/src/internal/function/misc.ts +0 -8
- package/src/internal/function/timer.ts +0 -168
- package/src/internal/is.ts +0 -244
- package/src/internal/math/aggregate.ts +0 -164
- package/src/internal/math/misc.ts +0 -28
- package/src/internal/number.ts +0 -129
- package/src/internal/random.ts +0 -49
- package/src/internal/result.ts +0 -92
- package/src/internal/sized.ts +0 -25
- package/src/internal/string.ts +0 -128
- package/src/internal/subscription.ts +0 -264
- package/src/internal/value/compare.ts +0 -170
- package/src/internal/value/equal.ts +0 -465
- package/src/internal/value/get.ts +0 -57
- package/src/internal/value/handlers.ts +0 -95
- package/src/internal/value/has.ts +0 -118
- package/src/internal/value/misc.ts +0 -125
- package/src/internal/value/partial.ts +0 -46
- package/src/internal/value/set.ts +0 -132
- package/src/is.ts +0 -182
- package/src/logger.ts +0 -230
- package/src/math.ts +0 -281
- package/src/models.ts +0 -286
- package/src/number.ts +0 -1
- package/src/promise/delay.ts +0 -62
- package/src/promise/helpers.ts +0 -96
- package/src/promise/index.ts +0 -300
- package/src/promise/misc.ts +0 -96
- package/src/promise/models.ts +0 -195
- package/src/promise/timed.ts +0 -91
- package/src/query.ts +0 -147
- package/src/queue.ts +0 -814
- package/src/random.ts +0 -125
- package/src/result/index.ts +0 -105
- package/src/result/match.ts +0 -124
- package/src/result/misc.ts +0 -129
- package/src/result/models.ts +0 -83
- package/src/result/work/flow.ts +0 -488
- package/src/result/work/pipe.ts +0 -496
- package/src/sized/map.ts +0 -110
- package/src/sized/set.ts +0 -107
- package/src/string/case.ts +0 -236
- package/src/string/fuzzy.ts +0 -444
- package/src/string/index.ts +0 -199
- package/src/string/match.ts +0 -80
- package/src/string/normalize.ts +0 -205
- package/src/string/template.ts +0 -161
- package/src/value/clone.ts +0 -319
- package/src/value/collection.ts +0 -63
- package/src/value/diff.ts +0 -232
- package/src/value/freeze.ts +0 -322
- package/src/value/handle.ts +0 -3
- package/src/value/index.ts +0 -5
- package/src/value/merge.ts +0 -278
- package/src/value/omit.ts +0 -20
- package/src/value/pick.ts +0 -20
- package/src/value/shake.ts +0 -45
- package/src/value/smush.ts +0 -89
- package/src/value/transform.ts +0 -153
- package/src/value/unsmush.ts +0 -84
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
|
package/src/array/splice.ts
DELETED
|
@@ -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
|