@oscarpalmer/atoms 0.217.0 → 0.219.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 +38 -4
- package/dist/array/find.d.mts +34 -0
- package/dist/array/first.d.mts +40 -4
- package/dist/array/insert.mjs +1 -1
- package/dist/array/last.d.mts +46 -10
- package/dist/array/partition.d.mts +24 -8
- package/dist/array/reverse.d.mts +6 -0
- package/dist/array/reverse.mjs +6 -0
- package/dist/array/select.d.mts +97 -116
- package/dist/array/select.mjs +19 -15
- package/dist/array/single.d.mts +22 -0
- package/dist/array/single.mjs +1 -1
- package/dist/array/slice.mjs +27 -14
- package/dist/array/sort.d.mts +54 -0
- package/dist/array/sort.mjs +2 -1
- package/dist/color/misc/get.mjs +3 -1
- package/dist/color/space/hex.d.mts +8 -0
- package/dist/color/space/hex.mjs +9 -1
- package/dist/function/retry.d.mts +1 -1
- package/dist/function/retry.mjs +5 -2
- package/dist/index.d.mts +879 -324
- package/dist/index.mjs +480 -173
- package/dist/internal/array/callbacks.mjs +1 -1
- package/dist/internal/array/find.d.mts +3 -3
- package/dist/internal/array/find.mjs +90 -48
- package/dist/internal/array/insert.mjs +1 -1
- package/dist/internal/array/match.mjs +21 -17
- package/dist/internal/array/sets.mjs +7 -1
- package/dist/internal/array/shuffle.d.mts +5 -0
- package/dist/internal/array/shuffle.mjs +5 -0
- package/dist/internal/math/aggregate.d.mts +69 -21
- package/dist/internal/math/aggregate.mjs +4 -1
- package/dist/internal/math/misc.d.mts +14 -0
- package/dist/internal/math/misc.mjs +14 -0
- package/dist/internal/number.d.mts +19 -1
- package/dist/internal/number.mjs +22 -2
- package/dist/internal/plan/plan.generate.mjs +3 -3
- package/dist/internal/random.d.mts +6 -0
- package/dist/internal/random.mjs +13 -2
- package/dist/internal/result/attempt.mjs +4 -4
- package/dist/internal/string/case.d.mts +63 -0
- package/dist/internal/string/case.mjs +63 -0
- package/dist/internal/string/misc.d.mts +20 -0
- package/dist/internal/string/misc.mjs +22 -1
- package/dist/internal/subscription.mjs +1 -1
- package/dist/internal/value/equal.mjs +2 -1
- package/dist/internal/value/partial.mjs +3 -1
- package/dist/logger.d.mts +12 -0
- package/dist/logger.mjs +12 -0
- package/dist/math.d.mts +90 -0
- package/dist/math.mjs +7 -0
- package/dist/models/array/array.find.model.d.mts +26 -0
- package/dist/models/array/array.misc.model.d.mts +2 -4
- package/dist/models/index.d.mts +1 -1
- package/dist/models/plan.model.d.mts +2 -0
- package/dist/models/plan.model.mjs +3 -1
- package/dist/models/string/string.fuzzy.model.d.mts +1 -1
- package/dist/models/string/string.misc.model.mjs +1 -1
- package/dist/models/value/value.merge.model.d.mts +4 -4
- package/dist/plan/index.d.mts +1 -4
- package/dist/plan/index.mjs +2 -3
- package/dist/plan/use.mjs +4 -4
- package/dist/promise/index.d.mts +6 -4
- package/dist/promise/index.mjs +4 -4
- package/dist/promise/misc.d.mts +6 -0
- package/dist/promise/misc.mjs +11 -2
- package/dist/promise/timed.d.mts +3 -0
- package/dist/promise/timed.mjs +5 -2
- package/dist/queue.d.mts +0 -1
- package/dist/queue.mjs +5 -6
- package/dist/random.d.mts +2 -2
- package/dist/random.mjs +3 -3
- package/dist/result/match.mjs +2 -2
- package/dist/result/misc.mjs +3 -10
- package/dist/result/work/pipe.d.mts +24 -22
- package/dist/result/work/pipe.mjs +7 -3
- package/dist/string/fuzzy.mjs +1 -1
- package/dist/string/index.d.mts +15 -1
- package/dist/string/index.mjs +16 -2
- package/dist/string/match.d.mts +24 -0
- package/dist/string/match.mjs +24 -0
- package/dist/string/template.d.mts +4 -0
- package/dist/value/clone.mjs +1 -1
- package/dist/value/merge.mjs +17 -4
- package/package.json +1 -1
package/dist/array/select.d.mts
CHANGED
|
@@ -1,190 +1,171 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ArrayMapKeyOrCallback, ArrayMapped, ArrayObjectKeysOf } from "../models/array/array.misc.model.mjs";
|
|
1
|
+
import { ArrayFlattened } from "../models/array/array.misc.model.mjs";
|
|
3
2
|
//#region src/array/select.d.ts
|
|
4
3
|
/**
|
|
5
|
-
* Get a
|
|
4
|
+
* Get a narrowed, mapped, and flattened array of items
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
* @param filterCallback Callback to get an item's value for matching
|
|
9
|
-
* @param filterValue Value to match against
|
|
10
|
-
* @param map Callback or key to map the matched items
|
|
11
|
-
* @returns Filtered and mapped array of items
|
|
6
|
+
* _Available as `filterAndFlatMap`, `filterAndMap.flat`, and `select.flat`_
|
|
12
7
|
*
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* item => item.id,
|
|
18
|
-
* 2,
|
|
19
|
-
* item => item.name,
|
|
20
|
-
* ); // => ['Bob']
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export declare function filterAndMap<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, Map extends ArrayMapKeyOrCallback<Item>>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, map: Map): Array<ArrayMapped<Item, Map>>;
|
|
24
|
-
/**
|
|
25
|
-
* Get a filtered and mapped array of items
|
|
26
|
-
*
|
|
27
|
-
* @param array Array to search in
|
|
28
|
-
* @param filterKey Key to get an item's value for matching
|
|
29
|
-
* @param filterValue Value to match against
|
|
30
|
-
* @param map Callback or key to map the matched items
|
|
31
|
-
* @returns Filtered and mapped array of items
|
|
8
|
+
* @param array Array to select from
|
|
9
|
+
* @param filter Type guard to filter and narrow down the items
|
|
10
|
+
* @param mapper Callback to map and flatten the narrowed items
|
|
11
|
+
* @returns Narrowed, mapped, and flattened array of items
|
|
32
12
|
*
|
|
33
13
|
* @example
|
|
34
14
|
* ```typescript
|
|
35
|
-
*
|
|
36
|
-
* [{id: 1,
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* ); // => ['Bob']
|
|
15
|
+
* filterAndFlatMap(
|
|
16
|
+
* [{id: 1, tags: ['a', 'b']}, {id: 2}, {id: 3, tags: ['c']}],
|
|
17
|
+
* (item): item is {id: number; tags: string[]} => item.tags != null,
|
|
18
|
+
* item => item.tags,
|
|
19
|
+
* ); // => ['a', 'b', 'c']
|
|
41
20
|
* ```
|
|
42
21
|
*/
|
|
43
|
-
export declare function
|
|
22
|
+
export declare function filterAndFlatMap<Item, Narrowed extends Item, Mapped>(array: Item[], filter: (item: Item, index: number, array: Item[]) => item is Narrowed, mapper: (item: Narrowed, index: number, array: Item[]) => Mapped): Array<ArrayFlattened<Mapped>>;
|
|
44
23
|
/**
|
|
45
|
-
* Get a filtered and
|
|
24
|
+
* Get a filtered, mapped, and flattened array of items
|
|
46
25
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @param
|
|
50
|
-
* @
|
|
26
|
+
* _Available as `filterAndFlatMap`, `filterAndMap.flat`, and `select.flat`_
|
|
27
|
+
*
|
|
28
|
+
* @param array Array to select from
|
|
29
|
+
* @param filter Callback to filter the items
|
|
30
|
+
* @param mapper Callback to map and flatten the filtered items
|
|
31
|
+
* @returns Filtered, mapped, and flattened array of items
|
|
51
32
|
*
|
|
52
33
|
* @example
|
|
53
34
|
* ```typescript
|
|
54
|
-
*
|
|
55
|
-
* [{id: 1,
|
|
56
|
-
* item => item.id
|
|
57
|
-
* item => item.
|
|
58
|
-
* ); // => ['
|
|
35
|
+
* filterAndFlatMap(
|
|
36
|
+
* [{id: 1, tags: ['a', 'b']}, {id: 2, tags: ['c']}, {id: 3, tags: ['d', 'e']}],
|
|
37
|
+
* item => item.id !== 2,
|
|
38
|
+
* item => item.tags,
|
|
39
|
+
* ); // => ['a', 'b', 'd', 'e']
|
|
59
40
|
* ```
|
|
60
41
|
*/
|
|
61
|
-
export declare function
|
|
42
|
+
export declare function filterAndFlatMap<Item, Mapped>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, mapper: (item: Item, index: number, array: Item[]) => Mapped): Array<ArrayFlattened<Mapped>>;
|
|
62
43
|
/**
|
|
63
|
-
* Get a
|
|
44
|
+
* Get a narrowed and mapped array of items
|
|
64
45
|
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* @param
|
|
68
|
-
* @
|
|
46
|
+
* _Available as `filterAndMap` and `select`_
|
|
47
|
+
*
|
|
48
|
+
* @param array Array to select from
|
|
49
|
+
* @param filter Type guard to filter and narrow down the items
|
|
50
|
+
* @param mapper Callback to map the narrowed items
|
|
51
|
+
* @returns Narrowed and mapped array of items
|
|
69
52
|
*
|
|
70
53
|
* @example
|
|
71
54
|
* ```typescript
|
|
72
|
-
*
|
|
73
|
-
* [1, 2, 3,
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* ); // => [
|
|
55
|
+
* filterAndMap(
|
|
56
|
+
* [{id: 1, name: 'Alice'}, {id: 2}, {id: 3, name: 'Charlie'}],
|
|
57
|
+
* (item): item is {id: number; name: string} => item.name != null,
|
|
58
|
+
* item => item.name.toUpperCase(),
|
|
59
|
+
* ); // => ['ALICE', 'CHARLIE']
|
|
77
60
|
* ```
|
|
78
61
|
*/
|
|
79
|
-
export declare function filterAndMap<Item,
|
|
62
|
+
export declare function filterAndMap<Item, Narrowed extends Item, Mapped>(array: Item[], filter: (item: Item, index: number, array: Item[]) => item is Narrowed, mapper: (item: Narrowed, index: number, array: Item[]) => Mapped): Mapped[];
|
|
80
63
|
/**
|
|
81
|
-
* Get a
|
|
64
|
+
* Get a filtered and mapped array of items
|
|
82
65
|
*
|
|
83
|
-
* _Available as `
|
|
66
|
+
* _Available as `filterAndMap` and `select`_
|
|
84
67
|
*
|
|
85
|
-
* @param array Array to
|
|
86
|
-
* @param
|
|
87
|
-
* @param
|
|
88
|
-
* @
|
|
89
|
-
* @returns Mapped and filtered array of items
|
|
68
|
+
* @param array Array to select from
|
|
69
|
+
* @param filter Callback to filter the items
|
|
70
|
+
* @param mapper Callback to map the filtered items
|
|
71
|
+
* @returns Filtered and mapped array of items
|
|
90
72
|
*
|
|
91
73
|
* @example
|
|
92
74
|
* ```typescript
|
|
93
|
-
*
|
|
75
|
+
* filterAndMap(
|
|
94
76
|
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
77
|
+
* item => item.id === 2,
|
|
95
78
|
* item => item.name,
|
|
96
|
-
* value => value.length,
|
|
97
|
-
* 3,
|
|
98
79
|
* ); // => ['Bob']
|
|
99
80
|
* ```
|
|
100
81
|
*/
|
|
101
|
-
export declare function
|
|
82
|
+
export declare function filterAndMap<Item, Mapped>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, mapper: (item: Item, index: number, array: Item[]) => Mapped): Mapped[];
|
|
102
83
|
/**
|
|
103
|
-
* Get a mapped and
|
|
84
|
+
* Get a mapped, flattened, and narrowed array of items
|
|
104
85
|
*
|
|
105
|
-
* _Available as `
|
|
86
|
+
* _Available as `flatMapAndFilter`, `mapAndFilter.flat`, and `reverseSelect.flat`_
|
|
106
87
|
*
|
|
107
|
-
* @param array Array to
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
110
|
-
* @
|
|
111
|
-
* @returns Mapped and filtered array of items
|
|
88
|
+
* @param array Array to select from
|
|
89
|
+
* @param mapper Callback to map and flatten the items
|
|
90
|
+
* @param filter Type guard to filter and narrow down the flattened items
|
|
91
|
+
* @returns Mapped, flattened, and narrowed array of items
|
|
112
92
|
*
|
|
113
93
|
* @example
|
|
114
94
|
* ```typescript
|
|
115
|
-
*
|
|
116
|
-
* [{id: 1,
|
|
117
|
-
* item =>
|
|
118
|
-
* '
|
|
119
|
-
*
|
|
120
|
-
* ); // => [{label: 'Bob'}]
|
|
95
|
+
* flatMapAndFilter(
|
|
96
|
+
* [{id: 1, values: [1, 'a']}, {id: 2, values: ['b', 2]}],
|
|
97
|
+
* item => item.values,
|
|
98
|
+
* (value): value is number => typeof value === 'number',
|
|
99
|
+
* ); // => [1, 2]
|
|
121
100
|
* ```
|
|
122
101
|
*/
|
|
123
|
-
export declare function
|
|
102
|
+
export declare function flatMapAndFilter<Item, Mapped, Narrowed extends ArrayFlattened<Mapped>>(array: Item[], mapper: (item: Item, index: number, array: Item[]) => Mapped, filter: (item: ArrayFlattened<Mapped>, index: number, array: Item[]) => item is Narrowed): Narrowed[];
|
|
124
103
|
/**
|
|
125
|
-
* Get a mapped and filtered array of items
|
|
104
|
+
* Get a mapped, flattened, and filtered array of items
|
|
126
105
|
*
|
|
127
|
-
* _Available as `
|
|
106
|
+
* _Available as `flatMapAndFilter`, `mapAndFilter.flat`, and `reverseSelect.flat`_
|
|
128
107
|
*
|
|
129
|
-
* @param array Array to
|
|
130
|
-
* @param
|
|
131
|
-
* @param
|
|
132
|
-
* @
|
|
133
|
-
* @returns Mapped and filtered array of items
|
|
108
|
+
* @param array Array to select from
|
|
109
|
+
* @param mapper Callback to map and flatten the items
|
|
110
|
+
* @param filter Callback to filter the flattened items
|
|
111
|
+
* @returns Mapped, flattened, and filtered array of items
|
|
134
112
|
*
|
|
135
113
|
* @example
|
|
136
114
|
* ```typescript
|
|
137
|
-
*
|
|
138
|
-
* [{
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* ); // => [{tag: 'b'}]
|
|
115
|
+
* flatMapAndFilter(
|
|
116
|
+
* [{id: 1, tags: ['a', 'b']}, {id: 2, tags: ['c']}, {id: 3, tags: ['b', 'd']}],
|
|
117
|
+
* item => item.tags,
|
|
118
|
+
* tag => tag !== 'b',
|
|
119
|
+
* ); // => ['a', 'c', 'd']
|
|
143
120
|
* ```
|
|
144
121
|
*/
|
|
145
|
-
export declare function
|
|
122
|
+
export declare function flatMapAndFilter<Item, Mapped>(array: Item[], mapper: (item: Item, index: number, array: Item[]) => Mapped, filter: (item: ArrayFlattened<Mapped>, index: number, array: Item[]) => boolean): Array<ArrayFlattened<Mapped>>;
|
|
146
123
|
/**
|
|
147
|
-
* Get a mapped and
|
|
124
|
+
* Get a mapped and narrowed array of items
|
|
148
125
|
*
|
|
149
|
-
* _Available as `reverseSelect`
|
|
126
|
+
* _Available as `mapAndFilter`, `reverseSelect`, `filterAndMap.reverse`, and `select.reverse`_
|
|
150
127
|
*
|
|
151
|
-
* @param array Array to
|
|
152
|
-
* @param
|
|
153
|
-
* @param filter
|
|
154
|
-
* @returns Mapped and
|
|
128
|
+
* @param array Array to select from
|
|
129
|
+
* @param mapper Callback to map the items
|
|
130
|
+
* @param filter Type guard to filter and narrow down the mapped items
|
|
131
|
+
* @returns Mapped and narrowed array of items
|
|
155
132
|
*
|
|
156
133
|
* @example
|
|
157
134
|
* ```typescript
|
|
158
|
-
*
|
|
159
|
-
* [{id: 1, name: 'Alice'}, {id: 2
|
|
135
|
+
* mapAndFilter(
|
|
136
|
+
* [{id: 1, name: 'Alice'}, {id: 2}, {id: 3, name: 'Charlie'}],
|
|
160
137
|
* item => item.name,
|
|
161
|
-
*
|
|
162
|
-
* ); // => ['
|
|
138
|
+
* (name): name is string => name != null,
|
|
139
|
+
* ); // => ['Alice', 'Charlie']
|
|
163
140
|
* ```
|
|
164
141
|
*/
|
|
165
|
-
export declare function mapAndFilter<Item,
|
|
142
|
+
export declare function mapAndFilter<Item, Mapped, Narrowed extends Mapped>(array: Item[], mapper: (item: Item, index: number, array: Item[]) => Mapped, filter: (item: Mapped, index: number, array: Item[]) => item is Narrowed): Narrowed[];
|
|
166
143
|
/**
|
|
167
144
|
* Get a mapped and filtered array of items
|
|
168
145
|
*
|
|
169
|
-
* _Available as `reverseSelect`
|
|
146
|
+
* _Available as `mapAndFilter`, `reverseSelect`, `filterAndMap.reverse`, and `select.reverse`_
|
|
170
147
|
*
|
|
171
|
-
* @param array Array to
|
|
172
|
-
* @param
|
|
173
|
-
* @param
|
|
148
|
+
* @param array Array to select from
|
|
149
|
+
* @param mapper Callback to map the items
|
|
150
|
+
* @param filter Callback to filter the mapped items
|
|
174
151
|
* @returns Mapped and filtered array of items
|
|
175
152
|
*
|
|
176
153
|
* @example
|
|
177
154
|
* ```typescript
|
|
178
|
-
*
|
|
179
|
-
* [1, 2,
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* ); // => [
|
|
155
|
+
* mapAndFilter(
|
|
156
|
+
* [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
|
|
157
|
+
* item => item.name,
|
|
158
|
+
* name => name.length > 3,
|
|
159
|
+
* ); // => ['Alice', 'Charlie']
|
|
183
160
|
* ```
|
|
184
161
|
*/
|
|
185
|
-
export declare function mapAndFilter<Item,
|
|
162
|
+
export declare function mapAndFilter<Item, Mapped>(array: Item[], mapper: (item: Item, index: number, array: Item[]) => Mapped, filter: (item: Mapped, index: number, array: Item[]) => boolean): Mapped[];
|
|
186
163
|
export declare namespace filterAndMap {
|
|
164
|
+
export var flat: typeof filterAndFlatMap;
|
|
187
165
|
export var reverse: typeof mapAndFilter;
|
|
188
166
|
}
|
|
167
|
+
export declare namespace mapAndFilter {
|
|
168
|
+
export var flat: typeof flatMapAndFilter;
|
|
169
|
+
}
|
|
189
170
|
//#endregion
|
|
190
171
|
export { filterAndMap as select, mapAndFilter as reverseSelect };
|
package/dist/array/select.mjs
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import "../models/array/array.find.model.mjs";
|
|
2
2
|
import { findValues } from "../internal/array/find.mjs";
|
|
3
3
|
//#region src/array/select.ts
|
|
4
|
-
function
|
|
5
|
-
return selectValues(array,
|
|
4
|
+
function filterAndFlatMap(array, filter, mapper) {
|
|
5
|
+
return selectValues(array, filter, mapper, true, false);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
return findValues("all", array, parameters, {
|
|
10
|
-
callback: mapper,
|
|
11
|
-
reverse: true
|
|
12
|
-
}).matched;
|
|
7
|
+
function filterAndMap(array, filter, mapper) {
|
|
8
|
+
return selectValues(array, filter, mapper, false, false);
|
|
13
9
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
return findValues("all", array, parameters, {
|
|
17
|
-
callback: mapper,
|
|
18
|
-
reverse: false
|
|
19
|
-
}).matched;
|
|
10
|
+
function flatMapAndFilter(array, mapper, filter) {
|
|
11
|
+
return selectValues(array, filter, mapper, true, true);
|
|
20
12
|
}
|
|
13
|
+
function mapAndFilter(array, mapper, filter) {
|
|
14
|
+
return selectValues(array, filter, mapper, false, true);
|
|
15
|
+
}
|
|
16
|
+
function selectValues(array, filter, mapper, flat, reverse) {
|
|
17
|
+
return findValues("all", array, [filter], { map: {
|
|
18
|
+
flat,
|
|
19
|
+
reverse,
|
|
20
|
+
callback: mapper
|
|
21
|
+
} }).matched;
|
|
22
|
+
}
|
|
23
|
+
filterAndMap.flat = filterAndFlatMap;
|
|
21
24
|
filterAndMap.reverse = mapAndFilter;
|
|
25
|
+
mapAndFilter.flat = flatMapAndFilter;
|
|
22
26
|
//#endregion
|
|
23
|
-
export { filterAndMap, filterAndMap as select, mapAndFilter, mapAndFilter as reverseSelect };
|
|
27
|
+
export { filterAndFlatMap, filterAndMap, filterAndMap as select, flatMapAndFilter, mapAndFilter, mapAndFilter as reverseSelect };
|
package/dist/array/single.d.mts
CHANGED
|
@@ -40,6 +40,28 @@ export declare function single<Item, Callback extends (item: Item, index: number
|
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
42
|
export declare function single<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Get the _only_ narrowed item matching the filter
|
|
45
|
+
*
|
|
46
|
+
* _Throws an error if multiple items match the filter_
|
|
47
|
+
*
|
|
48
|
+
* @param array Array to search in
|
|
49
|
+
* @param filter Type guard to match and narrow down the items
|
|
50
|
+
* @returns Only narrowed item that matches the filter, or `undefined` if no match is found
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* single(
|
|
55
|
+
* [{id: 1, value: 10}, {id: 2, value: 'a'}, {id: 3, value: 'b'}],
|
|
56
|
+
* (item): item is {id: number; value: number} => typeof item.value === 'number',
|
|
57
|
+
* ); // => {id: 1, value: 10}
|
|
58
|
+
* single(
|
|
59
|
+
* [{id: 1, value: 10}, {id: 2, value: 'a'}, {id: 3, value: 'b'}],
|
|
60
|
+
* (item): item is {id: number; value: string} => typeof item.value === 'string',
|
|
61
|
+
* ); // => throws an error as there are two matches
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function single<Item, Narrowed extends Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => item is Narrowed): Narrowed | undefined;
|
|
43
65
|
/**
|
|
44
66
|
* Get the _only_ item matching the filter
|
|
45
67
|
*
|
package/dist/array/single.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import "../models/array/array.find.model.mjs";
|
|
|
2
2
|
import { findValues } from "../internal/array/find.mjs";
|
|
3
3
|
//#region src/array/single.ts
|
|
4
4
|
function single(array, ...parameters) {
|
|
5
|
-
const { matched } = findValues("all", array, parameters);
|
|
5
|
+
const { matched } = findValues("all", array, parameters, { limit: 2 });
|
|
6
6
|
if (matched.length > 1) throw new Error(SINGLE_MESSAGE);
|
|
7
7
|
return matched[0];
|
|
8
8
|
}
|
package/dist/array/slice.mjs
CHANGED
|
@@ -8,34 +8,47 @@ function extract(type, array, first, second) {
|
|
|
8
8
|
if (!Array.isArray(array)) return [];
|
|
9
9
|
const { length } = array;
|
|
10
10
|
if (length === 0) return [];
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
const callbacks = second == null ? getArrayCallbacks(first) : getArrayCallbacks(void 0, void 0, first);
|
|
11
|
+
const take = type === ARRAY_SLICE_TAKE;
|
|
12
|
+
return typeof first === "number" ? getSlice(array, length, first, take) : extractSlice(array, length, first, second, take);
|
|
13
|
+
}
|
|
14
|
+
function extractSlice(array, length, first, second, take) {
|
|
15
|
+
const callbacks = getCallbacks(first, second);
|
|
16
|
+
if (callbacks?.bool == null && callbacks?.value == null) return take ? array.slice() : [];
|
|
19
17
|
const isBoolean = callbacks?.bool != null;
|
|
20
|
-
if (callbacks?.bool == null && callbacks?.value == null) return isTake ? array.slice() : [];
|
|
21
18
|
const extracted = [];
|
|
22
19
|
let push = false;
|
|
23
20
|
for (let index = 0; index < length; index += 1) {
|
|
24
21
|
const item = array[index];
|
|
25
|
-
const matches =
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
const matches = isMatch(item, index, array, callbacks, second, isBoolean);
|
|
23
|
+
const reached = isBoolean ? !matches : matches;
|
|
24
|
+
if (take && reached) break;
|
|
25
|
+
if (push || take) {
|
|
28
26
|
extracted.push(item);
|
|
29
27
|
continue;
|
|
30
28
|
}
|
|
31
|
-
if (
|
|
32
|
-
else if (isBoolean ? !matches : matches) {
|
|
29
|
+
if (reached) {
|
|
33
30
|
push = true;
|
|
34
31
|
if (isBoolean) extracted.push(item);
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
34
|
return extracted;
|
|
38
35
|
}
|
|
36
|
+
function getCallbacks(first, second) {
|
|
37
|
+
let callbacks;
|
|
38
|
+
if (second == null) callbacks = getArrayCallbacks(first);
|
|
39
|
+
else callbacks = getArrayCallbacks(void 0, void 0, first);
|
|
40
|
+
return callbacks;
|
|
41
|
+
}
|
|
42
|
+
function getSlice(array, length, amount, take) {
|
|
43
|
+
if (Math.abs(amount) >= length) return take ? array.slice() : [];
|
|
44
|
+
if (amount === 0) return take ? [] : array.slice();
|
|
45
|
+
if (take) return amount >= 0 ? array.slice(0, amount) : array.slice(length + amount);
|
|
46
|
+
return amount >= 0 ? array.slice(amount) : array.slice(0, length + amount);
|
|
47
|
+
}
|
|
48
|
+
function isMatch(item, index, array, callbacks, second, isBoolean) {
|
|
49
|
+
if (isBoolean) return callbacks.bool(item, index, array);
|
|
50
|
+
return Object.is(callbacks.value(item, index, array), second);
|
|
51
|
+
}
|
|
39
52
|
function slice(array, first, second) {
|
|
40
53
|
if (!Array.isArray(array) || array.length === 0) return [];
|
|
41
54
|
const firstIsNumber = typeof first === "number";
|
package/dist/array/sort.d.mts
CHANGED
|
@@ -13,6 +13,11 @@ declare function Sorter(this: any, sorters: SortHandler[]): void;
|
|
|
13
13
|
* @param sorters Sorters to use to determine sorting
|
|
14
14
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
15
15
|
* @returns Index for item
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* getSortedIndex([1, 3, 2], 2, [{key: 'value', direction: 'asc'}]); // => 1
|
|
20
|
+
* ```
|
|
16
21
|
*/
|
|
17
22
|
export declare function getSortedIndex<Item>(array: Item[], item: Item, sorters: Array<ArraySorter<Item>>, descending?: boolean): number;
|
|
18
23
|
/**
|
|
@@ -27,6 +32,11 @@ export declare function getSortedIndex<Item>(array: Item[], item: Item, sorters:
|
|
|
27
32
|
* @param sorter Sorter to use to determine sorting
|
|
28
33
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
29
34
|
* @returns Index for item
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* getSortedIndex([1, 3, 2], 2, {key: 'value', direction: 'asc'}); // => 1
|
|
39
|
+
* ```
|
|
30
40
|
*/
|
|
31
41
|
export declare function getSortedIndex<Item>(array: Item[], item: Item, sorter: ArraySorter<Item>, descending?: boolean): number;
|
|
32
42
|
/**
|
|
@@ -40,6 +50,11 @@ export declare function getSortedIndex<Item>(array: Item[], item: Item, sorter:
|
|
|
40
50
|
* @param item Item to get the index for
|
|
41
51
|
* @param descending Sorted in descending order? _(defaults to `false`)_
|
|
42
52
|
* @returns Index for item
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* getSortedIndex([1, 3, 2], 2); // => 1
|
|
57
|
+
* ```
|
|
43
58
|
*/
|
|
44
59
|
export declare function getSortedIndex<Item>(array: Item[], item: Item, descending?: boolean): number;
|
|
45
60
|
/**
|
|
@@ -80,6 +95,12 @@ export declare function initializeSorter<Item>(descending?: boolean): Sorter<Ite
|
|
|
80
95
|
* @param sorters Sorters to determine sorting
|
|
81
96
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
82
97
|
* @returns `true` if sorted, otherwise `false`
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* isSorted([1, 2, 3], [{key: 'value', direction: 'asc'}]); // => true
|
|
102
|
+
* isSorted([3, 2, 1], [{key: 'value', direction: 'asc'}]); // => false
|
|
103
|
+
* ```
|
|
83
104
|
*/
|
|
84
105
|
export declare function isSorted<Item>(array: Item[], sorters: Array<ArraySorter<Item>>, descending?: boolean): boolean;
|
|
85
106
|
/**
|
|
@@ -91,6 +112,12 @@ export declare function isSorted<Item>(array: Item[], sorters: Array<ArraySorter
|
|
|
91
112
|
* @param sorter Sorter to determine sorting
|
|
92
113
|
* @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
93
114
|
* @returns `true` if sorted, otherwise `false`
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* isSorted([1, 2, 3], {key: 'value', direction: 'asc'}); // => true
|
|
119
|
+
* isSorted([3, 2, 1], {key: 'value', direction: 'asc'}); // => false
|
|
120
|
+
* ```
|
|
94
121
|
*/
|
|
95
122
|
export declare function isSorted<Item>(array: Item[], sorter: ArraySorter<Item>, descending?: boolean): boolean;
|
|
96
123
|
/**
|
|
@@ -101,6 +128,12 @@ export declare function isSorted<Item>(array: Item[], sorter: ArraySorter<Item>,
|
|
|
101
128
|
* @param array Array to check
|
|
102
129
|
* @param descending Sorted in descending order? _(defaults to `false`)_
|
|
103
130
|
* @returns `true` if sorted, otherwise `false`
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* isSorted([1, 2, 3]); // => true
|
|
135
|
+
* isSorted([3, 2, 1]); // => false
|
|
136
|
+
* ```
|
|
104
137
|
*/
|
|
105
138
|
export declare function isSorted<Item>(array: Item[], descending?: boolean): boolean;
|
|
106
139
|
/**
|
|
@@ -127,6 +160,14 @@ export declare function sort<Item>(array: Item[], comparator: (first: Item, seco
|
|
|
127
160
|
* @param sorters Sorters to use for sorting
|
|
128
161
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
129
162
|
* @returns Sorted array
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* sort(
|
|
167
|
+
* [{id: 3}, {id: 1}, {id: 2}],
|
|
168
|
+
* [{key: 'id', direction: 'asc'}],
|
|
169
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
170
|
+
* ```
|
|
130
171
|
*/
|
|
131
172
|
export declare function sort<Item>(array: Item[], sorters: Array<ArraySorter<Item>>, descending?: boolean): Item[];
|
|
132
173
|
/**
|
|
@@ -136,6 +177,14 @@ export declare function sort<Item>(array: Item[], sorters: Array<ArraySorter<Ite
|
|
|
136
177
|
* @param sorter Sorter to use for sorting
|
|
137
178
|
* @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
|
|
138
179
|
* @returns Sorted array
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* sort(
|
|
184
|
+
* [{id: 3}, {id: 1}, {id: 2}],
|
|
185
|
+
* {key: 'id', direction: 'asc'},
|
|
186
|
+
* ); // => [{id: 1}, {id: 2}, {id: 3}]
|
|
187
|
+
* ```
|
|
139
188
|
*/
|
|
140
189
|
export declare function sort<Item>(array: Item[], sorter: ArraySorter<Item>, descending?: boolean): Item[];
|
|
141
190
|
/**
|
|
@@ -144,6 +193,11 @@ export declare function sort<Item>(array: Item[], sorter: ArraySorter<Item>, des
|
|
|
144
193
|
* @param array Array to sort
|
|
145
194
|
* @param descending Sort in descending order? _(defaults to `false`)_
|
|
146
195
|
* @returns Sorted array
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* sort([3, 1, 2]); // => [1, 2, 3]
|
|
200
|
+
* ```
|
|
147
201
|
*/
|
|
148
202
|
export declare function sort<Item>(array: Item[], descending?: boolean): Item[];
|
|
149
203
|
export declare namespace sort {
|
package/dist/array/sort.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isPlainObject } from "../internal/is.mjs";
|
|
2
2
|
import { floor, round } from "../internal/math/misc.mjs";
|
|
3
|
+
import { min } from "../internal/math/aggregate.mjs";
|
|
3
4
|
import { compare } from "../internal/value/compare.mjs";
|
|
4
5
|
import { SORTER_SYMBOL, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, arrayModifiers } from "../models/array/array.sort.model.mjs";
|
|
5
6
|
//#region src/array/sort.ts
|
|
@@ -106,7 +107,7 @@ function isSortedArray(array) {
|
|
|
106
107
|
let offset = 0;
|
|
107
108
|
if (length >= 100) {
|
|
108
109
|
offset = round(length / 10);
|
|
109
|
-
offset = offset
|
|
110
|
+
offset = min([offset, 100]);
|
|
110
111
|
for (let index = 0; index < offset; index += 1) {
|
|
111
112
|
const [firstItem, firstOffset] = [array[index], array[index + 1]];
|
|
112
113
|
const [secondItem, secondOffset] = [array[length - index - 2], array[length - index - 1]];
|
package/dist/color/misc/get.mjs
CHANGED
|
@@ -5,7 +5,9 @@ import { getColorFromState, getColorState } from "./state.mjs";
|
|
|
5
5
|
import { color } from "../instance.mjs";
|
|
6
6
|
//#region src/color/misc/get.ts
|
|
7
7
|
function getClampedValue(value, minimum, maximum, rounding) {
|
|
8
|
-
|
|
8
|
+
if (typeof value !== "number" || Number.isNaN(value)) return minimum;
|
|
9
|
+
const actual = rounding ?? false ? round(value) : value;
|
|
10
|
+
return clamp(actual, minimum, maximum);
|
|
9
11
|
}
|
|
10
12
|
function getForegroundColor(value, hex) {
|
|
11
13
|
const { blue, green, red } = getColorFromState(getColorState(value), COLOR_TYPE.rgb);
|
|
@@ -11,6 +11,14 @@ export declare function getHexaColor(this: InternalColor): string;
|
|
|
11
11
|
* @param value Value to normalize
|
|
12
12
|
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
13
13
|
* @returns Normalized hex color
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* getNormalizedHex('#abc'); // => '#aabbcc'
|
|
18
|
+
* getNormalizedHex('#abc', true); // => '#aabbccff'
|
|
19
|
+
* getNormalizedHex(123); // => '#000000'
|
|
20
|
+
* getNormalizedHex(123, true); // => '#000000ff'
|
|
21
|
+
* ```
|
|
14
22
|
*/
|
|
15
23
|
export declare function getNormalizedHex(value: unknown, alpha?: boolean): string;
|
|
16
24
|
/**
|
package/dist/color/space/hex.mjs
CHANGED
|
@@ -44,9 +44,17 @@ function getHexaColor() {
|
|
|
44
44
|
* @param value Value to normalize
|
|
45
45
|
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
46
46
|
* @returns Normalized hex color
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* getNormalizedHex('#abc'); // => '#aabbcc'
|
|
51
|
+
* getNormalizedHex('#abc', true); // => '#aabbccff'
|
|
52
|
+
* getNormalizedHex(123); // => '#000000'
|
|
53
|
+
* getNormalizedHex(123, true); // => '#000000ff'
|
|
54
|
+
* ```
|
|
47
55
|
*/
|
|
48
56
|
function getNormalizedHex(value, alpha) {
|
|
49
|
-
const includeAlpha = alpha
|
|
57
|
+
const includeAlpha = alpha === true;
|
|
50
58
|
if (!isHexColor(value)) return `${COLOR_DEFAULTS.hexBlack}${includeAlpha ? COLOR_ALPHA.fullHexLong : ""}`;
|
|
51
59
|
const normalized = value.replace(COLOR_EXPRESSION.prefix, "");
|
|
52
60
|
if (normalized.length < COLOR_LENGTHS.hexLong) {
|
|
@@ -20,7 +20,7 @@ declare function asyncRetry<Callback extends GenericAsyncCallback>(callback: Cal
|
|
|
20
20
|
* @param options Retry options
|
|
21
21
|
* @returns Callback result
|
|
22
22
|
*/
|
|
23
|
-
declare function asyncRetry<Callback extends
|
|
23
|
+
declare function asyncRetry<Callback extends GenericAsyncCallback>(callback: Callback, options?: RetryOptions): Promise<ReturnType<Callback>>;
|
|
24
24
|
/**
|
|
25
25
|
* Retry a callback a specified number of times
|
|
26
26
|
*
|
package/dist/function/retry.mjs
CHANGED
|
@@ -18,8 +18,11 @@ async function asyncRetry(callback, options) {
|
|
|
18
18
|
if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
19
19
|
async function handle() {
|
|
20
20
|
const result = await asyncAttempt(async () => callback());
|
|
21
|
-
if (result.ok)
|
|
22
|
-
|
|
21
|
+
if (result.ok) {
|
|
22
|
+
resolver(result.value);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (attempts >= times || !when(result.error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, result.error));
|
|
23
26
|
else {
|
|
24
27
|
attempts += 1;
|
|
25
28
|
limiter.run();
|