@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FIND_VALUE_INDEX, findValue } from "./find.mjs";
|
|
2
2
|
//#region src/internal/array/index-of.ts
|
|
3
3
|
function indexOf(array, ...parameters) {
|
|
4
|
-
return findValue(FIND_VALUE_INDEX, array, parameters);
|
|
4
|
+
return findValue(FIND_VALUE_INDEX, array, parameters, false);
|
|
5
5
|
}
|
|
6
6
|
//#endregion
|
|
7
7
|
export { indexOf };
|
|
@@ -10,23 +10,27 @@ import { Constructor, GenericCallback } from "../../models.mjs";
|
|
|
10
10
|
declare function compare(first: unknown, second: unknown): number;
|
|
11
11
|
declare namespace compare {
|
|
12
12
|
var handlers: {
|
|
13
|
-
|
|
14
|
-
unregister: (constructor: Constructor) => void;
|
|
13
|
+
deregister(constructor: Constructor): void;
|
|
15
14
|
handle(first: unknown, second: unknown, ...parameters: unknown[]): number;
|
|
15
|
+
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
16
16
|
};
|
|
17
|
+
var deregister: typeof deregisterComparator;
|
|
17
18
|
var register: typeof registerComparator;
|
|
18
|
-
var unregister: typeof unregisterComparator;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Deregister a custom comparison handler for a class
|
|
22
|
+
*
|
|
23
|
+
* Available as `deregisterComparator` and `compare.deregister`
|
|
22
24
|
* @param constructor Class constructor
|
|
23
|
-
* @param handler Method name or comparison function _(defaults to `compare`)_
|
|
24
25
|
*/
|
|
25
|
-
declare function
|
|
26
|
+
declare function deregisterComparator<Instance>(constructor: Constructor<Instance>): void;
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* Register a custom comparison handler for a class
|
|
29
|
+
*
|
|
30
|
+
* Available as `registerComparator` and `compare.register`
|
|
28
31
|
* @param constructor Class constructor
|
|
32
|
+
* @param handler Method name or comparison function _(defaults to `compare`)_
|
|
29
33
|
*/
|
|
30
|
-
declare function
|
|
34
|
+
declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | ((first: Instance, second: Instance) => number)): void;
|
|
31
35
|
//#endregion
|
|
32
|
-
export { compare };
|
|
36
|
+
export { compare, deregisterComparator, registerComparator };
|
|
@@ -37,8 +37,8 @@ compare.handlers = getCompareHandlers(compare, {
|
|
|
37
37
|
},
|
|
38
38
|
method: COMPARE_NAME
|
|
39
39
|
});
|
|
40
|
+
compare.deregister = deregisterComparator;
|
|
40
41
|
compare.register = registerComparator;
|
|
41
|
-
compare.unregister = unregisterComparator;
|
|
42
42
|
function compareNumbers(first, second) {
|
|
43
43
|
if (Object.is(first, second)) return 0;
|
|
44
44
|
if (Number.isNaN(first)) return -1;
|
|
@@ -54,25 +54,29 @@ function compareValue(first, second, compareStrings) {
|
|
|
54
54
|
if (first instanceof Date && second instanceof Date) return compareNumbers(first.getTime(), second.getTime());
|
|
55
55
|
return compare.handlers.handle(first, second, compareStrings);
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Deregister a custom comparison handler for a class
|
|
59
|
+
*
|
|
60
|
+
* Available as `deregisterComparator` and `compare.deregister`
|
|
61
|
+
* @param constructor Class constructor
|
|
62
|
+
*/
|
|
63
|
+
function deregisterComparator(constructor) {
|
|
64
|
+
compare.handlers.deregister(constructor);
|
|
65
|
+
}
|
|
57
66
|
function getComparisonParts(value) {
|
|
58
67
|
if (Array.isArray(value)) return value;
|
|
59
68
|
return typeof value === "object" ? [value] : words(getString(value));
|
|
60
69
|
}
|
|
61
70
|
/**
|
|
62
71
|
* Register a custom comparison handler for a class
|
|
72
|
+
*
|
|
73
|
+
* Available as `registerComparator` and `compare.register`
|
|
63
74
|
* @param constructor Class constructor
|
|
64
75
|
* @param handler Method name or comparison function _(defaults to `compare`)_
|
|
65
76
|
*/
|
|
66
77
|
function registerComparator(constructor, handler) {
|
|
67
78
|
compare.handlers.register(constructor, handler);
|
|
68
79
|
}
|
|
69
|
-
/**
|
|
70
|
-
* Unregister a custom comparison handler for a class
|
|
71
|
-
* @param constructor Class constructor
|
|
72
|
-
*/
|
|
73
|
-
function unregisterComparator(constructor) {
|
|
74
|
-
compare.handlers.unregister(constructor);
|
|
75
|
-
}
|
|
76
80
|
const comparators = {
|
|
77
81
|
bigint: compareNumbers,
|
|
78
82
|
boolean: (first, second) => compareNumbers(first ? 1 : 0, second ? 1 : 0),
|
|
@@ -80,4 +84,4 @@ const comparators = {
|
|
|
80
84
|
symbol: compareSymbols
|
|
81
85
|
};
|
|
82
86
|
//#endregion
|
|
83
|
-
export { compare };
|
|
87
|
+
export { compare, deregisterComparator, registerComparator };
|
|
@@ -19,6 +19,14 @@ type EqualOptions = {
|
|
|
19
19
|
relaxedNullish?: boolean;
|
|
20
20
|
};
|
|
21
21
|
type Equalizer = {
|
|
22
|
+
/**
|
|
23
|
+
* Are two strings equal?
|
|
24
|
+
* @param first First string
|
|
25
|
+
* @param second Second string
|
|
26
|
+
* @param ignoreCase If `true`, comparison will be case-insensitive
|
|
27
|
+
* @returns `true` if the strings are equal, otherwise `false`
|
|
28
|
+
*/
|
|
29
|
+
(first: string, second: string, ignoreCase?: boolean): boolean;
|
|
22
30
|
/**
|
|
23
31
|
* Are two values equal?
|
|
24
32
|
* @param first First value
|
|
@@ -27,17 +35,24 @@ type Equalizer = {
|
|
|
27
35
|
*/
|
|
28
36
|
(first: unknown, second: unknown): boolean;
|
|
29
37
|
/**
|
|
30
|
-
*
|
|
38
|
+
* Deregister a equality comparison handler for a specific class
|
|
31
39
|
* @param constructor Class constructor
|
|
32
|
-
* @param fn Comparison function
|
|
33
40
|
*/
|
|
34
|
-
|
|
41
|
+
deregister: <Instance>(constructor: Constructor<Instance>) => void;
|
|
35
42
|
/**
|
|
36
|
-
*
|
|
43
|
+
* Register a equality comparison function for a specific class
|
|
37
44
|
* @param constructor Class constructor
|
|
45
|
+
* @param handler Comparison function
|
|
38
46
|
*/
|
|
39
|
-
|
|
47
|
+
register: <Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean) => void;
|
|
40
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Deregister a equality comparison handler for a specific class
|
|
51
|
+
*
|
|
52
|
+
* Available as `deregisterEqualizer` and `equal.deregister`
|
|
53
|
+
* @param constructor Class constructor
|
|
54
|
+
*/
|
|
55
|
+
declare function deregisterEqualizer<Instance>(constructor: Constructor<Instance>): void;
|
|
41
56
|
/**
|
|
42
57
|
* Are two strings equal?
|
|
43
58
|
* @param first First string
|
|
@@ -56,30 +71,29 @@ declare function equal(first: string, second: string, ignoreCase?: boolean): boo
|
|
|
56
71
|
declare function equal(first: unknown, second: unknown, options?: EqualOptions): boolean;
|
|
57
72
|
declare namespace equal {
|
|
58
73
|
var handlers: {
|
|
59
|
-
|
|
60
|
-
unregister: (constructor: Constructor) => void;
|
|
74
|
+
deregister(constructor: Constructor): void;
|
|
61
75
|
handle(first: unknown, second: unknown, ...parameters: unknown[]): boolean;
|
|
76
|
+
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
62
77
|
};
|
|
78
|
+
var deregister: typeof deregisterEqualizer;
|
|
63
79
|
var initialize: typeof initializeEqualizer;
|
|
64
80
|
var register: typeof registerEqualizer;
|
|
65
|
-
var unregister: typeof unregisterEqualizer;
|
|
66
81
|
}
|
|
67
82
|
/**
|
|
68
83
|
* Create an equalizer with predefined options
|
|
84
|
+
*
|
|
85
|
+
* Available as `initializeEqualizer` and `equal.initialize`
|
|
69
86
|
* @param options Comparison options
|
|
70
87
|
* @returns Equalizer function
|
|
71
88
|
*/
|
|
72
89
|
declare function initializeEqualizer(options?: EqualOptions): Equalizer;
|
|
73
90
|
/**
|
|
74
91
|
* Register a equality comparison function for a specific class
|
|
92
|
+
*
|
|
93
|
+
* Available as `registerEqualizer` and `equal.register`
|
|
75
94
|
* @param constructor Class constructor
|
|
76
|
-
* @param
|
|
95
|
+
* @param handler Comparison function
|
|
77
96
|
*/
|
|
78
97
|
declare function registerEqualizer<Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean): void;
|
|
79
|
-
/**
|
|
80
|
-
* Unregister a equality comparison handler for a specific class
|
|
81
|
-
* @param constructor Class constructor
|
|
82
|
-
*/
|
|
83
|
-
declare function unregisterEqualizer<Instance>(constructor: Constructor<Instance>): void;
|
|
84
98
|
//#endregion
|
|
85
|
-
export { EqualOptions, equal };
|
|
99
|
+
export { EqualOptions, deregisterEqualizer, equal, initializeEqualizer, registerEqualizer };
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import { isPlainObject, isPrimitive, isTypedArray } from "../is.mjs";
|
|
2
2
|
import { getCompareHandlers } from "./handlers.mjs";
|
|
3
3
|
//#region src/internal/value/equal.ts
|
|
4
|
+
/**
|
|
5
|
+
* Deregister a equality comparison handler for a specific class
|
|
6
|
+
*
|
|
7
|
+
* Available as `deregisterEqualizer` and `equal.deregister`
|
|
8
|
+
* @param constructor Class constructor
|
|
9
|
+
*/
|
|
10
|
+
function deregisterEqualizer(constructor) {
|
|
11
|
+
equal.handlers.deregister(constructor);
|
|
12
|
+
}
|
|
13
|
+
function filterKey(key, options) {
|
|
14
|
+
if (typeof key !== "string") return true;
|
|
15
|
+
if (options.ignoreExpressions.enabled && options.ignoreExpressions.values.some((expression) => expression.test(key))) return false;
|
|
16
|
+
if (options.ignoreKeys.enabled && options.ignoreKeys.values.has(key)) return false;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
4
19
|
function equal(first, second, options) {
|
|
5
20
|
return equalValue(first, second, getEqualOptions(options));
|
|
6
21
|
}
|
|
7
22
|
equal.handlers = getCompareHandlers(equal, { callback: Object.is });
|
|
23
|
+
equal.deregister = deregisterEqualizer;
|
|
8
24
|
equal.initialize = initializeEqualizer;
|
|
9
25
|
equal.register = registerEqualizer;
|
|
10
|
-
equal.unregister = unregisterEqualizer;
|
|
11
26
|
function equalArray(first, second, options) {
|
|
12
27
|
const { length } = first;
|
|
13
28
|
if (length !== second.length) return false;
|
|
@@ -99,39 +114,6 @@ function equalValue(first, second, options) {
|
|
|
99
114
|
default: return equal.handlers.handle(first, second, options);
|
|
100
115
|
}
|
|
101
116
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Create an equalizer with predefined options
|
|
104
|
-
* @param options Comparison options
|
|
105
|
-
* @returns Equalizer function
|
|
106
|
-
*/
|
|
107
|
-
function initializeEqualizer(options) {
|
|
108
|
-
const actual = getEqualOptions(options);
|
|
109
|
-
const equalizer = (first, second) => equalValue(first, second, actual);
|
|
110
|
-
equalizer.register = registerEqualizer;
|
|
111
|
-
equalizer.unregister = unregisterEqualizer;
|
|
112
|
-
return equalizer;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Register a equality comparison function for a specific class
|
|
116
|
-
* @param constructor Class constructor
|
|
117
|
-
* @param fn Comparison function
|
|
118
|
-
*/
|
|
119
|
-
function registerEqualizer(constructor, handler) {
|
|
120
|
-
equal.handlers.register(constructor, handler);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Unregister a equality comparison handler for a specific class
|
|
124
|
-
* @param constructor Class constructor
|
|
125
|
-
*/
|
|
126
|
-
function unregisterEqualizer(constructor) {
|
|
127
|
-
equal.handlers.unregister(constructor);
|
|
128
|
-
}
|
|
129
|
-
function filterKey(key, options) {
|
|
130
|
-
if (typeof key !== "string") return true;
|
|
131
|
-
if (options.ignoreExpressions.enabled && options.ignoreExpressions.values.some((expression) => expression.test(key))) return false;
|
|
132
|
-
if (options.ignoreKeys.enabled && options.ignoreKeys.values.has(key)) return false;
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
117
|
function getEqualOptions(input) {
|
|
136
118
|
const options = {
|
|
137
119
|
ignoreCase: false,
|
|
@@ -158,10 +140,34 @@ function getEqualOptions(input) {
|
|
|
158
140
|
options.relaxedNullish = input.relaxedNullish === true;
|
|
159
141
|
return options;
|
|
160
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Create an equalizer with predefined options
|
|
145
|
+
*
|
|
146
|
+
* Available as `initializeEqualizer` and `equal.initialize`
|
|
147
|
+
* @param options Comparison options
|
|
148
|
+
* @returns Equalizer function
|
|
149
|
+
*/
|
|
150
|
+
function initializeEqualizer(options) {
|
|
151
|
+
const actual = getEqualOptions(options);
|
|
152
|
+
const equalizer = (first, second) => equalValue(first, second, actual);
|
|
153
|
+
equalizer.deregister = deregisterEqualizer;
|
|
154
|
+
equalizer.register = registerEqualizer;
|
|
155
|
+
return equalizer;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Register a equality comparison function for a specific class
|
|
159
|
+
*
|
|
160
|
+
* Available as `registerEqualizer` and `equal.register`
|
|
161
|
+
* @param constructor Class constructor
|
|
162
|
+
* @param handler Comparison function
|
|
163
|
+
*/
|
|
164
|
+
function registerEqualizer(constructor, handler) {
|
|
165
|
+
equal.handlers.register(constructor, handler);
|
|
166
|
+
}
|
|
161
167
|
const ARRAY_PEEK_PERCENTAGE = 10;
|
|
162
168
|
const ARRAY_THRESHOLD = 100;
|
|
163
169
|
const ERROR_PROPERTIES = ["name", "message"];
|
|
164
170
|
const EXPRESSION_PROPERTIES = ["source", "flags"];
|
|
165
171
|
const MINIMUM_LENGTH_FOR_SET = 16;
|
|
166
172
|
//#endregion
|
|
167
|
-
export { equal };
|
|
173
|
+
export { deregisterEqualizer, equal, initializeEqualizer, registerEqualizer };
|
|
@@ -6,14 +6,14 @@ type Options = {
|
|
|
6
6
|
method?: string;
|
|
7
7
|
};
|
|
8
8
|
declare function getCompareHandlers<Value>(owner: GenericCallback, options: Options): {
|
|
9
|
-
|
|
10
|
-
unregister: (constructor: Constructor) => void;
|
|
9
|
+
deregister(constructor: Constructor): void;
|
|
11
10
|
handle(first: unknown, second: unknown, ...parameters: unknown[]): Value;
|
|
11
|
+
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
12
12
|
};
|
|
13
13
|
declare function getSelfHandlers(owner: GenericCallback, options: Options): {
|
|
14
|
-
|
|
15
|
-
unregister: (constructor: Constructor) => void;
|
|
14
|
+
deregister(constructor: Constructor): void;
|
|
16
15
|
handle(value: unknown, ...parameters: unknown[]): any;
|
|
16
|
+
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
17
17
|
};
|
|
18
18
|
//#endregion
|
|
19
19
|
export { getCompareHandlers, getSelfHandlers };
|
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
import { isConstructor } from "../is.mjs";
|
|
2
2
|
//#region src/internal/value/handlers.ts
|
|
3
3
|
function getCompareHandlers(owner, options) {
|
|
4
|
-
const
|
|
4
|
+
const handlers = getHandlers(owner, options);
|
|
5
5
|
return {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
deregister(constructor) {
|
|
7
|
+
handlers.deregister(constructor);
|
|
8
|
+
},
|
|
8
9
|
handle(first, second, ...parameters) {
|
|
9
|
-
const handler = get(first, second);
|
|
10
|
+
const handler = handlers.get(first, second);
|
|
10
11
|
if (handler == null) return options.callback(first, second, ...parameters);
|
|
11
12
|
return typeof handler === "function" ? handler(first, second) : first[handler](second);
|
|
13
|
+
},
|
|
14
|
+
register(constructor, handler) {
|
|
15
|
+
handlers.register(constructor, handler);
|
|
12
16
|
}
|
|
13
17
|
};
|
|
14
18
|
}
|
|
15
19
|
function getHandlers(owner, options) {
|
|
16
20
|
const handlers = /* @__PURE__ */ new WeakMap();
|
|
17
21
|
return {
|
|
22
|
+
deregister(constructor) {
|
|
23
|
+
handlers.delete(constructor);
|
|
24
|
+
},
|
|
18
25
|
get(first, second) {
|
|
19
26
|
if (isConstructable(first) && isConstructable(second) && first.constructor === second.constructor) return handlers.get(first.constructor);
|
|
20
27
|
},
|
|
@@ -24,21 +31,22 @@ function getHandlers(owner, options) {
|
|
|
24
31
|
if (typeof actual !== "function" && typeof actual !== "string") return;
|
|
25
32
|
if (typeof actual === "string") actual = typeof constructor.prototype[actual] === "function" ? actual : void 0;
|
|
26
33
|
if (actual != null) handlers.set(constructor, actual);
|
|
27
|
-
},
|
|
28
|
-
unregister(constructor) {
|
|
29
|
-
handlers.delete(constructor);
|
|
30
34
|
}
|
|
31
35
|
};
|
|
32
36
|
}
|
|
33
37
|
function getSelfHandlers(owner, options) {
|
|
34
|
-
const
|
|
38
|
+
const handlers = getHandlers(owner, options);
|
|
35
39
|
return {
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
deregister(constructor) {
|
|
41
|
+
handlers.deregister(constructor);
|
|
42
|
+
},
|
|
38
43
|
handle(value, ...parameters) {
|
|
39
|
-
const handler = get(value, value);
|
|
44
|
+
const handler = handlers.get(value, value);
|
|
40
45
|
if (handler == null) return options.callback(value, ...parameters);
|
|
41
46
|
return typeof handler === "function" ? handler(value) : value[handler]();
|
|
47
|
+
},
|
|
48
|
+
register(constructor, handler) {
|
|
49
|
+
handlers.register(constructor, handler);
|
|
42
50
|
}
|
|
43
51
|
};
|
|
44
52
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { NestedKeys, NestedValue, PlainObject, ToString } from "../../models.mjs";
|
|
2
|
+
import { Result } from "../../result/models.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/internal/value/has.d.ts
|
|
4
|
-
type HasValue<Value> = {
|
|
5
|
-
exists: boolean;
|
|
6
|
-
value: Value;
|
|
7
|
-
};
|
|
8
5
|
/**
|
|
9
6
|
* Check if a nested property is defined in an object
|
|
10
7
|
* @param data Object to check in
|
|
@@ -21,23 +18,27 @@ declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data
|
|
|
21
18
|
*/
|
|
22
19
|
declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
|
|
23
20
|
declare namespace hasValue {
|
|
24
|
-
var get: typeof
|
|
21
|
+
var get: typeof hasValueResult;
|
|
25
22
|
}
|
|
26
23
|
/**
|
|
27
24
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
25
|
+
*
|
|
26
|
+
* Available as `hasValueResult` and `hasValue.get`
|
|
28
27
|
* @param data Object to check in
|
|
29
28
|
* @param path Path for property
|
|
30
29
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
31
30
|
* @return Result object
|
|
32
31
|
*/
|
|
33
|
-
declare function
|
|
32
|
+
declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>, undefined>;
|
|
34
33
|
/**
|
|
35
34
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
35
|
+
*
|
|
36
|
+
* Available as `hasValueResult` and `hasValue.get`
|
|
36
37
|
* @param data Object to check in
|
|
37
38
|
* @param path Path for property
|
|
38
39
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
39
40
|
* @return Result object
|
|
40
41
|
*/
|
|
41
|
-
declare function
|
|
42
|
+
declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, undefined>;
|
|
42
43
|
//#endregion
|
|
43
|
-
export {
|
|
44
|
+
export { hasValue };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { getNestedValue } from "./misc.mjs";
|
|
2
2
|
//#region src/internal/value/has.ts
|
|
3
3
|
function hasValue(data, path, ignoreCase) {
|
|
4
|
-
return getNestedValue(data, path, ignoreCase === true).
|
|
4
|
+
return getNestedValue(data, path, ignoreCase === true).ok;
|
|
5
5
|
}
|
|
6
|
-
hasValue.get =
|
|
7
|
-
function
|
|
6
|
+
hasValue.get = hasValueResult;
|
|
7
|
+
function hasValueResult(data, path, ignoreCase) {
|
|
8
8
|
return getNestedValue(data, path, ignoreCase === true);
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
+
import { Result } from "../../result/models.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/internal/value/misc.d.ts
|
|
2
4
|
declare function findKey(needle: string, haystack: object): string;
|
|
3
|
-
declare function getNestedValue(data: object, path: string, ignoreCase: boolean):
|
|
4
|
-
exists: boolean;
|
|
5
|
-
value: unknown;
|
|
6
|
-
};
|
|
5
|
+
declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown, undefined>;
|
|
7
6
|
declare function getPaths(path: string, lowercase: boolean): string | string[];
|
|
8
|
-
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean):
|
|
9
|
-
exists: boolean;
|
|
10
|
-
value: unknown;
|
|
11
|
-
};
|
|
7
|
+
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown, undefined>;
|
|
12
8
|
declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
|
|
13
9
|
//#endregion
|
|
14
10
|
export { findKey, getNestedValue, getPaths, handleValue };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ignoreKey } from "../string.mjs";
|
|
2
|
+
import { error, ok } from "../../result/misc.mjs";
|
|
2
3
|
//#region src/internal/value/misc.ts
|
|
3
4
|
function findKey(needle, haystack) {
|
|
4
5
|
const keys = Object.keys(haystack);
|
|
@@ -6,10 +7,7 @@ function findKey(needle, haystack) {
|
|
|
6
7
|
return index > -1 ? keys[index] : needle;
|
|
7
8
|
}
|
|
8
9
|
function getNestedValue(data, path, ignoreCase) {
|
|
9
|
-
if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return
|
|
10
|
-
exists: false,
|
|
11
|
-
value: void 0
|
|
12
|
-
};
|
|
10
|
+
if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return error(void 0);
|
|
13
11
|
const shouldIgnoreCase = ignoreCase === true;
|
|
14
12
|
const paths = getPaths(path, shouldIgnoreCase);
|
|
15
13
|
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
@@ -18,13 +16,10 @@ function getNestedValue(data, path, ignoreCase) {
|
|
|
18
16
|
for (let index = 0; index < length; index += 1) {
|
|
19
17
|
const part = paths[index];
|
|
20
18
|
const handled = handleValue(current, part, null, true, shouldIgnoreCase);
|
|
21
|
-
if (!handled.
|
|
19
|
+
if (!handled.ok) return handled;
|
|
22
20
|
current = handled.value;
|
|
23
21
|
}
|
|
24
|
-
return
|
|
25
|
-
exists: true,
|
|
26
|
-
value: current
|
|
27
|
-
};
|
|
22
|
+
return ok(current);
|
|
28
23
|
}
|
|
29
24
|
function getPaths(path, lowercase) {
|
|
30
25
|
const normalized = lowercase ? path.toLowerCase() : path;
|
|
@@ -34,16 +29,10 @@ function getPaths(path, lowercase) {
|
|
|
34
29
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
35
30
|
if (typeof data === "object" && data !== null && !ignoreKey(path)) {
|
|
36
31
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
37
|
-
if (get) return
|
|
38
|
-
exists: key in data,
|
|
39
|
-
value: data[key]
|
|
40
|
-
};
|
|
32
|
+
if (get) return key in data ? ok(data[key]) : error(void 0);
|
|
41
33
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
42
34
|
}
|
|
43
|
-
if (get) return
|
|
44
|
-
exists: false,
|
|
45
|
-
value: void 0
|
|
46
|
-
};
|
|
35
|
+
if (get) return error(void 0);
|
|
47
36
|
}
|
|
48
37
|
const EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
49
38
|
const EXPRESSION_DOTS = /^\.|\.$/g;
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -4,6 +4,8 @@ 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`
|
|
7
9
|
* @param promise Promise to wrap
|
|
8
10
|
* @param options Options for the promise
|
|
9
11
|
* @returns Wrapped promise
|
|
@@ -11,6 +13,8 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
11
13
|
declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
12
14
|
/**
|
|
13
15
|
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
16
|
+
*
|
|
17
|
+
* Available as `attemptPromise` and `attempt.promise`
|
|
14
18
|
* @param callback Callback to wrap
|
|
15
19
|
* @param options Options for the promise
|
|
16
20
|
* @returns Promise-wrapped callback
|
|
@@ -18,6 +22,8 @@ declare function attemptPromise<Value>(promise: Promise<Value>, options?: Promis
|
|
|
18
22
|
declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
19
23
|
/**
|
|
20
24
|
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
25
|
+
*
|
|
26
|
+
* Available as `attemptPromise` and `attempt.promise`
|
|
21
27
|
* @param callback Callback to wrap
|
|
22
28
|
* @param options Options for the promise
|
|
23
29
|
* @returns Promise-wrapped callback
|
|
@@ -80,6 +86,8 @@ declare namespace promises {
|
|
|
80
86
|
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
81
87
|
*
|
|
82
88
|
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
89
|
+
*
|
|
90
|
+
* Available as `resultPromises` and `promises.result`
|
|
83
91
|
* @param items List of promises
|
|
84
92
|
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
85
93
|
* @returns List of results
|
|
@@ -89,10 +97,12 @@ declare function resultPromises<Items extends unknown[]>(items: [...Items], sign
|
|
|
89
97
|
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
90
98
|
*
|
|
91
99
|
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
100
|
+
*
|
|
101
|
+
* Available as `resultPromises` and `promises.result`
|
|
92
102
|
* @param items List of promises
|
|
93
103
|
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
94
104
|
* @returns List of results
|
|
95
105
|
*/
|
|
96
106
|
declare function resultPromises<Value>(items: Promise<Value>[], signal?: AbortSignal): Promise<Result<Awaited<Value>>[]>;
|
|
97
107
|
//#endregion
|
|
98
|
-
export { attemptPromise, promises };
|
|
108
|
+
export { attemptPromise, promises, resultPromises };
|
package/dist/promise/index.mjs
CHANGED
package/dist/result/index.d.mts
CHANGED
|
@@ -7,6 +7,8 @@ import { attemptPipe } from "./work/pipe.mjs";
|
|
|
7
7
|
//#region src/result/index.d.ts
|
|
8
8
|
/**
|
|
9
9
|
* Executes a promise, catching any errors, and returns a result
|
|
10
|
+
*
|
|
11
|
+
* Available as `asyncAttempt` and `attempt.async`
|
|
10
12
|
* @param promise Promise to execute
|
|
11
13
|
* @param error Error value
|
|
12
14
|
* @returns Callback result
|
|
@@ -14,6 +16,8 @@ import { attemptPipe } from "./work/pipe.mjs";
|
|
|
14
16
|
declare function asyncAttempt<Value, E>(promise: Promise<Value>, error: E): Promise<ExtendedResult<Awaited<Value>, E>>;
|
|
15
17
|
/**
|
|
16
18
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
19
|
+
*
|
|
20
|
+
* Available as `asyncAttempt` and `attempt.async`
|
|
17
21
|
* @param callback Callback to execute
|
|
18
22
|
* @param error Error value
|
|
19
23
|
* @returns Callback result
|
|
@@ -21,12 +25,16 @@ declare function asyncAttempt<Value, E>(promise: Promise<Value>, error: E): Prom
|
|
|
21
25
|
declare function asyncAttempt<Value, E>(callback: () => Promise<Value>, error: E): Promise<ExtendedResult<Awaited<Value>, E>>;
|
|
22
26
|
/**
|
|
23
27
|
* Executes a promise, catching any errors, and returns a result
|
|
28
|
+
*
|
|
29
|
+
* Available as `asyncAttempt` and `attempt.async`
|
|
24
30
|
* @param promise Promise to execute
|
|
25
31
|
* @returns Callback result
|
|
26
32
|
*/
|
|
27
33
|
declare function asyncAttempt<Value>(promise: Promise<Value>): Promise<Result<Awaited<Value>>>;
|
|
28
34
|
/**
|
|
29
35
|
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
36
|
+
*
|
|
37
|
+
* Available as `asyncAttempt` and `attempt.async`
|
|
30
38
|
* @param callback Callback to execute
|
|
31
39
|
* @returns Callback result
|
|
32
40
|
*/
|
|
@@ -52,4 +60,4 @@ declare namespace attempt {
|
|
|
52
60
|
var promise: typeof attemptPromise;
|
|
53
61
|
}
|
|
54
62
|
//#endregion
|
|
55
|
-
export { attempt };
|
|
63
|
+
export { asyncAttempt, attempt };
|
package/dist/result/index.mjs
CHANGED
package/dist/result/match.d.mts
CHANGED
|
@@ -16,12 +16,16 @@ declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<
|
|
|
16
16
|
declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>), ok: ResultMatch<Value, Returned, E>['ok'], error: ResultMatch<Value, Returned, E>['error']): Promise<Returned>;
|
|
17
17
|
/**
|
|
18
18
|
* Handles a result with match callbacks
|
|
19
|
+
*
|
|
20
|
+
* Available as `matchResult` and `attempt.match`
|
|
19
21
|
* @param result Result to handle
|
|
20
22
|
* @param handler Match callbacks
|
|
21
23
|
*/
|
|
22
24
|
declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | (() => AnyResult<Value, E>), handler: ResultMatch<Value, Returned, E>): Returned;
|
|
23
25
|
/**
|
|
24
26
|
* Handles a result with match callbacks
|
|
27
|
+
*
|
|
28
|
+
* Available as `matchResult` and `attempt.match`
|
|
25
29
|
* @param result Result to handle
|
|
26
30
|
* @param ok Ok callback
|
|
27
31
|
* @param error Error callback
|
|
@@ -31,4 +35,4 @@ declare namespace matchResult {
|
|
|
31
35
|
var async: typeof asyncMatchResult;
|
|
32
36
|
}
|
|
33
37
|
//#endregion
|
|
34
|
-
export { matchResult };
|
|
38
|
+
export { asyncMatchResult, matchResult };
|
package/dist/result/match.mjs
CHANGED
|
@@ -24,4 +24,4 @@ function matchResult(result, first, error) {
|
|
|
24
24
|
matchResult.async = asyncMatchResult;
|
|
25
25
|
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
26
26
|
//#endregion
|
|
27
|
-
export { matchResult };
|
|
27
|
+
export { asyncMatchResult, matchResult };
|