@oscarpalmer/atoms 0.198.0 → 0.200.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/flatten.d.mts +1 -1
- package/dist/array/from.d.mts +1 -1
- package/dist/array/group-by.d.mts +2 -2
- package/dist/array/sort.d.mts +4 -0
- package/dist/array/to-map.d.mts +2 -2
- package/dist/array/to-record.d.mts +2 -2
- package/dist/beacon.mjs +46 -43
- package/dist/color/instance.mjs +14 -10
- package/dist/function/retry.mjs +6 -8
- package/dist/index.d.mts +153 -53
- package/dist/index.mjs +347 -259
- package/dist/internal/array/compact.d.mts +2 -2
- package/dist/internal/array/find.d.mts +2 -2
- package/dist/internal/array/find.mjs +11 -11
- package/dist/internal/function/memoize.mjs +4 -3
- package/dist/internal/function/timer.mjs +3 -5
- package/dist/internal/herald.mjs +26 -23
- package/dist/internal/subscription.d.mts +72 -16
- package/dist/internal/subscription.mjs +94 -29
- package/dist/internal/value/compare.mjs +2 -2
- package/dist/internal/value/equal.mjs +30 -30
- package/dist/internal/value/handlers.d.mts +3 -3
- package/dist/internal/value/handlers.mjs +6 -6
- package/dist/promise/constants.d.mts +3 -1
- package/dist/promise/constants.mjs +3 -1
- package/dist/promise/delay.mjs +2 -2
- package/dist/promise/helpers.d.mts +4 -5
- package/dist/promise/helpers.mjs +13 -5
- package/dist/promise/index.d.mts +34 -36
- package/dist/promise/index.mjs +28 -23
- package/dist/promise/misc.mjs +2 -2
- package/dist/promise/models.d.mts +7 -6
- package/dist/promise/timed.mjs +2 -2
- package/dist/queue.d.mts +4 -0
- package/dist/queue.mjs +3 -3
- package/dist/string/fuzzy.d.mts +2 -0
- package/dist/string/fuzzy.mjs +20 -18
- package/dist/string/normalize.mjs +12 -12
- package/dist/string/template.mjs +8 -8
- package/dist/subscription.d.mts +2 -2
- package/dist/subscription.mjs +2 -2
- package/dist/value/clone.mjs +13 -13
- package/dist/value/freeze.d.mts +2 -0
- package/dist/value/freeze.mjs +2 -0
- package/dist/value/merge.d.mts +1 -1
- package/dist/value/merge.mjs +7 -6
- package/package.json +4 -4
- package/plugin/index.js +1 -1
|
@@ -4,15 +4,15 @@ type Options = {
|
|
|
4
4
|
callback: GenericCallback;
|
|
5
5
|
method?: string;
|
|
6
6
|
};
|
|
7
|
-
declare function
|
|
7
|
+
declare function createCompareHandlers<Value>(owner: GenericCallback, options: Options): {
|
|
8
8
|
deregister(constructor: Constructor): void;
|
|
9
9
|
handle(first: unknown, second: unknown, ...parameters: unknown[]): Value;
|
|
10
10
|
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
11
11
|
};
|
|
12
|
-
declare function
|
|
12
|
+
declare function createSelfHandlers(owner: GenericCallback, options: Options): {
|
|
13
13
|
deregister(constructor: Constructor): void;
|
|
14
14
|
handle(value: unknown, ...parameters: unknown[]): unknown;
|
|
15
15
|
register(constructor: Constructor, handler?: string | GenericCallback): void;
|
|
16
16
|
};
|
|
17
17
|
//#endregion
|
|
18
|
-
export {
|
|
18
|
+
export { createCompareHandlers, createSelfHandlers };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isNonConstructor } from "../is.mjs";
|
|
2
2
|
//#region src/internal/value/handlers.ts
|
|
3
|
-
function
|
|
4
|
-
const handlers =
|
|
3
|
+
function createCompareHandlers(owner, options) {
|
|
4
|
+
const handlers = createHandlers(owner, options);
|
|
5
5
|
return {
|
|
6
6
|
deregister(constructor) {
|
|
7
7
|
handlers.deregister(constructor);
|
|
@@ -16,7 +16,7 @@ function getCompareHandlers(owner, options) {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function createHandlers(owner, options) {
|
|
20
20
|
const handlers = /* @__PURE__ */ new WeakMap();
|
|
21
21
|
return {
|
|
22
22
|
deregister(constructor) {
|
|
@@ -34,8 +34,8 @@ function getHandlers(owner, options) {
|
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
function
|
|
38
|
-
const handlers =
|
|
37
|
+
function createSelfHandlers(owner, options) {
|
|
38
|
+
const handlers = createHandlers(owner, options);
|
|
39
39
|
return {
|
|
40
40
|
deregister(constructor) {
|
|
41
41
|
handlers.deregister(constructor);
|
|
@@ -54,4 +54,4 @@ function isConstructable(value) {
|
|
|
54
54
|
return typeof value === "object" && value !== null;
|
|
55
55
|
}
|
|
56
56
|
//#endregion
|
|
57
|
-
export {
|
|
57
|
+
export { createCompareHandlers, createSelfHandlers };
|
|
@@ -6,6 +6,8 @@ declare const PROMISE_ABORT_OPTIONS: {
|
|
|
6
6
|
};
|
|
7
7
|
declare const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
8
8
|
declare const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
9
|
+
declare const PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY = "promises expected at least one promise-function or promise in the array or object";
|
|
10
|
+
declare const PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE = "promises expected an array or object holding promise-functions or promises";
|
|
9
11
|
declare const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
10
12
|
declare const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
11
13
|
declare const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
@@ -14,4 +16,4 @@ declare const PROMISE_STRATEGY_DEFAULT: PromiseStrategy;
|
|
|
14
16
|
declare const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
15
17
|
declare const PROMISE_TYPE_REJECTED = "rejected";
|
|
16
18
|
//#endregion
|
|
17
|
-
export { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED };
|
|
19
|
+
export { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY, PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED };
|
|
@@ -3,6 +3,8 @@ const PROMISE_ABORT_EVENT = "abort";
|
|
|
3
3
|
const PROMISE_ABORT_OPTIONS = { once: true };
|
|
4
4
|
const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
5
5
|
const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
6
|
+
const PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY = "promises expected at least one promise-function or promise in the array or object";
|
|
7
|
+
const PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE = "promises expected an array or object holding promise-functions or promises";
|
|
6
8
|
const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
7
9
|
const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
8
10
|
const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
@@ -11,4 +13,4 @@ const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
|
11
13
|
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
12
14
|
const PROMISE_TYPE_REJECTED = "rejected";
|
|
13
15
|
//#endregion
|
|
14
|
-
export { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED };
|
|
16
|
+
export { PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY, PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED };
|
package/dist/promise/delay.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createAborter } from "../internal/abort.mjs";
|
|
2
2
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { createPromiseOptions } from "./helpers.mjs";
|
|
4
4
|
import { settlePromise } from "./misc.mjs";
|
|
5
5
|
//#region src/promise/delay.ts
|
|
6
6
|
function delay(options) {
|
|
7
|
-
const { signal, time } =
|
|
7
|
+
const { signal, time } = createPromiseOptions(options);
|
|
8
8
|
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
9
9
|
const aborter = createAborter(signal, () => {
|
|
10
10
|
timer.cancel();
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { RequiredKeys } from "../models.mjs";
|
|
2
|
-
import { Result } from "../internal/result/models.mjs";
|
|
3
2
|
import { FulfilledPromise, PromiseOptions, PromiseStrategy, PromisesOptions, PromisesValue, RejectedPromise } from "./models.mjs";
|
|
4
3
|
//#region src/promise/helpers.d.ts
|
|
5
|
-
declare function
|
|
6
|
-
declare function
|
|
7
|
-
declare function getResultsFromPromises
|
|
4
|
+
declare function createPromiseOptions(input: unknown): RequiredKeys<PromiseOptions, 'time'>;
|
|
5
|
+
declare function createPromisesOptions(input: unknown): RequiredKeys<PromisesOptions, 'strategy'>;
|
|
6
|
+
declare function getResultsFromPromises(promised: Array<PromisesValue<unknown>> | Record<string, PromisesValue<unknown>>): unknown;
|
|
8
7
|
declare function getStrategyOrDefault(value: unknown): PromiseStrategy;
|
|
9
8
|
/**
|
|
10
9
|
* Is the value a fulfilled _Promise_ result?
|
|
@@ -21,4 +20,4 @@ declare function isFulfilled<Value>(value: unknown): value is FulfilledPromise<V
|
|
|
21
20
|
*/
|
|
22
21
|
declare function isRejected(value: unknown): value is RejectedPromise;
|
|
23
22
|
//#endregion
|
|
24
|
-
export {
|
|
23
|
+
export { createPromiseOptions, createPromisesOptions, getResultsFromPromises, getStrategyOrDefault, isFulfilled, isRejected };
|
package/dist/promise/helpers.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { error, ok } from "../internal/result/misc.mjs";
|
|
2
1
|
import { getNumberOrDefault } from "../internal/defaults.mjs";
|
|
2
|
+
import { error, ok } from "../internal/result/misc.mjs";
|
|
3
3
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
|
|
4
4
|
//#region src/promise/helpers.ts
|
|
5
|
-
function
|
|
5
|
+
function createPromiseOptions(input) {
|
|
6
6
|
if (typeof input === "number") return { time: getNumberOrDefault(input, 0) };
|
|
7
7
|
if (input instanceof AbortSignal) return {
|
|
8
8
|
signal: input,
|
|
@@ -14,7 +14,7 @@ function getPromiseOptions(input) {
|
|
|
14
14
|
time: getNumberOrDefault(options.time, 0)
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function createPromisesOptions(input) {
|
|
18
18
|
if (typeof input === "string") return { strategy: getStrategyOrDefault(input) };
|
|
19
19
|
if (input instanceof AbortSignal) return {
|
|
20
20
|
signal: input,
|
|
@@ -27,7 +27,15 @@ function getPromisesOptions(input) {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
function getResultsFromPromises(promised) {
|
|
30
|
-
|
|
30
|
+
const isArray = Array.isArray(promised);
|
|
31
|
+
const entries = isArray ? promised.map((value, index) => [index, value]) : Object.entries(promised);
|
|
32
|
+
const { length } = entries;
|
|
33
|
+
const results = isArray ? [] : {};
|
|
34
|
+
for (let index = 0; index < length; index += 1) {
|
|
35
|
+
const [key, value] = entries[index];
|
|
36
|
+
results[key] = isFulfilled(value) ? ok(value.value) : error(value.reason);
|
|
37
|
+
}
|
|
38
|
+
return results;
|
|
31
39
|
}
|
|
32
40
|
function getStrategyOrDefault(value) {
|
|
33
41
|
return PROMISE_STRATEGY_ALL.has(value) ? value : PROMISE_STRATEGY_DEFAULT;
|
|
@@ -54,4 +62,4 @@ function isType(value, type) {
|
|
|
54
62
|
return typeof value === "object" && value !== null && value.status === type;
|
|
55
63
|
}
|
|
56
64
|
//#endregion
|
|
57
|
-
export {
|
|
65
|
+
export { createPromiseOptions, createPromisesOptions, getResultsFromPromises, getStrategyOrDefault, isFulfilled, isRejected };
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped,
|
|
1
|
+
import { PlainObject } from "../models.mjs";
|
|
2
|
+
import { PromiseOptions, PromiseStrategy, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValues } from "./models.mjs";
|
|
3
3
|
//#region src/promise/index.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Wrap a _Promise_ with safety handlers
|
|
5
|
+
* Wrap a _Promise_ with safety handlers
|
|
6
6
|
*
|
|
7
7
|
* @param promise _Promise_ to wrap
|
|
8
8
|
* @param options Options for the _Promise_
|
|
@@ -10,7 +10,7 @@ import { PromiseOptions, PromisesItems, PromisesOptions, PromisesResult, Promise
|
|
|
10
10
|
*/
|
|
11
11
|
declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
12
12
|
/**
|
|
13
|
-
* Wrap a _Promise_-returning callback with safety handlers
|
|
13
|
+
* Wrap a _Promise_-returning callback with safety handlers
|
|
14
14
|
*
|
|
15
15
|
* @param callback Callback to wrap
|
|
16
16
|
* @param options Options for the _Promise_
|
|
@@ -18,7 +18,7 @@ declare function attemptPromise<Value>(promise: Promise<Value>, options?: Promis
|
|
|
18
18
|
*/
|
|
19
19
|
declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
20
20
|
/**
|
|
21
|
-
* Wrap a callback with a _Promise_ and safety handlers
|
|
21
|
+
* Wrap a callback with a _Promise_ and safety handlers
|
|
22
22
|
*
|
|
23
23
|
* @param callback Callback to wrap
|
|
24
24
|
* @param options Options for the _Promise_
|
|
@@ -28,7 +28,7 @@ declare function attemptPromise<Value>(callback: () => Value, options?: PromiseO
|
|
|
28
28
|
/**
|
|
29
29
|
* Handle a list of _Promises_, returning their results in an ordered array
|
|
30
30
|
*
|
|
31
|
-
*
|
|
31
|
+
* _Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results_
|
|
32
32
|
*
|
|
33
33
|
* @param items List of _Promises_
|
|
34
34
|
* @param options Options for handling the _Promises_
|
|
@@ -36,77 +36,75 @@ declare function attemptPromise<Value>(callback: () => Value, options?: PromiseO
|
|
|
36
36
|
*/
|
|
37
37
|
declare function promises<Items extends unknown[], Options extends PromisesOptions>(items: [...Items], options?: Options): Promise<Options['strategy'] extends 'first' ? PromisesUnwrapped<Items> : PromisesValues<PromisesItems<Items>>>;
|
|
38
38
|
/**
|
|
39
|
-
* Handle
|
|
39
|
+
* Handle keyed _Promises_, returning their results in a similarly keyed object
|
|
40
40
|
*
|
|
41
|
-
*
|
|
41
|
+
* _Depending on the strategy, the function will either reject on the first error encountered or return an object of rejected and resolved results_
|
|
42
42
|
*
|
|
43
|
-
* @param items
|
|
43
|
+
* @param items Keyed _Promises_
|
|
44
44
|
* @param options Options for handling the _Promises_
|
|
45
|
-
* @returns
|
|
45
|
+
* @returns Keyed object of results
|
|
46
46
|
*/
|
|
47
|
-
declare function promises<
|
|
47
|
+
declare function promises<Items extends PlainObject, Options extends PromisesOptions>(items: Items, options?: Options): Promise<Options['strategy'] extends 'first' ? PromisesUnwrapped<Items> : PromisesValues<PromisesItems<Items>>>;
|
|
48
48
|
/**
|
|
49
49
|
* Handle a list of _Promises_, returning their results in an ordered array
|
|
50
50
|
*
|
|
51
|
-
*
|
|
51
|
+
* _If the strategy is `first`, the function will reject on the first error encountered; otherwise, it returns an array of rejected and resolved results_
|
|
52
52
|
*
|
|
53
53
|
* @param items List of _Promises_
|
|
54
|
-
* @param strategy Strategy for handling the _Promises_
|
|
54
|
+
* @param strategy Strategy for handling the _Promises_
|
|
55
55
|
* @returns List of results
|
|
56
56
|
*/
|
|
57
|
-
declare function promises<Items extends unknown[]>(items: [...Items], strategy:
|
|
57
|
+
declare function promises<Items extends unknown[], Strategy extends PromiseStrategy>(items: [...Items], strategy: Strategy): Promise<Strategy extends 'first' ? PromisesUnwrapped<Items> : PromisesValues<PromisesItems<Items>>>;
|
|
58
58
|
/**
|
|
59
|
-
* Handle
|
|
59
|
+
* Handle keyed _Promises_, returning their results in a similarly keyed object
|
|
60
60
|
*
|
|
61
|
-
*
|
|
61
|
+
* _If the strategy is `first`, the function will reject on the first error encountered; otherwise, it returns an object of rejected and resolved results_
|
|
62
62
|
*
|
|
63
|
-
* @param items
|
|
64
|
-
* @param strategy Strategy for handling the _Promises_
|
|
65
|
-
* @returns
|
|
63
|
+
* @param items Keyed _Promises_
|
|
64
|
+
* @param strategy Strategy for handling the _Promises_
|
|
65
|
+
* @returns Keyed object of results
|
|
66
66
|
*/
|
|
67
|
-
declare function promises<
|
|
67
|
+
declare function promises<Items extends PlainObject, Strategy extends PromiseStrategy>(items: Items, strategy: Strategy): Promise<Strategy extends 'first' ? PromisesUnwrapped<Items> : PromisesValues<PromisesItems<Items>>>;
|
|
68
68
|
/**
|
|
69
|
-
* Handle a list of _Promises_, returning their results in an ordered array
|
|
69
|
+
* Handle a list of _Promises_, returning their results in an ordered array
|
|
70
70
|
*
|
|
71
71
|
* @param items List of _Promises_
|
|
72
|
-
* @param signal
|
|
72
|
+
* @param signal _AbortSignal_ for aborting all _Promises_
|
|
73
73
|
* @returns List of results
|
|
74
74
|
*/
|
|
75
75
|
declare function promises<Items extends unknown[]>(items: [...Items], signal?: AbortSignal): Promise<PromisesValues<PromisesItems<Items>>>;
|
|
76
76
|
/**
|
|
77
|
-
* Handle
|
|
77
|
+
* Handle keyed _Promises_, returning their results in a similarly keyed object
|
|
78
78
|
*
|
|
79
|
-
* @param items
|
|
80
|
-
* @param signal
|
|
81
|
-
* @returns
|
|
79
|
+
* @param items Keyed _Promises_
|
|
80
|
+
* @param signal _AbortSignal_ for aborting all _Promises_
|
|
81
|
+
* @returns Keyed object of results
|
|
82
82
|
*/
|
|
83
|
-
declare function promises<
|
|
83
|
+
declare function promises<Items extends PlainObject>(items: Items, signal?: AbortSignal): Promise<PromisesValues<PromisesItems<Items>>>;
|
|
84
84
|
declare namespace promises {
|
|
85
85
|
var result: typeof resultPromises;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Handle a list of _Promises_, returning their results in an ordered array of results _({@link Result})_
|
|
89
89
|
*
|
|
90
|
-
*
|
|
90
|
+
* _Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results_
|
|
91
91
|
*
|
|
92
92
|
* _Available as `resultPromises` and `promises.result`_
|
|
93
93
|
*
|
|
94
94
|
* @param items List of _Promises_
|
|
95
|
-
* @param signal AbortSignal for aborting
|
|
95
|
+
* @param signal AbortSignal for aborting all _Promises_
|
|
96
96
|
* @returns List of results
|
|
97
97
|
*/
|
|
98
98
|
declare function resultPromises<Items extends unknown[]>(items: [...Items], signal?: AbortSignal): Promise<PromisesResult<PromisesItems<Items>>>;
|
|
99
99
|
/**
|
|
100
|
-
* Handle
|
|
101
|
-
*
|
|
102
|
-
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
100
|
+
* Handle keyed _Promises_, returning their results in a similarly keyed object of results _({@link Result})_
|
|
103
101
|
*
|
|
104
102
|
* _Available as `resultPromises` and `promises.result`_
|
|
105
103
|
*
|
|
106
|
-
* @param items
|
|
107
|
-
* @param signal AbortSignal for aborting
|
|
108
|
-
* @returns
|
|
104
|
+
* @param items Keyed _Promises_
|
|
105
|
+
* @param signal AbortSignal for aborting all _Promises_
|
|
106
|
+
* @returns Keyed object of results
|
|
109
107
|
*/
|
|
110
|
-
declare function resultPromises<
|
|
108
|
+
declare function resultPromises<Items extends PlainObject>(items: Items, signal?: AbortSignal): Promise<PromisesResult<PromisesItems<Items>>>;
|
|
111
109
|
//#endregion
|
|
112
110
|
export { attemptPromise, promises, resultPromises };
|
package/dist/promise/index.mjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { isArrayOrPlainObject } from "../internal/is.mjs";
|
|
1
2
|
import { createAborter } from "../internal/abort.mjs";
|
|
2
|
-
import { PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY, PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
|
|
4
|
+
import { createPromiseOptions, createPromisesOptions, getResultsFromPromises } from "./helpers.mjs";
|
|
4
5
|
import { handleResult, settlePromise } from "./misc.mjs";
|
|
5
6
|
import { getTimedPromise } from "./timed.mjs";
|
|
6
7
|
//#region src/promise/index.ts
|
|
7
8
|
async function attemptPromise(value, options) {
|
|
8
9
|
const isFunction = typeof value === "function";
|
|
9
10
|
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
10
|
-
const { signal, time } =
|
|
11
|
+
const { signal, time } = createPromiseOptions(options);
|
|
11
12
|
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
12
13
|
const aborter = createAborter(signal, () => {
|
|
13
14
|
rejector(signal?.reason);
|
|
@@ -29,19 +30,20 @@ async function attemptPromise(value, options) {
|
|
|
29
30
|
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
30
31
|
}
|
|
31
32
|
async function promises(items, options) {
|
|
32
|
-
const { signal, strategy } =
|
|
33
|
+
const { signal, strategy } = createPromisesOptions(options);
|
|
33
34
|
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
34
|
-
if (!
|
|
35
|
-
const
|
|
35
|
+
if (!isArrayOrPlainObject(items)) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
|
|
36
|
+
const isArray = Array.isArray(items);
|
|
37
|
+
const actual = (isArray ? items.map((item, index) => [index, item]) : Object.entries(items)).map(([key, value]) => [key, typeof value === "function" ? value() : value]).filter(([, value]) => value instanceof Promise);
|
|
36
38
|
const { length } = actual;
|
|
37
|
-
if (length === 0) return Promise.
|
|
39
|
+
if (length === 0) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY);
|
|
38
40
|
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
39
41
|
const aborter = createAborter(signal, () => {
|
|
40
42
|
handlers.reject(signal?.reason);
|
|
41
43
|
});
|
|
42
44
|
const data = {
|
|
43
45
|
last: length - 1,
|
|
44
|
-
result: []
|
|
46
|
+
result: isArray ? [] : {}
|
|
45
47
|
};
|
|
46
48
|
let handlers;
|
|
47
49
|
return new Promise((resolve, reject) => {
|
|
@@ -49,21 +51,24 @@ async function promises(items, options) {
|
|
|
49
51
|
reject,
|
|
50
52
|
resolve
|
|
51
53
|
};
|
|
52
|
-
for (let index = 0; index < length; index += 1)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
for (let index = 0; index < length; index += 1) {
|
|
55
|
+
const [key, promise] = actual[index];
|
|
56
|
+
const parameters = {
|
|
57
|
+
aborter,
|
|
58
|
+
complete,
|
|
59
|
+
data,
|
|
60
|
+
handlers,
|
|
61
|
+
index,
|
|
62
|
+
key
|
|
63
|
+
};
|
|
64
|
+
promise.then((value) => {
|
|
65
|
+
parameters.value = value;
|
|
66
|
+
handleResult(PROMISE_TYPE_FULFILLED, parameters);
|
|
67
|
+
}).catch((reason) => {
|
|
68
|
+
parameters.value = reason;
|
|
69
|
+
handleResult(PROMISE_TYPE_REJECTED, parameters);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
67
72
|
});
|
|
68
73
|
}
|
|
69
74
|
async function resultPromises(items, signal) {
|
package/dist/promise/misc.mjs
CHANGED
|
@@ -14,13 +14,13 @@ function cancelable(executor) {
|
|
|
14
14
|
return new CancelablePromise(executor);
|
|
15
15
|
}
|
|
16
16
|
function handleResult(status, parameters) {
|
|
17
|
-
const { aborter, complete, data, handlers, index, value } = parameters;
|
|
17
|
+
const { aborter, complete, data, handlers, index, key, value } = parameters;
|
|
18
18
|
if (aborter?.signal.aborted ?? false) return;
|
|
19
19
|
if (!complete && status === "rejected") {
|
|
20
20
|
settlePromise(handlers.reject, value, aborter);
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
data.result[
|
|
23
|
+
data.result[key] = !complete ? value : status === "fulfilled" ? {
|
|
24
24
|
status,
|
|
25
25
|
value
|
|
26
26
|
} : {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GenericCallback } from "../models.mjs";
|
|
1
|
+
import { ArrayOrPlainObject, GenericCallback, Key } from "../models.mjs";
|
|
2
2
|
import { Result } from "../internal/result/models.mjs";
|
|
3
3
|
import { Aborter } from "../internal/abort.mjs";
|
|
4
4
|
import { PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
|
|
@@ -31,7 +31,7 @@ type FulfilledPromise<Value> = {
|
|
|
31
31
|
};
|
|
32
32
|
type PromiseData = {
|
|
33
33
|
last: number;
|
|
34
|
-
result:
|
|
34
|
+
result: ArrayOrPlainObject;
|
|
35
35
|
};
|
|
36
36
|
type PromiseHandlers = {
|
|
37
37
|
resolve: (value: unknown[]) => void;
|
|
@@ -56,6 +56,7 @@ type PromiseParameters = {
|
|
|
56
56
|
data: PromiseData;
|
|
57
57
|
handlers: PromiseHandlers;
|
|
58
58
|
index: number;
|
|
59
|
+
key: Key;
|
|
59
60
|
value?: unknown;
|
|
60
61
|
};
|
|
61
62
|
/**
|
|
@@ -71,7 +72,7 @@ type PromiseStrategy = 'complete' | 'first';
|
|
|
71
72
|
declare class PromiseTimeoutError extends Error {
|
|
72
73
|
constructor();
|
|
73
74
|
}
|
|
74
|
-
type PromisesItems<Items extends
|
|
75
|
+
type PromisesItems<Items extends ArrayOrPlainObject> = { [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]>; };
|
|
75
76
|
/**
|
|
76
77
|
* Options for handling multiple _Promises_
|
|
77
78
|
*/
|
|
@@ -85,10 +86,10 @@ type PromisesOptions = {
|
|
|
85
86
|
*/
|
|
86
87
|
strategy?: PromiseStrategy;
|
|
87
88
|
};
|
|
88
|
-
type PromisesResult<Items extends
|
|
89
|
-
type PromisesUnwrapped<Items extends
|
|
89
|
+
type PromisesResult<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? Result<Awaited<Value>> : never; };
|
|
90
|
+
type PromisesUnwrapped<Items extends ArrayOrPlainObject> = { [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; };
|
|
90
91
|
type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
91
|
-
type PromisesValues<Items extends
|
|
92
|
+
type PromisesValues<Items extends ArrayOrPlainObject> = { [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never; };
|
|
92
93
|
/**
|
|
93
94
|
* A _Promise_ that was rejected
|
|
94
95
|
*/
|
package/dist/promise/timed.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { createAborter } from "../internal/abort.mjs";
|
|
|
2
2
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.mjs";
|
|
3
3
|
import { PROMISE_MESSAGE_EXPECTATION_TIMED } from "./constants.mjs";
|
|
4
4
|
import { PromiseTimeoutError } from "./models.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { createPromiseOptions } from "./helpers.mjs";
|
|
6
6
|
import { settlePromise } from "./misc.mjs";
|
|
7
7
|
//#region src/promise/timed.ts
|
|
8
8
|
async function getTimedPromise(promise, time, signal) {
|
|
@@ -24,7 +24,7 @@ async function getTimedPromise(promise, time, signal) {
|
|
|
24
24
|
}
|
|
25
25
|
async function timed(promise, options) {
|
|
26
26
|
if (!(promise instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
27
|
-
const { signal, time } =
|
|
27
|
+
const { signal, time } = createPromiseOptions(options);
|
|
28
28
|
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
29
29
|
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
30
30
|
}
|
package/dist/queue.d.mts
CHANGED
|
@@ -216,6 +216,8 @@ declare function isQueueInstance<Instance>(name: string, value: unknown): value
|
|
|
216
216
|
/**
|
|
217
217
|
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
218
218
|
*
|
|
219
|
+
* _Available as `keyedQueue` and `queue.keyed`_
|
|
220
|
+
*
|
|
219
221
|
* @param callback Callback function for queued items
|
|
220
222
|
* @param options Queue options
|
|
221
223
|
*/
|
|
@@ -223,6 +225,8 @@ declare function keyedQueue<Callback extends (key: string, ...parameters: any[])
|
|
|
223
225
|
/**
|
|
224
226
|
* Create a keyed queue for an asynchronous callback function, where each key has its own queue
|
|
225
227
|
*
|
|
228
|
+
* _Available as `keyedQueue` and `queue.keyed`_
|
|
229
|
+
*
|
|
226
230
|
* @param callback Callback function for queued items
|
|
227
231
|
* @param options Queue options
|
|
228
232
|
*/
|
package/dist/queue.mjs
CHANGED
|
@@ -54,7 +54,7 @@ function createQueue(callback, options, key) {
|
|
|
54
54
|
handled: [],
|
|
55
55
|
id: 0,
|
|
56
56
|
items: [],
|
|
57
|
-
options:
|
|
57
|
+
options: createQueueOptions(options),
|
|
58
58
|
paused: !getBooleanOrDefault(options?.autostart, true),
|
|
59
59
|
runners: 0
|
|
60
60
|
};
|
|
@@ -104,7 +104,7 @@ function createQueue(callback, options, key) {
|
|
|
104
104
|
});
|
|
105
105
|
return Object.freeze(instance);
|
|
106
106
|
}
|
|
107
|
-
function
|
|
107
|
+
function createQueueOptions(input) {
|
|
108
108
|
const options = typeof input === "object" && input != null ? input : {};
|
|
109
109
|
return {
|
|
110
110
|
autostart: getBooleanOrDefault(options.autostart, true),
|
|
@@ -182,7 +182,7 @@ function keyedQueue(callback, options) {
|
|
|
182
182
|
if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
|
|
183
183
|
const state = {
|
|
184
184
|
callback,
|
|
185
|
-
options:
|
|
185
|
+
options: createQueueOptions(options),
|
|
186
186
|
queues: /* @__PURE__ */ new Map()
|
|
187
187
|
};
|
|
188
188
|
const instance = {
|
package/dist/string/fuzzy.d.mts
CHANGED
|
@@ -115,6 +115,8 @@ declare namespace fuzzy {
|
|
|
115
115
|
/**
|
|
116
116
|
* Does the needle match the haystack in a fuzzy way?
|
|
117
117
|
*
|
|
118
|
+
* _Available as `fuzzyMatch` and `fuzzy.match`_
|
|
119
|
+
*
|
|
118
120
|
* @param haystack Haystack to search through
|
|
119
121
|
* @param needle Needle to search for
|
|
120
122
|
* @returns `true` if the needle matches the haystack in a fuzzy way, otherwise `false`
|
package/dist/string/fuzzy.mjs
CHANGED
|
@@ -3,20 +3,7 @@ import { getString } from "../internal/string/misc.mjs";
|
|
|
3
3
|
import { lowerCase } from "../internal/string/case.mjs";
|
|
4
4
|
import { includes } from "./match.mjs";
|
|
5
5
|
//#region src/string/fuzzy.ts
|
|
6
|
-
function
|
|
7
|
-
if (input == null || input === getString) return getString;
|
|
8
|
-
switch (typeof input) {
|
|
9
|
-
case "function": return input;
|
|
10
|
-
case "string": return (item) => item[input];
|
|
11
|
-
default:
|
|
12
|
-
if (isPlainObject(input)) return getHandler(input.key ?? input.handler);
|
|
13
|
-
throw new TypeError(FUZZY_MESSAGE_HANDLER);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function getFuzzyItems(items) {
|
|
17
|
-
return items.sort((first, second) => first.haystack.localeCompare(second.haystack)).map(({ item }) => item);
|
|
18
|
-
}
|
|
19
|
-
function getFuzzyOptions(input, state) {
|
|
6
|
+
function createFuzzyOptions(input, state) {
|
|
20
7
|
const options = isPlainObject(input) ? input : {};
|
|
21
8
|
const limit = typeof input === "number" ? input : options.limit;
|
|
22
9
|
if (typeof limit === "number" && !Number.isNaN(limit) && limit >= 1) options.limit = Math.floor(limit);
|
|
@@ -24,9 +11,9 @@ function getFuzzyOptions(input, state) {
|
|
|
24
11
|
options.tolerance = getTolerance(options.tolerance, state);
|
|
25
12
|
return options;
|
|
26
13
|
}
|
|
27
|
-
function
|
|
14
|
+
function createFuzzyState(items, input) {
|
|
28
15
|
const handler = getHandler(input);
|
|
29
|
-
const options =
|
|
16
|
+
const options = createFuzzyOptions(input);
|
|
30
17
|
return {
|
|
31
18
|
handler,
|
|
32
19
|
items: items.slice(),
|
|
@@ -35,14 +22,27 @@ function getFuzzyState(items, input) {
|
|
|
35
22
|
tolerance: options.tolerance
|
|
36
23
|
};
|
|
37
24
|
}
|
|
25
|
+
function getHandler(input) {
|
|
26
|
+
if (input == null || input === getString) return getString;
|
|
27
|
+
switch (typeof input) {
|
|
28
|
+
case "function": return input;
|
|
29
|
+
case "string": return (item) => item[input];
|
|
30
|
+
default:
|
|
31
|
+
if (isPlainObject(input)) return getHandler(input.key ?? input.handler);
|
|
32
|
+
throw new TypeError(FUZZY_MESSAGE_HANDLER);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function getFuzzyItems(items) {
|
|
36
|
+
return items.sort((first, second) => first.haystack.localeCompare(second.haystack)).map(({ item }) => item);
|
|
37
|
+
}
|
|
38
38
|
function getTolerance(input, state) {
|
|
39
39
|
if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 : Math.floor(input);
|
|
40
40
|
return state?.tolerance ?? FUZZY_PROXIMITY_THRESHOLD;
|
|
41
41
|
}
|
|
42
42
|
function fuzzy(items, configuration) {
|
|
43
43
|
if (!Array.isArray(items)) throw new TypeError(FUZZY_MESSAGE_ARRAY);
|
|
44
|
-
const state =
|
|
45
|
-
const instance = { search: (value, options) => search(state.items, state.strings, value, options == null ? state :
|
|
44
|
+
const state = createFuzzyState(items, configuration);
|
|
45
|
+
const instance = { search: (value, options) => search(state.items, state.strings, value, options == null ? state : createFuzzyOptions(options, state)) };
|
|
46
46
|
Object.defineProperties(instance, {
|
|
47
47
|
items: {
|
|
48
48
|
enumerable: true,
|
|
@@ -58,6 +58,8 @@ function fuzzy(items, configuration) {
|
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Does the needle match the haystack in a fuzzy way?
|
|
61
|
+
*
|
|
62
|
+
* _Available as `fuzzyMatch` and `fuzzy.match`_
|
|
61
63
|
*
|
|
62
64
|
* @param haystack Haystack to search through
|
|
63
65
|
* @param needle Needle to search for
|