@oscarpalmer/atoms 0.184.2 → 0.186.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/difference.d.mts +29 -0
- package/dist/array/exists.d.mts +35 -0
- package/dist/array/filter.d.mts +72 -2
- package/dist/array/find.d.mts +70 -0
- package/dist/array/first.d.mts +77 -2
- package/dist/array/flatten.d.mts +6 -0
- package/dist/array/flatten.mjs +6 -0
- package/dist/array/from.d.mts +36 -0
- package/dist/array/get.d.mts +21 -13
- package/dist/array/group-by.d.mts +142 -0
- package/dist/array/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/insert.d.mts +16 -0
- package/dist/array/intersection.d.mts +29 -0
- package/dist/array/last.d.mts +75 -2
- package/dist/array/{position.d.mts → match.d.mts} +168 -36
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.d.mts +78 -8
- package/dist/array/move.mjs +11 -1
- package/dist/array/partition.d.mts +35 -0
- package/dist/array/push.d.mts +8 -0
- package/dist/array/push.mjs +8 -0
- package/dist/array/reverse.d.mts +1 -0
- package/dist/array/reverse.mjs +1 -0
- package/dist/array/select.d.mts +94 -8
- package/dist/array/single.d.mts +29 -0
- package/dist/array/slice.d.mts +106 -16
- package/dist/array/sort.d.mts +30 -4
- package/dist/array/sort.mjs +1 -1
- package/dist/array/splice.d.mts +48 -0
- package/dist/array/splice.mjs +2 -1
- package/dist/array/swap.d.mts +113 -8
- package/dist/array/swap.mjs +2 -1
- package/dist/array/to-map.d.mts +124 -0
- package/dist/array/to-record.d.mts +124 -0
- package/dist/array/to-set.d.mts +24 -0
- package/dist/array/toggle.d.mts +38 -3
- package/dist/array/union.d.mts +29 -0
- package/dist/array/unique.d.mts +24 -0
- package/dist/array/update.d.mts +38 -3
- package/dist/beacon.d.mts +12 -0
- package/dist/beacon.mjs +9 -0
- package/dist/color/instance.d.mts +8 -0
- package/dist/color/instance.mjs +3 -0
- package/dist/color/models.d.mts +30 -0
- package/dist/function/assert.d.mts +29 -8
- package/dist/function/assert.mjs +29 -8
- package/dist/function/memoize.d.mts +3 -0
- package/dist/function/memoize.mjs +3 -0
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +3 -0
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +2158 -288
- package/dist/index.mjs +294 -181
- package/dist/internal/array/chunk.d.mts +6 -0
- package/dist/internal/array/chunk.mjs +6 -0
- package/dist/internal/array/compact.d.mts +12 -0
- package/dist/internal/array/index-of.d.mts +70 -0
- package/dist/internal/math/aggregate.d.mts +29 -0
- package/dist/internal/value/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/get.d.mts +27 -5
- package/dist/internal/value/has.d.mts +7 -7
- package/dist/internal/value/has.mjs +1 -1
- package/dist/internal/value/misc.d.mts +2 -2
- package/dist/internal/value/misc.mjs +10 -4
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +11 -0
- package/dist/models.d.mts +14 -1
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.d.mts +0 -6
- package/dist/promise/models.d.mts +36 -0
- package/dist/promise/models.mjs +6 -0
- package/dist/queue.d.mts +13 -1
- package/dist/queue.mjs +9 -0
- package/dist/result/index.d.mts +0 -8
- package/dist/result/index.mjs +0 -8
- package/dist/result/match.d.mts +4 -4
- package/dist/result/work/flow.d.mts +12 -36
- package/dist/result/work/pipe.d.mts +11 -33
- package/dist/sized/set.d.mts +3 -2
- package/dist/sized/set.mjs +3 -2
- package/dist/value/collection.d.mts +1 -1
- package/dist/value/handle.mjs +1 -1
- package/dist/value/merge.d.mts +28 -25
- package/dist/value/merge.mjs +29 -18
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +10 -1
- package/dist/value/unsmush.d.mts +2 -3
- package/package.json +5 -5
- package/src/array/difference.ts +33 -0
- package/src/array/exists.ts +35 -0
- package/src/array/filter.ts +72 -2
- package/src/array/find.ts +70 -0
- package/src/array/first.ts +77 -3
- package/src/array/flatten.ts +6 -0
- package/src/array/from.ts +40 -0
- package/src/array/get.ts +21 -15
- package/src/array/group-by.ts +142 -0
- package/src/array/index.ts +1 -1
- package/src/array/insert.ts +16 -2
- package/src/array/intersection.ts +33 -0
- package/src/array/last.ts +75 -2
- package/src/array/{position.ts → match.ts} +197 -65
- package/src/array/move.ts +87 -13
- package/src/array/partition.ts +35 -0
- package/src/array/push.ts +8 -2
- package/src/array/reverse.ts +5 -0
- package/src/array/select.ts +96 -13
- package/src/array/single.ts +29 -0
- package/src/array/slice.ts +114 -24
- package/src/array/sort.ts +30 -4
- package/src/array/splice.ts +52 -4
- package/src/array/swap.ts +122 -13
- package/src/array/to-map.ts +124 -0
- package/src/array/to-record.ts +124 -0
- package/src/array/to-set.ts +24 -0
- package/src/array/toggle.ts +42 -3
- package/src/array/union.ts +33 -0
- package/src/array/unique.ts +24 -0
- package/src/array/update.ts +38 -3
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +9 -1
- package/src/color/models.ts +30 -0
- package/src/function/assert.ts +66 -7
- package/src/function/memoize.ts +3 -0
- package/src/function/once.ts +5 -1
- package/src/function/retry.ts +3 -0
- package/src/internal/array/chunk.ts +6 -0
- package/src/internal/array/compact.ts +12 -0
- package/src/internal/array/index-of.ts +70 -0
- package/src/internal/math/aggregate.ts +29 -0
- package/src/internal/string.ts +0 -2
- package/src/internal/value/compare.ts +2 -1
- package/src/internal/value/equal.ts +5 -0
- package/src/internal/value/get.ts +27 -5
- package/src/internal/value/has.ts +10 -10
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/models.ts +18 -0
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +13 -1
- package/src/result/index.ts +0 -8
- package/src/result/match.ts +4 -4
- package/src/result/work/flow.ts +12 -36
- package/src/result/work/pipe.ts +11 -33
- package/src/sized/set.ts +4 -3
- package/src/value/collection.ts +1 -1
- package/src/value/merge.ts +88 -66
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +10 -1
- package/src/value/unsmush.ts +2 -8
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
//#region src/internal/array/chunk.d.ts
|
|
2
2
|
/**
|
|
3
3
|
* Chunk an array into smaller arrays
|
|
4
|
+
*
|
|
4
5
|
* @param array Array to chunk
|
|
5
6
|
* @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
|
|
6
7
|
* @returns Array of arrays
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
|
|
12
|
+
* ```
|
|
7
13
|
*/
|
|
8
14
|
declare function chunk<Item>(array: Item[], size?: number): Item[][];
|
|
9
15
|
//#endregion
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
//#region src/internal/array/chunk.ts
|
|
2
2
|
/**
|
|
3
3
|
* Chunk an array into smaller arrays
|
|
4
|
+
*
|
|
4
5
|
* @param array Array to chunk
|
|
5
6
|
* @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
|
|
6
7
|
* @returns Array of arrays
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
|
|
12
|
+
* ```
|
|
7
13
|
*/
|
|
8
14
|
function chunk(array, size) {
|
|
9
15
|
if (!Array.isArray(array)) return [];
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
//#region src/internal/array/compact.d.ts
|
|
2
2
|
/**
|
|
3
3
|
* Compact an array _(removing all false-y values)_
|
|
4
|
+
*
|
|
4
5
|
* @param array Array to compact
|
|
5
6
|
* @param strict True to remove all false-y values
|
|
6
7
|
* @returns Compacted array
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
|
|
12
|
+
* ```
|
|
7
13
|
*/
|
|
8
14
|
declare function compact<Item>(array: Item[], strict: true): Exclude<Item, 0 | '' | false | null | undefined>[];
|
|
9
15
|
/**
|
|
10
16
|
* Compact an array _(removing all `null` and `undefined` values)_
|
|
17
|
+
*
|
|
11
18
|
* @param array Array to compact
|
|
12
19
|
* @returns Compacted array
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
|
|
24
|
+
* ```
|
|
13
25
|
*/
|
|
14
26
|
declare function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
|
|
15
27
|
//#endregion
|
|
@@ -3,32 +3,67 @@ import { PlainObject } from "../../models.mjs";
|
|
|
3
3
|
//#region src/internal/array/index-of.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Get the index of the first matching item by callback
|
|
6
|
+
*
|
|
6
7
|
* @param array Array to search in
|
|
7
8
|
* @param callback Callback to get an item's value
|
|
8
9
|
* @param value Value to match against
|
|
9
10
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* indexOf(
|
|
15
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
16
|
+
* item => item.id,
|
|
17
|
+
* 2,
|
|
18
|
+
* ); // => 1
|
|
19
|
+
* ```
|
|
10
20
|
*/
|
|
11
21
|
declare function indexOf<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
|
|
12
22
|
/**
|
|
13
23
|
* Get the index of the first matching item by key
|
|
24
|
+
*
|
|
14
25
|
* @param array Array to search in
|
|
15
26
|
* @param key Key to match items by
|
|
16
27
|
* @param value Value to match against
|
|
17
28
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* indexOf(
|
|
33
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
34
|
+
* 'id',
|
|
35
|
+
* 2,
|
|
36
|
+
* ); // => 1
|
|
37
|
+
* ```
|
|
18
38
|
*/
|
|
19
39
|
declare function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): number;
|
|
20
40
|
/**
|
|
21
41
|
* Get the index of the first item matching the filter
|
|
42
|
+
*
|
|
22
43
|
* @param array Array to search in
|
|
23
44
|
* @param filter Filter callback to match items
|
|
24
45
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* indexOf(
|
|
50
|
+
* [{id: 1}, {id: 2}, {id: 3}],
|
|
51
|
+
* item => item.id === 2,
|
|
52
|
+
* ); // => 1
|
|
53
|
+
* ```
|
|
25
54
|
*/
|
|
26
55
|
declare function indexOf<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): number;
|
|
27
56
|
/**
|
|
28
57
|
* Get the index of the first item matching the given item
|
|
58
|
+
*
|
|
29
59
|
* @param array Array to search in
|
|
30
60
|
* @param item Item to match against
|
|
31
61
|
* @returns Index of the first matching item, or `-1` if no match is found
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* indexOf([1, 2, 3, 4, 5], 3); // => 2
|
|
66
|
+
* ```
|
|
32
67
|
*/
|
|
33
68
|
declare function indexOf<Item>(array: Item[], item: Item): number;
|
|
34
69
|
declare namespace indexOf {
|
|
@@ -38,38 +73,73 @@ declare namespace indexOf {
|
|
|
38
73
|
* Get the index of the last matching item by callback
|
|
39
74
|
*
|
|
40
75
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
76
|
+
*
|
|
41
77
|
* @param array Array to search in
|
|
42
78
|
* @param callback Callback to get an item's value
|
|
43
79
|
* @param value Value to match against
|
|
44
80
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* lastIndexOf(
|
|
85
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
86
|
+
* item => item.id,
|
|
87
|
+
* 2,
|
|
88
|
+
* ); // => 3
|
|
89
|
+
* ```
|
|
45
90
|
*/
|
|
46
91
|
declare function lastIndexOf<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
|
|
47
92
|
/**
|
|
48
93
|
* Get the index of the last matching item by key
|
|
49
94
|
*
|
|
50
95
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
96
|
+
*
|
|
51
97
|
* @param array Array to search in
|
|
52
98
|
* @param key Key to match items by
|
|
53
99
|
* @param value Value to match against
|
|
54
100
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* lastIndexOf(
|
|
105
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
106
|
+
* 'id',
|
|
107
|
+
* 2,
|
|
108
|
+
* ); // => 3
|
|
109
|
+
* ```
|
|
55
110
|
*/
|
|
56
111
|
declare function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): number;
|
|
57
112
|
/**
|
|
58
113
|
* Get the index of the last item matching the filter
|
|
59
114
|
*
|
|
60
115
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
116
|
+
*
|
|
61
117
|
* @param array Array to search in
|
|
62
118
|
* @param filter Filter callback to match items
|
|
63
119
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* lastIndexOf(
|
|
124
|
+
* [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
|
|
125
|
+
* item => item.id === 2,
|
|
126
|
+
* ); // => 3
|
|
127
|
+
* ```
|
|
64
128
|
*/
|
|
65
129
|
declare function lastIndexOf<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): number;
|
|
66
130
|
/**
|
|
67
131
|
* Get the index of the last item matching the given item
|
|
68
132
|
*
|
|
69
133
|
* Available as `lastIndexOf` and `indexOf.last`
|
|
134
|
+
*
|
|
70
135
|
* @param array Array to search in
|
|
71
136
|
* @param item Item to match against
|
|
72
137
|
* @returns Index of the last matching item, or `-1` if no match is found
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* lastIndexOf([1, 2, 3, 2, 1], 2); // => 3
|
|
142
|
+
* ```
|
|
73
143
|
*/
|
|
74
144
|
declare function lastIndexOf<Item>(array: Item[], item: Item): number;
|
|
75
145
|
//#endregion
|
|
@@ -11,6 +11,17 @@ declare function aggregate(type: AggregationType, array: unknown[], key: unknown
|
|
|
11
11
|
declare function getAggregateCallback(key: unknown): Function | undefined;
|
|
12
12
|
/**
|
|
13
13
|
* Get the maximum value from a list of items
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* max(
|
|
18
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}],
|
|
19
|
+
* item => item.value,
|
|
20
|
+
* ); // 20
|
|
21
|
+
*
|
|
22
|
+
* max([], item => item.value); // Number.NaN
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
14
25
|
* @param items List of items
|
|
15
26
|
* @param callback Callback to get an item's value
|
|
16
27
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
@@ -18,6 +29,17 @@ declare function getAggregateCallback(key: unknown): Function | undefined;
|
|
|
18
29
|
declare function max<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): number;
|
|
19
30
|
/**
|
|
20
31
|
* Get the maximum value from a list of items
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* max(
|
|
36
|
+
* [{id: 1, value: 10}, {id: 2, value: 20}],
|
|
37
|
+
* 'value',
|
|
38
|
+
* ); // 20
|
|
39
|
+
*
|
|
40
|
+
* max([], 'value'); // Number.NaN
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
21
43
|
* @param items List of items
|
|
22
44
|
* @param key Key to use for value
|
|
23
45
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
@@ -25,6 +47,13 @@ declare function max<Item>(items: Item[], callback: (item: Item, index: number,
|
|
|
25
47
|
declare function max<Item extends PlainObject, ItemKey extends keyof NumericalValues<Item>>(items: Item[], key: ItemKey): number;
|
|
26
48
|
/**
|
|
27
49
|
* Get the maximum value from a list of numbers
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* max([10, 20]); // 20
|
|
54
|
+
* max([]); // Number.NaN
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
28
57
|
* @param values List of numbers
|
|
29
58
|
* @returns Maximum value, or `NaN` if no maximum can be found
|
|
30
59
|
*/
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Constructor, GenericCallback } from "../../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/compare.d.ts
|
|
4
|
+
type Comparator<Value = any> = (first: Value, second: Value) => number;
|
|
4
5
|
/**
|
|
5
6
|
* Compare two values _(for sorting purposes)_
|
|
6
7
|
* @param first First value
|
|
@@ -31,6 +32,6 @@ declare function deregisterComparator<Instance>(constructor: Constructor<Instanc
|
|
|
31
32
|
* @param constructor Class constructor
|
|
32
33
|
* @param handler Method name or comparison function _(defaults to `compare`)_
|
|
33
34
|
*/
|
|
34
|
-
declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string |
|
|
35
|
+
declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | Comparator<Instance>): void;
|
|
35
36
|
//#endregion
|
|
36
37
|
export { compare, deregisterComparator, registerComparator };
|
|
@@ -18,6 +18,11 @@ type EqualOptions = {
|
|
|
18
18
|
*/
|
|
19
19
|
relaxedNullish?: boolean;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* An equalizer function for comparing values for equality, with predefined options
|
|
23
|
+
*
|
|
24
|
+
* Can be used to compare values, and register or deregister equality comparison handlers for specific classes
|
|
25
|
+
*/
|
|
21
26
|
type Equalizer = {
|
|
22
27
|
/**
|
|
23
28
|
* Are two strings equal?
|
|
@@ -1,18 +1,40 @@
|
|
|
1
|
-
import { NestedKeys, NestedValue, PlainObject
|
|
1
|
+
import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/get.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Get the value from an object using a known path
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const data = {foo: {bar: {baz: 42}}};
|
|
10
|
+
*
|
|
11
|
+
* getValue(data, 'foo'); // {bar: {baz: 42}}
|
|
12
|
+
* getValue(data, 'foo.bar'); // {baz: 42}
|
|
13
|
+
* getValue(data, 'foo.bar.baz'); // 42
|
|
14
|
+
* getValue(data, 'foo.nope'); // undefined
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
6
17
|
* @param data Object to get value from
|
|
7
|
-
* @param path Path for value
|
|
18
|
+
* @param path Path for value
|
|
8
19
|
* @returns Found value, or `undefined`
|
|
9
20
|
*/
|
|
10
|
-
declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data,
|
|
21
|
+
declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
|
|
11
22
|
/**
|
|
12
23
|
* Get the value from an object using an unknown path
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const data = {foo: {bar: {baz: 42}}};
|
|
28
|
+
*
|
|
29
|
+
* getValue(data, 'foo'); // {bar: {baz: 42}}
|
|
30
|
+
* getValue(data, 'foo.bar'); // {baz: 42}
|
|
31
|
+
* getValue(data, 'Foo.Bar.Baz', true); // 42
|
|
32
|
+
* getValue(data, 'foo.nope'); // undefined
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
13
35
|
* @param data Object to get value from
|
|
14
|
-
* @param path Path for value
|
|
15
|
-
* @param ignoreCase If `true`,
|
|
36
|
+
* @param path Path for value
|
|
37
|
+
* @param ignoreCase If `true`, path matching is case-insensitive
|
|
16
38
|
* @returns Found value, or `undefined`
|
|
17
39
|
*/
|
|
18
40
|
declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
|
|
@@ -6,7 +6,7 @@ import { Result } from "../../result/models.mjs";
|
|
|
6
6
|
* Check if a nested property is defined in an object
|
|
7
7
|
* @param data Object to check in
|
|
8
8
|
* @param path Path for property
|
|
9
|
-
* @
|
|
9
|
+
* @returns `true` if the property exists, `false` otherwise
|
|
10
10
|
*/
|
|
11
11
|
declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): boolean;
|
|
12
12
|
/**
|
|
@@ -14,7 +14,7 @@ declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data
|
|
|
14
14
|
* @param data Object to check in
|
|
15
15
|
* @param path Path for property
|
|
16
16
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
17
|
-
* @
|
|
17
|
+
* @returns `true` if the property exists, `false` otherwise
|
|
18
18
|
*/
|
|
19
19
|
declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
|
|
20
20
|
declare namespace hasValue {
|
|
@@ -27,9 +27,9 @@ declare namespace hasValue {
|
|
|
27
27
|
* @param data Object to check in
|
|
28
28
|
* @param path Path for property
|
|
29
29
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
30
|
-
* @
|
|
30
|
+
* @returns Result object
|
|
31
31
|
*/
|
|
32
|
-
declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>,
|
|
32
|
+
declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>, string>;
|
|
33
33
|
/**
|
|
34
34
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
35
35
|
*
|
|
@@ -37,8 +37,8 @@ declare function hasValueResult<Data extends PlainObject, Path extends NestedKey
|
|
|
37
37
|
* @param data Object to check in
|
|
38
38
|
* @param path Path for property
|
|
39
39
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
40
|
-
* @
|
|
40
|
+
* @returns Result object
|
|
41
41
|
*/
|
|
42
|
-
declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown,
|
|
42
|
+
declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
|
|
43
43
|
//#endregion
|
|
44
|
-
export { hasValue };
|
|
44
|
+
export { hasValue, hasValueResult };
|
|
@@ -2,9 +2,9 @@ import { Result } from "../../result/models.mjs";
|
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/misc.d.ts
|
|
4
4
|
declare function findKey(needle: string, haystack: object): string;
|
|
5
|
-
declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown,
|
|
5
|
+
declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown, string>;
|
|
6
6
|
declare function getPaths(path: string, lowercase: boolean): string | string[];
|
|
7
|
-
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown,
|
|
7
|
+
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown, string>;
|
|
8
8
|
declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
|
|
9
9
|
//#endregion
|
|
10
10
|
export { findKey, getNestedValue, getPaths, handleValue };
|
|
@@ -7,7 +7,8 @@ function findKey(needle, haystack) {
|
|
|
7
7
|
return index > -1 ? keys[index] : needle;
|
|
8
8
|
}
|
|
9
9
|
function getNestedValue(data, path, ignoreCase) {
|
|
10
|
-
if (typeof data !== "object" || data === null
|
|
10
|
+
if (typeof data !== "object" || data === null) return error(NESTED_MESSAGE_INPUT);
|
|
11
|
+
if (typeof path !== "string" || path.trim().length === 0) return error(NESTED_MESSAGE_PATH);
|
|
11
12
|
const shouldIgnoreCase = ignoreCase === true;
|
|
12
13
|
const paths = getPaths(path, shouldIgnoreCase);
|
|
13
14
|
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
@@ -27,15 +28,20 @@ function getPaths(path, lowercase) {
|
|
|
27
28
|
return normalized.replace(EXPRESSION_BRACKET, ".$1").replace(EXPRESSION_DOTS, "").split(".");
|
|
28
29
|
}
|
|
29
30
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
30
|
-
if (typeof data === "object" && data !== null
|
|
31
|
+
if (typeof data === "object" && data !== null) {
|
|
32
|
+
if (ignoreKey(path)) return error(NESTED_MESSAGE_UNSAFE);
|
|
31
33
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
32
|
-
if (get) return key in data ? ok(data[key]) : error(
|
|
34
|
+
if (get) return key in data ? ok(data[key]) : error(NESTED_MESSAGE_MISSING);
|
|
33
35
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
34
36
|
}
|
|
35
|
-
if (get) return error(
|
|
37
|
+
if (get) return error(NESTED_MESSAGE_MISSING);
|
|
36
38
|
}
|
|
37
39
|
const EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
38
40
|
const EXPRESSION_DOTS = /^\.|\.$/g;
|
|
39
41
|
const EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
42
|
+
const NESTED_MESSAGE_INPUT = "Expected data to be an object";
|
|
43
|
+
const NESTED_MESSAGE_MISSING = "Expected property to exist in object";
|
|
44
|
+
const NESTED_MESSAGE_PATH = "Expected path to be a string";
|
|
45
|
+
const NESTED_MESSAGE_UNSAFE = "Access to this property is not allowed";
|
|
40
46
|
//#endregion
|
|
41
47
|
export { findKey, getNestedValue, getPaths, handleValue };
|
package/dist/logger.d.mts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
//#region src/logger.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A logger that can be used to log messages to the console
|
|
4
|
+
*
|
|
5
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
6
|
+
*/
|
|
2
7
|
declare class Logger {
|
|
3
8
|
/**
|
|
4
9
|
* Log any number of values at the "debug" log level
|
|
@@ -47,8 +52,14 @@ declare class Logger {
|
|
|
47
52
|
*/
|
|
48
53
|
time(label: string): Time;
|
|
49
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* A named timer that can be used to log durations to the console
|
|
57
|
+
*/
|
|
50
58
|
declare class Time {
|
|
51
59
|
#private;
|
|
60
|
+
/**
|
|
61
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
62
|
+
*/
|
|
52
63
|
get active(): boolean;
|
|
53
64
|
/**
|
|
54
65
|
* Log the current duration of the timer _(ignored if logging is disabled)_
|
package/dist/logger.mjs
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
//#region src/logger.ts
|
|
3
|
+
/**
|
|
4
|
+
* A logger that can be used to log messages to the console
|
|
5
|
+
*
|
|
6
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
7
|
+
*/
|
|
3
8
|
var Logger = class {
|
|
4
9
|
/**
|
|
5
10
|
* Log any number of values at the "debug" log level
|
|
@@ -70,10 +75,16 @@ var Logger = class {
|
|
|
70
75
|
return new Time(label);
|
|
71
76
|
}
|
|
72
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* A named timer that can be used to log durations to the console
|
|
80
|
+
*/
|
|
73
81
|
var Time = class {
|
|
74
82
|
#logger;
|
|
75
83
|
#stopper;
|
|
76
84
|
#state;
|
|
85
|
+
/**
|
|
86
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
87
|
+
*/
|
|
77
88
|
get active() {
|
|
78
89
|
return this.#state.started && !this.#state.stopped && enabled;
|
|
79
90
|
}
|
package/dist/models.d.mts
CHANGED
|
@@ -143,5 +143,18 @@ type ToString<Value> = Value extends string | number ? `${Value}` : never;
|
|
|
143
143
|
* A union type of all typed arrays
|
|
144
144
|
*/
|
|
145
145
|
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
146
|
+
/**
|
|
147
|
+
* Converts a union type to an intersection type
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* type A = {a: string};
|
|
152
|
+
* type B = {b: number};
|
|
153
|
+
* type C = UnionToIntersection<A | B>; // {a: string} & {b: number}
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* Thanks, type-fest!
|
|
157
|
+
*/
|
|
158
|
+
type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
|
|
146
159
|
//#endregion
|
|
147
|
-
export { ArrayOrPlainObject, AsyncCancelableCallback, BuiltIns, CancelableCallback, Constructor, EventPosition, GenericAsyncCallback, GenericCallback, Key, KeyedValue, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NumericalKeys, NumericalValues, OnceAsyncCallback, OnceCallback, PlainObject, Primitive, RequiredKeys, Simplify, ToString, TypedArray };
|
|
160
|
+
export { ArrayOrPlainObject, AsyncCancelableCallback, BuiltIns, CancelableCallback, Constructor, EventPosition, GenericAsyncCallback, GenericCallback, Key, KeyedValue, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NumericalKeys, NumericalValues, OnceAsyncCallback, OnceCallback, PlainObject, Primitive, RequiredKeys, Simplify, ToString, TypedArray, UnionToIntersection };
|
package/dist/promise/helpers.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getNumberOrDefault } from "../internal/number.mjs";
|
|
2
1
|
import { error, ok } from "../result/misc.mjs";
|
|
2
|
+
import { getNumberOrDefault } from "../internal/number.mjs";
|
|
3
3
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.mjs";
|
|
4
4
|
//#region src/promise/helpers.ts
|
|
5
5
|
function getPromiseOptions(input) {
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -4,8 +4,6 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
4
4
|
//#region src/promise/index.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
7
|
-
*
|
|
8
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
9
7
|
* @param promise Promise to wrap
|
|
10
8
|
* @param options Options for the promise
|
|
11
9
|
* @returns Wrapped promise
|
|
@@ -13,8 +11,6 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
13
11
|
declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
14
12
|
/**
|
|
15
13
|
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
16
|
-
*
|
|
17
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
18
14
|
* @param callback Callback to wrap
|
|
19
15
|
* @param options Options for the promise
|
|
20
16
|
* @returns Promise-wrapped callback
|
|
@@ -22,8 +18,6 @@ declare function attemptPromise<Value>(promise: Promise<Value>, options?: Promis
|
|
|
22
18
|
declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
23
19
|
/**
|
|
24
20
|
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
25
|
-
*
|
|
26
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
27
21
|
* @param callback Callback to wrap
|
|
28
22
|
* @param options Options for the promise
|
|
29
23
|
* @returns Promise-wrapped callback
|
|
@@ -2,6 +2,9 @@ import { GenericCallback } from "../models.mjs";
|
|
|
2
2
|
import { Result } from "../result/models.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/promise/models.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A promise that can be canceled
|
|
7
|
+
*/
|
|
5
8
|
declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
6
9
|
#private;
|
|
7
10
|
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
@@ -11,8 +14,17 @@ declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
11
14
|
*/
|
|
12
15
|
cancel(reason?: unknown): void;
|
|
13
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* A promise that was fulfilled
|
|
19
|
+
*/
|
|
14
20
|
type FulfilledPromise<Value> = {
|
|
21
|
+
/**
|
|
22
|
+
* Status of the promise
|
|
23
|
+
*/
|
|
15
24
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
25
|
+
/**
|
|
26
|
+
* Value of the promise
|
|
27
|
+
*/
|
|
16
28
|
value: Awaited<Value>;
|
|
17
29
|
};
|
|
18
30
|
type PromiseData = {
|
|
@@ -23,6 +35,9 @@ type PromiseHandlers = {
|
|
|
23
35
|
resolve: (value: unknown[]) => void;
|
|
24
36
|
reject: (reason: unknown) => void;
|
|
25
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Options for a promise-handling function
|
|
40
|
+
*/
|
|
26
41
|
type PromiseOptions = {
|
|
27
42
|
/**
|
|
28
43
|
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
@@ -51,20 +66,41 @@ type PromiseParameters = {
|
|
|
51
66
|
* - Returns an array of values
|
|
52
67
|
*/
|
|
53
68
|
type PromiseStrategy = 'complete' | 'first';
|
|
69
|
+
/**
|
|
70
|
+
* An error thrown when a promise times out
|
|
71
|
+
*/
|
|
54
72
|
declare class PromiseTimeoutError extends Error {
|
|
55
73
|
constructor();
|
|
56
74
|
}
|
|
57
75
|
type PromisesItems<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Promise<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Promise<Value> : Promise<Items[ItemsKey]> };
|
|
76
|
+
/**
|
|
77
|
+
* Options for handling multiple promises
|
|
78
|
+
*/
|
|
58
79
|
type PromisesOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* AbortSignal for aborting the promises; when aborted, the promises will reject with the reason of the signal
|
|
82
|
+
*/
|
|
59
83
|
signal?: AbortSignal;
|
|
84
|
+
/**
|
|
85
|
+
* Strategy for handling the promises; defaults to `complete`
|
|
86
|
+
*/
|
|
60
87
|
strategy?: PromiseStrategy;
|
|
61
88
|
};
|
|
62
89
|
type PromisesResult<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? Result<Awaited<Value>> : never };
|
|
63
90
|
type PromisesUnwrapped<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Awaited<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Awaited<Value> : never };
|
|
64
91
|
type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
65
92
|
type PromisesValues<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never : Items[ItemsKey] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never };
|
|
93
|
+
/**
|
|
94
|
+
* A promise that was rejected
|
|
95
|
+
*/
|
|
66
96
|
type RejectedPromise = {
|
|
97
|
+
/**
|
|
98
|
+
* Status of the promise
|
|
99
|
+
*/
|
|
67
100
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
101
|
+
/**
|
|
102
|
+
* Reason for the rejection
|
|
103
|
+
*/
|
|
68
104
|
reason: unknown;
|
|
69
105
|
};
|
|
70
106
|
declare const PROMISE_ABORT_EVENT = "abort";
|
package/dist/promise/models.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
//#region src/promise/models.ts
|
|
2
|
+
/**
|
|
3
|
+
* A promise that can be canceled
|
|
4
|
+
*/
|
|
2
5
|
var CancelablePromise = class extends Promise {
|
|
3
6
|
#rejector;
|
|
4
7
|
constructor(executor) {
|
|
@@ -17,6 +20,9 @@ var CancelablePromise = class extends Promise {
|
|
|
17
20
|
this.#rejector(reason);
|
|
18
21
|
}
|
|
19
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* An error thrown when a promise times out
|
|
25
|
+
*/
|
|
20
26
|
var PromiseTimeoutError = class extends Error {
|
|
21
27
|
constructor() {
|
|
22
28
|
super(PROMISE_MESSAGE_TIMEOUT);
|