@dereekb/util 13.0.7 → 13.2.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/fetch/package.json +2 -2
- package/index.cjs.js +4107 -1516
- package/index.esm.js +4107 -1516
- package/package.json +1 -2
- package/src/lib/array/array.d.ts +92 -47
- package/src/lib/array/array.find.d.ts +14 -18
- package/src/lib/array/array.index.d.ts +43 -14
- package/src/lib/array/array.indexed.d.ts +66 -17
- package/src/lib/array/array.limit.d.ts +11 -0
- package/src/lib/array/array.make.d.ts +9 -3
- package/src/lib/array/array.map.d.ts +20 -8
- package/src/lib/array/array.number.d.ts +63 -5
- package/src/lib/array/array.random.d.ts +12 -9
- package/src/lib/array/array.set.d.ts +24 -1
- package/src/lib/array/array.string.d.ts +104 -0
- package/src/lib/array/array.unique.d.ts +60 -8
- package/src/lib/array/array.value.d.ts +20 -11
- package/src/lib/assertion/assertion.d.ts +30 -2
- package/src/lib/assertion/assertion.generic.d.ts +8 -0
- package/src/lib/assertion/assertion.number.d.ts +16 -0
- package/src/lib/auth/auth.role.claims.d.ts +14 -8
- package/src/lib/boolean.d.ts +54 -1
- package/src/lib/contact/domain.d.ts +22 -4
- package/src/lib/contact/random.d.ts +24 -0
- package/src/lib/date/date.d.ts +8 -13
- package/src/lib/date/hour.d.ts +51 -32
- package/src/lib/date/minute.d.ts +4 -4
- package/src/lib/date/time.d.ts +12 -3
- package/src/lib/date/week.d.ts +77 -10
- package/src/lib/error/error.d.ts +54 -3
- package/src/lib/error/error.server.d.ts +25 -2
- package/src/lib/filter/filter.d.ts +12 -7
- package/src/lib/function/function.boolean.d.ts +4 -4
- package/src/lib/function/function.d.ts +2 -5
- package/src/lib/function/function.forward.d.ts +19 -3
- package/src/lib/getter/getter.cache.d.ts +9 -5
- package/src/lib/getter/getter.d.ts +39 -22
- package/src/lib/getter/getter.map.d.ts +10 -4
- package/src/lib/getter/getter.util.d.ts +3 -3
- package/src/lib/grouping.d.ts +98 -31
- package/src/lib/hash.d.ts +15 -8
- package/src/lib/iterable/iterable.d.ts +62 -50
- package/src/lib/iterable/iterable.map.d.ts +4 -4
- package/src/lib/iterate.d.ts +17 -1
- package/src/lib/key.d.ts +32 -10
- package/src/lib/lifecycle.d.ts +10 -1
- package/src/lib/map/map.d.ts +17 -16
- package/src/lib/map/map.intersection.d.ts +5 -4
- package/src/lib/map/map.key.d.ts +16 -15
- package/src/lib/misc/host.d.ts +6 -3
- package/src/lib/model/id.batch.d.ts +7 -3
- package/src/lib/model/id.factory.d.ts +16 -1
- package/src/lib/model/model.conversion.d.ts +49 -6
- package/src/lib/model/model.conversion.field.d.ts +13 -3
- package/src/lib/model/model.copy.d.ts +11 -0
- package/src/lib/model/model.d.ts +174 -1
- package/src/lib/model/model.modify.d.ts +24 -5
- package/src/lib/nodejs/stream.d.ts +8 -6
- package/src/lib/number/bitwise.dencoder.d.ts +43 -3
- package/src/lib/number/bound.d.ts +34 -5
- package/src/lib/number/dollar.d.ts +13 -6
- package/src/lib/number/factory.d.ts +11 -3
- package/src/lib/number/number.d.ts +55 -32
- package/src/lib/number/random.d.ts +13 -3
- package/src/lib/number/round.d.ts +47 -26
- package/src/lib/number/sort.d.ts +7 -1
- package/src/lib/number/transform.d.ts +16 -0
- package/src/lib/object/object.array.d.ts +16 -2
- package/src/lib/object/object.array.delta.d.ts +6 -2
- package/src/lib/object/object.d.ts +33 -5
- package/src/lib/object/object.empty.d.ts +4 -2
- package/src/lib/object/object.equal.d.ts +21 -4
- package/src/lib/object/object.filter.tuple.d.ts +105 -3
- package/src/lib/object/object.key.d.ts +13 -6
- package/src/lib/object/object.map.d.ts +30 -19
- package/src/lib/page/page.d.ts +21 -0
- package/src/lib/page/page.filter.d.ts +16 -9
- package/src/lib/path/path.d.ts +101 -14
- package/src/lib/path/path.tree.d.ts +16 -0
- package/src/lib/promise/callback.d.ts +18 -0
- package/src/lib/promise/is.d.ts +10 -3
- package/src/lib/promise/map.d.ts +5 -3
- package/src/lib/promise/poll.d.ts +14 -4
- package/src/lib/promise/promise.d.ts +84 -18
- package/src/lib/promise/promise.factory.d.ts +18 -2
- package/src/lib/promise/promise.limit.d.ts +22 -2
- package/src/lib/promise/promise.loop.d.ts +69 -1
- package/src/lib/promise/promise.ref.d.ts +14 -2
- package/src/lib/promise/promise.task.d.ts +8 -4
- package/src/lib/promise/promise.type.d.ts +4 -4
- package/src/lib/promise/use.d.ts +6 -4
- package/src/lib/promise/wait.d.ts +5 -2
- package/src/lib/relation/relation.d.ts +98 -0
- package/src/lib/service/handler.config.d.ts +47 -9
- package/src/lib/service/handler.d.ts +43 -1
- package/src/lib/service/typed.service.d.ts +21 -2
- package/src/lib/set/set.allowed.d.ts +6 -4
- package/src/lib/set/set.d.ts +174 -46
- package/src/lib/set/set.decision.d.ts +4 -2
- package/src/lib/set/set.delta.d.ts +16 -3
- package/src/lib/set/set.hashset.d.ts +43 -0
- package/src/lib/set/set.selection.d.ts +7 -3
- package/src/lib/sort.d.ts +36 -16
- package/src/lib/string/char.d.ts +48 -34
- package/src/lib/string/dencoder.d.ts +84 -13
- package/src/lib/string/factory.d.ts +13 -4
- package/src/lib/string/html.d.ts +7 -6
- package/src/lib/string/mimetype.d.ts +65 -1
- package/src/lib/string/prefix.d.ts +21 -7
- package/src/lib/string/record.d.ts +7 -4
- package/src/lib/string/replace.d.ts +77 -33
- package/src/lib/string/search.d.ts +19 -9
- package/src/lib/string/sort.d.ts +10 -1
- package/src/lib/string/string.d.ts +74 -21
- package/src/lib/string/transform.d.ts +7 -0
- package/src/lib/string/tree.d.ts +91 -29
- package/src/lib/string/url.d.ts +148 -63
- package/src/lib/tree/tree.explore.d.ts +84 -17
- package/src/lib/tree/tree.flatten.d.ts +44 -7
- package/src/lib/type.d.ts +15 -12
- package/src/lib/value/address.d.ts +66 -17
- package/src/lib/value/bound.d.ts +253 -17
- package/src/lib/value/build.d.ts +30 -5
- package/src/lib/value/comparator.d.ts +53 -25
- package/src/lib/value/cron.d.ts +14 -6
- package/src/lib/value/decision.d.ts +57 -13
- package/src/lib/value/equal.d.ts +44 -13
- package/src/lib/value/indexed.d.ts +253 -82
- package/src/lib/value/label.d.ts +16 -5
- package/src/lib/value/map.d.ts +77 -20
- package/src/lib/value/maybe.d.ts +47 -49
- package/src/lib/value/modifier.d.ts +82 -27
- package/src/lib/value/point.d.ts +248 -54
- package/src/lib/value/url.d.ts +10 -3
- package/src/lib/value/use.d.ts +99 -8
- package/src/lib/value/vector.d.ts +71 -9
- package/test/index.cjs.js +86 -23
- package/test/index.esm.js +86 -23
- package/test/package.json +2 -2
- package/test/src/lib/shared/shared.d.ts +31 -5
- package/test/src/lib/shared/shared.fail.d.ts +72 -9
- package/test/src/lib/shared/shared.function.d.ts +40 -2
- package/test/src/lib/shared/shared.wrap.d.ts +20 -1
package/src/lib/value/map.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type ArrayOrValue } from '../array/array';
|
|
|
2
2
|
import { type PromiseOrValue } from '../promise/promise.type';
|
|
3
3
|
import { type Maybe, type MaybeNot } from './maybe.type';
|
|
4
4
|
/**
|
|
5
|
-
* Converts
|
|
5
|
+
* Converts a value of one type to another, serving as the fundamental transformation primitive throughout the library.
|
|
6
6
|
*/
|
|
7
7
|
export type MapFunction<I, O> = (input: I) => O;
|
|
8
8
|
/**
|
|
@@ -12,45 +12,77 @@ export type MapFunction<I, O> = (input: I) => O;
|
|
|
12
12
|
*/
|
|
13
13
|
export type ReadValueFunction<I, O> = MapFunction<I, O>;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Wraps a MapFunction so that null/undefined inputs are passed through without invoking the map,
|
|
16
|
+
* avoiding errors on nullable values.
|
|
16
17
|
*
|
|
17
|
-
* @param mapFunction
|
|
18
|
-
* @returns
|
|
18
|
+
* @param mapFunction - function to apply only when the input is defined
|
|
19
|
+
* @returns a new function that short-circuits on null/undefined inputs
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const double = (x: number) => x * 2;
|
|
24
|
+
* const maybeDouble = mapMaybeFunction(double);
|
|
25
|
+
*
|
|
26
|
+
* maybeDouble(3); // 6
|
|
27
|
+
* maybeDouble(undefined); // undefined
|
|
28
|
+
* maybeDouble(null); // null
|
|
29
|
+
* ```
|
|
19
30
|
*/
|
|
20
31
|
export declare function mapMaybeFunction<I, O>(mapFunction: MapFunction<I, O>): MapFunction<Maybe<I>, Maybe<O>>;
|
|
21
32
|
/**
|
|
22
|
-
* MapFunction
|
|
33
|
+
* A MapFunction whose output type matches its input type, useful for in-place transformations or chained pipelines.
|
|
23
34
|
*/
|
|
24
35
|
export type MapSameFunction<I> = MapFunction<I, I>;
|
|
25
36
|
/**
|
|
26
|
-
*
|
|
37
|
+
* Derives an asynchronous variant of a MapFunction, allowing the output to be either a value or a Promise.
|
|
27
38
|
*/
|
|
28
39
|
export type AsyncMapFunction<F extends MapFunction<any, any>> = F extends MapFunction<infer I, infer O> ? MapFunction<I, PromiseOrValue<O>> : never;
|
|
29
40
|
/**
|
|
30
|
-
*
|
|
41
|
+
* Derives an array variant of a MapFunction that maps each element individually, producing an array of results.
|
|
31
42
|
*/
|
|
32
43
|
export type MapArrayFunction<F extends MapFunction<any, any>> = F extends MapFunction<infer I, infer O> ? MapFunction<I[], O[]> : never;
|
|
33
44
|
/**
|
|
34
|
-
*
|
|
45
|
+
* Maps values from the input to the output type, optionally merging into an existing partial target object.
|
|
35
46
|
*/
|
|
36
47
|
export type ApplyMapFunction<I, O> = (input: I, target?: Maybe<Partial<O>>) => O;
|
|
37
48
|
/**
|
|
38
|
-
*
|
|
49
|
+
* Maps values from the input to the output type, optionally merging into a partial target and accepting additional options.
|
|
39
50
|
*/
|
|
40
51
|
export type ApplyMapFunctionWithOptions<I, O, C> = (input: I, target?: Maybe<Partial<O>>, options?: Maybe<C>) => O;
|
|
52
|
+
/**
|
|
53
|
+
* Lifts a per-element MapFunction into one that operates on arrays, applying the mapping to each element.
|
|
54
|
+
*
|
|
55
|
+
* @param mapFunction - per-element transformation
|
|
56
|
+
* @returns a function that maps entire arrays
|
|
57
|
+
*/
|
|
41
58
|
export declare function mapArrayFunction<I, O>(mapFunction: MapFunction<I, O>): MapArrayFunction<MapFunction<I, O>>;
|
|
59
|
+
/**
|
|
60
|
+
* The canonical identity MapFunction. Returns its input unchanged.
|
|
61
|
+
*
|
|
62
|
+
* Used as a sentinel value so that {@link chainMapSameFunctions} and other combinators can detect
|
|
63
|
+
* and skip no-op mappings for efficiency.
|
|
64
|
+
*/
|
|
42
65
|
export declare const MAP_IDENTITY: <T>(input: T) => T;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the shared {@link MAP_IDENTITY} function cast to the requested type, useful for providing a typed no-op transformation.
|
|
68
|
+
*/
|
|
43
69
|
export declare function mapIdentityFunction<T>(): MapFunction<T, T>;
|
|
70
|
+
/**
|
|
71
|
+
* Checks whether the given function is the singleton {@link MAP_IDENTITY} reference.
|
|
72
|
+
*/
|
|
44
73
|
export declare function isMapIdentityFunction(fn: unknown): fn is typeof MAP_IDENTITY;
|
|
74
|
+
/**
|
|
75
|
+
* Captures both the input and output of a MapFunction invocation, useful for debugging or auditing transformations.
|
|
76
|
+
*/
|
|
45
77
|
export type MapFunctionOutputPair<O, I = unknown> = {
|
|
46
78
|
input: I;
|
|
47
79
|
output: O;
|
|
48
80
|
};
|
|
49
81
|
/**
|
|
50
|
-
* Wraps a MapFunction
|
|
82
|
+
* Wraps a MapFunction so that each invocation returns a {@link MapFunctionOutputPair} containing both the original input and the computed output.
|
|
51
83
|
*
|
|
52
|
-
* @param fn
|
|
53
|
-
* @returns
|
|
84
|
+
* @param fn - the map function to wrap
|
|
85
|
+
* @returns a new function that returns input/output pairs
|
|
54
86
|
*/
|
|
55
87
|
export declare function mapFunctionOutputPair<O, I = unknown>(fn: MapFunction<I, O>): MapFunction<I, MapFunctionOutputPair<O, I>>;
|
|
56
88
|
/**
|
|
@@ -60,25 +92,50 @@ export type MapFunctionOutput<O extends object, I = unknown> = O & {
|
|
|
60
92
|
readonly _input: I;
|
|
61
93
|
};
|
|
62
94
|
/**
|
|
95
|
+
* Wraps a MapFunction so that its object output is augmented with a readonly `_input` property referencing the original input.
|
|
96
|
+
* Useful for retaining provenance through a transformation pipeline.
|
|
63
97
|
*
|
|
64
|
-
* @param fn
|
|
65
|
-
* @returns
|
|
98
|
+
* @param fn - the map function whose output will be augmented
|
|
99
|
+
* @returns a new function that returns a {@link MapFunctionOutput} with the `_input` reference attached
|
|
66
100
|
*/
|
|
67
101
|
export declare function wrapMapFunctionOutput<O extends object, I = unknown>(fn: MapFunction<I, O>): MapFunction<I, MapFunctionOutput<O, I>>;
|
|
102
|
+
/**
|
|
103
|
+
* Attaches a readonly `_input` property to the given output object, creating a {@link MapFunctionOutput}.
|
|
104
|
+
*
|
|
105
|
+
* @param output - the computed output object
|
|
106
|
+
* @param input - the original input value to attach
|
|
107
|
+
* @returns the output augmented with `_input`
|
|
108
|
+
*/
|
|
68
109
|
export declare function mapFunctionOutput<O extends object, I = unknown>(output: O, input: I): MapFunctionOutput<O, I>;
|
|
69
110
|
/**
|
|
70
|
-
* Chains together multiple MapSameFunctions
|
|
111
|
+
* Chains together multiple MapSameFunctions into a single pipeline executed left-to-right.
|
|
112
|
+
* Null/undefined entries and identity functions are automatically removed for efficiency.
|
|
113
|
+
* Returns the identity function if no meaningful functions remain.
|
|
114
|
+
*
|
|
115
|
+
* @param input - one or more optional same-type map functions to chain
|
|
116
|
+
* @returns a single composed function that runs all provided functions in order
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const fnChain = chainMapSameFunctions([
|
|
121
|
+
* (x: string) => x,
|
|
122
|
+
* (x: string) => x,
|
|
123
|
+
* ]);
|
|
71
124
|
*
|
|
72
|
-
*
|
|
125
|
+
* const result = fnChain('aaaab');
|
|
126
|
+
* // result === 'aaaab'
|
|
127
|
+
* ```
|
|
73
128
|
*/
|
|
74
129
|
export declare function chainMapSameFunctions<I>(input: ArrayOrValue<Maybe<MapSameFunction<I>>>): MapSameFunction<I>;
|
|
75
130
|
/**
|
|
76
|
-
* Creates a single function that
|
|
131
|
+
* Creates a single function that pipes the output of `a` into `b`.
|
|
77
132
|
*
|
|
78
|
-
* If apply is false, or
|
|
133
|
+
* If `apply` is false, or `b` is null/undefined, returns `a` unchanged. This conditional chaining
|
|
134
|
+
* is useful when a second transform step is optional.
|
|
79
135
|
*
|
|
80
|
-
* @param a
|
|
81
|
-
* @param b
|
|
136
|
+
* @param a - the first map function
|
|
137
|
+
* @param b - the optional second map function to chain after `a`
|
|
138
|
+
* @param apply - when false, skips chaining and returns `a` directly
|
|
82
139
|
*/
|
|
83
140
|
export declare function chainMapFunction<I>(a: MapSameFunction<I>, b: Maybe<MapSameFunction<I>>): MapSameFunction<I>;
|
|
84
141
|
export declare function chainMapFunction<I>(a: MapSameFunction<I>, b: Maybe<MapSameFunction<I>>, apply?: boolean): MapSameFunction<I>;
|
package/src/lib/value/maybe.d.ts
CHANGED
|
@@ -1,107 +1,105 @@
|
|
|
1
1
|
import { type Maybe, type MaybeNot, type MaybeSo } from './maybe.type';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Type guard that returns `true` if the value is not `null` or `undefined`.
|
|
4
4
|
*
|
|
5
|
-
* @param value
|
|
6
|
-
* @returns
|
|
5
|
+
* @param value - the value to check
|
|
7
6
|
*/
|
|
8
7
|
export declare function hasNonNullValue<T = unknown>(value: Maybe<T>): value is MaybeSo<T>;
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* Type guard that checks whether the input has a meaningful value.
|
|
11
10
|
*
|
|
12
|
-
*
|
|
13
|
-
* - empty
|
|
14
|
-
*
|
|
15
|
-
* - null/undefined values.
|
|
11
|
+
* Returns `false` for empty iterables (arrays, Sets, Maps), empty strings, and nullish values.
|
|
12
|
+
* Non-iterable objects (e.g. `{}`) are considered non-empty by this function;
|
|
13
|
+
* use {@link hasValueOrNotEmptyObject} to also reject empty objects.
|
|
16
14
|
*
|
|
17
15
|
* NaN has undefined behavior.
|
|
16
|
+
*
|
|
17
|
+
* @param value - the value to check
|
|
18
18
|
*/
|
|
19
19
|
export declare function hasValueOrNotEmpty<T = unknown>(value: Maybe<T>): value is MaybeSo<T>;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Type guard that checks whether the input has a meaningful value, including checking for empty objects.
|
|
22
22
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* - empty strings
|
|
26
|
-
* - empty objects (no keys)
|
|
27
|
-
* - null/undefined values.
|
|
23
|
+
* Returns `false` for empty iterables, empty strings, objects with no own keys (`{}`), and nullish values.
|
|
24
|
+
* This is stricter than {@link hasValueOrNotEmpty}, which considers `{}` as having a value.
|
|
28
25
|
*
|
|
29
26
|
* NaN has undefined behavior.
|
|
27
|
+
*
|
|
28
|
+
* @param value - the value to check
|
|
30
29
|
*/
|
|
31
30
|
export declare function hasValueOrNotEmptyObject<T = unknown>(value: Maybe<T>): value is MaybeSo<T>;
|
|
32
31
|
/**
|
|
33
|
-
* Returns true if the input value is a non-empty string or is true
|
|
32
|
+
* Returns `true` if the input value is a non-empty string or is `true`.
|
|
34
33
|
*
|
|
35
|
-
* @param value
|
|
36
|
-
* @returns
|
|
34
|
+
* @param value - the value to check
|
|
37
35
|
*/
|
|
38
36
|
export declare function isStringOrTrue(value: Maybe<string | boolean>): boolean;
|
|
39
37
|
/**
|
|
40
|
-
*
|
|
38
|
+
* Type guard that returns `true` if the input is not nullish and not an empty string.
|
|
39
|
+
*
|
|
40
|
+
* Useful for filtering out both nullish values and empty strings in a single check.
|
|
41
41
|
*
|
|
42
|
-
* @param value
|
|
43
|
-
* @returns
|
|
42
|
+
* @param value - the value to check
|
|
44
43
|
*/
|
|
45
44
|
export declare function isNotNullOrEmptyString<T>(value: Maybe<MaybeNot | '' | T>): value is MaybeSo<T>;
|
|
46
45
|
/**
|
|
47
|
-
*
|
|
46
|
+
* Type guard that returns `true` if the input is `null` or `undefined`.
|
|
48
47
|
*
|
|
49
|
-
* @param value
|
|
50
|
-
* @returns
|
|
48
|
+
* @param value - the value to check
|
|
51
49
|
*/
|
|
52
50
|
export declare function isMaybeNot<T = unknown>(value: Maybe<T>): value is MaybeNot;
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
52
|
+
* Type guard that returns `true` if the input is neither `null` nor `undefined`.
|
|
55
53
|
*
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
54
|
+
* Equivalent to {@link hasNonNullValue} but with the `MaybeSo` narrowing type.
|
|
55
|
+
*
|
|
56
|
+
* @param value - the value to check
|
|
58
57
|
*/
|
|
59
58
|
export declare function isMaybeSo<T>(value: Maybe<T>): value is MaybeSo<T>;
|
|
60
59
|
/**
|
|
61
|
-
*
|
|
60
|
+
* Type guard that returns `true` if the input is nullish or is strictly `true`.
|
|
61
|
+
*
|
|
62
|
+
* Useful for optional boolean flags where both absence and `true` indicate the same behavior.
|
|
62
63
|
*
|
|
63
|
-
* @param value
|
|
64
|
-
* @returns
|
|
64
|
+
* @param value - the value to check
|
|
65
65
|
*/
|
|
66
66
|
export declare function isMaybeNotOrTrue<T = unknown>(value: Maybe<T | true>): value is MaybeNot | true;
|
|
67
67
|
/**
|
|
68
|
-
*
|
|
68
|
+
* Returns `true` if the input is not `null`, `undefined`, or `false`.
|
|
69
69
|
*
|
|
70
|
-
* @param value
|
|
71
|
-
* @returns
|
|
70
|
+
* @param value - the value to check
|
|
72
71
|
*/
|
|
73
72
|
export declare function isDefinedAndNotFalse<T = unknown>(value: Maybe<T>): boolean;
|
|
74
73
|
/**
|
|
75
|
-
*
|
|
74
|
+
* Returns `true` if the input is not strictly `false`. Nullish values return `true`.
|
|
76
75
|
*
|
|
77
|
-
* @param value
|
|
78
|
-
* @returns
|
|
76
|
+
* @param value - the value to check
|
|
79
77
|
*/
|
|
80
78
|
export declare function isNotFalse<T = unknown>(value: Maybe<T>): boolean;
|
|
81
79
|
/**
|
|
82
|
-
* Returns true if both
|
|
80
|
+
* Returns `true` if both inputs are non-nullish and strictly equal (`===`).
|
|
83
81
|
*
|
|
84
|
-
* @param a
|
|
85
|
-
* @param b
|
|
86
|
-
* @returns
|
|
82
|
+
* @param a - first value
|
|
83
|
+
* @param b - second value
|
|
87
84
|
*/
|
|
88
85
|
export declare function isSameNonNullValue<T>(a: Maybe<T>, b: Maybe<T>): a is NonNullable<T>;
|
|
89
86
|
/**
|
|
90
|
-
* Returns true if both inputs are
|
|
87
|
+
* Returns `true` if both inputs are nullish (using loose equality `==`) or are strictly the same value.
|
|
88
|
+
*
|
|
89
|
+
* This means `null` and `undefined` are considered equivalent to each other, but `false` and `null` are not.
|
|
91
90
|
*
|
|
92
|
-
* @param a
|
|
93
|
-
* @param b
|
|
94
|
-
* @returns
|
|
91
|
+
* @param a - first value
|
|
92
|
+
* @param b - second value
|
|
95
93
|
*/
|
|
96
94
|
export declare function valuesAreBothNullishOrEquivalent<T>(a: Maybe<T>, b: Maybe<T>): boolean;
|
|
97
95
|
/**
|
|
98
|
-
*
|
|
96
|
+
* Merges two `Maybe` values using `undefined` as the "no change" sentinel.
|
|
99
97
|
*
|
|
100
|
-
* - If b is
|
|
101
|
-
* - If b is
|
|
102
|
-
* - If b is
|
|
98
|
+
* - If `b` is `undefined`, returns `a` (no update).
|
|
99
|
+
* - If `b` is `null`, returns `null` (explicit clear).
|
|
100
|
+
* - If `b` is defined, returns `b` (new value).
|
|
103
101
|
*
|
|
104
|
-
* @param a
|
|
105
|
-
* @param b
|
|
102
|
+
* @param a - the current value
|
|
103
|
+
* @param b - the update value
|
|
106
104
|
*/
|
|
107
105
|
export declare function updateMaybeValue<T>(a: Maybe<T>, b: Maybe<T>): Maybe<T>;
|
|
@@ -1,78 +1,133 @@
|
|
|
1
1
|
import { type ArrayOrValue } from '../array';
|
|
2
2
|
import { type Maybe } from './maybe.type';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* String key that uniquely identifies a modifier within a {@link ModifierMap}.
|
|
5
5
|
*/
|
|
6
6
|
export type ModifierKey = string;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Function that mutates the input value in place.
|
|
9
9
|
*/
|
|
10
10
|
export type ModifierFunction<T> = (input: T) => void;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Holds a reference to a {@link ModifierFunction}.
|
|
13
13
|
*/
|
|
14
14
|
export interface ModifierFunctionRef<T> {
|
|
15
15
|
readonly modify: ModifierFunction<T>;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* A modifier that
|
|
18
|
+
* A keyed modifier that pairs a unique {@link ModifierKey} with a {@link ModifierFunction}.
|
|
19
|
+
*
|
|
20
|
+
* The key allows modifiers to be added, replaced, or removed from a {@link ModifierMap} by identity.
|
|
19
21
|
*/
|
|
20
22
|
export interface Modifier<T> extends ModifierFunctionRef<T> {
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* Unique key identifying this modifier.
|
|
23
25
|
*/
|
|
24
26
|
readonly key: ModifierKey;
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
|
-
* Creates a
|
|
29
|
+
* Creates a {@link Modifier} with the given key and modify function.
|
|
30
|
+
*
|
|
31
|
+
* @param key - unique identifier for the modifier
|
|
32
|
+
* @param modify - function that mutates the target value
|
|
28
33
|
*
|
|
29
|
-
* @
|
|
30
|
-
*
|
|
31
|
-
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const uppercaseName = modifier<{ name: string }>('uppercase', (x) => { x.name = x.name.toUpperCase(); });
|
|
37
|
+
* const obj = { name: 'alice' };
|
|
38
|
+
* uppercaseName.modify(obj);
|
|
39
|
+
* // obj.name === 'ALICE'
|
|
40
|
+
* ```
|
|
32
41
|
*/
|
|
33
42
|
export declare function modifier<T>(key: string, modify: ModifierFunction<T>): Modifier<T>;
|
|
34
43
|
/**
|
|
35
|
-
*
|
|
44
|
+
* A no-operation modifier that does nothing to the input. Useful as a default/fallback.
|
|
36
45
|
*/
|
|
37
46
|
export declare const NOOP_MODIFIER: ModifierFunction<any>;
|
|
38
47
|
/**
|
|
39
|
-
* Map of
|
|
48
|
+
* Map of {@link Modifier} instances keyed by their {@link ModifierKey}, enabling lookup, replacement, and removal by key.
|
|
40
49
|
*/
|
|
41
50
|
export type ModifierMap<T> = Map<ModifierKey, Modifier<T>>;
|
|
42
51
|
/**
|
|
43
|
-
* Adds
|
|
52
|
+
* Adds one or more modifiers to the map, creating a new map if none is provided.
|
|
53
|
+
*
|
|
54
|
+
* If a modifier with the same key already exists, it is replaced.
|
|
55
|
+
*
|
|
56
|
+
* @param modifiers - modifier(s) to add
|
|
57
|
+
* @param map - existing map to add to, or undefined to create a new one
|
|
44
58
|
*
|
|
45
|
-
* @
|
|
46
|
-
*
|
|
47
|
-
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const mod = modifier<{ x: number }>('double', (o) => { o.x *= 2; });
|
|
62
|
+
* const map = addModifiers(mod);
|
|
63
|
+
* map.has('double'); // true
|
|
64
|
+
* ```
|
|
48
65
|
*/
|
|
49
66
|
export declare function addModifiers<T>(modifiers: ArrayOrValue<Modifier<T>>, map?: Maybe<ModifierMap<T>>): ModifierMap<T>;
|
|
50
67
|
/**
|
|
51
|
-
* Removes
|
|
68
|
+
* Removes one or more modifiers from the map by key. Returns an empty map if no map is provided.
|
|
52
69
|
*
|
|
53
|
-
* @param modifier
|
|
54
|
-
* @param map
|
|
70
|
+
* @param modifiers - modifier(s) whose keys should be removed
|
|
71
|
+
* @param map - the map to remove from
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const mod = modifier<{ x: number }>('double', (o) => { o.x *= 2; });
|
|
76
|
+
* const map = addModifiers(mod);
|
|
77
|
+
* const result = removeModifiers(mod, map);
|
|
78
|
+
* result.has('double'); // false
|
|
79
|
+
* ```
|
|
55
80
|
*/
|
|
56
81
|
export declare function removeModifiers<T>(modifiers: ArrayOrValue<Modifier<T>>, map: Maybe<ModifierMap<T>>): ModifierMap<T>;
|
|
82
|
+
/**
|
|
83
|
+
* Converts a {@link ModifierMap} to a single {@link ModifierFunction} that applies all modifiers in sequence.
|
|
84
|
+
*
|
|
85
|
+
* Returns {@link NOOP_MODIFIER} if the map is nullish or empty.
|
|
86
|
+
*
|
|
87
|
+
* @param map - the modifier map to convert
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const mod = modifier<{ x: number }>('inc', (o) => { o.x += 1; });
|
|
92
|
+
* const map = addModifiers(mod);
|
|
93
|
+
* const fn = modifierMapToFunction(map);
|
|
94
|
+
* const obj = { x: 0 };
|
|
95
|
+
* fn(obj);
|
|
96
|
+
* // obj.x === 1
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
57
99
|
export declare function modifierMapToFunction<T>(map: Maybe<ModifierMap<T>>): ModifierFunction<T>;
|
|
58
100
|
/**
|
|
59
|
-
* Converts a ModifierMap to a ModifierFunction if the map is
|
|
101
|
+
* Converts a {@link ModifierMap} to a single {@link ModifierFunction} if the map is non-null.
|
|
102
|
+
*
|
|
103
|
+
* Returns undefined if no map is provided, allowing callers to distinguish "no modifiers" from "empty modifiers".
|
|
60
104
|
*
|
|
61
|
-
* @param map
|
|
62
|
-
* @returns
|
|
105
|
+
* @param map - the modifier map to convert
|
|
63
106
|
*/
|
|
64
107
|
export declare function maybeModifierMapToFunction<T>(map: Maybe<ModifierMap<T>>): Maybe<ModifierFunction<T>>;
|
|
65
108
|
/**
|
|
66
|
-
* Merges
|
|
109
|
+
* Merges an array of {@link ModifierFunction} values into a single function that applies them all in order.
|
|
67
110
|
*
|
|
68
|
-
* @
|
|
69
|
-
*
|
|
111
|
+
* Returns {@link NOOP_MODIFIER} if the array is empty or nullish.
|
|
112
|
+
*
|
|
113
|
+
* @param modifiers - array of modifier functions to merge
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const add1 = (o: { x: number }) => { o.x += 1; };
|
|
118
|
+
* const double = (o: { x: number }) => { o.x *= 2; };
|
|
119
|
+
* const merged = mergeModifiers([add1, double]);
|
|
120
|
+
* const obj = { x: 3 };
|
|
121
|
+
* merged(obj);
|
|
122
|
+
* // obj.x === 8 (3 + 1 = 4, then 4 * 2 = 8)
|
|
123
|
+
* ```
|
|
70
124
|
*/
|
|
71
125
|
export declare function mergeModifiers<T>(modifiers: ModifierFunction<T>[]): ModifierFunction<T>;
|
|
72
126
|
/**
|
|
73
|
-
* Merges
|
|
127
|
+
* Merges an array of {@link ModifierFunction} values into a single function. Returns undefined if the input is nullish.
|
|
128
|
+
*
|
|
129
|
+
* If only one modifier is provided, returns it directly without wrapping.
|
|
74
130
|
*
|
|
75
|
-
* @param
|
|
76
|
-
* @returns
|
|
131
|
+
* @param modifiers - array of modifier functions to merge, or undefined
|
|
77
132
|
*/
|
|
78
133
|
export declare function maybeMergeModifiers<T>(modifiers: Maybe<ModifierFunction<T>[]>): Maybe<ModifierFunction<T>>;
|