@oscarpalmer/atoms 0.184.1 → 0.185.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/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/{position.d.mts → match.d.mts} +9 -6
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.mjs +1 -1
- package/dist/array/single.mjs +2 -2
- package/dist/array/sort.d.mts +9 -4
- package/dist/array/sort.mjs +6 -6
- package/dist/array/swap.mjs +1 -1
- 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 +5 -2
- package/dist/color/misc/get.mjs +8 -8
- package/dist/color/misc/state.d.mts +2 -2
- package/dist/color/misc/state.mjs +2 -2
- 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/once.mjs +9 -9
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +11 -8
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +272 -160
- package/dist/index.mjs +279 -216
- package/dist/internal/number.d.mts +2 -1
- package/dist/internal/number.mjs +4 -1
- package/dist/internal/value/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/equal.mjs +5 -5
- package/dist/internal/value/get.d.mts +2 -2
- package/dist/internal/value/has.d.mts +3 -3
- 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 +1 -1
- package/dist/promise/helpers.mjs +3 -5
- 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 +14 -7
- 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/string/fuzzy.mjs +2 -2
- package/dist/value/handle.mjs +1 -1
- package/dist/value/merge.d.mts +0 -1
- package/dist/value/merge.mjs +0 -1
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +9 -0
- package/dist/value/unsmush.d.mts +3 -0
- package/package.json +3 -3
- package/src/array/difference.ts +4 -0
- package/src/array/from.ts +4 -0
- package/src/array/index.ts +1 -1
- package/src/array/intersection.ts +4 -0
- package/src/array/{position.ts → match.ts} +28 -25
- package/src/array/move.ts +5 -1
- package/src/array/reverse.ts +4 -0
- package/src/array/select.ts +2 -0
- package/src/array/single.ts +2 -2
- package/src/array/sort.ts +14 -9
- package/src/array/swap.ts +5 -1
- package/src/array/toggle.ts +4 -0
- package/src/array/union.ts +4 -0
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +11 -3
- package/src/color/misc/get.ts +8 -8
- package/src/color/misc/state.ts +1 -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 +13 -9
- package/src/function/retry.ts +11 -8
- package/src/internal/number.ts +6 -0
- package/src/internal/value/compare.ts +2 -1
- package/src/internal/value/equal.ts +10 -5
- package/src/internal/value/get.ts +2 -2
- package/src/internal/value/has.ts +6 -6
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/models.ts +1 -1
- package/src/promise/helpers.ts +3 -6
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +23 -11
- 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/string/fuzzy.ts +2 -2
- package/src/value/merge.ts +0 -1
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +9 -0
- package/src/value/unsmush.ts +3 -0
|
@@ -22,5 +22,6 @@ declare function clamp(value: number, minimum: number, maximum: number, loop?: b
|
|
|
22
22
|
* @returns Original value as a number, or `NaN` if the value is unable to be parsed
|
|
23
23
|
*/
|
|
24
24
|
declare function getNumber(value: unknown): number;
|
|
25
|
+
declare function getNumberOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
25
26
|
//#endregion
|
|
26
|
-
export { between, clamp, getNumber };
|
|
27
|
+
export { between, clamp, getNumber, getNumberOrDefault };
|
package/dist/internal/number.mjs
CHANGED
|
@@ -53,9 +53,12 @@ function getNumber(value) {
|
|
|
53
53
|
if (isBinary || EXPRESSION_OCTAL.test(trimmed)) return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
|
|
54
54
|
return Number(trimmed);
|
|
55
55
|
}
|
|
56
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
57
|
+
return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? Math.floor(value) : defaultValue;
|
|
58
|
+
}
|
|
56
59
|
const EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
57
60
|
const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
58
61
|
const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
59
62
|
const OCTAL_VALUE = 8;
|
|
60
63
|
//#endregion
|
|
61
|
-
export { between, clamp, getNumber };
|
|
64
|
+
export { between, clamp, getNumber, getNumberOrDefault };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Constructor, GenericCallback } from "../../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/compare.d.ts
|
|
4
|
+
type Comparator<Value = any> = (first: Value, second: Value) => number;
|
|
4
5
|
/**
|
|
5
6
|
* Compare two values _(for sorting purposes)_
|
|
6
7
|
* @param first First value
|
|
@@ -31,6 +32,6 @@ declare function deregisterComparator<Instance>(constructor: Constructor<Instanc
|
|
|
31
32
|
* @param constructor Class constructor
|
|
32
33
|
* @param handler Method name or comparison function _(defaults to `compare`)_
|
|
33
34
|
*/
|
|
34
|
-
declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string |
|
|
35
|
+
declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | Comparator<Instance>): void;
|
|
35
36
|
//#endregion
|
|
36
37
|
export { compare, deregisterComparator, registerComparator };
|
|
@@ -18,6 +18,11 @@ type EqualOptions = {
|
|
|
18
18
|
*/
|
|
19
19
|
relaxedNullish?: boolean;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* An equalizer function for comparing values for equality, with predefined options
|
|
23
|
+
*
|
|
24
|
+
* Can be used to compare values, and register or deregister equality comparison handlers for specific classes
|
|
25
|
+
*/
|
|
21
26
|
type Equalizer = {
|
|
22
27
|
/**
|
|
23
28
|
* Are two strings equal?
|
|
@@ -27,9 +27,9 @@ function equalArray(first, second, options) {
|
|
|
27
27
|
const { length } = first;
|
|
28
28
|
if (length !== second.length) return false;
|
|
29
29
|
let offset = 0;
|
|
30
|
-
if (length >=
|
|
31
|
-
offset = Math.round(length /
|
|
32
|
-
offset = offset >
|
|
30
|
+
if (length >= EQUAL_ARRAY_THRESHOLD) {
|
|
31
|
+
offset = Math.round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
|
|
32
|
+
offset = offset > EQUAL_ARRAY_THRESHOLD ? EQUAL_ARRAY_THRESHOLD : offset;
|
|
33
33
|
for (let index = 0; index < offset; index += 1) if (!(equalValue(first[index], second[index], options) && equalValue(first[length - index - 1], second[length - index - 1], options))) return false;
|
|
34
34
|
}
|
|
35
35
|
const end = length - offset;
|
|
@@ -164,8 +164,8 @@ function initializeEqualizer(options) {
|
|
|
164
164
|
function registerEqualizer(constructor, handler) {
|
|
165
165
|
equal.handlers.register(constructor, handler);
|
|
166
166
|
}
|
|
167
|
-
const
|
|
168
|
-
const
|
|
167
|
+
const EQUAL_ARRAY_PEEK_PERCENTAGE = 10;
|
|
168
|
+
const EQUAL_ARRAY_THRESHOLD = 100;
|
|
169
169
|
const ERROR_PROPERTIES = ["name", "message"];
|
|
170
170
|
const EXPRESSION_PROPERTIES = ["source", "flags"];
|
|
171
171
|
const MINIMUM_LENGTH_FOR_SET = 16;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NestedKeys, NestedValue, PlainObject
|
|
1
|
+
import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/get.d.ts
|
|
4
4
|
/**
|
|
@@ -7,7 +7,7 @@ import { NestedKeys, NestedValue, PlainObject, ToString } from "../../models.mjs
|
|
|
7
7
|
* @param path Path for value, e.g., `foo.bar.baz`
|
|
8
8
|
* @returns Found value, or `undefined`
|
|
9
9
|
*/
|
|
10
|
-
declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data,
|
|
10
|
+
declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
|
|
11
11
|
/**
|
|
12
12
|
* Get the value from an object using an unknown path
|
|
13
13
|
* @param data Object to get value from
|
|
@@ -29,7 +29,7 @@ declare namespace hasValue {
|
|
|
29
29
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
30
30
|
* @return Result object
|
|
31
31
|
*/
|
|
32
|
-
declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>,
|
|
32
|
+
declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>, string>;
|
|
33
33
|
/**
|
|
34
34
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
35
35
|
*
|
|
@@ -39,6 +39,6 @@ declare function hasValueResult<Data extends PlainObject, Path extends NestedKey
|
|
|
39
39
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
40
40
|
* @return Result object
|
|
41
41
|
*/
|
|
42
|
-
declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown,
|
|
42
|
+
declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
|
|
43
43
|
//#endregion
|
|
44
|
-
export { hasValue };
|
|
44
|
+
export { hasValue, hasValueResult };
|
|
@@ -2,9 +2,9 @@ import { Result } from "../../result/models.mjs";
|
|
|
2
2
|
|
|
3
3
|
//#region src/internal/value/misc.d.ts
|
|
4
4
|
declare function findKey(needle: string, haystack: object): string;
|
|
5
|
-
declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown,
|
|
5
|
+
declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown, string>;
|
|
6
6
|
declare function getPaths(path: string, lowercase: boolean): string | string[];
|
|
7
|
-
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown,
|
|
7
|
+
declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown, string>;
|
|
8
8
|
declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
|
|
9
9
|
//#endregion
|
|
10
10
|
export { findKey, getNestedValue, getPaths, handleValue };
|
|
@@ -7,7 +7,8 @@ function findKey(needle, haystack) {
|
|
|
7
7
|
return index > -1 ? keys[index] : needle;
|
|
8
8
|
}
|
|
9
9
|
function getNestedValue(data, path, ignoreCase) {
|
|
10
|
-
if (typeof data !== "object" || data === null
|
|
10
|
+
if (typeof data !== "object" || data === null) return error(NESTED_MESSAGE_INPUT);
|
|
11
|
+
if (typeof path !== "string" || path.trim().length === 0) return error(NESTED_MESSAGE_PATH);
|
|
11
12
|
const shouldIgnoreCase = ignoreCase === true;
|
|
12
13
|
const paths = getPaths(path, shouldIgnoreCase);
|
|
13
14
|
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
@@ -27,15 +28,20 @@ function getPaths(path, lowercase) {
|
|
|
27
28
|
return normalized.replace(EXPRESSION_BRACKET, ".$1").replace(EXPRESSION_DOTS, "").split(".");
|
|
28
29
|
}
|
|
29
30
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
30
|
-
if (typeof data === "object" && data !== null
|
|
31
|
+
if (typeof data === "object" && data !== null) {
|
|
32
|
+
if (ignoreKey(path)) return error(NESTED_MESSAGE_UNSAFE);
|
|
31
33
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
32
|
-
if (get) return key in data ? ok(data[key]) : error(
|
|
34
|
+
if (get) return key in data ? ok(data[key]) : error(NESTED_MESSAGE_MISSING);
|
|
33
35
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
34
36
|
}
|
|
35
|
-
if (get) return error(
|
|
37
|
+
if (get) return error(NESTED_MESSAGE_MISSING);
|
|
36
38
|
}
|
|
37
39
|
const EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
38
40
|
const EXPRESSION_DOTS = /^\.|\.$/g;
|
|
39
41
|
const EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
42
|
+
const NESTED_MESSAGE_INPUT = "Expected data to be an object";
|
|
43
|
+
const NESTED_MESSAGE_MISSING = "Expected property to exist in object";
|
|
44
|
+
const NESTED_MESSAGE_PATH = "Expected path to be a string";
|
|
45
|
+
const NESTED_MESSAGE_UNSAFE = "Access to this property is not allowed";
|
|
40
46
|
//#endregion
|
|
41
47
|
export { findKey, getNestedValue, getPaths, handleValue };
|
package/dist/logger.d.mts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
//#region src/logger.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A logger that can be used to log messages to the console
|
|
4
|
+
*
|
|
5
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
6
|
+
*/
|
|
2
7
|
declare class Logger {
|
|
3
8
|
/**
|
|
4
9
|
* Log any number of values at the "debug" log level
|
|
@@ -47,8 +52,14 @@ declare class Logger {
|
|
|
47
52
|
*/
|
|
48
53
|
time(label: string): Time;
|
|
49
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* A named timer that can be used to log durations to the console
|
|
57
|
+
*/
|
|
50
58
|
declare class Time {
|
|
51
59
|
#private;
|
|
60
|
+
/**
|
|
61
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
62
|
+
*/
|
|
52
63
|
get active(): boolean;
|
|
53
64
|
/**
|
|
54
65
|
* Log the current duration of the timer _(ignored if logging is disabled)_
|
package/dist/logger.mjs
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
//#region src/logger.ts
|
|
3
|
+
/**
|
|
4
|
+
* A logger that can be used to log messages to the console
|
|
5
|
+
*
|
|
6
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
7
|
+
*/
|
|
3
8
|
var Logger = class {
|
|
4
9
|
/**
|
|
5
10
|
* Log any number of values at the "debug" log level
|
|
@@ -70,10 +75,16 @@ var Logger = class {
|
|
|
70
75
|
return new Time(label);
|
|
71
76
|
}
|
|
72
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* A named timer that can be used to log durations to the console
|
|
80
|
+
*/
|
|
73
81
|
var Time = class {
|
|
74
82
|
#logger;
|
|
75
83
|
#stopper;
|
|
76
84
|
#state;
|
|
85
|
+
/**
|
|
86
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
87
|
+
*/
|
|
77
88
|
get active() {
|
|
78
89
|
return this.#state.started && !this.#state.stopped && enabled;
|
|
79
90
|
}
|
package/dist/models.d.mts
CHANGED
|
@@ -13,7 +13,7 @@ type AsyncCancelableCallback<Callback extends GenericAsyncCallback | GenericCall
|
|
|
13
13
|
cancel: () => void;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
|
-
* For
|
|
16
|
+
* For matching any `void`, `Date`, primitive, or `RegExp` values
|
|
17
17
|
*
|
|
18
18
|
* (Thanks, type-fest!)
|
|
19
19
|
*/
|
package/dist/promise/helpers.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { error, ok } from "../result/misc.mjs";
|
|
2
|
+
import { getNumberOrDefault } from "../internal/number.mjs";
|
|
2
3
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.mjs";
|
|
3
4
|
//#region src/promise/helpers.ts
|
|
4
|
-
function getNumberOrDefault(value) {
|
|
5
|
-
return typeof value === "number" && value > 0 ? value : 0;
|
|
6
|
-
}
|
|
7
5
|
function getPromiseOptions(input) {
|
|
8
|
-
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
6
|
+
if (typeof input === "number") return { time: getNumberOrDefault(input, 0) };
|
|
9
7
|
if (input instanceof AbortSignal) return {
|
|
10
8
|
signal: input,
|
|
11
9
|
time: 0
|
|
@@ -13,7 +11,7 @@ function getPromiseOptions(input) {
|
|
|
13
11
|
const options = typeof input === "object" && input !== null ? input : {};
|
|
14
12
|
return {
|
|
15
13
|
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
16
|
-
time: getNumberOrDefault(options.time)
|
|
14
|
+
time: getNumberOrDefault(options.time, 0)
|
|
17
15
|
};
|
|
18
16
|
}
|
|
19
17
|
function getPromisesOptions(input) {
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -4,8 +4,6 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
4
4
|
//#region src/promise/index.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
7
|
-
*
|
|
8
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
9
7
|
* @param promise Promise to wrap
|
|
10
8
|
* @param options Options for the promise
|
|
11
9
|
* @returns Wrapped promise
|
|
@@ -13,8 +11,6 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
13
11
|
declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
14
12
|
/**
|
|
15
13
|
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
16
|
-
*
|
|
17
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
18
14
|
* @param callback Callback to wrap
|
|
19
15
|
* @param options Options for the promise
|
|
20
16
|
* @returns Promise-wrapped callback
|
|
@@ -22,8 +18,6 @@ declare function attemptPromise<Value>(promise: Promise<Value>, options?: Promis
|
|
|
22
18
|
declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
23
19
|
/**
|
|
24
20
|
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
25
|
-
*
|
|
26
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
27
21
|
* @param callback Callback to wrap
|
|
28
22
|
* @param options Options for the promise
|
|
29
23
|
* @returns Promise-wrapped callback
|
|
@@ -2,6 +2,9 @@ import { GenericCallback } from "../models.mjs";
|
|
|
2
2
|
import { Result } from "../result/models.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/promise/models.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A promise that can be canceled
|
|
7
|
+
*/
|
|
5
8
|
declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
6
9
|
#private;
|
|
7
10
|
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
@@ -11,8 +14,17 @@ declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
11
14
|
*/
|
|
12
15
|
cancel(reason?: unknown): void;
|
|
13
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* A promise that was fulfilled
|
|
19
|
+
*/
|
|
14
20
|
type FulfilledPromise<Value> = {
|
|
21
|
+
/**
|
|
22
|
+
* Status of the promise
|
|
23
|
+
*/
|
|
15
24
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
25
|
+
/**
|
|
26
|
+
* Value of the promise
|
|
27
|
+
*/
|
|
16
28
|
value: Awaited<Value>;
|
|
17
29
|
};
|
|
18
30
|
type PromiseData = {
|
|
@@ -23,6 +35,9 @@ type PromiseHandlers = {
|
|
|
23
35
|
resolve: (value: unknown[]) => void;
|
|
24
36
|
reject: (reason: unknown) => void;
|
|
25
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Options for a promise-handling function
|
|
40
|
+
*/
|
|
26
41
|
type PromiseOptions = {
|
|
27
42
|
/**
|
|
28
43
|
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
@@ -51,20 +66,41 @@ type PromiseParameters = {
|
|
|
51
66
|
* - Returns an array of values
|
|
52
67
|
*/
|
|
53
68
|
type PromiseStrategy = 'complete' | 'first';
|
|
69
|
+
/**
|
|
70
|
+
* An error thrown when a promise times out
|
|
71
|
+
*/
|
|
54
72
|
declare class PromiseTimeoutError extends Error {
|
|
55
73
|
constructor();
|
|
56
74
|
}
|
|
57
75
|
type PromisesItems<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Promise<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Promise<Value> : Promise<Items[ItemsKey]> };
|
|
76
|
+
/**
|
|
77
|
+
* Options for handling multiple promises
|
|
78
|
+
*/
|
|
58
79
|
type PromisesOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* AbortSignal for aborting the promises; when aborted, the promises will reject with the reason of the signal
|
|
82
|
+
*/
|
|
59
83
|
signal?: AbortSignal;
|
|
84
|
+
/**
|
|
85
|
+
* Strategy for handling the promises; defaults to `complete`
|
|
86
|
+
*/
|
|
60
87
|
strategy?: PromiseStrategy;
|
|
61
88
|
};
|
|
62
89
|
type PromisesResult<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? Result<Awaited<Value>> : never };
|
|
63
90
|
type PromisesUnwrapped<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? Awaited<Value> : never : Items[ItemsKey] extends Promise<infer Value> ? Awaited<Value> : never };
|
|
64
91
|
type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
65
92
|
type PromisesValues<Items extends unknown[]> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback ? ReturnType<Items[ItemsKey]> extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never : Items[ItemsKey] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never };
|
|
93
|
+
/**
|
|
94
|
+
* A promise that was rejected
|
|
95
|
+
*/
|
|
66
96
|
type RejectedPromise = {
|
|
97
|
+
/**
|
|
98
|
+
* Status of the promise
|
|
99
|
+
*/
|
|
67
100
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
101
|
+
/**
|
|
102
|
+
* Reason for the rejection
|
|
103
|
+
*/
|
|
68
104
|
reason: unknown;
|
|
69
105
|
};
|
|
70
106
|
declare const PROMISE_ABORT_EVENT = "abort";
|
package/dist/promise/models.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
//#region src/promise/models.ts
|
|
2
|
+
/**
|
|
3
|
+
* A promise that can be canceled
|
|
4
|
+
*/
|
|
2
5
|
var CancelablePromise = class extends Promise {
|
|
3
6
|
#rejector;
|
|
4
7
|
constructor(executor) {
|
|
@@ -17,6 +20,9 @@ var CancelablePromise = class extends Promise {
|
|
|
17
20
|
this.#rejector(reason);
|
|
18
21
|
}
|
|
19
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* An error thrown when a promise times out
|
|
25
|
+
*/
|
|
20
26
|
var PromiseTimeoutError = class extends Error {
|
|
21
27
|
constructor() {
|
|
22
28
|
super(PROMISE_MESSAGE_TIMEOUT);
|
package/dist/queue.d.mts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { GenericAsyncCallback, GenericCallback } from "./models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/queue.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
6
|
+
*/
|
|
4
7
|
declare class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
5
8
|
#private;
|
|
6
9
|
/**
|
|
@@ -91,6 +94,9 @@ declare class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallb
|
|
|
91
94
|
*/
|
|
92
95
|
resume(key?: string): void;
|
|
93
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* A queue that can be used to manage (a)synchronous tasks
|
|
99
|
+
*/
|
|
94
100
|
declare class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
95
101
|
#private;
|
|
96
102
|
/**
|
|
@@ -154,6 +160,9 @@ declare class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>,
|
|
|
154
160
|
*/
|
|
155
161
|
resume(): void;
|
|
156
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* An error thrown by the Queue when an operation fails
|
|
165
|
+
*/
|
|
157
166
|
declare class QueueError extends Error {
|
|
158
167
|
constructor(message: string);
|
|
159
168
|
}
|
|
@@ -171,9 +180,12 @@ type QueueOptions = {
|
|
|
171
180
|
*/
|
|
172
181
|
maximum?: number;
|
|
173
182
|
};
|
|
183
|
+
/**
|
|
184
|
+
* A queued item
|
|
185
|
+
*/
|
|
174
186
|
type Queued<Value> = {
|
|
175
187
|
/**
|
|
176
|
-
* ID of the queued
|
|
188
|
+
* ID of the queued item _(can be used to remove it from the queue)_
|
|
177
189
|
*/
|
|
178
190
|
readonly id: number;
|
|
179
191
|
/**
|
package/dist/queue.mjs
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { getNumberOrDefault } from "./internal/number.mjs";
|
|
1
2
|
//#region src/queue.ts
|
|
3
|
+
/**
|
|
4
|
+
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
5
|
+
*/
|
|
2
6
|
var KeyedQueue = class {
|
|
3
7
|
#callback;
|
|
4
8
|
#options;
|
|
@@ -148,6 +152,9 @@ var KeyedQueue = class {
|
|
|
148
152
|
for (const queue of queues) queue[type]();
|
|
149
153
|
}
|
|
150
154
|
};
|
|
155
|
+
/**
|
|
156
|
+
* A queue that can be used to manage (a)synchronous tasks
|
|
157
|
+
*/
|
|
151
158
|
var Queue = class {
|
|
152
159
|
#callback;
|
|
153
160
|
#handled = [];
|
|
@@ -315,16 +322,19 @@ var Queue = class {
|
|
|
315
322
|
if (this.#paused) {
|
|
316
323
|
const paused = item;
|
|
317
324
|
this.#handled.push(() => {
|
|
318
|
-
|
|
325
|
+
handleQueuedResult(paused, error, result, this.#items.length === 0);
|
|
319
326
|
});
|
|
320
327
|
break;
|
|
321
328
|
}
|
|
322
|
-
|
|
329
|
+
handleQueuedResult(item, error, result, this.#items.length === 0);
|
|
323
330
|
item = this.#items.shift();
|
|
324
331
|
}
|
|
325
332
|
this.#runners -= 1;
|
|
326
333
|
}
|
|
327
334
|
};
|
|
335
|
+
/**
|
|
336
|
+
* An error thrown by the Queue when an operation fails
|
|
337
|
+
*/
|
|
328
338
|
var QueueError = class extends Error {
|
|
329
339
|
constructor(message) {
|
|
330
340
|
super(message);
|
|
@@ -334,18 +344,15 @@ var QueueError = class extends Error {
|
|
|
334
344
|
function getBooleanOrDefault(value, defaultValue) {
|
|
335
345
|
return typeof value === "boolean" ? value : defaultValue;
|
|
336
346
|
}
|
|
337
|
-
function getNumberOrDefault(value, defaultValue) {
|
|
338
|
-
return typeof value === "number" && value > 0 ? Math.floor(value) : defaultValue;
|
|
339
|
-
}
|
|
340
347
|
function getOptions(input) {
|
|
341
348
|
const options = typeof input === "object" && input != null ? input : {};
|
|
342
349
|
return {
|
|
343
350
|
autostart: getBooleanOrDefault(options.autostart, true),
|
|
344
|
-
concurrency: getNumberOrDefault(options.concurrency, 1),
|
|
351
|
+
concurrency: getNumberOrDefault(options.concurrency, 1, 1),
|
|
345
352
|
maximum: getNumberOrDefault(options.maximum, 0)
|
|
346
353
|
};
|
|
347
354
|
}
|
|
348
|
-
function
|
|
355
|
+
function handleQueuedResult(item, error, result, finished) {
|
|
349
356
|
item.signal?.removeEventListener(EVENT_NAME, item.abort);
|
|
350
357
|
if (item.signal?.aborted ?? false) item.reject();
|
|
351
358
|
else if (error) item.reject(result);
|
package/dist/result/index.d.mts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { ExtendedResult, Result } from "./models.mjs";
|
|
2
|
-
import { attemptPromise } from "../promise/index.mjs";
|
|
3
|
-
import { matchResult } from "./match.mjs";
|
|
4
|
-
import { attemptFlow } from "./work/flow.mjs";
|
|
5
|
-
import { attemptPipe } from "./work/pipe.mjs";
|
|
6
2
|
|
|
7
3
|
//#region src/result/index.d.ts
|
|
8
4
|
/**
|
|
@@ -54,10 +50,6 @@ declare function attempt<Value, E>(callback: () => Value, error: E): ExtendedRes
|
|
|
54
50
|
declare function attempt<Value>(callback: () => Value): Result<Value, Error>;
|
|
55
51
|
declare namespace attempt {
|
|
56
52
|
var async: typeof asyncAttempt;
|
|
57
|
-
var flow: typeof attemptFlow;
|
|
58
|
-
var match: typeof matchResult;
|
|
59
|
-
var pipe: typeof attemptPipe;
|
|
60
|
-
var promise: typeof attemptPromise;
|
|
61
53
|
}
|
|
62
54
|
//#endregion
|
|
63
55
|
export { asyncAttempt, attempt };
|
package/dist/result/index.mjs
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { getError, ok } from "./misc.mjs";
|
|
2
|
-
import { attemptPromise } from "../promise/index.mjs";
|
|
3
|
-
import { matchResult } from "./match.mjs";
|
|
4
|
-
import { attemptFlow } from "./work/flow.mjs";
|
|
5
|
-
import { attemptPipe } from "./work/pipe.mjs";
|
|
6
2
|
//#region src/result/index.ts
|
|
7
3
|
async function asyncAttempt(value, err) {
|
|
8
4
|
try {
|
|
@@ -21,9 +17,5 @@ function attempt(callback, err) {
|
|
|
21
17
|
}
|
|
22
18
|
}
|
|
23
19
|
attempt.async = asyncAttempt;
|
|
24
|
-
attempt.flow = attemptFlow;
|
|
25
|
-
attempt.match = matchResult;
|
|
26
|
-
attempt.pipe = attemptPipe;
|
|
27
|
-
attempt.promise = attemptPromise;
|
|
28
20
|
//#endregion
|
|
29
21
|
export { asyncAttempt, attempt };
|
package/dist/result/match.d.mts
CHANGED
|
@@ -3,12 +3,16 @@ import { AnyResult, ResultMatch } from "./models.mjs";
|
|
|
3
3
|
//#region src/result/match.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Handles a result with match callbacks
|
|
6
|
+
*
|
|
7
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
6
8
|
* @param result Result to handle
|
|
7
9
|
* @param handler Match callbacks
|
|
8
10
|
*/
|
|
9
11
|
declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>), handler: ResultMatch<Value, Returned, E>): Promise<Returned>;
|
|
10
12
|
/**
|
|
11
13
|
* Handles a result with match callbacks
|
|
14
|
+
*
|
|
15
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
12
16
|
* @param result Result to handle
|
|
13
17
|
* @param ok Ok callback
|
|
14
18
|
* @param error Error callback
|
|
@@ -16,16 +20,12 @@ declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<
|
|
|
16
20
|
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
21
|
/**
|
|
18
22
|
* Handles a result with match callbacks
|
|
19
|
-
*
|
|
20
|
-
* Available as `matchResult` and `attempt.match`
|
|
21
23
|
* @param result Result to handle
|
|
22
24
|
* @param handler Match callbacks
|
|
23
25
|
*/
|
|
24
26
|
declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | (() => AnyResult<Value, E>), handler: ResultMatch<Value, Returned, E>): Returned;
|
|
25
27
|
/**
|
|
26
28
|
* Handles a result with match callbacks
|
|
27
|
-
*
|
|
28
|
-
* Available as `matchResult` and `attempt.match`
|
|
29
29
|
* @param result Result to handle
|
|
30
30
|
* @param ok Ok callback
|
|
31
31
|
* @param error Error callback
|