@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
|
@@ -22,6 +22,11 @@ export type EqualOptions = {
|
|
|
22
22
|
relaxedNullish?: boolean;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* An equalizer function for comparing values for equality, with predefined options
|
|
27
|
+
*
|
|
28
|
+
* Can be used to compare values, and register or deregister equality comparison handlers for specific classes
|
|
29
|
+
*/
|
|
25
30
|
type Equalizer = {
|
|
26
31
|
/**
|
|
27
32
|
* Are two strings equal?
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {NestedKeys, NestedValue, PlainObject
|
|
1
|
+
import type {NestedKeys, NestedValue, PlainObject} from '../../models';
|
|
2
2
|
import type {Ok} from '../../result/models';
|
|
3
3
|
import {getNestedValue} from './misc';
|
|
4
4
|
|
|
@@ -6,20 +6,42 @@ import {getNestedValue} from './misc';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Get the value from an object using a known path
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const data = {foo: {bar: {baz: 42}}};
|
|
13
|
+
*
|
|
14
|
+
* getValue(data, 'foo'); // {bar: {baz: 42}}
|
|
15
|
+
* getValue(data, 'foo.bar'); // {baz: 42}
|
|
16
|
+
* getValue(data, 'foo.bar.baz'); // 42
|
|
17
|
+
* getValue(data, 'foo.nope'); // undefined
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
9
20
|
* @param data Object to get value from
|
|
10
|
-
* @param path Path for value
|
|
21
|
+
* @param path Path for value
|
|
11
22
|
* @returns Found value, or `undefined`
|
|
12
23
|
*/
|
|
13
24
|
export function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
14
25
|
data: Data,
|
|
15
26
|
path: Path,
|
|
16
|
-
): NestedValue<Data,
|
|
27
|
+
): NestedValue<Data, Path>;
|
|
17
28
|
|
|
18
29
|
/**
|
|
19
30
|
* Get the value from an object using an unknown path
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const data = {foo: {bar: {baz: 42}}};
|
|
35
|
+
*
|
|
36
|
+
* getValue(data, 'foo'); // {bar: {baz: 42}}
|
|
37
|
+
* getValue(data, 'foo.bar'); // {baz: 42}
|
|
38
|
+
* getValue(data, 'Foo.Bar.Baz', true); // 42
|
|
39
|
+
* getValue(data, 'foo.nope'); // undefined
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
20
42
|
* @param data Object to get value from
|
|
21
|
-
* @param path Path for value
|
|
22
|
-
* @param ignoreCase If `true`,
|
|
43
|
+
* @param path Path for value
|
|
44
|
+
* @param ignoreCase If `true`, path matching is case-insensitive
|
|
23
45
|
* @returns Found value, or `undefined`
|
|
24
46
|
*/
|
|
25
47
|
export function getValue<Data extends PlainObject>(
|
|
@@ -8,7 +8,7 @@ import {getNestedValue} from './misc';
|
|
|
8
8
|
* Check if a nested property is defined in an object
|
|
9
9
|
* @param data Object to check in
|
|
10
10
|
* @param path Path for property
|
|
11
|
-
* @
|
|
11
|
+
* @returns `true` if the property exists, `false` otherwise
|
|
12
12
|
*/
|
|
13
13
|
export function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
14
14
|
data: Data,
|
|
@@ -20,7 +20,7 @@ export function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>
|
|
|
20
20
|
* @param data Object to check in
|
|
21
21
|
* @param path Path for property
|
|
22
22
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
23
|
-
* @
|
|
23
|
+
* @returns `true` if the property exists, `false` otherwise
|
|
24
24
|
*/
|
|
25
25
|
export function hasValue<Data extends PlainObject>(
|
|
26
26
|
data: Data,
|
|
@@ -41,13 +41,13 @@ hasValue.get = hasValueResult;
|
|
|
41
41
|
* @param data Object to check in
|
|
42
42
|
* @param path Path for property
|
|
43
43
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
44
|
-
* @
|
|
44
|
+
* @returns Result object
|
|
45
45
|
*/
|
|
46
|
-
function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
46
|
+
export function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
47
47
|
data: Data,
|
|
48
48
|
path: Path,
|
|
49
49
|
ignoreCase?: boolean,
|
|
50
|
-
): Result<NestedValue<Data, ToString<Path>>,
|
|
50
|
+
): Result<NestedValue<Data, ToString<Path>>, string>;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
@@ -56,19 +56,19 @@ function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>
|
|
|
56
56
|
* @param data Object to check in
|
|
57
57
|
* @param path Path for property
|
|
58
58
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
59
|
-
* @
|
|
59
|
+
* @returns Result object
|
|
60
60
|
*/
|
|
61
|
-
function hasValueResult<Data extends PlainObject>(
|
|
61
|
+
export function hasValueResult<Data extends PlainObject>(
|
|
62
62
|
data: Data,
|
|
63
63
|
path: string,
|
|
64
64
|
ignoreCase?: boolean,
|
|
65
|
-
): Result<unknown,
|
|
65
|
+
): Result<unknown, string>;
|
|
66
66
|
|
|
67
|
-
function hasValueResult(
|
|
67
|
+
export function hasValueResult(
|
|
68
68
|
data: PlainObject,
|
|
69
69
|
path: string,
|
|
70
70
|
ignoreCase?: boolean,
|
|
71
|
-
): Result<unknown,
|
|
71
|
+
): Result<unknown, string> {
|
|
72
72
|
return getNestedValue(data, path, ignoreCase === true);
|
|
73
73
|
}
|
|
74
74
|
|
|
@@ -17,14 +17,13 @@ export function getNestedValue(
|
|
|
17
17
|
data: object,
|
|
18
18
|
path: string,
|
|
19
19
|
ignoreCase: boolean,
|
|
20
|
-
): Result<unknown,
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return error(undefined);
|
|
20
|
+
): Result<unknown, string> {
|
|
21
|
+
if (typeof data !== 'object' || data === null) {
|
|
22
|
+
return error(NESTED_MESSAGE_INPUT);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof path !== 'string' || path.trim().length === 0) {
|
|
26
|
+
return error(NESTED_MESSAGE_PATH);
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
const shouldIgnoreCase = ignoreCase === true;
|
|
@@ -69,7 +68,7 @@ export function handleValue(
|
|
|
69
68
|
value: unknown,
|
|
70
69
|
get: true,
|
|
71
70
|
ignoreCase: boolean,
|
|
72
|
-
): Result<unknown,
|
|
71
|
+
): Result<unknown, string>;
|
|
73
72
|
|
|
74
73
|
export function handleValue(
|
|
75
74
|
data: object,
|
|
@@ -85,19 +84,23 @@ export function handleValue(
|
|
|
85
84
|
value: unknown,
|
|
86
85
|
get: boolean,
|
|
87
86
|
ignoreCase: boolean,
|
|
88
|
-
): Result<unknown,
|
|
89
|
-
if (typeof data === 'object' && data !== null
|
|
87
|
+
): Result<unknown, string> | void {
|
|
88
|
+
if (typeof data === 'object' && data !== null) {
|
|
89
|
+
if (ignoreKey(path)) {
|
|
90
|
+
return error(NESTED_MESSAGE_UNSAFE);
|
|
91
|
+
}
|
|
92
|
+
|
|
90
93
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
91
94
|
|
|
92
95
|
if (get) {
|
|
93
|
-
return key in data ? ok(data[key as never]) : error(
|
|
96
|
+
return key in data ? ok(data[key as never]) : error(NESTED_MESSAGE_MISSING);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
(data as PlainObject)[key] = typeof value === 'function' ? value(data[key as never]) : value;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
if (get) {
|
|
100
|
-
return error(
|
|
103
|
+
return error(NESTED_MESSAGE_MISSING);
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -111,4 +114,12 @@ const EXPRESSION_DOTS = /^\.|\.$/g;
|
|
|
111
114
|
|
|
112
115
|
const EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
113
116
|
|
|
117
|
+
const NESTED_MESSAGE_INPUT = 'Expected data to be an object';
|
|
118
|
+
|
|
119
|
+
const NESTED_MESSAGE_MISSING = 'Expected property to exist in object';
|
|
120
|
+
|
|
121
|
+
const NESTED_MESSAGE_PATH = 'Expected path to be a string';
|
|
122
|
+
|
|
123
|
+
const NESTED_MESSAGE_UNSAFE = 'Access to this property is not allowed';
|
|
124
|
+
|
|
114
125
|
// #endregion
|
package/src/logger.ts
CHANGED
|
@@ -2,6 +2,11 @@ import {noop} from './internal/function/misc';
|
|
|
2
2
|
|
|
3
3
|
// #region Types
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A logger that can be used to log messages to the console
|
|
7
|
+
*
|
|
8
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
9
|
+
*/
|
|
5
10
|
class Logger {
|
|
6
11
|
/**
|
|
7
12
|
* Log any number of values at the "debug" log level
|
|
@@ -83,12 +88,18 @@ class Logger {
|
|
|
83
88
|
}
|
|
84
89
|
}
|
|
85
90
|
|
|
91
|
+
/**
|
|
92
|
+
* A named timer that can be used to log durations to the console
|
|
93
|
+
*/
|
|
86
94
|
class Time {
|
|
87
95
|
#logger: typeof console.timeLog | undefined;
|
|
88
96
|
#stopper: typeof console.timeEnd | undefined;
|
|
89
97
|
|
|
90
98
|
readonly #state: TimeState;
|
|
91
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
102
|
+
*/
|
|
92
103
|
get active(): boolean {
|
|
93
104
|
return this.#state.started && !this.#state.stopped && enabled;
|
|
94
105
|
}
|
package/src/models.ts
CHANGED
|
@@ -260,4 +260,22 @@ export type TypedArray =
|
|
|
260
260
|
| BigInt64Array
|
|
261
261
|
| BigUint64Array;
|
|
262
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Converts a union type to an intersection type
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* type A = {a: string};
|
|
269
|
+
* type B = {b: number};
|
|
270
|
+
* type C = UnionToIntersection<A | B>; // {a: string} & {b: number}
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* Thanks, type-fest!
|
|
274
|
+
*/
|
|
275
|
+
export type UnionToIntersection<Union> = (
|
|
276
|
+
Union extends unknown ? (distributedUnion: Union) => void : never
|
|
277
|
+
) extends (mergedIntersection: infer Intersection) => void
|
|
278
|
+
? Intersection & Union
|
|
279
|
+
: never;
|
|
280
|
+
|
|
263
281
|
// #endregion
|
package/src/promise/index.ts
CHANGED
|
@@ -24,8 +24,6 @@ import {getTimedPromise} from './timed';
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
27
|
-
*
|
|
28
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
29
27
|
* @param promise Promise to wrap
|
|
30
28
|
* @param options Options for the promise
|
|
31
29
|
* @returns Wrapped promise
|
|
@@ -37,8 +35,6 @@ export async function attemptPromise<Value>(
|
|
|
37
35
|
|
|
38
36
|
/**
|
|
39
37
|
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
40
|
-
*
|
|
41
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
42
38
|
* @param callback Callback to wrap
|
|
43
39
|
* @param options Options for the promise
|
|
44
40
|
* @returns Promise-wrapped callback
|
|
@@ -50,8 +46,6 @@ export async function attemptPromise<Value>(
|
|
|
50
46
|
|
|
51
47
|
/**
|
|
52
48
|
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
53
|
-
*
|
|
54
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
55
49
|
* @param callback Callback to wrap
|
|
56
50
|
* @param options Options for the promise
|
|
57
51
|
* @returns Promise-wrapped callback
|
package/src/promise/models.ts
CHANGED
|
@@ -3,6 +3,9 @@ import type {Result} from '../result/models';
|
|
|
3
3
|
|
|
4
4
|
// #region Types
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A promise that can be canceled
|
|
8
|
+
*/
|
|
6
9
|
export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
7
10
|
#rejector!: (reason: unknown) => void;
|
|
8
11
|
|
|
@@ -29,8 +32,17 @@ export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
/**
|
|
36
|
+
* A promise that was fulfilled
|
|
37
|
+
*/
|
|
32
38
|
export type FulfilledPromise<Value> = {
|
|
39
|
+
/**
|
|
40
|
+
* Status of the promise
|
|
41
|
+
*/
|
|
33
42
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
43
|
+
/**
|
|
44
|
+
* Value of the promise
|
|
45
|
+
*/
|
|
34
46
|
value: Awaited<Value>;
|
|
35
47
|
};
|
|
36
48
|
|
|
@@ -44,6 +56,9 @@ export type PromiseHandlers = {
|
|
|
44
56
|
reject: (reason: unknown) => void;
|
|
45
57
|
};
|
|
46
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Options for a promise-handling function
|
|
61
|
+
*/
|
|
47
62
|
export type PromiseOptions = {
|
|
48
63
|
/**
|
|
49
64
|
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
@@ -75,6 +90,9 @@ export type PromiseParameters = {
|
|
|
75
90
|
*/
|
|
76
91
|
export type PromiseStrategy = 'complete' | 'first';
|
|
77
92
|
|
|
93
|
+
/**
|
|
94
|
+
* An error thrown when a promise times out
|
|
95
|
+
*/
|
|
78
96
|
export class PromiseTimeoutError extends Error {
|
|
79
97
|
constructor() {
|
|
80
98
|
super(PROMISE_MESSAGE_TIMEOUT);
|
|
@@ -93,8 +111,17 @@ export type PromisesItems<Items extends unknown[]> = {
|
|
|
93
111
|
: Promise<Items[ItemsKey]>;
|
|
94
112
|
};
|
|
95
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Options for handling multiple promises
|
|
116
|
+
*/
|
|
96
117
|
export type PromisesOptions = {
|
|
118
|
+
/**
|
|
119
|
+
* AbortSignal for aborting the promises; when aborted, the promises will reject with the reason of the signal
|
|
120
|
+
*/
|
|
97
121
|
signal?: AbortSignal;
|
|
122
|
+
/**
|
|
123
|
+
* Strategy for handling the promises; defaults to `complete`
|
|
124
|
+
*/
|
|
98
125
|
strategy?: PromiseStrategy;
|
|
99
126
|
};
|
|
100
127
|
|
|
@@ -126,8 +153,17 @@ export type PromisesValues<Items extends unknown[]> = {
|
|
|
126
153
|
: never;
|
|
127
154
|
};
|
|
128
155
|
|
|
156
|
+
/**
|
|
157
|
+
* A promise that was rejected
|
|
158
|
+
*/
|
|
129
159
|
export type RejectedPromise = {
|
|
160
|
+
/**
|
|
161
|
+
* Status of the promise
|
|
162
|
+
*/
|
|
130
163
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
164
|
+
/**
|
|
165
|
+
* Reason for the rejection
|
|
166
|
+
*/
|
|
131
167
|
reason: unknown;
|
|
132
168
|
};
|
|
133
169
|
|
package/src/queue.ts
CHANGED
|
@@ -5,6 +5,9 @@ import type {GenericAsyncCallback, GenericCallback} from './models';
|
|
|
5
5
|
|
|
6
6
|
type HandleType = 'clear' | 'pause' | 'resume';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
10
|
+
*/
|
|
8
11
|
class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
9
12
|
readonly #callback: GenericAsyncCallback;
|
|
10
13
|
|
|
@@ -240,6 +243,9 @@ class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Ca
|
|
|
240
243
|
}
|
|
241
244
|
}
|
|
242
245
|
|
|
246
|
+
/**
|
|
247
|
+
* A queue that can be used to manage (a)synchronous tasks
|
|
248
|
+
*/
|
|
243
249
|
class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
244
250
|
readonly #callback: GenericAsyncCallback;
|
|
245
251
|
|
|
@@ -487,6 +493,9 @@ class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, Callbac
|
|
|
487
493
|
}
|
|
488
494
|
}
|
|
489
495
|
|
|
496
|
+
/**
|
|
497
|
+
* An error thrown by the Queue when an operation fails
|
|
498
|
+
*/
|
|
490
499
|
class QueueError extends Error {
|
|
491
500
|
constructor(message: string) {
|
|
492
501
|
super(message);
|
|
@@ -510,9 +519,12 @@ type QueueOptions = {
|
|
|
510
519
|
maximum?: number;
|
|
511
520
|
};
|
|
512
521
|
|
|
522
|
+
/**
|
|
523
|
+
* A queued item
|
|
524
|
+
*/
|
|
513
525
|
type Queued<Value> = {
|
|
514
526
|
/**
|
|
515
|
-
* ID of the queued
|
|
527
|
+
* ID of the queued item _(can be used to remove it from the queue)_
|
|
516
528
|
*/
|
|
517
529
|
readonly id: number;
|
|
518
530
|
/**
|
package/src/result/index.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import {attemptPromise} from '../promise';
|
|
2
|
-
import {matchResult} from './match';
|
|
3
1
|
import {getError, ok} from './misc';
|
|
4
2
|
import type {ExtendedErr, ExtendedResult, Result} from './models';
|
|
5
|
-
import {attemptFlow} from './work/flow';
|
|
6
|
-
import {attemptPipe} from './work/pipe';
|
|
7
3
|
|
|
8
4
|
// #region Functions
|
|
9
5
|
|
|
@@ -99,9 +95,5 @@ export function attempt<Value, E>(
|
|
|
99
95
|
}
|
|
100
96
|
|
|
101
97
|
attempt.async = asyncAttempt;
|
|
102
|
-
attempt.flow = attemptFlow;
|
|
103
|
-
attempt.match = matchResult;
|
|
104
|
-
attempt.pipe = attemptPipe;
|
|
105
|
-
attempt.promise = attemptPromise;
|
|
106
98
|
|
|
107
99
|
// #endregion
|
package/src/result/match.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type {AnyResult, ExtendedErr, ResultMatch} from './models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Handles a result with match callbacks
|
|
8
|
+
*
|
|
9
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
8
10
|
* @param result Result to handle
|
|
9
11
|
* @param handler Match callbacks
|
|
10
12
|
*/
|
|
@@ -15,6 +17,8 @@ export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
19
|
* Handles a result with match callbacks
|
|
20
|
+
*
|
|
21
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
18
22
|
* @param result Result to handle
|
|
19
23
|
* @param ok Ok callback
|
|
20
24
|
* @param error Error callback
|
|
@@ -58,8 +62,6 @@ export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
|
58
62
|
|
|
59
63
|
/**
|
|
60
64
|
* Handles a result with match callbacks
|
|
61
|
-
*
|
|
62
|
-
* Available as `matchResult` and `attempt.match`
|
|
63
65
|
* @param result Result to handle
|
|
64
66
|
* @param handler Match callbacks
|
|
65
67
|
*/
|
|
@@ -70,8 +72,6 @@ export function matchResult<Value, Returned, E = Error>(
|
|
|
70
72
|
|
|
71
73
|
/**
|
|
72
74
|
* Handles a result with match callbacks
|
|
73
|
-
*
|
|
74
|
-
* Available as `matchResult` and `attempt.match`
|
|
75
75
|
* @param result Result to handle
|
|
76
76
|
* @param ok Ok callback
|
|
77
77
|
* @param error Error callback
|
package/src/result/work/flow.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type AttemptFlowPromise<Callback extends GenericCallback, Value> = (
|
|
|
29
29
|
/**
|
|
30
30
|
* Create an asynchronous Flow, a function that attempts to pipe a value through a series of functions
|
|
31
31
|
*
|
|
32
|
-
* Available as `attemptAsyncFlow` and `
|
|
32
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
33
33
|
* @returns Flow function
|
|
34
34
|
*/
|
|
35
35
|
export function attemptAsyncFlow<Fn extends GenericCallback>(
|
|
@@ -39,7 +39,7 @@ export function attemptAsyncFlow<Fn extends GenericCallback>(
|
|
|
39
39
|
/**
|
|
40
40
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
41
41
|
*
|
|
42
|
-
* Available as `attemptAsyncFlow` and `
|
|
42
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
43
43
|
* @returns Flow function
|
|
44
44
|
*/
|
|
45
45
|
export function attemptAsyncFlow<First extends GenericCallback, Second>(
|
|
@@ -50,7 +50,7 @@ export function attemptAsyncFlow<First extends GenericCallback, Second>(
|
|
|
50
50
|
/**
|
|
51
51
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
52
52
|
*
|
|
53
|
-
* Available as `attemptAsyncFlow` and `
|
|
53
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
54
54
|
* @returns Flow function
|
|
55
55
|
*/
|
|
56
56
|
export function attemptAsyncFlow<First extends GenericCallback, Second, Third>(
|
|
@@ -62,7 +62,7 @@ export function attemptAsyncFlow<First extends GenericCallback, Second, Third>(
|
|
|
62
62
|
/**
|
|
63
63
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
64
64
|
*
|
|
65
|
-
* Available as `attemptAsyncFlow` and `
|
|
65
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
66
66
|
* @returns Flow function
|
|
67
67
|
*/
|
|
68
68
|
export function attemptAsyncFlow<First extends GenericCallback, Second, Third, Fourth>(
|
|
@@ -75,7 +75,7 @@ export function attemptAsyncFlow<First extends GenericCallback, Second, Third, F
|
|
|
75
75
|
/**
|
|
76
76
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
77
77
|
*
|
|
78
|
-
* Available as `attemptAsyncFlow` and `
|
|
78
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
79
79
|
* @returns Flow function
|
|
80
80
|
*/
|
|
81
81
|
export function attemptAsyncFlow<First extends GenericCallback, Second, Third, Fourth, Fifth>(
|
|
@@ -89,7 +89,7 @@ export function attemptAsyncFlow<First extends GenericCallback, Second, Third, F
|
|
|
89
89
|
/**
|
|
90
90
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
91
91
|
*
|
|
92
|
-
* Available as `attemptAsyncFlow` and `
|
|
92
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
93
93
|
* @returns Flow function
|
|
94
94
|
*/
|
|
95
95
|
export function attemptAsyncFlow<
|
|
@@ -111,7 +111,7 @@ export function attemptAsyncFlow<
|
|
|
111
111
|
/**
|
|
112
112
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
113
113
|
*
|
|
114
|
-
* Available as `attemptAsyncFlow` and `
|
|
114
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
115
115
|
* @returns Flow function
|
|
116
116
|
*/
|
|
117
117
|
export function attemptAsyncFlow<
|
|
@@ -135,7 +135,7 @@ export function attemptAsyncFlow<
|
|
|
135
135
|
/**
|
|
136
136
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
137
137
|
*
|
|
138
|
-
* Available as `attemptAsyncFlow` and `
|
|
138
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
139
139
|
* @returns Flow function
|
|
140
140
|
*/
|
|
141
141
|
export function attemptAsyncFlow<
|
|
@@ -161,7 +161,7 @@ export function attemptAsyncFlow<
|
|
|
161
161
|
/**
|
|
162
162
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
163
163
|
*
|
|
164
|
-
* Available as `attemptAsyncFlow` and `
|
|
164
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
165
165
|
* @returns Flow function
|
|
166
166
|
*/
|
|
167
167
|
export function attemptAsyncFlow<
|
|
@@ -189,7 +189,7 @@ export function attemptAsyncFlow<
|
|
|
189
189
|
/**
|
|
190
190
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
191
191
|
*
|
|
192
|
-
* Available as `attemptAsyncFlow` and `
|
|
192
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
193
193
|
* @returns Flow function
|
|
194
194
|
*/
|
|
195
195
|
export function attemptAsyncFlow<
|
|
@@ -219,7 +219,7 @@ export function attemptAsyncFlow<
|
|
|
219
219
|
/**
|
|
220
220
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
221
221
|
*
|
|
222
|
-
* Available as `attemptAsyncFlow` and `
|
|
222
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
223
223
|
* @returns Flow function
|
|
224
224
|
*/
|
|
225
225
|
export function attemptAsyncFlow<Fn extends GenericCallback>(
|
|
@@ -230,7 +230,7 @@ export function attemptAsyncFlow<Fn extends GenericCallback>(
|
|
|
230
230
|
/**
|
|
231
231
|
* Create an asynchronous Flow, a function that pipes values through a series of functions
|
|
232
232
|
*
|
|
233
|
-
* Available as `attemptAsyncFlow` and `
|
|
233
|
+
* Available as `attemptAsyncFlow` and `attemptFlow.async`
|
|
234
234
|
* @returns Flow function
|
|
235
235
|
*/
|
|
236
236
|
export function attemptAsyncFlow(
|
|
@@ -264,16 +264,12 @@ export function attemptAsyncFlow(
|
|
|
264
264
|
|
|
265
265
|
/**
|
|
266
266
|
* Create a Flow, a function that attempts to pipe values through a function
|
|
267
|
-
*
|
|
268
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
269
267
|
* @returns Flow function
|
|
270
268
|
*/
|
|
271
269
|
export function attemptFlow<Fn extends GenericCallback>(fn: Fn): AttemptFlow<Fn, ReturnType<Fn>>;
|
|
272
270
|
|
|
273
271
|
/**
|
|
274
272
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
275
|
-
*
|
|
276
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
277
273
|
* @returns Flow function
|
|
278
274
|
*/
|
|
279
275
|
export function attemptFlow<First extends GenericCallback, Second>(
|
|
@@ -283,8 +279,6 @@ export function attemptFlow<First extends GenericCallback, Second>(
|
|
|
283
279
|
|
|
284
280
|
/**
|
|
285
281
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
286
|
-
*
|
|
287
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
288
282
|
* @returns Flow function
|
|
289
283
|
*/
|
|
290
284
|
export function attemptFlow<First extends GenericCallback, Second, Third>(
|
|
@@ -295,8 +289,6 @@ export function attemptFlow<First extends GenericCallback, Second, Third>(
|
|
|
295
289
|
|
|
296
290
|
/**
|
|
297
291
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
298
|
-
*
|
|
299
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
300
292
|
* @returns Flow function
|
|
301
293
|
*/
|
|
302
294
|
export function attemptFlow<First extends GenericCallback, Second, Third, Fourth>(
|
|
@@ -308,8 +300,6 @@ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth
|
|
|
308
300
|
|
|
309
301
|
/**
|
|
310
302
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
311
|
-
*
|
|
312
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
313
303
|
* @returns Flow function
|
|
314
304
|
*/
|
|
315
305
|
export function attemptFlow<First extends GenericCallback, Second, Third, Fourth, Fifth>(
|
|
@@ -322,8 +312,6 @@ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth
|
|
|
322
312
|
|
|
323
313
|
/**
|
|
324
314
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
325
|
-
*
|
|
326
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
327
315
|
* @returns Flow function
|
|
328
316
|
*/
|
|
329
317
|
export function attemptFlow<First extends GenericCallback, Second, Third, Fourth, Fifth, Sixth>(
|
|
@@ -337,8 +325,6 @@ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth
|
|
|
337
325
|
|
|
338
326
|
/**
|
|
339
327
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
340
|
-
*
|
|
341
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
342
328
|
* @returns Flow function
|
|
343
329
|
*/
|
|
344
330
|
export function attemptFlow<
|
|
@@ -361,8 +347,6 @@ export function attemptFlow<
|
|
|
361
347
|
|
|
362
348
|
/**
|
|
363
349
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
364
|
-
*
|
|
365
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
366
350
|
* @returns Flow function
|
|
367
351
|
*/
|
|
368
352
|
export function attemptFlow<
|
|
@@ -387,8 +371,6 @@ export function attemptFlow<
|
|
|
387
371
|
|
|
388
372
|
/**
|
|
389
373
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
390
|
-
*
|
|
391
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
392
374
|
* @returns Flow function
|
|
393
375
|
*/
|
|
394
376
|
export function attemptFlow<
|
|
@@ -415,8 +397,6 @@ export function attemptFlow<
|
|
|
415
397
|
|
|
416
398
|
/**
|
|
417
399
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
418
|
-
*
|
|
419
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
420
400
|
* @returns Flow function
|
|
421
401
|
*/
|
|
422
402
|
export function attemptFlow<
|
|
@@ -445,8 +425,6 @@ export function attemptFlow<
|
|
|
445
425
|
|
|
446
426
|
/**
|
|
447
427
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
448
|
-
*
|
|
449
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
450
428
|
* @returns Flow function
|
|
451
429
|
*/
|
|
452
430
|
export function attemptFlow<First extends GenericCallback>(
|
|
@@ -456,8 +434,6 @@ export function attemptFlow<First extends GenericCallback>(
|
|
|
456
434
|
|
|
457
435
|
/**
|
|
458
436
|
* Create a Flow, a function that attempts to pipe values through a series of functions
|
|
459
|
-
*
|
|
460
|
-
* Available as `attemptFlow` and `attempt.flow`
|
|
461
437
|
* @returns Flow function
|
|
462
438
|
*/
|
|
463
439
|
export function attemptFlow(...fns: GenericCallback[]): (...args: unknown[]) => Result<unknown>;
|