@oscarpalmer/atoms 0.193.1 → 0.195.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.mjs +2 -2
- package/dist/array/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/intersection.mjs +2 -2
- package/dist/array/move.mjs +1 -1
- package/dist/array/slice.mjs +5 -5
- package/dist/array/sort.d.mts +5 -1
- package/dist/array/sort.mjs +4 -2
- package/dist/array/swap.mjs +1 -1
- package/dist/array/union.mjs +2 -2
- package/dist/beacon.d.mts +8 -1
- package/dist/beacon.mjs +52 -60
- package/dist/color/constants.d.mts +1 -1
- package/dist/color/constants.mjs +3 -5
- package/dist/color/instance.mjs +34 -41
- package/dist/color/misc/format.d.mts +3 -2
- package/dist/color/misc/format.mjs +4 -1
- package/dist/color/misc/is.d.mts +9 -1
- package/dist/color/misc/is.mjs +11 -1
- package/dist/color/misc/state.d.mts +6 -6
- package/dist/color/misc/state.mjs +11 -14
- package/dist/color/models.d.mts +23 -68
- package/dist/color/space/hex.mjs +1 -1
- package/dist/color/space/rgb.mjs +1 -1
- package/dist/function/index.d.mts +1 -2
- package/dist/function/index.mjs +1 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +17 -14
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/herald.d.mts +2 -72
- package/dist/herald.mjs +2 -92
- package/dist/index.d.mts +3332 -3418
- package/dist/index.mjs +1850 -1870
- package/dist/internal/abort.d.mts +10 -0
- package/dist/internal/abort.mjs +14 -0
- package/dist/internal/array/chunk.mjs +2 -2
- package/dist/internal/array/find.mjs +2 -2
- package/dist/{array → internal/array}/get.d.mts +2 -2
- package/dist/{array → internal/array}/get.mjs +2 -2
- package/dist/{array → internal/array}/match.d.mts +2 -2
- package/dist/{array → internal/array}/match.mjs +15 -15
- package/dist/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/defaults.d.mts +5 -0
- package/dist/internal/defaults.mjs +9 -0
- package/dist/{function → internal/function}/assert.d.mts +2 -2
- package/dist/{function → internal/function}/assert.mjs +4 -4
- package/dist/{function → internal/function}/memoize.d.mts +2 -2
- package/dist/{function → internal/function}/memoize.mjs +22 -35
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/{function → internal/function}/work.d.mts +2 -2
- package/dist/{function → internal/function}/work.mjs +15 -13
- package/dist/internal/herald.d.mts +71 -0
- package/dist/internal/herald.mjs +110 -0
- package/dist/internal/number.d.mts +1 -2
- package/dist/internal/number.mjs +8 -11
- package/dist/internal/{result.d.mts → result/misc.d.mts} +42 -3
- package/dist/internal/result/misc.mjs +51 -0
- package/dist/{result → internal/result}/models.d.mts +2 -2
- package/dist/{sized → internal/sized}/map.d.mts +1 -1
- package/dist/{sized → internal/sized}/map.mjs +2 -2
- package/dist/internal/{sized.d.mts → sized/misc.d.mts} +1 -1
- package/dist/internal/sized/misc.mjs +12 -0
- package/dist/{string → internal/string}/case.d.mts +1 -1
- package/dist/{string → internal/string}/case.mjs +22 -22
- package/dist/internal/{string.d.mts → string/misc.d.mts} +1 -1
- package/dist/internal/{string.mjs → string/misc.mjs} +5 -5
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/compare.mjs +1 -1
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/has.d.mts +1 -1
- package/dist/internal/value/misc.d.mts +1 -1
- package/dist/internal/value/misc.mjs +16 -16
- package/dist/internal/value/set.mjs +2 -2
- package/dist/is.mjs +2 -2
- package/dist/logger.mjs +15 -39
- package/dist/promise/constants.d.mts +17 -0
- package/dist/promise/constants.mjs +14 -0
- package/dist/promise/delay.mjs +7 -10
- package/dist/promise/helpers.d.mts +1 -1
- package/dist/promise/helpers.mjs +3 -3
- package/dist/promise/index.d.mts +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +3 -2
- package/dist/promise/misc.mjs +10 -8
- package/dist/promise/models.d.mts +5 -17
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +16 -16
- package/dist/queue.mjs +104 -120
- package/dist/random.mjs +8 -8
- package/dist/result/index.d.mts +1 -4
- package/dist/result/index.mjs +2 -2
- package/dist/result/match.d.mts +1 -4
- package/dist/result/match.mjs +5 -5
- package/dist/result/misc.d.mts +3 -42
- package/dist/result/misc.mjs +4 -30
- package/dist/result/work/flow.d.mts +1 -4
- package/dist/result/work/flow.mjs +6 -6
- package/dist/result/work/pipe.d.mts +1 -4
- package/dist/result/work/pipe.mjs +5 -5
- package/dist/sized/set.mjs +1 -1
- package/dist/string/fuzzy.mjs +25 -20
- package/dist/string/index.d.mts +1 -1
- package/dist/string/index.mjs +8 -8
- package/dist/string/match.mjs +1 -1
- package/dist/string/normalize.mjs +16 -16
- package/dist/string/template.mjs +3 -3
- package/dist/subscription.d.mts +2 -0
- package/dist/subscription.mjs +2 -0
- package/dist/value/clone.mjs +10 -10
- package/dist/value/diff.mjs +1 -1
- package/dist/value/smush.mjs +3 -3
- package/package.json +35 -14
- package/dist/internal/result.mjs +0 -25
- package/dist/internal/sized.mjs +0 -12
- /package/dist/{result → internal/result}/models.mjs +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GenericCallback } from "../models.mjs";
|
|
2
|
+
//#region src/internal/abort.d.ts
|
|
3
|
+
type Aborter = {
|
|
4
|
+
callback: GenericCallback;
|
|
5
|
+
signal: AbortSignal;
|
|
6
|
+
cancel: () => void;
|
|
7
|
+
};
|
|
8
|
+
declare function createAborter(value: unknown, onAbort: () => void): Aborter | undefined;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { Aborter, createAborter };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/internal/abort.ts
|
|
2
|
+
function createAborter(value, onAbort) {
|
|
3
|
+
if (!(value instanceof AbortSignal)) return;
|
|
4
|
+
value.addEventListener(ABORT_EVENT, onAbort, ABORT_OPTIONS);
|
|
5
|
+
return {
|
|
6
|
+
callback: onAbort,
|
|
7
|
+
signal: value,
|
|
8
|
+
cancel: () => value.removeEventListener(ABORT_EVENT, onAbort)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
const ABORT_EVENT = "abort";
|
|
12
|
+
const ABORT_OPTIONS = { once: true };
|
|
13
|
+
//#endregion
|
|
14
|
+
export { createAborter };
|
|
@@ -15,7 +15,7 @@ function chunk(array, size) {
|
|
|
15
15
|
if (!Array.isArray(array)) return [];
|
|
16
16
|
if (array.length === 0) return [];
|
|
17
17
|
const { length } = array;
|
|
18
|
-
const actualSize = typeof size === "number" && size > 0 && size <=
|
|
18
|
+
const actualSize = typeof size === "number" && size > 0 && size <= CHUNK_MAX_SIZE ? size : CHUNK_MAX_SIZE;
|
|
19
19
|
if (length <= actualSize) return [array];
|
|
20
20
|
const chunks = [];
|
|
21
21
|
let index = 0;
|
|
@@ -25,6 +25,6 @@ function chunk(array, size) {
|
|
|
25
25
|
}
|
|
26
26
|
return chunks;
|
|
27
27
|
}
|
|
28
|
-
const
|
|
28
|
+
const CHUNK_MAX_SIZE = 5e3;
|
|
29
29
|
//#endregion
|
|
30
30
|
export { chunk };
|
|
@@ -40,7 +40,7 @@ function findValues(type, array, parameters, mapper) {
|
|
|
40
40
|
const { length } = array;
|
|
41
41
|
const { bool, key, value } = getFindParameters(parameters);
|
|
42
42
|
const callbacks = getArrayCallbacks(bool, key);
|
|
43
|
-
if (type === "unique" && callbacks?.keyed == null && length >=
|
|
43
|
+
if (type === "unique" && callbacks?.keyed == null && length >= FIND_UNIQUE_THRESHOLD) {
|
|
44
44
|
result.matched = [...new Set(array)];
|
|
45
45
|
return result;
|
|
46
46
|
}
|
|
@@ -77,6 +77,6 @@ const FIND_VALUE_INDEX = "index";
|
|
|
77
77
|
const FIND_VALUE_ITEM = "item";
|
|
78
78
|
const FIND_VALUES_ALL = "all";
|
|
79
79
|
const FIND_VALUES_UNIQUE = "unique";
|
|
80
|
-
const
|
|
80
|
+
const FIND_UNIQUE_THRESHOLD = 100;
|
|
81
81
|
//#endregion
|
|
82
82
|
export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_ITEM, findAbsoluteValueOrDefault, findValue, findValues, getFindParameters };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NumericalKeys, PlainObject } from "
|
|
2
|
-
//#region src/array/get.d.ts
|
|
1
|
+
import { NumericalKeys, PlainObject } from "../../models.mjs";
|
|
2
|
+
//#region src/internal/array/get.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Get an array from an object, where only values with numerical keys will be included
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isNonPlainObject } from "../
|
|
2
|
-
//#region src/array/get.ts
|
|
1
|
+
import { isNonPlainObject } from "../is.mjs";
|
|
2
|
+
//#region src/internal/array/get.ts
|
|
3
3
|
function getArray(value, indiced) {
|
|
4
4
|
if (Array.isArray(value)) return value;
|
|
5
5
|
if (value instanceof Map) return [...value.entries()];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getArrayCallback } from "
|
|
2
|
-
//#region src/array/match.ts
|
|
1
|
+
import { getArrayCallback } from "./callbacks.mjs";
|
|
2
|
+
//#region src/internal/array/match.ts
|
|
3
3
|
function endsWithArray(haystack, needle, key) {
|
|
4
4
|
return endings.has(getPosition(haystack, needle, key)[1]);
|
|
5
5
|
}
|
|
@@ -7,8 +7,8 @@ function getArrayComparison(haystack, needle, key) {
|
|
|
7
7
|
return getPosition(haystack, needle, key)[1];
|
|
8
8
|
}
|
|
9
9
|
function getName(start, haystack, needle) {
|
|
10
|
-
if (start === 0) return haystack === needle ?
|
|
11
|
-
return start + needle === haystack ?
|
|
10
|
+
if (start === 0) return haystack === needle ? MATCH_COMPARISON_SAME : MATCH_COMPARISON_START;
|
|
11
|
+
return start + needle === haystack ? MATCH_COMPARISON_END : MATCH_COMPARISON_INSIDE;
|
|
12
12
|
}
|
|
13
13
|
function getPosition(haystack, needle, key) {
|
|
14
14
|
if (!Array.isArray(haystack) || !Array.isArray(needle)) return invalid;
|
|
@@ -43,16 +43,16 @@ function indexOfArray(haystack, needle, key) {
|
|
|
43
43
|
function startsWithArray(haystack, needle, key) {
|
|
44
44
|
return starts.has(getPosition(haystack, needle, key)[1]);
|
|
45
45
|
}
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const endings = /* @__PURE__ */ new Set([
|
|
53
|
-
const invalid = [-1,
|
|
54
|
-
const outside = [-1,
|
|
55
|
-
const outsides = /* @__PURE__ */ new Set([
|
|
56
|
-
const starts = /* @__PURE__ */ new Set([
|
|
46
|
+
const MATCH_COMPARISON_END = "end";
|
|
47
|
+
const MATCH_COMPARISON_INSIDE = "inside";
|
|
48
|
+
const MATCH_COMPARISON_INVALID = "invalid";
|
|
49
|
+
const MATCH_COMPARISON_OUTSIDE = "outside";
|
|
50
|
+
const MATCH_COMPARISON_SAME = "same";
|
|
51
|
+
const MATCH_COMPARISON_START = "start";
|
|
52
|
+
const endings = /* @__PURE__ */ new Set([MATCH_COMPARISON_END, MATCH_COMPARISON_SAME]);
|
|
53
|
+
const invalid = [-1, MATCH_COMPARISON_INVALID];
|
|
54
|
+
const outside = [-1, MATCH_COMPARISON_OUTSIDE];
|
|
55
|
+
const outsides = /* @__PURE__ */ new Set([MATCH_COMPARISON_INVALID, MATCH_COMPARISON_OUTSIDE]);
|
|
56
|
+
const starts = /* @__PURE__ */ new Set([MATCH_COMPARISON_START, MATCH_COMPARISON_SAME]);
|
|
57
57
|
//#endregion
|
|
58
58
|
export { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
//#region src/internal/array/sets.d.ts
|
|
2
2
|
type CompareSetsType = 'difference' | 'intersection' | 'union';
|
|
3
3
|
declare function compareSets(type: CompareSetsType, first: unknown[], second: unknown[], key?: unknown): unknown[];
|
|
4
|
-
declare const
|
|
5
|
-
declare const
|
|
6
|
-
declare const
|
|
4
|
+
declare const SETS_COMPARE_DIFFERENCE: CompareSetsType;
|
|
5
|
+
declare const SETS_COMPARE_INTERSECTION: CompareSetsType;
|
|
6
|
+
declare const SETS_COMPARE_UNION: CompareSetsType;
|
|
7
7
|
//#endregion
|
|
8
|
-
export {
|
|
8
|
+
export { SETS_COMPARE_DIFFERENCE, SETS_COMPARE_INTERSECTION, SETS_COMPARE_UNION, compareSets };
|
|
@@ -2,9 +2,9 @@ import { getArrayCallback } from "./callbacks.mjs";
|
|
|
2
2
|
//#region src/internal/array/sets.ts
|
|
3
3
|
function compareSets(type, first, second, key) {
|
|
4
4
|
if (!Array.isArray(first)) return [];
|
|
5
|
-
const isDifference = type ===
|
|
6
|
-
const isIntersection = type ===
|
|
7
|
-
const isUnion = type ===
|
|
5
|
+
const isDifference = type === SETS_COMPARE_DIFFERENCE;
|
|
6
|
+
const isIntersection = type === SETS_COMPARE_INTERSECTION;
|
|
7
|
+
const isUnion = type === SETS_COMPARE_UNION;
|
|
8
8
|
if (first.length === 0) return isDifference ? first.slice() : isIntersection ? [] : second.slice();
|
|
9
9
|
if (!Array.isArray(second) || second.length === 0) return isIntersection ? [] : first.slice();
|
|
10
10
|
const callback = getArrayCallback(key);
|
|
@@ -25,8 +25,8 @@ function compareSets(type, first, second, key) {
|
|
|
25
25
|
}
|
|
26
26
|
return result;
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
28
|
+
const SETS_COMPARE_DIFFERENCE = "difference";
|
|
29
|
+
const SETS_COMPARE_INTERSECTION = "intersection";
|
|
30
|
+
const SETS_COMPARE_UNION = "union";
|
|
31
31
|
//#endregion
|
|
32
|
-
export {
|
|
32
|
+
export { SETS_COMPARE_DIFFERENCE, SETS_COMPARE_INTERSECTION, SETS_COMPARE_UNION, compareSets };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
//#region src/internal/defaults.d.ts
|
|
2
|
+
declare function getBooleanOrDefault(value: unknown, defaultValue: boolean): boolean;
|
|
3
|
+
declare function getNumberOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { getBooleanOrDefault, getNumberOrDefault };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/internal/defaults.ts
|
|
2
|
+
function getBooleanOrDefault(value, defaultValue) {
|
|
3
|
+
return typeof value === "boolean" ? value : defaultValue;
|
|
4
|
+
}
|
|
5
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
6
|
+
return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? value : defaultValue;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { getBooleanOrDefault, getNumberOrDefault };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Constructor, NestedKeys, NestedValue, PlainObject } from "
|
|
2
|
-
//#region src/function/assert.d.ts
|
|
1
|
+
import { Constructor, NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
|
|
2
|
+
//#region src/internal/function/assert.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Asserter for a property of a value
|
|
5
5
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { hasValueResult } from "../
|
|
2
|
-
//#region src/function/assert.ts
|
|
1
|
+
import { hasValueResult } from "../value/has.mjs";
|
|
2
|
+
//#region src/internal/function/assert.ts
|
|
3
3
|
/**
|
|
4
4
|
* Asserts that a condition is true, throwing an error if it is not
|
|
5
5
|
*
|
|
@@ -40,7 +40,7 @@ function assertCondition(condition, message, error) {
|
|
|
40
40
|
* @param error Error constructor _(defaults to `Error`)_
|
|
41
41
|
*/
|
|
42
42
|
function assertDefined(value, message, error) {
|
|
43
|
-
assert(() => value != null, message ??
|
|
43
|
+
assert(() => value != null, message ?? ASSERT_MESSAGE_VALUE_DEFINED, error);
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Creates an _Asserter_ that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
@@ -91,6 +91,6 @@ function assertProperty(path, condition, message, error) {
|
|
|
91
91
|
}, message, error);
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
|
-
const
|
|
94
|
+
const ASSERT_MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
95
95
|
//#endregion
|
|
96
96
|
export { assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { GenericCallback } from "
|
|
2
|
-
//#region src/function/memoize.d.ts
|
|
1
|
+
import { GenericCallback } from "../../models.mjs";
|
|
2
|
+
//#region src/internal/function/memoize.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* A _Memoized_ function instance, caching and retrieving results based on the its parameters _(or a custom cache key)_
|
|
5
5
|
*/
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { isPlainObject } from "../
|
|
2
|
-
import { getString, join } from "../
|
|
1
|
+
import { isPlainObject } from "../is.mjs";
|
|
2
|
+
import { getString, join } from "../string/misc.mjs";
|
|
3
3
|
import { SizedMap } from "../sized/map.mjs";
|
|
4
|
-
//#region src/function/memoize.ts
|
|
4
|
+
//#region src/internal/function/memoize.ts
|
|
5
5
|
function getMemoizationOptions(input) {
|
|
6
6
|
const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
|
|
7
7
|
return {
|
|
8
8
|
cacheKey: typeof cacheKey === "function" ? cacheKey : void 0,
|
|
9
|
-
cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize :
|
|
9
|
+
cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize : MEMOIZED_CACHE_SIZE_DEFAULT
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
@@ -17,54 +17,41 @@ function getMemoizationOptions(input) {
|
|
|
17
17
|
* @returns _Memoized_ instance
|
|
18
18
|
*/
|
|
19
19
|
function memoize(callback, options) {
|
|
20
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
21
|
-
const state = {
|
|
20
|
+
if (typeof callback !== "function") throw new TypeError(MEMOIZED_CALLBACK);
|
|
21
|
+
const state = {
|
|
22
|
+
cache: void 0,
|
|
23
|
+
getter: void 0,
|
|
24
|
+
options: getMemoizationOptions(options)
|
|
25
|
+
};
|
|
22
26
|
state.cache = new SizedMap(state.options.cacheSize);
|
|
23
27
|
state.getter = (...parameters) => {
|
|
24
|
-
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString),
|
|
28
|
+
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), MEMOIZED_KEY_SEPARATOR));
|
|
25
29
|
if (state.cache.has(key)) return state.cache.get(key);
|
|
26
30
|
const value = callback(...parameters);
|
|
27
|
-
state.cache
|
|
31
|
+
state.cache.set(key, value);
|
|
28
32
|
return value;
|
|
29
33
|
};
|
|
30
34
|
const instance = {
|
|
31
|
-
clear: () =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
},
|
|
37
|
-
destroy: () => {
|
|
38
|
-
state.cache?.clear();
|
|
39
|
-
state.cache = void 0;
|
|
40
|
-
state.getter = void 0;
|
|
41
|
-
},
|
|
42
|
-
get: (key) => {
|
|
43
|
-
return state.cache?.get(key);
|
|
44
|
-
},
|
|
45
|
-
has: (key) => {
|
|
46
|
-
return state.cache?.has(key) ?? false;
|
|
47
|
-
},
|
|
48
|
-
run: (...parameters) => {
|
|
49
|
-
if (state.cache == null || state.getter == null) throw new Error(MEMOIZED_ERROR_DESTROYED);
|
|
50
|
-
return state.getter(...parameters);
|
|
51
|
-
}
|
|
35
|
+
clear: () => state.cache.clear(),
|
|
36
|
+
delete: (key) => state.cache.delete(key),
|
|
37
|
+
get: (key) => state.cache.get(key),
|
|
38
|
+
has: (key) => state.cache.has(key),
|
|
39
|
+
run: (...parameters) => state.getter(...parameters)
|
|
52
40
|
};
|
|
53
41
|
Object.defineProperties(instance, {
|
|
54
42
|
maximum: {
|
|
55
43
|
enumerable: true,
|
|
56
|
-
get: () => state.cache
|
|
44
|
+
get: () => state.cache.maximum
|
|
57
45
|
},
|
|
58
46
|
size: {
|
|
59
47
|
enumerable: true,
|
|
60
|
-
get: () => state.cache
|
|
48
|
+
get: () => state.cache.size
|
|
61
49
|
}
|
|
62
50
|
});
|
|
63
51
|
return Object.freeze(instance);
|
|
64
52
|
}
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
const SEPARATOR = "_";
|
|
53
|
+
const MEMOIZED_CACHE_SIZE_DEFAULT = 1024;
|
|
54
|
+
const MEMOIZED_CALLBACK = "Memoized requires a callback function";
|
|
55
|
+
const MEMOIZED_KEY_SEPARATOR = "_";
|
|
69
56
|
//#endregion
|
|
70
57
|
export { memoize };
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
//#region src/internal/function/timer.ts
|
|
2
2
|
function getAsyncTimer(type, callback, time) {
|
|
3
|
+
function cancel() {
|
|
4
|
+
clearTimer(id);
|
|
5
|
+
if (last != null && !last.running) {
|
|
6
|
+
last.reject();
|
|
7
|
+
last = void 0;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
3
10
|
async function run(item) {
|
|
4
11
|
const now = performance.now();
|
|
5
12
|
start ??= now;
|
|
6
|
-
if (interval === 0 || now - start >= interval -
|
|
13
|
+
if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
|
|
7
14
|
start = throttle ? now : void 0;
|
|
8
15
|
item.running = true;
|
|
9
16
|
try {
|
|
@@ -23,7 +30,7 @@ function getAsyncTimer(type, callback, time) {
|
|
|
23
30
|
let last;
|
|
24
31
|
let start;
|
|
25
32
|
const timer = (...parameters) => {
|
|
26
|
-
|
|
33
|
+
cancel();
|
|
27
34
|
const next = {
|
|
28
35
|
parameters,
|
|
29
36
|
running: false
|
|
@@ -34,25 +41,23 @@ function getAsyncTimer(type, callback, time) {
|
|
|
34
41
|
});
|
|
35
42
|
last = next;
|
|
36
43
|
if (throttle) run(next);
|
|
37
|
-
else id = startTimer(() =>
|
|
38
|
-
run(next);
|
|
39
|
-
});
|
|
44
|
+
else id = startTimer(() => run(next));
|
|
40
45
|
return next.promise;
|
|
41
46
|
};
|
|
42
|
-
timer
|
|
43
|
-
clearTimer(id);
|
|
44
|
-
if (last != null && !last.running) last.reject();
|
|
45
|
-
};
|
|
47
|
+
Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
|
|
46
48
|
return timer;
|
|
47
49
|
}
|
|
48
50
|
function getInterval(value) {
|
|
49
51
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
50
52
|
}
|
|
51
53
|
function getTimer(type, callback, time) {
|
|
54
|
+
function cancel() {
|
|
55
|
+
clearTimer(id);
|
|
56
|
+
}
|
|
52
57
|
function run() {
|
|
53
58
|
const now = performance.now();
|
|
54
59
|
start ??= now;
|
|
55
|
-
if (interval === 0 || now - start >= interval -
|
|
60
|
+
if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
|
|
56
61
|
start = throttle ? now : void 0;
|
|
57
62
|
callback(...args);
|
|
58
63
|
} else id = startTimer(run);
|
|
@@ -63,18 +68,17 @@ function getTimer(type, callback, time) {
|
|
|
63
68
|
let id;
|
|
64
69
|
let start;
|
|
65
70
|
const timer = (...parameters) => {
|
|
66
|
-
|
|
71
|
+
cancel();
|
|
67
72
|
args = parameters;
|
|
68
73
|
if (throttle) run();
|
|
69
74
|
else id = startTimer(run);
|
|
70
75
|
};
|
|
71
|
-
timer
|
|
72
|
-
clearTimer(id);
|
|
73
|
-
};
|
|
76
|
+
Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
|
|
74
77
|
return timer;
|
|
75
78
|
}
|
|
76
|
-
const
|
|
79
|
+
const TIMER_CANCEL = "cancel";
|
|
77
80
|
const TIMER_DEBOUNCE = "debounce";
|
|
81
|
+
const TIMER_OFFSET = 5;
|
|
78
82
|
const TIMER_THROTTLE = "throttle";
|
|
79
83
|
const TIMER_WAIT = "wait";
|
|
80
84
|
// istanbul ignore next
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GenericCallback } from "
|
|
1
|
+
import { GenericCallback } from "../../models.mjs";
|
|
2
2
|
import { UnwrapValue } from "../result/models.mjs";
|
|
3
|
-
//#region src/function/work.d.ts
|
|
3
|
+
//#region src/internal/function/work.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* A synchronous _Flow_, a function that pipe a value through a series of functions
|
|
6
6
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isError, isOk } from "../
|
|
1
|
+
import { isError, isOk } from "../result/misc.mjs";
|
|
2
2
|
import { assert } from "./assert.mjs";
|
|
3
|
-
//#region src/function/work.ts
|
|
3
|
+
//#region src/internal/function/work.ts
|
|
4
4
|
function asyncFlow(...fns) {
|
|
5
5
|
assertFlowFunctions(fns);
|
|
6
6
|
return (...args) => asyncWork(args.map((value) => {
|
|
@@ -15,7 +15,6 @@ function flow(...fns) {
|
|
|
15
15
|
return isOk(value) ? value.value : value;
|
|
16
16
|
}), fns, true);
|
|
17
17
|
}
|
|
18
|
-
flow.async = asyncFlow;
|
|
19
18
|
async function asyncPipe(value, ...pipes) {
|
|
20
19
|
assertPipeFunctions(pipes);
|
|
21
20
|
return asyncWork(value, pipes, false);
|
|
@@ -24,7 +23,6 @@ function pipe(value, ...pipes) {
|
|
|
24
23
|
assertPipeFunctions(pipes);
|
|
25
24
|
return work(value, pipes, false);
|
|
26
25
|
}
|
|
27
|
-
pipe.async = asyncPipe;
|
|
28
26
|
async function asyncWork(initial, functions, flow) {
|
|
29
27
|
const { length } = functions;
|
|
30
28
|
let transformed = unwrapValue(initial);
|
|
@@ -36,10 +34,10 @@ async function asyncWork(initial, functions, flow) {
|
|
|
36
34
|
}
|
|
37
35
|
function unwrapValue(value, flow, nested) {
|
|
38
36
|
if (typeof value === "function") {
|
|
39
|
-
if (nested != null) throw new TypeError(
|
|
37
|
+
if (nested != null) throw new TypeError(WORK_MESSAGE_NESTING);
|
|
40
38
|
return unwrapValue(value(), flow, true);
|
|
41
39
|
}
|
|
42
|
-
if (flow != null && value instanceof Promise) throw new TypeError(flow ?
|
|
40
|
+
if (flow != null && value instanceof Promise) throw new TypeError(flow ? WORK_MESSAGE_FLOW_PROMISE : WORK_MESSAGE_PIPE_PROMISE);
|
|
43
41
|
if (isError(value)) throw value.error;
|
|
44
42
|
return isOk(value) ? value.value : value;
|
|
45
43
|
}
|
|
@@ -52,12 +50,16 @@ function work(initial, functions, flow) {
|
|
|
52
50
|
}
|
|
53
51
|
return transformed;
|
|
54
52
|
}
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"),
|
|
61
|
-
const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"),
|
|
53
|
+
const WORK_MESSAGE_FLOW_ARRAY = "Flow expected to receive an array of functions";
|
|
54
|
+
const WORK_MESSAGE_FLOW_PROMISE = "Synchronous Flow received a promise. Use `flow.async` instead.";
|
|
55
|
+
const WORK_MESSAGE_NESTING = "Return values are too deeply nested.";
|
|
56
|
+
const WORK_MESSAGE_PIPE_ARRAY = "Pipe expected to receive an array of functions";
|
|
57
|
+
const WORK_MESSAGE_PIPE_PROMISE = "Synchronous Pipe received a promise. Use `pipe.async` instead.";
|
|
58
|
+
const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_FLOW_ARRAY, TypeError);
|
|
59
|
+
const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_PIPE_ARRAY, TypeError);
|
|
60
|
+
flow.async = asyncFlow;
|
|
61
|
+
pipe.async = asyncPipe;
|
|
62
|
+
Object.defineProperty(flow, "async", { value: asyncFlow });
|
|
63
|
+
Object.defineProperty(pipe, "async", { value: asyncPipe });
|
|
62
64
|
//#endregion
|
|
63
65
|
export { asyncFlow, asyncPipe, flow, pipe };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { GenericCallback } from "../models.mjs";
|
|
2
|
+
import { Subscription, SubscriptionProperty } from "./subscription.mjs";
|
|
3
|
+
//#region src/internal/herald.d.ts
|
|
4
|
+
type Herald<Events extends Record<string, GenericCallback>> = {
|
|
5
|
+
readonly events: HeraldEvents<Events>;
|
|
6
|
+
/**
|
|
7
|
+
* Remove all event subscribers
|
|
8
|
+
*/
|
|
9
|
+
clear(): void;
|
|
10
|
+
/**
|
|
11
|
+
* Emit an event with parameters
|
|
12
|
+
*
|
|
13
|
+
* @param event Event name
|
|
14
|
+
* @param parameters Event parameters
|
|
15
|
+
*/
|
|
16
|
+
emit<Event extends keyof Events>(event: Event, ...parameters: Parameters<Events[Event]>): void;
|
|
17
|
+
/**
|
|
18
|
+
* Is the event observed by any subscribers?
|
|
19
|
+
*
|
|
20
|
+
* @param event Event name
|
|
21
|
+
* @returns `true` if the event is observed, otherwise `false`
|
|
22
|
+
*/
|
|
23
|
+
observed<Event extends keyof Events>(event: Event): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Subscribe to an event with a callback
|
|
26
|
+
*
|
|
27
|
+
* @param event Event name
|
|
28
|
+
* @param callback Callback function
|
|
29
|
+
* @param signal Optional abort signal to cancel the subscription
|
|
30
|
+
* @returns Subscription instance
|
|
31
|
+
*/
|
|
32
|
+
subscribe<Event extends keyof Events>(event: Event, callback: Events[Event], signal?: AbortSignal): Subscription;
|
|
33
|
+
};
|
|
34
|
+
type HeraldEvents<Events extends Record<string, GenericCallback>> = { [Key in keyof Events]: Key extends '*' ? never : (callback: Events[Key], signal?: AbortSignal) => void; };
|
|
35
|
+
type HeraldOnCreate<Events extends Record<string, GenericCallback>> = <Event extends keyof Events>(event: Event, callback: Events[Event], subscription: Subscription) => void;
|
|
36
|
+
type HeraldOptions<Events extends Record<string, GenericCallback>> = {
|
|
37
|
+
names: Array<keyof Events>;
|
|
38
|
+
property?: SubscriptionProperty;
|
|
39
|
+
onCreate?: HeraldOnCreate<Events>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Create a _Herald_ for announcing named events
|
|
43
|
+
*
|
|
44
|
+
* @param names Event names
|
|
45
|
+
* @param property Optional property for subscription identification _(defaults to `$herald`)_
|
|
46
|
+
* @returns _Herald_ instance
|
|
47
|
+
*/
|
|
48
|
+
declare function herald<Events extends Record<string, GenericCallback>>(options: HeraldOptions<Events>): Herald<Events>;
|
|
49
|
+
/**
|
|
50
|
+
* Is the value a herald?
|
|
51
|
+
*
|
|
52
|
+
* @param value Value to check
|
|
53
|
+
* @returns `true` if the value is a herald, otherwise `false`
|
|
54
|
+
*/
|
|
55
|
+
declare function isHerald<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is Herald<Events>;
|
|
56
|
+
/**
|
|
57
|
+
* Is the value events for a herald?
|
|
58
|
+
*
|
|
59
|
+
* @param value Value to check
|
|
60
|
+
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
61
|
+
*/
|
|
62
|
+
declare function isHeraldEvents<Events extends Record<string, GenericCallback> = Record<string, GenericCallback>>(value: unknown): value is HeraldEvents<Events>;
|
|
63
|
+
/**
|
|
64
|
+
* Is the value a herald subscription?
|
|
65
|
+
*
|
|
66
|
+
* @param value Value to check
|
|
67
|
+
* @returns `true` if the value is a herald subscription, otherwise `false`
|
|
68
|
+
*/
|
|
69
|
+
declare function isHeraldSubscription(value: unknown): value is Subscription;
|
|
70
|
+
//#endregion
|
|
71
|
+
export { Herald, HeraldEvents, HeraldOptions, herald, isHerald, isHeraldEvents, isHeraldSubscription };
|