@oscarpalmer/atoms 0.166.3 → 0.168.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/index.d.mts +1 -2
- package/dist/array/index.mjs +1 -2
- package/dist/array/sort.d.mts +29 -5
- package/dist/array/sort.mjs +27 -14
- package/dist/index.d.mts +287 -231
- package/dist/index.mjs +196 -183
- package/dist/promise/index.d.mts +2 -7
- package/dist/promise/index.mjs +5 -7
- package/dist/promise/misc.d.mts +3 -1
- package/dist/promise/misc.mjs +3 -2
- package/dist/result/index.d.mts +2 -5
- package/dist/result/index.mjs +2 -4
- package/dist/result/misc.d.mts +2 -1
- package/dist/result/misc.mjs +2 -2
- package/package.json +34 -2
- package/src/array/index.ts +0 -1
- package/src/array/sort.ts +100 -63
- package/src/index.ts +10 -0
- package/src/promise/index.ts +0 -22
- package/src/promise/misc.ts +7 -0
- package/src/result/index.ts +0 -9
- package/src/result/misc.ts +6 -0
package/dist/array/index.d.mts
CHANGED
|
@@ -15,11 +15,10 @@ import { ArrayPosition, endsWithArray, getArrayPosition, includesArray, indexOfA
|
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { select } from "./select.mjs";
|
|
17
17
|
import { drop, slice, take } from "./slice.mjs";
|
|
18
|
-
import { ArrayComparisonSorter, ArrayKeySorter, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, sort } from "./sort.mjs";
|
|
19
18
|
import { splice } from "./splice.mjs";
|
|
20
19
|
import { toSet } from "./to-set.mjs";
|
|
21
20
|
import { toggle } from "./toggle.mjs";
|
|
22
21
|
import { union } from "./union.mjs";
|
|
23
22
|
import { unique } from "./unique.mjs";
|
|
24
23
|
import { update } from "./update.mjs";
|
|
25
|
-
export {
|
|
24
|
+
export { ArrayPosition, chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, select, shuffle, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
|
package/dist/array/index.mjs
CHANGED
|
@@ -15,11 +15,10 @@ import { endsWithArray, getArrayPosition, includesArray, indexOfArray, startsWit
|
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { select } from "./select.mjs";
|
|
17
17
|
import { drop, slice, take } from "./slice.mjs";
|
|
18
|
-
import { SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, sort } from "./sort.mjs";
|
|
19
18
|
import { splice } from "./splice.mjs";
|
|
20
19
|
import { toSet } from "./to-set.mjs";
|
|
21
20
|
import { toggle } from "./toggle.mjs";
|
|
22
21
|
import { union } from "./union.mjs";
|
|
23
22
|
import { unique } from "./unique.mjs";
|
|
24
23
|
import { update } from "./update.mjs";
|
|
25
|
-
export {
|
|
24
|
+
export { chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, select, shuffle, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
|
package/dist/array/sort.d.mts
CHANGED
|
@@ -35,6 +35,10 @@ type ArrayKeySorter<Item extends PlainObject, ItemKey extends keyof Item> = {
|
|
|
35
35
|
* Sorters based on keys in an object
|
|
36
36
|
*/
|
|
37
37
|
type ArrayKeySorters<Item extends PlainObject> = { [ItemKey in keyof Item]: ArrayKeySorter<Item, ItemKey> }[keyof Item];
|
|
38
|
+
/**
|
|
39
|
+
* Sorter to use for sorting
|
|
40
|
+
*/
|
|
41
|
+
type ArraySorter<Item> = Item extends PlainObject ? keyof Item | ArrayComparisonSorter<Item> | ArrayKeySorters<Item> | ArrayValueSorter<Item> | ComparisonSorter<Item> : ArrayComparisonSorter<Item> | ArrayValueSorter<Item> | ComparisonSorter<Item>;
|
|
38
42
|
/**
|
|
39
43
|
* Sorting information for arrays _(using a value callback and built-in comparison)_
|
|
40
44
|
*/
|
|
@@ -61,10 +65,27 @@ type ComparisonSorter<Item> = (first: Item, second: Item) => number;
|
|
|
61
65
|
* Direction to sort by
|
|
62
66
|
*/
|
|
63
67
|
type SortDirection = 'ascending' | 'descending';
|
|
68
|
+
type Sorter<Item> = (array: Item[]) => Item[];
|
|
64
69
|
/**
|
|
65
|
-
*
|
|
70
|
+
* Initialize a sort handler with sorters _(and an optional default direction)_
|
|
71
|
+
* @param sorters Sorters to use for sorting
|
|
72
|
+
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
73
|
+
* @returns Sort handler
|
|
74
|
+
*/
|
|
75
|
+
declare function initializeSort<Item>(sorters: Array<ArraySorter<Item>>, descending?: boolean): Sorter<Item>;
|
|
76
|
+
/**
|
|
77
|
+
* Initialize a sort handler with a sorter _(and an optional default direction)_
|
|
78
|
+
* @param sorter Sorter to use for sorting
|
|
79
|
+
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
80
|
+
* @returns Sort handler
|
|
81
|
+
*/
|
|
82
|
+
declare function initializeSort<Item>(sorter: ArraySorter<Item>, descending?: boolean): Sorter<Item>;
|
|
83
|
+
/**
|
|
84
|
+
* Initialize a sort handler _(with an optional default direction)_
|
|
85
|
+
* @param descending Sort in descending order? _(defaults to `false`)_
|
|
86
|
+
* @returns Sort handler
|
|
66
87
|
*/
|
|
67
|
-
|
|
88
|
+
declare function initializeSort<Item>(descending?: boolean): Sorter<Item>;
|
|
68
89
|
/**
|
|
69
90
|
* Sort an array of items, using multiple sorters to sort by specific values
|
|
70
91
|
* @param array Array to sort
|
|
@@ -72,7 +93,7 @@ type Sorter<Item> = Item extends PlainObject ? keyof Item | ArrayComparisonSorte
|
|
|
72
93
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
73
94
|
* @returns Sorted array
|
|
74
95
|
*/
|
|
75
|
-
declare function sort<Item>(array: Item[], sorters: Array<
|
|
96
|
+
declare function sort<Item>(array: Item[], sorters: Array<ArraySorter<Item>>, descending?: boolean): Item[];
|
|
76
97
|
/**
|
|
77
98
|
* Sort an array of items, using multiple sorters to sort by specific values
|
|
78
99
|
* @param array Array to sort
|
|
@@ -80,7 +101,7 @@ declare function sort<Item>(array: Item[], sorters: Array<Sorter<Item>>, descend
|
|
|
80
101
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
81
102
|
* @returns Sorted array
|
|
82
103
|
*/
|
|
83
|
-
declare function sort<Item>(array: Item[], sorter:
|
|
104
|
+
declare function sort<Item>(array: Item[], sorter: ArraySorter<Item>, descending?: boolean): Item[];
|
|
84
105
|
/**
|
|
85
106
|
* Sort an array of items
|
|
86
107
|
* @param array Array to sort
|
|
@@ -88,7 +109,10 @@ declare function sort<Item>(array: Item[], sorter: Sorter<Item>, descending?: bo
|
|
|
88
109
|
* @returns Sorted array
|
|
89
110
|
*/
|
|
90
111
|
declare function sort<Item>(array: Item[], descending?: boolean): Item[];
|
|
112
|
+
declare namespace sort {
|
|
113
|
+
var initialize: typeof initializeSort;
|
|
114
|
+
}
|
|
91
115
|
declare const SORT_DIRECTION_ASCENDING: SortDirection;
|
|
92
116
|
declare const SORT_DIRECTION_DESCENDING: SortDirection;
|
|
93
117
|
//#endregion
|
|
94
|
-
export { ArrayComparisonSorter, ArrayKeySorter, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, sort };
|
|
118
|
+
export { ArrayComparisonSorter, ArrayKeySorter, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, Sorter, sort };
|
package/dist/array/sort.mjs
CHANGED
|
@@ -9,6 +9,9 @@ function getComparisonSorter(callback, modifier) {
|
|
|
9
9
|
identifier: String(callback)
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
+
function getModifier(first, second) {
|
|
13
|
+
return modifiers[first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING];
|
|
14
|
+
}
|
|
12
15
|
function getObjectSorter(obj, modifier) {
|
|
13
16
|
let sorter;
|
|
14
17
|
if (typeof obj.comparison === "function") sorter = getComparisonSorter(obj.comparison, modifier);
|
|
@@ -27,6 +30,17 @@ function getSorter(value, modifier) {
|
|
|
27
30
|
default: break;
|
|
28
31
|
}
|
|
29
32
|
}
|
|
33
|
+
function getSorters(value, modifier) {
|
|
34
|
+
const array = Array.isArray(value) ? value : [value];
|
|
35
|
+
const { length } = array;
|
|
36
|
+
const sorters = [];
|
|
37
|
+
for (let index = 0; index < length; index += 1) {
|
|
38
|
+
const item = array[index];
|
|
39
|
+
const sorter = getSorter(item, modifier);
|
|
40
|
+
if (sorter != null) sorters.push(sorter);
|
|
41
|
+
}
|
|
42
|
+
return sorters.filter((value, index, array) => array.findIndex((next) => next.identifier === value.identifier) === index);
|
|
43
|
+
}
|
|
30
44
|
function getValueSorter(value, modifier) {
|
|
31
45
|
return {
|
|
32
46
|
modifier,
|
|
@@ -35,32 +49,31 @@ function getValueSorter(value, modifier) {
|
|
|
35
49
|
value: typeof value === "function" ? value : (item) => item[value]
|
|
36
50
|
};
|
|
37
51
|
}
|
|
52
|
+
function initializeSort(first, second) {
|
|
53
|
+
const modifier = getModifier(first, second);
|
|
54
|
+
const sorters = getSorters(first, modifier);
|
|
55
|
+
return (array) => work(array, sorters, modifier);
|
|
56
|
+
}
|
|
38
57
|
function sort(array, first, second) {
|
|
58
|
+
const modifier = getModifier(first, second);
|
|
59
|
+
return work(array, getSorters(first, modifier), modifier);
|
|
60
|
+
}
|
|
61
|
+
function work(array, sorters, modifier) {
|
|
39
62
|
if (!Array.isArray(array)) return [];
|
|
40
63
|
if (array.length < 2) return array;
|
|
41
|
-
const modifier = modifiers[first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING];
|
|
42
|
-
const sorters = (Array.isArray(first) ? first : [first]).map((item) => getSorter(item, modifier)).filter((sorter) => sorter != null).filter((current, index, filtered) => filtered.findIndex((next) => next.identifier === current.identifier) === index);
|
|
43
64
|
const { length } = sorters;
|
|
44
65
|
if (length === 0) return array.sort((first, second) => compare(first, second) * modifier);
|
|
45
|
-
|
|
46
|
-
const sorter = sorters[0];
|
|
47
|
-
return array.sort((firstItem, secondItem) => {
|
|
48
|
-
const firstValue = sorter.get ? sorter.value(firstItem) : firstItem;
|
|
49
|
-
const secondValue = sorter.get ? sorter.value(secondItem) : secondItem;
|
|
50
|
-
return (sorter.compare?.complex?.(firstItem, firstValue, secondItem, secondValue) ?? sorter.compare?.simple?.(firstItem, secondItem) ?? compare(firstValue, secondValue)) * sorter.modifier;
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
return array.sort((firstItem, secondItem) => {
|
|
66
|
+
return array.sort((first, second) => {
|
|
54
67
|
for (let index = 0; index < length; index += 1) {
|
|
55
68
|
const sorter = sorters[index];
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const comparison = (sorter.compare?.complex?.(firstItem, firstValue, secondItem, secondValue) ?? sorter.compare?.simple?.(firstItem, secondItem) ?? compare(firstValue, secondValue)) * sorter.modifier;
|
|
69
|
+
const values = [sorter.get ? sorter.value(first) : first, sorter.get ? sorter.value(second) : second];
|
|
70
|
+
const comparison = (sorter.compare?.complex?.(first, values[0], second, values[1]) ?? sorter.compare?.simple?.(values[0], values[1]) ?? compare(values[0], values[1])) * sorter.modifier;
|
|
59
71
|
if (comparison !== 0) return comparison;
|
|
60
72
|
}
|
|
61
73
|
return 0;
|
|
62
74
|
});
|
|
63
75
|
}
|
|
76
|
+
sort.initialize = initializeSort;
|
|
64
77
|
const SORT_DIRECTION_ASCENDING = "ascending";
|
|
65
78
|
const SORT_DIRECTION_DESCENDING = "descending";
|
|
66
79
|
const modifiers = {
|