@oscarpalmer/atoms 0.179.0 → 0.180.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.
- package/dist/array/filter.d.mts +36 -28
- package/dist/array/filter.mjs +5 -5
- package/dist/array/first.d.mts +13 -5
- package/dist/array/first.mjs +1 -1
- package/dist/array/group-by.d.mts +13 -1
- package/dist/array/group-by.mjs +1 -1
- package/dist/array/last.d.mts +9 -1
- package/dist/array/last.mjs +1 -1
- package/dist/array/move.d.mts +9 -1
- package/dist/array/move.mjs +3 -1
- package/dist/array/reverse.d.mts +5 -0
- package/dist/array/reverse.mjs +5 -0
- package/dist/array/select.d.mts +2 -2
- package/dist/array/sort.d.mts +23 -9
- package/dist/array/sort.mjs +22 -22
- package/dist/array/swap.d.mts +2 -0
- package/dist/array/swap.mjs +2 -0
- package/dist/array/to-map.d.mts +13 -1
- package/dist/array/to-map.mjs +1 -1
- package/dist/array/to-record.d.mts +13 -1
- package/dist/array/to-record.mjs +1 -1
- package/dist/function/assert.d.mts +9 -1
- package/dist/function/assert.mjs +9 -1
- package/dist/function/limit.d.mts +5 -1
- package/dist/function/limit.mjs +5 -1
- package/dist/function/once.d.mts +3 -1
- package/dist/function/once.mjs +3 -1
- package/dist/function/retry.d.mts +4 -0
- package/dist/function/retry.mjs +2 -0
- package/dist/function/work.d.mts +49 -1
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +415 -242
- package/dist/index.mjs +224 -167
- package/dist/internal/array/index-of.mjs +1 -1
- package/dist/internal/function/timer.mjs +3 -1
- package/dist/internal/value/compare.d.mts +13 -9
- package/dist/internal/value/compare.mjs +13 -9
- package/dist/internal/value/equal.d.mts +29 -15
- package/dist/internal/value/equal.mjs +41 -35
- package/dist/internal/value/handlers.d.mts +4 -4
- package/dist/internal/value/handlers.mjs +19 -11
- package/dist/internal/value/has.d.mts +9 -8
- package/dist/internal/value/has.mjs +3 -3
- package/dist/internal/value/misc.d.mts +4 -8
- package/dist/internal/value/misc.mjs +6 -17
- package/dist/promise/index.d.mts +11 -1
- package/dist/promise/index.mjs +1 -1
- package/dist/result/index.d.mts +9 -1
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +5 -1
- package/dist/result/match.mjs +1 -1
- package/dist/result/misc.d.mts +3 -3
- package/dist/result/work/flow.d.mts +49 -1
- package/dist/result/work/flow.mjs +1 -1
- package/dist/result/work/pipe.d.mts +67 -155
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/fuzzy.d.mts +11 -1
- package/dist/string/fuzzy.mjs +22 -6
- package/dist/string/template.d.mts +3 -1
- package/dist/string/template.mjs +3 -1
- package/dist/value/clone.d.mts +13 -9
- package/dist/value/clone.mjs +21 -17
- package/dist/value/merge.d.mts +9 -7
- package/dist/value/merge.mjs +7 -5
- package/package.json +3 -3
- package/plugin/helpers.js +2 -2
- package/src/array/filter.ts +44 -36
- package/src/array/first.ts +18 -9
- package/src/array/group-by.ts +22 -10
- package/src/array/last.ts +17 -5
- package/src/array/move.ts +18 -5
- package/src/array/reverse.ts +5 -0
- package/src/array/select.ts +2 -2
- package/src/array/sort.ts +110 -86
- package/src/array/swap.ts +2 -0
- package/src/array/to-map.ts +22 -10
- package/src/array/to-record.ts +22 -10
- package/src/function/assert.ts +12 -4
- package/src/function/limit.ts +6 -2
- package/src/function/once.ts +3 -1
- package/src/function/retry.ts +8 -2
- package/src/function/work.ts +92 -26
- package/src/internal/array/index-of.ts +1 -1
- package/src/internal/function/timer.ts +4 -2
- package/src/internal/string.ts +2 -0
- package/src/internal/value/compare.ts +14 -11
- package/src/internal/value/equal.ts +79 -67
- package/src/internal/value/handlers.ts +19 -11
- package/src/internal/value/has.ts +16 -16
- package/src/internal/value/misc.ts +10 -8
- package/src/promise/index.ts +14 -4
- package/src/result/index.ts +15 -5
- package/src/result/match.ts +7 -3
- package/src/result/misc.ts +3 -3
- package/src/result/work/flow.ts +68 -13
- package/src/result/work/pipe.ts +97 -392
- package/src/string/fuzzy.ts +34 -8
- package/src/string/template.ts +3 -1
- package/src/value/clone.ts +25 -22
- package/src/value/merge.ts +14 -12
package/src/array/last.ts
CHANGED
|
@@ -55,26 +55,30 @@ last.default = lastOrDefault;
|
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* Get the last item matching the given value
|
|
58
|
+
*
|
|
59
|
+
* Available as `lastOrDefault` and `last.default`
|
|
58
60
|
* @param array Array to search in
|
|
59
61
|
* @param defaultValue Default value to return if no match is found
|
|
60
62
|
* @param callback Callback to get an item's value for matching
|
|
61
63
|
* @param value Value to match against
|
|
62
64
|
* @returns Last item that matches the value, or the default value if no match is found
|
|
63
65
|
*/
|
|
64
|
-
function lastOrDefault<
|
|
66
|
+
export function lastOrDefault<
|
|
65
67
|
Item,
|
|
66
68
|
Callback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
67
69
|
>(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType<Callback>): Item;
|
|
68
70
|
|
|
69
71
|
/**
|
|
70
72
|
* Get the last item matching the given value by key
|
|
73
|
+
*
|
|
74
|
+
* Available as `lastOrDefault` and `last.default`
|
|
71
75
|
* @param array Array to search in
|
|
72
76
|
* @param defaultValue Default value to return if no match is found
|
|
73
77
|
* @param key Key to get an item's value for matching
|
|
74
78
|
* @param value Value to match against
|
|
75
79
|
* @returns Last item that matches the value, or the default value if no match is found
|
|
76
80
|
*/
|
|
77
|
-
function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
81
|
+
export function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
78
82
|
array: Item[],
|
|
79
83
|
defaultValue: Item,
|
|
80
84
|
key: ItemKey,
|
|
@@ -83,12 +87,14 @@ function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
83
87
|
|
|
84
88
|
/**
|
|
85
89
|
* Get the last item matching the filter
|
|
90
|
+
*
|
|
91
|
+
* Available as `lastOrDefault` and `last.default`
|
|
86
92
|
* @param array Array to search in
|
|
87
93
|
* @param defaultValue Default value to return if no match is found
|
|
88
94
|
* @param filter Filter callback to match items
|
|
89
95
|
* @returns Last item that matches the filter, or the default value if no match is found
|
|
90
96
|
*/
|
|
91
|
-
function lastOrDefault<Item>(
|
|
97
|
+
export function lastOrDefault<Item>(
|
|
92
98
|
array: Item[],
|
|
93
99
|
defaultValue: Item,
|
|
94
100
|
filter: (item: Item, index: number, array: Item[]) => boolean,
|
|
@@ -96,13 +102,19 @@ function lastOrDefault<Item>(
|
|
|
96
102
|
|
|
97
103
|
/**
|
|
98
104
|
* Get the last item from an array
|
|
105
|
+
*
|
|
106
|
+
* Available as `lastOrDefault` and `last.default`
|
|
99
107
|
* @param array Array to get from
|
|
100
108
|
* @param defaultValue Default value to return if the array is empty
|
|
101
109
|
* @return Last item from the array, or the default value if the array is empty
|
|
102
110
|
*/
|
|
103
|
-
function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
|
|
111
|
+
export function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
|
|
104
112
|
|
|
105
|
-
function lastOrDefault(
|
|
113
|
+
export function lastOrDefault(
|
|
114
|
+
array: unknown[],
|
|
115
|
+
defaultValue: unknown,
|
|
116
|
+
...parameters: unknown[]
|
|
117
|
+
): unknown {
|
|
106
118
|
return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true);
|
|
107
119
|
}
|
|
108
120
|
|
package/src/array/move.ts
CHANGED
|
@@ -112,12 +112,14 @@ move.toIndex = moveToIndex;
|
|
|
112
112
|
* Move an item from one index to another within an array
|
|
113
113
|
*
|
|
114
114
|
* If the from index is out of bounds, the array will be returned unchanged
|
|
115
|
+
*
|
|
116
|
+
* Available as `moveIndices` and `move.indices`
|
|
115
117
|
* @param array Array to move within
|
|
116
118
|
* @param from Index to move from
|
|
117
119
|
* @param to Index to move to
|
|
118
120
|
* @returns Original array with item moved _(or unchanged if unable to move)_
|
|
119
121
|
*/
|
|
120
|
-
function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
|
|
122
|
+
export function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
|
|
121
123
|
if (!Array.isArray(array)) {
|
|
122
124
|
return [];
|
|
123
125
|
}
|
|
@@ -150,13 +152,15 @@ function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
|
|
|
150
152
|
* Move an item _(or array of items)_ to an index within an array
|
|
151
153
|
*
|
|
152
154
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
155
|
+
*
|
|
156
|
+
* Available as `moveToIndex` and `move.toIndex`
|
|
153
157
|
* @param array Array to move within
|
|
154
158
|
* @param value Item or items to move
|
|
155
159
|
* @param index Index to move to
|
|
156
160
|
* @param key Key to get an item's value for matching
|
|
157
161
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
158
162
|
*/
|
|
159
|
-
function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
163
|
+
export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
160
164
|
array: Item[],
|
|
161
165
|
value: Item | Item[],
|
|
162
166
|
index: number,
|
|
@@ -167,13 +171,15 @@ function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
|
167
171
|
* Move an item _(or array of items)_ to an index within an array
|
|
168
172
|
*
|
|
169
173
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
174
|
+
*
|
|
175
|
+
* Available as `moveToIndex` and `move.toIndex`
|
|
170
176
|
* @param array Array to move within
|
|
171
177
|
* @param value Item or items to move
|
|
172
178
|
* @param index Index to move to
|
|
173
179
|
* @param callback Callback to get an item's value for matching
|
|
174
180
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
175
181
|
*/
|
|
176
|
-
function moveToIndex<Item>(
|
|
182
|
+
export function moveToIndex<Item>(
|
|
177
183
|
array: Item[],
|
|
178
184
|
value: Item | Item[],
|
|
179
185
|
index: number,
|
|
@@ -184,14 +190,21 @@ function moveToIndex<Item>(
|
|
|
184
190
|
* Move an item _(or array of items)_ to an index within an array
|
|
185
191
|
*
|
|
186
192
|
* If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
|
|
193
|
+
*
|
|
194
|
+
* Available as `moveToIndex` and `move.toIndex`
|
|
187
195
|
* @param array Array to move within
|
|
188
196
|
* @param value Item or items to move
|
|
189
197
|
* @param index Index to move to
|
|
190
198
|
* @returns Original array with items moved _(or unchanged if unable to move)_
|
|
191
199
|
*/
|
|
192
|
-
function moveToIndex<Item>(array: Item[], value: Item | Item[], index: number): Item[];
|
|
200
|
+
export function moveToIndex<Item>(array: Item[], value: Item | Item[], index: number): Item[];
|
|
193
201
|
|
|
194
|
-
function moveToIndex(
|
|
202
|
+
export function moveToIndex(
|
|
203
|
+
array: unknown[],
|
|
204
|
+
value: unknown,
|
|
205
|
+
index: number,
|
|
206
|
+
key?: unknown,
|
|
207
|
+
): unknown[] {
|
|
195
208
|
if (!Array.isArray(array)) {
|
|
196
209
|
return [];
|
|
197
210
|
}
|
package/src/array/reverse.ts
CHANGED
package/src/array/select.ts
CHANGED
|
@@ -152,7 +152,7 @@ export function select<Item extends PlainObject, MapKey extends keyof Item>(
|
|
|
152
152
|
export function select<
|
|
153
153
|
Item,
|
|
154
154
|
MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
155
|
-
>(array: Item[],
|
|
155
|
+
>(array: Item[], item: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
|
|
156
156
|
|
|
157
157
|
/**
|
|
158
158
|
* Get a filtered and mapped array of items
|
|
@@ -163,7 +163,7 @@ export function select<
|
|
|
163
163
|
*/
|
|
164
164
|
export function select<Item extends PlainObject, MapKey extends keyof Item>(
|
|
165
165
|
array: Item[],
|
|
166
|
-
|
|
166
|
+
item: Item,
|
|
167
167
|
map: MapKey,
|
|
168
168
|
): Array<Item[MapKey]>;
|
|
169
169
|
|
package/src/array/sort.ts
CHANGED
|
@@ -172,17 +172,87 @@ function getComparisonValue(
|
|
|
172
172
|
return 0;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
function getIndex(array: unknown[], item: unknown, sorters: InternalSorter[]): number {
|
|
176
|
+
if (!Array.isArray(array)) {
|
|
177
|
+
return -1;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const {length} = array;
|
|
181
|
+
|
|
182
|
+
if (length === 0) {
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const sortersLength = sorters.length;
|
|
187
|
+
|
|
188
|
+
if (getComparisonValue(item, array[0], sorters, sortersLength) < 0) {
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (getComparisonValue(item, array[length - 1], sorters, sortersLength) >= 0) {
|
|
193
|
+
return length;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
let low = 0;
|
|
197
|
+
let high = length - 1;
|
|
198
|
+
|
|
199
|
+
while (low <= high) {
|
|
200
|
+
const mid = Math.floor((low + high) / 2);
|
|
201
|
+
|
|
202
|
+
if (getComparisonValue(item, array[mid], sorters, sortersLength) < 0) {
|
|
203
|
+
high = mid - 1;
|
|
204
|
+
} else {
|
|
205
|
+
low = mid + 1;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return low;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function getModifier(first: unknown, second: unknown): number {
|
|
213
|
+
const direction =
|
|
214
|
+
first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING;
|
|
215
|
+
|
|
216
|
+
return modifiers[direction];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getObjectSorter(obj: PlainObject, modifier: number): InternalSorter | undefined {
|
|
220
|
+
let sorter: InternalSorter | undefined;
|
|
221
|
+
|
|
222
|
+
if (typeof obj.comparison === 'function') {
|
|
223
|
+
sorter = getComparisonSorter(obj.comparison, modifier);
|
|
224
|
+
} else if (typeof obj.key === 'string') {
|
|
225
|
+
sorter = getValueSorter(obj.key, modifier);
|
|
226
|
+
|
|
227
|
+
if (typeof obj.compare === 'function') {
|
|
228
|
+
sorter.compare = {
|
|
229
|
+
complex: obj.compare,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
} else if (typeof obj.value === 'function') {
|
|
233
|
+
sorter = getValueSorter(obj.value, modifier);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (sorter != null && typeof obj.direction === 'string') {
|
|
237
|
+
sorter.modifier = modifiers[obj.direction as SortDirection] ?? modifier;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return sorter;
|
|
241
|
+
}
|
|
242
|
+
|
|
175
243
|
/**
|
|
176
244
|
* Get the index for an item _(to be inserted into an array of items)_ based on sorters _(and an optional default direction)_
|
|
177
245
|
*
|
|
178
246
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
247
|
+
*
|
|
248
|
+
* Available as `getSortedIndex` and `sort.index`
|
|
179
249
|
* @param array Array to get the index from
|
|
180
250
|
* @param item Item to get the index for
|
|
181
251
|
* @param sorters Sorters to use to determine sorting
|
|
182
252
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
183
253
|
* @returns Index for item
|
|
184
254
|
*/
|
|
185
|
-
function
|
|
255
|
+
export function getSortedIndex<Item>(
|
|
186
256
|
array: Item[],
|
|
187
257
|
item: Item,
|
|
188
258
|
sorters: Array<ArraySorter<Item>>,
|
|
@@ -193,13 +263,15 @@ function getIndex<Item>(
|
|
|
193
263
|
* Get the index for an item _(to be inserted into an array of items)_ based on a sorter _(and an optional default direction)_
|
|
194
264
|
*
|
|
195
265
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
266
|
+
*
|
|
267
|
+
* Available as `getSortedIndex` and `sort.index`
|
|
196
268
|
* @param array Array to get the index from
|
|
197
269
|
* @param item Item to get the index for
|
|
198
270
|
* @param sorter Sorter to use to determine sorting
|
|
199
271
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
200
272
|
* @returns Index for item
|
|
201
273
|
*/
|
|
202
|
-
function
|
|
274
|
+
export function getSortedIndex<Item>(
|
|
203
275
|
array: Item[],
|
|
204
276
|
item: Item,
|
|
205
277
|
sorter: ArraySorter<Item>,
|
|
@@ -210,83 +282,22 @@ function getIndex<Item>(
|
|
|
210
282
|
* Get the index for an item _(to be inserted into an array of items)_ based on an optional default direction_
|
|
211
283
|
*
|
|
212
284
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
285
|
+
*
|
|
286
|
+
* Available as `getSortedIndex` and `sort.index`
|
|
213
287
|
* @param array Array to get the index from
|
|
214
288
|
* @param item Item to get the index for
|
|
215
289
|
* @param descending Sorted in descending order? _(defaults to `false`)_
|
|
216
290
|
* @returns Index for item
|
|
217
291
|
*/
|
|
218
|
-
function
|
|
219
|
-
|
|
220
|
-
function getIndex(array: unknown[], item: unknown, first?: unknown, second?: unknown): number {
|
|
221
|
-
return getSortedIndex(array, item, getSorters(first, getModifier(first, second)));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function getModifier(first: unknown, second: unknown): number {
|
|
225
|
-
const direction =
|
|
226
|
-
first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING;
|
|
292
|
+
export function getSortedIndex<Item>(array: Item[], item: Item, descending?: boolean): number;
|
|
227
293
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
sorter = getComparisonSorter(obj.comparison, modifier);
|
|
236
|
-
} else if (typeof obj.key === 'string') {
|
|
237
|
-
sorter = getValueSorter(obj.key, modifier);
|
|
238
|
-
|
|
239
|
-
if (typeof obj.compare === 'function') {
|
|
240
|
-
sorter.compare = {
|
|
241
|
-
complex: obj.compare,
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
} else if (typeof obj.value === 'function') {
|
|
245
|
-
sorter = getValueSorter(obj.value, modifier);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (sorter != null && typeof obj.direction === 'string') {
|
|
249
|
-
sorter.modifier = modifiers[obj.direction as SortDirection] ?? modifier;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return sorter;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function getSortedIndex(array: unknown[], item: unknown, sorters: InternalSorter[]): number {
|
|
256
|
-
if (!Array.isArray(array)) {
|
|
257
|
-
return -1;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const {length} = array;
|
|
261
|
-
|
|
262
|
-
if (length === 0) {
|
|
263
|
-
return 0;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const sortersLength = sorters.length;
|
|
267
|
-
|
|
268
|
-
if (getComparisonValue(item, array[0], sorters, sortersLength) < 0) {
|
|
269
|
-
return 0;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (getComparisonValue(item, array[length - 1], sorters, sortersLength) >= 0) {
|
|
273
|
-
return length;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
let low = 0;
|
|
277
|
-
let high = length - 1;
|
|
278
|
-
|
|
279
|
-
while (low <= high) {
|
|
280
|
-
const mid = Math.floor((low + high) / 2);
|
|
281
|
-
|
|
282
|
-
if (getComparisonValue(item, array[mid], sorters, sortersLength) < 0) {
|
|
283
|
-
high = mid - 1;
|
|
284
|
-
} else {
|
|
285
|
-
low = mid + 1;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return low;
|
|
294
|
+
export function getSortedIndex(
|
|
295
|
+
array: unknown[],
|
|
296
|
+
item: unknown,
|
|
297
|
+
first?: unknown,
|
|
298
|
+
second?: unknown,
|
|
299
|
+
): number {
|
|
300
|
+
return getIndex(array, item, getSorters(first, getModifier(first, second)));
|
|
290
301
|
}
|
|
291
302
|
|
|
292
303
|
function getSorter(value: unknown, modifier: number): InternalSorter | undefined {
|
|
@@ -348,36 +359,45 @@ function getValueSorter(value: string | Function, modifier: number): InternalSor
|
|
|
348
359
|
|
|
349
360
|
/**
|
|
350
361
|
* Initialize a sort handler with sorters _(and an optional default direction)_
|
|
362
|
+
*
|
|
363
|
+
* Available as `initializeSorter` and `sort.initialize`
|
|
351
364
|
* @param sorters Sorters to use for sorting
|
|
352
365
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
353
366
|
* @returns Sort handler
|
|
354
367
|
*/
|
|
355
|
-
function
|
|
368
|
+
export function initializeSorter<Item>(
|
|
356
369
|
sorters: Array<ArraySorter<Item>>,
|
|
357
370
|
descending?: boolean,
|
|
358
371
|
): Sorter<Item>;
|
|
359
372
|
|
|
360
373
|
/**
|
|
361
374
|
* Initialize a sort handler with a sorter _(and an optional default direction)_
|
|
375
|
+
*
|
|
376
|
+
* Available as `initializeSorter` and `sort.initialize`
|
|
362
377
|
* @param sorter Sorter to use for sorting
|
|
363
378
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
364
379
|
* @returns Sort handler
|
|
365
380
|
*/
|
|
366
|
-
function
|
|
381
|
+
export function initializeSorter<Item>(
|
|
382
|
+
sorter: ArraySorter<Item>,
|
|
383
|
+
descending?: boolean,
|
|
384
|
+
): Sorter<Item>;
|
|
367
385
|
|
|
368
386
|
/**
|
|
369
387
|
* Initialize a sort handler _(with an optional default direction)_
|
|
388
|
+
*
|
|
389
|
+
* Available as `initializeSorter` and `sort.initialize`
|
|
370
390
|
* @param descending Sort in descending order? _(defaults to `false`)_
|
|
371
391
|
* @returns Sort handler
|
|
372
392
|
*/
|
|
373
|
-
function
|
|
393
|
+
export function initializeSorter<Item>(descending?: boolean): Sorter<Item>;
|
|
374
394
|
|
|
375
|
-
function
|
|
395
|
+
export function initializeSorter(first?: unknown, second?: unknown): Sorter<unknown> {
|
|
376
396
|
const sorters = getSorters(first, getModifier(first, second));
|
|
377
397
|
|
|
378
398
|
const sorter = (array: unknown[]) => sortArray(array, sorters);
|
|
379
399
|
|
|
380
|
-
sorter.index = (array: unknown[], item: unknown) =>
|
|
400
|
+
sorter.index = (array: unknown[], item: unknown) => getIndex(array, item, sorters);
|
|
381
401
|
sorter.is = (array: unknown[]) => isSortedArray(array, sorters);
|
|
382
402
|
|
|
383
403
|
return sorter as unknown as Sorter<unknown>;
|
|
@@ -390,7 +410,7 @@ function initializeSort(first?: unknown, second?: unknown): Sorter<unknown> {
|
|
|
390
410
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
391
411
|
* @returns `true` if sorted, otherwise `false`
|
|
392
412
|
*/
|
|
393
|
-
function isSorted<Item>(
|
|
413
|
+
export function isSorted<Item>(
|
|
394
414
|
array: Item[],
|
|
395
415
|
sorters: Array<ArraySorter<Item>>,
|
|
396
416
|
descending?: boolean,
|
|
@@ -403,17 +423,23 @@ function isSorted<Item>(
|
|
|
403
423
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
404
424
|
* @returns `true` if sorted, otherwise `false`
|
|
405
425
|
*/
|
|
406
|
-
function isSorted<Item>(
|
|
426
|
+
export function isSorted<Item>(
|
|
427
|
+
array: Item[],
|
|
428
|
+
sorter: ArraySorter<Item>,
|
|
429
|
+
descending?: boolean,
|
|
430
|
+
): boolean;
|
|
407
431
|
|
|
408
432
|
/**
|
|
409
433
|
* Is the array sorted?
|
|
434
|
+
*
|
|
435
|
+
* Available as `isSorted` and `sort.is`
|
|
410
436
|
* @param array Array to check
|
|
411
437
|
* @param descending Sorted in descending order? _(defaults to `false`)_
|
|
412
438
|
* @returns `true` if sorted, otherwise `false`
|
|
413
439
|
*/
|
|
414
|
-
function isSorted<Item>(array: Item[], descending?: boolean): boolean;
|
|
440
|
+
export function isSorted<Item>(array: Item[], descending?: boolean): boolean;
|
|
415
441
|
|
|
416
|
-
function isSorted(array: unknown[], first?: unknown, second?: unknown): boolean {
|
|
442
|
+
export function isSorted(array: unknown[], first?: unknown, second?: unknown): boolean {
|
|
417
443
|
return isSortedArray(array, getSorters(first, getModifier(first, second)));
|
|
418
444
|
}
|
|
419
445
|
|
|
@@ -513,10 +539,8 @@ function sortArray(array: unknown[], sorters: InternalSorter[]): unknown[] {
|
|
|
513
539
|
: array;
|
|
514
540
|
}
|
|
515
541
|
|
|
516
|
-
sort.index =
|
|
517
|
-
|
|
518
|
-
sort.initialize = initializeSort;
|
|
519
|
-
|
|
542
|
+
sort.index = getSortedIndex;
|
|
543
|
+
sort.initialize = initializeSorter;
|
|
520
544
|
sort.is = isSorted;
|
|
521
545
|
|
|
522
546
|
// #endregion
|
package/src/array/swap.ts
CHANGED
|
@@ -163,6 +163,8 @@ function swapArrays(array: unknown[], from: unknown[], to: unknown[], key: unkno
|
|
|
163
163
|
* Swap two indiced items in an array
|
|
164
164
|
*
|
|
165
165
|
* If either index is out of bounds, the array will be returned unchanged
|
|
166
|
+
*
|
|
167
|
+
* Available as `swapIndices` and `swap.indices`
|
|
166
168
|
* @param array Array of items to swap
|
|
167
169
|
* @param first First index _(can be negative to count from the end)_
|
|
168
170
|
* @param second Second index _(can be negative to count from the end)_
|
package/src/array/to-map.ts
CHANGED
|
@@ -144,12 +144,14 @@ toMap.arrays = toMapArrays;
|
|
|
144
144
|
|
|
145
145
|
/**
|
|
146
146
|
* Create a Map from an array of items using callbacks, grouping values into arrays
|
|
147
|
+
*
|
|
148
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
147
149
|
* @param array Array to convert
|
|
148
150
|
* @param key Callback to get an item's grouping key
|
|
149
151
|
* @param value Callback to get an item's value
|
|
150
152
|
* @returns Map of keyed arrays of values
|
|
151
153
|
*/
|
|
152
|
-
function toMapArrays<
|
|
154
|
+
export function toMapArrays<
|
|
153
155
|
Item,
|
|
154
156
|
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
155
157
|
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
@@ -161,12 +163,14 @@ function toMapArrays<
|
|
|
161
163
|
|
|
162
164
|
/**
|
|
163
165
|
* Create a Map from an array of items using a callback and value, grouping values into arrays
|
|
166
|
+
*
|
|
167
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
164
168
|
* @param array Array to convert
|
|
165
169
|
* @param key Callback to get an item's grouping key
|
|
166
170
|
* @param value Key to use for value
|
|
167
171
|
* @returns Map of keyed arrays of values
|
|
168
172
|
*/
|
|
169
|
-
function toMapArrays<
|
|
173
|
+
export function toMapArrays<
|
|
170
174
|
Item extends PlainObject,
|
|
171
175
|
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
172
176
|
ItemValue extends keyof Item,
|
|
@@ -178,12 +182,14 @@ function toMapArrays<
|
|
|
178
182
|
|
|
179
183
|
/**
|
|
180
184
|
* Create a Map from an array of items using a key and callback, grouping values into arrays
|
|
185
|
+
*
|
|
186
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
181
187
|
* @param array Array to convert
|
|
182
188
|
* @param key Key to use for grouping
|
|
183
189
|
* @param value Callback to get an item's value
|
|
184
190
|
* @returns Map of keyed arrays of values
|
|
185
191
|
*/
|
|
186
|
-
function toMapArrays<
|
|
192
|
+
export function toMapArrays<
|
|
187
193
|
Item extends PlainObject,
|
|
188
194
|
ItemKey extends keyof Item,
|
|
189
195
|
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
@@ -195,12 +201,14 @@ function toMapArrays<
|
|
|
195
201
|
|
|
196
202
|
/**
|
|
197
203
|
* Create a Map from an array of items using a key and value, grouping values into arrays
|
|
204
|
+
*
|
|
205
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
198
206
|
* @param array Array to convert
|
|
199
207
|
* @param key Key to use for grouping
|
|
200
208
|
* @param value Key to use for value
|
|
201
209
|
* @returns Map of keyed arrays of values
|
|
202
210
|
*/
|
|
203
|
-
function toMapArrays<
|
|
211
|
+
export function toMapArrays<
|
|
204
212
|
Item extends PlainObject,
|
|
205
213
|
ItemKey extends keyof Item,
|
|
206
214
|
ItemValue extends keyof Item,
|
|
@@ -208,27 +216,31 @@ function toMapArrays<
|
|
|
208
216
|
|
|
209
217
|
/**
|
|
210
218
|
* Create a Map from an array of items using a callback, grouping items into arrays
|
|
219
|
+
*
|
|
220
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
211
221
|
* @param array Array to convert
|
|
212
222
|
* @param callback Callback to get an item's grouping key
|
|
213
223
|
* @returns Map of keyed arrays of items
|
|
214
224
|
*/
|
|
215
|
-
function toMapArrays<
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
): Map<ReturnType<Callback>, Item[]>;
|
|
225
|
+
export function toMapArrays<
|
|
226
|
+
Item,
|
|
227
|
+
Callback extends (item: Item, index: number, array: Item[]) => Key,
|
|
228
|
+
>(array: Item[], callback: Callback): Map<ReturnType<Callback>, Item[]>;
|
|
219
229
|
|
|
220
230
|
/**
|
|
221
231
|
* Create a Map from an array of items using a key, grouping items into arrays
|
|
232
|
+
*
|
|
233
|
+
* Available as `toMapArrays` and `toMap.arrays`
|
|
222
234
|
* @param array Array to convert
|
|
223
235
|
* @param key Key to use for grouping
|
|
224
236
|
* @returns Map of keyed arrays of items
|
|
225
237
|
*/
|
|
226
|
-
function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
238
|
+
export function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
227
239
|
array: Item[],
|
|
228
240
|
key: ItemKey,
|
|
229
241
|
): Map<Item[ItemKey], Item[]>;
|
|
230
242
|
|
|
231
|
-
function toMapArrays(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
243
|
+
export function toMapArrays(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
232
244
|
return getMapValues(array, first, second, true);
|
|
233
245
|
}
|
|
234
246
|
|
package/src/array/to-record.ts
CHANGED
|
@@ -120,12 +120,14 @@ toRecord.arrays = toRecordArrays;
|
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
122
|
* Create a record from an array of items using callbacks, grouping values into arrays
|
|
123
|
+
*
|
|
124
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
123
125
|
* @param array Array to convert
|
|
124
126
|
* @param key Callback to get an item's grouping key
|
|
125
127
|
* @param value Callback to get an item's value
|
|
126
128
|
* @returns Record of keyed arrays of values
|
|
127
129
|
*/
|
|
128
|
-
function toRecordArrays<
|
|
130
|
+
export function toRecordArrays<
|
|
129
131
|
Item,
|
|
130
132
|
KeyCallback extends (item: Item, index: number, array: Item[]) => Key,
|
|
131
133
|
ValueCallback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
@@ -137,12 +139,14 @@ function toRecordArrays<
|
|
|
137
139
|
|
|
138
140
|
/**
|
|
139
141
|
* Create a record from an array of items using a callback and value, grouping values into arrays
|
|
142
|
+
*
|
|
143
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
140
144
|
* @param array Array to convert
|
|
141
145
|
* @param callback Callback to get an item's grouping key
|
|
142
146
|
* @param value Key to use for value
|
|
143
147
|
* @returns Record of keyed arrays of values
|
|
144
148
|
*/
|
|
145
|
-
function toRecordArrays<
|
|
149
|
+
export function toRecordArrays<
|
|
146
150
|
Item extends PlainObject,
|
|
147
151
|
Callback extends (item: Item, index: number, array: Item[]) => Key,
|
|
148
152
|
ItemValue extends keyof Item,
|
|
@@ -154,12 +158,14 @@ function toRecordArrays<
|
|
|
154
158
|
|
|
155
159
|
/**
|
|
156
160
|
* Create a record from an array of items using a key and callback, grouping values into arrays
|
|
161
|
+
*
|
|
162
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
157
163
|
* @param array Array to convert
|
|
158
164
|
* @param key Key to use for grouping
|
|
159
165
|
* @param callback Callback to get an item's value
|
|
160
166
|
* @returns Record of keyed arrays of values
|
|
161
167
|
*/
|
|
162
|
-
function toRecordArrays<
|
|
168
|
+
export function toRecordArrays<
|
|
163
169
|
Item extends PlainObject,
|
|
164
170
|
ItemKey extends keyof Item,
|
|
165
171
|
Callback extends (item: Item, index: number, array: Item[]) => unknown,
|
|
@@ -171,12 +177,14 @@ function toRecordArrays<
|
|
|
171
177
|
|
|
172
178
|
/**
|
|
173
179
|
* Create a record from an array of items using a key and value, grouping values into arrays
|
|
180
|
+
*
|
|
181
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
174
182
|
* @param array Array to convert
|
|
175
183
|
* @param key Key to use for grouping
|
|
176
184
|
* @param value Key to use for value
|
|
177
185
|
* @returns Record of keyed arrays of values
|
|
178
186
|
*/
|
|
179
|
-
function toRecordArrays<
|
|
187
|
+
export function toRecordArrays<
|
|
180
188
|
Item extends PlainObject,
|
|
181
189
|
ItemKey extends keyof Item,
|
|
182
190
|
ItemValue extends keyof Item,
|
|
@@ -188,27 +196,31 @@ function toRecordArrays<
|
|
|
188
196
|
|
|
189
197
|
/**
|
|
190
198
|
* Create a record from an array of items using a callback, grouping items into arrays
|
|
199
|
+
*
|
|
200
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
191
201
|
* @param array Array to convert
|
|
192
202
|
* @param callback Callback to get an item's grouping key
|
|
193
203
|
* @returns Record of keyed arrays of items
|
|
194
204
|
*/
|
|
195
|
-
function toRecordArrays<
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
): Record<ReturnType<Callback>, Item[]>;
|
|
205
|
+
export function toRecordArrays<
|
|
206
|
+
Item,
|
|
207
|
+
Callback extends (item: Item, index: number, array: Item[]) => Key,
|
|
208
|
+
>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item[]>;
|
|
199
209
|
|
|
200
210
|
/**
|
|
201
211
|
* Create a record from an array of items using a key, grouping items into arrays
|
|
212
|
+
*
|
|
213
|
+
* Available as `toRecordArrays` and `toRecord.arrays`
|
|
202
214
|
* @param array Array to convert
|
|
203
215
|
* @param key Key to use for grouping
|
|
204
216
|
* @returns Record of keyed arrays of items
|
|
205
217
|
*/
|
|
206
|
-
function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
218
|
+
export function toRecordArrays<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
207
219
|
array: Item[],
|
|
208
220
|
key: ItemKey,
|
|
209
221
|
): Simplify<Record<KeyedValue<Item, ItemKey>, Item[]>>;
|
|
210
222
|
|
|
211
|
-
function toRecordArrays(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
223
|
+
export function toRecordArrays(array: unknown[], first?: unknown, second?: unknown): unknown {
|
|
212
224
|
return groupValues(array, first, second, true);
|
|
213
225
|
}
|
|
214
226
|
|