@oscarpalmer/atoms 0.193.1 → 0.194.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/intersection.mjs +2 -2
- package/dist/array/match.mjs +13 -13
- 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/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/function/assert.mjs +2 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/memoize.mjs +19 -32
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +16 -13
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/function/work.mjs +13 -11
- package/dist/herald.d.mts +30 -31
- package/dist/herald.mjs +74 -56
- package/dist/index.d.mts +100 -185
- package/dist/index.mjs +680 -702
- 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/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/internal/number.mjs +7 -7
- package/dist/internal/result.mjs +3 -3
- package/dist/internal/sized.mjs +4 -4
- package/dist/internal/string.mjs +4 -4
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/misc.mjs +14 -14
- package/dist/internal/value/set.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.mjs +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +2 -1
- package/dist/promise/misc.mjs +8 -7
- package/dist/promise/models.d.mts +4 -16
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +15 -15
- package/dist/queue.mjs +103 -116
- package/dist/random.mjs +7 -7
- package/dist/result/index.d.mts +0 -3
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +0 -3
- package/dist/result/match.mjs +4 -4
- package/dist/result/misc.mjs +2 -2
- package/dist/result/work/flow.d.mts +0 -3
- package/dist/result/work/flow.mjs +5 -5
- package/dist/result/work/pipe.d.mts +0 -3
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/case.mjs +20 -20
- package/dist/string/fuzzy.mjs +23 -18
- package/dist/string/index.mjs +7 -7
- package/dist/string/normalize.mjs +14 -14
- package/dist/string/template.mjs +2 -2
- package/dist/value/clone.mjs +10 -10
- package/dist/value/smush.mjs +2 -2
- package/package.json +13 -4
package/dist/index.mjs
CHANGED
|
@@ -55,7 +55,7 @@ function findValues(type, array, parameters, mapper) {
|
|
|
55
55
|
const { length } = array;
|
|
56
56
|
const { bool, key, value } = getFindParameters(parameters);
|
|
57
57
|
const callbacks = getArrayCallbacks(bool, key);
|
|
58
|
-
if (type === "unique" && callbacks?.keyed == null && length >=
|
|
58
|
+
if (type === "unique" && callbacks?.keyed == null && length >= FIND_UNIQUE_THRESHOLD) {
|
|
59
59
|
result.matched = [...new Set(array)];
|
|
60
60
|
return result;
|
|
61
61
|
}
|
|
@@ -91,7 +91,7 @@ function getFindParameters(original) {
|
|
|
91
91
|
const FIND_VALUE_INDEX = "index";
|
|
92
92
|
const FIND_VALUE_ITEM = "item";
|
|
93
93
|
const FIND_VALUES_UNIQUE = "unique";
|
|
94
|
-
const
|
|
94
|
+
const FIND_UNIQUE_THRESHOLD = 100;
|
|
95
95
|
//#endregion
|
|
96
96
|
//#region src/array/filter.ts
|
|
97
97
|
function exclude(array, ...parameters) {
|
|
@@ -156,7 +156,7 @@ function chunk(array, size) {
|
|
|
156
156
|
if (!Array.isArray(array)) return [];
|
|
157
157
|
if (array.length === 0) return [];
|
|
158
158
|
const { length } = array;
|
|
159
|
-
const actualSize = typeof size === "number" && size > 0 && size <=
|
|
159
|
+
const actualSize = typeof size === "number" && size > 0 && size <= CHUNK_MAX_SIZE ? size : CHUNK_MAX_SIZE;
|
|
160
160
|
if (length <= actualSize) return [array];
|
|
161
161
|
const chunks = [];
|
|
162
162
|
let index = 0;
|
|
@@ -166,7 +166,7 @@ function chunk(array, size) {
|
|
|
166
166
|
}
|
|
167
167
|
return chunks;
|
|
168
168
|
}
|
|
169
|
-
const
|
|
169
|
+
const CHUNK_MAX_SIZE = 5e3;
|
|
170
170
|
//#endregion
|
|
171
171
|
//#region src/internal/array/compact.ts
|
|
172
172
|
function compact(array, strict) {
|
|
@@ -416,9 +416,9 @@ function shuffle(array) {
|
|
|
416
416
|
//#region src/internal/array/sets.ts
|
|
417
417
|
function compareSets(type, first, second, key) {
|
|
418
418
|
if (!Array.isArray(first)) return [];
|
|
419
|
-
const isDifference = type ===
|
|
420
|
-
const isIntersection = type ===
|
|
421
|
-
const isUnion = type ===
|
|
419
|
+
const isDifference = type === SETS_COMPARE_DIFFERENCE;
|
|
420
|
+
const isIntersection = type === SETS_COMPARE_INTERSECTION;
|
|
421
|
+
const isUnion = type === SETS_COMPARE_UNION;
|
|
422
422
|
if (first.length === 0) return isDifference ? first.slice() : isIntersection ? [] : second.slice();
|
|
423
423
|
if (!Array.isArray(second) || second.length === 0) return isIntersection ? [] : first.slice();
|
|
424
424
|
const callback = getArrayCallback(key);
|
|
@@ -439,13 +439,13 @@ function compareSets(type, first, second, key) {
|
|
|
439
439
|
}
|
|
440
440
|
return result;
|
|
441
441
|
}
|
|
442
|
-
const
|
|
443
|
-
const
|
|
444
|
-
const
|
|
442
|
+
const SETS_COMPARE_DIFFERENCE = "difference";
|
|
443
|
+
const SETS_COMPARE_INTERSECTION = "intersection";
|
|
444
|
+
const SETS_COMPARE_UNION = "union";
|
|
445
445
|
//#endregion
|
|
446
446
|
//#region src/array/difference.ts
|
|
447
447
|
function difference(first, second, key) {
|
|
448
|
-
return compareSets(
|
|
448
|
+
return compareSets(SETS_COMPARE_DIFFERENCE, first, second, key);
|
|
449
449
|
}
|
|
450
450
|
//#endregion
|
|
451
451
|
//#region src/array/exists.ts
|
|
@@ -549,7 +549,7 @@ function insert(array, indexOrItems, items) {
|
|
|
549
549
|
//#endregion
|
|
550
550
|
//#region src/array/intersection.ts
|
|
551
551
|
function intersection(first, second, key) {
|
|
552
|
-
return compareSets(
|
|
552
|
+
return compareSets(SETS_COMPARE_INTERSECTION, first, second, key);
|
|
553
553
|
}
|
|
554
554
|
//#endregion
|
|
555
555
|
//#region src/array/partition.ts
|
|
@@ -566,8 +566,8 @@ function getArrayComparison(haystack, needle, key) {
|
|
|
566
566
|
return getPosition(haystack, needle, key)[1];
|
|
567
567
|
}
|
|
568
568
|
function getName(start, haystack, needle) {
|
|
569
|
-
if (start === 0) return haystack === needle ?
|
|
570
|
-
return start + needle === haystack ?
|
|
569
|
+
if (start === 0) return haystack === needle ? MATCH_COMPARISON_SAME : MATCH_COMPARISON_START;
|
|
570
|
+
return start + needle === haystack ? MATCH_COMPARISON_END : MATCH_COMPARISON_INSIDE;
|
|
571
571
|
}
|
|
572
572
|
function getPosition(haystack, needle, key) {
|
|
573
573
|
if (!Array.isArray(haystack) || !Array.isArray(needle)) return invalid;
|
|
@@ -602,17 +602,17 @@ function indexOfArray(haystack, needle, key) {
|
|
|
602
602
|
function startsWithArray(haystack, needle, key) {
|
|
603
603
|
return starts.has(getPosition(haystack, needle, key)[1]);
|
|
604
604
|
}
|
|
605
|
-
const
|
|
606
|
-
const
|
|
607
|
-
const
|
|
608
|
-
const
|
|
609
|
-
const
|
|
610
|
-
const
|
|
611
|
-
const endings = /* @__PURE__ */ new Set([
|
|
612
|
-
const invalid = [-1,
|
|
613
|
-
const outside = [-1,
|
|
614
|
-
const outsides = /* @__PURE__ */ new Set([
|
|
615
|
-
const starts = /* @__PURE__ */ new Set([
|
|
605
|
+
const MATCH_COMPARISON_END = "end";
|
|
606
|
+
const MATCH_COMPARISON_INSIDE = "inside";
|
|
607
|
+
const MATCH_COMPARISON_INVALID = "invalid";
|
|
608
|
+
const MATCH_COMPARISON_OUTSIDE = "outside";
|
|
609
|
+
const MATCH_COMPARISON_SAME = "same";
|
|
610
|
+
const MATCH_COMPARISON_START = "start";
|
|
611
|
+
const endings = /* @__PURE__ */ new Set([MATCH_COMPARISON_END, MATCH_COMPARISON_SAME]);
|
|
612
|
+
const invalid = [-1, MATCH_COMPARISON_INVALID];
|
|
613
|
+
const outside = [-1, MATCH_COMPARISON_OUTSIDE];
|
|
614
|
+
const outsides = /* @__PURE__ */ new Set([MATCH_COMPARISON_INVALID, MATCH_COMPARISON_OUTSIDE]);
|
|
615
|
+
const starts = /* @__PURE__ */ new Set([MATCH_COMPARISON_START, MATCH_COMPARISON_SAME]);
|
|
616
616
|
//#endregion
|
|
617
617
|
//#region src/array/push.ts
|
|
618
618
|
/**
|
|
@@ -669,13 +669,13 @@ const SINGLE_MESSAGE = "Multiple items were found";
|
|
|
669
669
|
//#endregion
|
|
670
670
|
//#region src/array/slice.ts
|
|
671
671
|
function drop(array, first, second) {
|
|
672
|
-
return extract(
|
|
672
|
+
return extract(SLICE_DROP, array, first, second);
|
|
673
673
|
}
|
|
674
674
|
function extract(type, array, first, second) {
|
|
675
675
|
if (!Array.isArray(array)) return [];
|
|
676
676
|
const { length } = array;
|
|
677
677
|
if (length === 0) return [];
|
|
678
|
-
const isTake = type ===
|
|
678
|
+
const isTake = type === SLICE_TAKE;
|
|
679
679
|
if (typeof first === "number") {
|
|
680
680
|
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
681
681
|
if (first === 0) return isTake ? [] : array.slice();
|
|
@@ -713,10 +713,10 @@ function slice(array, first, second) {
|
|
|
713
713
|
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
714
714
|
}
|
|
715
715
|
function take(array, first, second) {
|
|
716
|
-
return extract(
|
|
716
|
+
return extract(SLICE_TAKE, array, first, second);
|
|
717
717
|
}
|
|
718
|
-
const
|
|
719
|
-
const
|
|
718
|
+
const SLICE_DROP = "drop";
|
|
719
|
+
const SLICE_TAKE = "take";
|
|
720
720
|
//#endregion
|
|
721
721
|
//#region src/array/splice.ts
|
|
722
722
|
function splice(array, start, deleteCountOrItems, items) {
|
|
@@ -764,7 +764,7 @@ function toggle(array, values, key) {
|
|
|
764
764
|
//#endregion
|
|
765
765
|
//#region src/array/union.ts
|
|
766
766
|
function union(first, second, key) {
|
|
767
|
-
return compareSets(
|
|
767
|
+
return compareSets(SETS_COMPARE_UNION, first, second, key);
|
|
768
768
|
}
|
|
769
769
|
//#endregion
|
|
770
770
|
//#region src/array/unique.ts
|
|
@@ -954,7 +954,7 @@ function getString(value) {
|
|
|
954
954
|
return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
|
|
955
955
|
}
|
|
956
956
|
function ignoreKey(key) {
|
|
957
|
-
return
|
|
957
|
+
return STRING_EXPRESSION_IGNORED.test(key);
|
|
958
958
|
}
|
|
959
959
|
function interpolate(strings, values) {
|
|
960
960
|
const { length } = strings;
|
|
@@ -1005,10 +1005,10 @@ function tryEncode(value) {
|
|
|
1005
1005
|
* @returns Array of words found in the string
|
|
1006
1006
|
*/
|
|
1007
1007
|
function words(value) {
|
|
1008
|
-
return typeof value === "string" ? value.match(
|
|
1008
|
+
return typeof value === "string" ? value.match(STRING_EXPRESSION_WORDS) ?? [] : [];
|
|
1009
1009
|
}
|
|
1010
|
-
const
|
|
1011
|
-
const
|
|
1010
|
+
const STRING_EXPRESSION_IGNORED = /(^|\.)(__proto__|constructor|prototype)(\.|$)/i;
|
|
1011
|
+
const STRING_EXPRESSION_WORDS = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
|
|
1012
1012
|
//#endregion
|
|
1013
1013
|
//#region src/internal/value/handlers.ts
|
|
1014
1014
|
function getCompareHandlers(owner, options) {
|
|
@@ -1236,8 +1236,10 @@ function getValueSorter(value, modifier) {
|
|
|
1236
1236
|
function initializeSorter(first, second) {
|
|
1237
1237
|
const sorters = getSorters(first, getModifier(first, second));
|
|
1238
1238
|
const sorter = (array) => sortArray(array, sorters);
|
|
1239
|
-
|
|
1240
|
-
|
|
1239
|
+
Object.defineProperties(sorter, {
|
|
1240
|
+
index: { value: (array, item) => getIndex(array, item, sorters) },
|
|
1241
|
+
is: { value: (array) => isSortedArray(array, sorters) }
|
|
1242
|
+
});
|
|
1241
1243
|
return sorter;
|
|
1242
1244
|
}
|
|
1243
1245
|
function isSorted(array, first, second) {
|
|
@@ -1397,10 +1399,10 @@ function isResult(value) {
|
|
|
1397
1399
|
}
|
|
1398
1400
|
function isResultValue(value, okValue) {
|
|
1399
1401
|
if (isNonPlainObject(value)) return false;
|
|
1400
|
-
return value.ok === okValue && (okValue ?
|
|
1402
|
+
return value.ok === okValue && (okValue ? RESULT_PROPERTY_VALUE : RESULT_PROPERTY_ERROR) in value;
|
|
1401
1403
|
}
|
|
1402
|
-
const
|
|
1403
|
-
const
|
|
1404
|
+
const RESULT_PROPERTY_ERROR = "error";
|
|
1405
|
+
const RESULT_PROPERTY_VALUE = "value";
|
|
1404
1406
|
//#endregion
|
|
1405
1407
|
//#region src/result/misc.ts
|
|
1406
1408
|
function error(value, original) {
|
|
@@ -1436,13 +1438,13 @@ function ok(value) {
|
|
|
1436
1438
|
*/
|
|
1437
1439
|
async function toPromise(result) {
|
|
1438
1440
|
const actual = typeof result === "function" ? result() : result;
|
|
1439
|
-
if (!isResult(actual)) return Promise.reject(new Error(
|
|
1441
|
+
if (!isResult(actual)) return Promise.reject(new Error(RESULT_MESSAGE_PROMISE));
|
|
1440
1442
|
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
1441
1443
|
}
|
|
1442
1444
|
function unwrap(value, defaultValue) {
|
|
1443
1445
|
return isOk(value) ? value.value : defaultValue;
|
|
1444
1446
|
}
|
|
1445
|
-
const
|
|
1447
|
+
const RESULT_MESSAGE_PROMISE = "toPromise expected to receive a Result";
|
|
1446
1448
|
//#endregion
|
|
1447
1449
|
//#region src/internal/value/misc.ts
|
|
1448
1450
|
function findKey(needle, haystack) {
|
|
@@ -1451,8 +1453,8 @@ function findKey(needle, haystack) {
|
|
|
1451
1453
|
return index > -1 ? keys[index] : needle;
|
|
1452
1454
|
}
|
|
1453
1455
|
function getNestedValue(data, path, ignoreCase) {
|
|
1454
|
-
if (typeof data !== "object" || data === null) return error(
|
|
1455
|
-
if (typeof path !== "string" || path.trim().length === 0) return error(
|
|
1456
|
+
if (typeof data !== "object" || data === null) return error(MISC_NESTED_MESSAGE_INPUT);
|
|
1457
|
+
if (typeof path !== "string" || path.trim().length === 0) return error(MISC_NESTED_MESSAGE_PATH);
|
|
1456
1458
|
const shouldIgnoreCase = ignoreCase === true;
|
|
1457
1459
|
const paths = getPaths(path, shouldIgnoreCase);
|
|
1458
1460
|
if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
|
|
@@ -1468,25 +1470,25 @@ function getNestedValue(data, path, ignoreCase) {
|
|
|
1468
1470
|
}
|
|
1469
1471
|
function getPaths(path, lowercase) {
|
|
1470
1472
|
const normalized = lowercase ? path.toLowerCase() : path;
|
|
1471
|
-
if (!
|
|
1472
|
-
return normalized.replace(
|
|
1473
|
+
if (!MISC_EXPRESSION_NESTED.test(normalized)) return normalized;
|
|
1474
|
+
return normalized.replace(MISC_EXPRESSION_BRACKET, ".$1").replace(MISC_EXPRESSION_DOTS, "").split(".");
|
|
1473
1475
|
}
|
|
1474
1476
|
function handleValue(data, path, value, get, ignoreCase) {
|
|
1475
1477
|
if (typeof data === "object" && data !== null) {
|
|
1476
|
-
if (ignoreKey(path)) return error(
|
|
1478
|
+
if (ignoreKey(path)) return error(MISC_NESTED_MESSAGE_UNSAFE);
|
|
1477
1479
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
1478
|
-
if (get) return key in data ? ok(data[key]) : error(
|
|
1480
|
+
if (get) return key in data ? ok(data[key]) : error(MISC_NESTED_MESSAGE_MISSING);
|
|
1479
1481
|
data[key] = typeof value === "function" ? value(data[key]) : value;
|
|
1480
1482
|
}
|
|
1481
|
-
if (get) return error(
|
|
1483
|
+
if (get) return error(MISC_NESTED_MESSAGE_MISSING);
|
|
1482
1484
|
}
|
|
1483
|
-
const
|
|
1484
|
-
const
|
|
1485
|
-
const
|
|
1486
|
-
const
|
|
1487
|
-
const
|
|
1488
|
-
const
|
|
1489
|
-
const
|
|
1485
|
+
const MISC_EXPRESSION_BRACKET = /\[(\w+)\]/g;
|
|
1486
|
+
const MISC_EXPRESSION_DOTS = /^\.|\.$/g;
|
|
1487
|
+
const MISC_EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
1488
|
+
const MISC_NESTED_MESSAGE_INPUT = "Expected data to be an object";
|
|
1489
|
+
const MISC_NESTED_MESSAGE_MISSING = "Expected property to exist in object";
|
|
1490
|
+
const MISC_NESTED_MESSAGE_PATH = "Expected path to be a string";
|
|
1491
|
+
const MISC_NESTED_MESSAGE_UNSAFE = "Access to this property is not allowed";
|
|
1490
1492
|
//#endregion
|
|
1491
1493
|
//#region src/internal/value/has.ts
|
|
1492
1494
|
function hasValue(data, path, ignoreCase) {
|
|
@@ -1538,7 +1540,7 @@ function assertCondition(condition, message, error) {
|
|
|
1538
1540
|
* @param error Error constructor _(defaults to `Error`)_
|
|
1539
1541
|
*/
|
|
1540
1542
|
function assertDefined(value, message, error) {
|
|
1541
|
-
assert(() => value != null, message ??
|
|
1543
|
+
assert(() => value != null, message ?? ASSERT_MESSAGE_VALUE_DEFINED, error);
|
|
1542
1544
|
}
|
|
1543
1545
|
/**
|
|
1544
1546
|
* Creates an _Asserter_ that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
@@ -1589,7 +1591,7 @@ function assertProperty(path, condition, message, error) {
|
|
|
1589
1591
|
}, message, error);
|
|
1590
1592
|
};
|
|
1591
1593
|
}
|
|
1592
|
-
const
|
|
1594
|
+
const ASSERT_MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
1593
1595
|
//#endregion
|
|
1594
1596
|
//#region src/internal/function/misc.ts
|
|
1595
1597
|
/**
|
|
@@ -1657,28 +1659,28 @@ function getNumber(value) {
|
|
|
1657
1659
|
if (typeof parsed !== "string") return getNumber(parsed);
|
|
1658
1660
|
const trimmed = parsed.trim();
|
|
1659
1661
|
if (trimmed.length === 0) return NaN;
|
|
1660
|
-
if (
|
|
1661
|
-
const isBinary =
|
|
1662
|
-
if (isBinary ||
|
|
1662
|
+
if (NUMBER_EXPRESSION_ZEROISH.test(parsed)) return 0;
|
|
1663
|
+
const isBinary = NUMBER_EXPRESSION_BINARY.test(trimmed);
|
|
1664
|
+
if (isBinary || NUMBER_EXPRESSION_OCTAL.test(trimmed)) return Number.parseInt(trimmed.slice(2), isBinary ? 2 : NUMBER_OCTAL_VALUE);
|
|
1663
1665
|
return Number(trimmed);
|
|
1664
1666
|
}
|
|
1665
1667
|
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
1666
1668
|
return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? Math.floor(value) : defaultValue;
|
|
1667
1669
|
}
|
|
1668
|
-
const
|
|
1669
|
-
const
|
|
1670
|
-
const
|
|
1671
|
-
const
|
|
1670
|
+
const NUMBER_EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
1671
|
+
const NUMBER_EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
1672
|
+
const NUMBER_EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
1673
|
+
const NUMBER_OCTAL_VALUE = 8;
|
|
1672
1674
|
//#endregion
|
|
1673
1675
|
//#region src/internal/sized.ts
|
|
1674
1676
|
function getSizedMaximum(first, second) {
|
|
1675
1677
|
let actual;
|
|
1676
1678
|
if (typeof first === "number") actual = first;
|
|
1677
|
-
else actual = typeof second === "number" ? second :
|
|
1678
|
-
return clamp(actual, 1,
|
|
1679
|
+
else actual = typeof second === "number" ? second : SIZED_MAXIMUM_DEFAULT;
|
|
1680
|
+
return clamp(actual, 1, SIZED_MAXIMUM_ABSOLUTE);
|
|
1679
1681
|
}
|
|
1680
|
-
const
|
|
1681
|
-
const
|
|
1682
|
+
const SIZED_MAXIMUM_ABSOLUTE = 16777216;
|
|
1683
|
+
const SIZED_MAXIMUM_DEFAULT = 1048576;
|
|
1682
1684
|
//#endregion
|
|
1683
1685
|
//#region src/sized/map.ts
|
|
1684
1686
|
/**
|
|
@@ -1739,7 +1741,7 @@ function getMemoizationOptions(input) {
|
|
|
1739
1741
|
const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
|
|
1740
1742
|
return {
|
|
1741
1743
|
cacheKey: typeof cacheKey === "function" ? cacheKey : void 0,
|
|
1742
|
-
cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize :
|
|
1744
|
+
cacheSize: typeof cacheSize === "number" && cacheSize > 0 ? cacheSize : MEMOIZED_CACHE_SIZE_DEFAULT
|
|
1743
1745
|
};
|
|
1744
1746
|
}
|
|
1745
1747
|
/**
|
|
@@ -1750,62 +1752,56 @@ function getMemoizationOptions(input) {
|
|
|
1750
1752
|
* @returns _Memoized_ instance
|
|
1751
1753
|
*/
|
|
1752
1754
|
function memoize(callback, options) {
|
|
1753
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
1754
|
-
const state = {
|
|
1755
|
+
if (typeof callback !== "function") throw new TypeError(MEMOIZED_CALLBACK);
|
|
1756
|
+
const state = {
|
|
1757
|
+
cache: void 0,
|
|
1758
|
+
getter: void 0,
|
|
1759
|
+
options: getMemoizationOptions(options)
|
|
1760
|
+
};
|
|
1755
1761
|
state.cache = new SizedMap(state.options.cacheSize);
|
|
1756
1762
|
state.getter = (...parameters) => {
|
|
1757
|
-
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString),
|
|
1763
|
+
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), MEMOIZED_KEY_SEPARATOR));
|
|
1758
1764
|
if (state.cache.has(key)) return state.cache.get(key);
|
|
1759
1765
|
const value = callback(...parameters);
|
|
1760
|
-
state.cache
|
|
1766
|
+
state.cache.set(key, value);
|
|
1761
1767
|
return value;
|
|
1762
1768
|
};
|
|
1763
1769
|
const instance = {
|
|
1764
|
-
clear: () =>
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
},
|
|
1770
|
-
destroy: () => {
|
|
1771
|
-
state.cache?.clear();
|
|
1772
|
-
state.cache = void 0;
|
|
1773
|
-
state.getter = void 0;
|
|
1774
|
-
},
|
|
1775
|
-
get: (key) => {
|
|
1776
|
-
return state.cache?.get(key);
|
|
1777
|
-
},
|
|
1778
|
-
has: (key) => {
|
|
1779
|
-
return state.cache?.has(key) ?? false;
|
|
1780
|
-
},
|
|
1781
|
-
run: (...parameters) => {
|
|
1782
|
-
if (state.cache == null || state.getter == null) throw new Error(MEMOIZED_ERROR_DESTROYED);
|
|
1783
|
-
return state.getter(...parameters);
|
|
1784
|
-
}
|
|
1770
|
+
clear: () => state.cache.clear(),
|
|
1771
|
+
delete: (key) => state.cache.delete(key),
|
|
1772
|
+
get: (key) => state.cache.get(key),
|
|
1773
|
+
has: (key) => state.cache.has(key),
|
|
1774
|
+
run: (...parameters) => state.getter(...parameters)
|
|
1785
1775
|
};
|
|
1786
1776
|
Object.defineProperties(instance, {
|
|
1787
1777
|
maximum: {
|
|
1788
1778
|
enumerable: true,
|
|
1789
|
-
get: () => state.cache
|
|
1779
|
+
get: () => state.cache.maximum
|
|
1790
1780
|
},
|
|
1791
1781
|
size: {
|
|
1792
1782
|
enumerable: true,
|
|
1793
|
-
get: () => state.cache
|
|
1783
|
+
get: () => state.cache.size
|
|
1794
1784
|
}
|
|
1795
1785
|
});
|
|
1796
1786
|
return Object.freeze(instance);
|
|
1797
1787
|
}
|
|
1798
|
-
const
|
|
1799
|
-
const
|
|
1800
|
-
const
|
|
1801
|
-
const SEPARATOR = "_";
|
|
1788
|
+
const MEMOIZED_CACHE_SIZE_DEFAULT = 1024;
|
|
1789
|
+
const MEMOIZED_CALLBACK = "Memoized requires a callback function";
|
|
1790
|
+
const MEMOIZED_KEY_SEPARATOR = "_";
|
|
1802
1791
|
//#endregion
|
|
1803
1792
|
//#region src/internal/function/timer.ts
|
|
1804
1793
|
function getAsyncTimer(type, callback, time) {
|
|
1794
|
+
function cancel() {
|
|
1795
|
+
clearTimer(id);
|
|
1796
|
+
if (last != null && !last.running) {
|
|
1797
|
+
last.reject();
|
|
1798
|
+
last = void 0;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1805
1801
|
async function run(item) {
|
|
1806
1802
|
const now = performance.now();
|
|
1807
1803
|
start ??= now;
|
|
1808
|
-
if (interval === 0 || now - start >= interval -
|
|
1804
|
+
if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
|
|
1809
1805
|
start = throttle ? now : void 0;
|
|
1810
1806
|
item.running = true;
|
|
1811
1807
|
try {
|
|
@@ -1825,7 +1821,7 @@ function getAsyncTimer(type, callback, time) {
|
|
|
1825
1821
|
let last;
|
|
1826
1822
|
let start;
|
|
1827
1823
|
const timer = (...parameters) => {
|
|
1828
|
-
|
|
1824
|
+
cancel();
|
|
1829
1825
|
const next = {
|
|
1830
1826
|
parameters,
|
|
1831
1827
|
running: false
|
|
@@ -1836,25 +1832,23 @@ function getAsyncTimer(type, callback, time) {
|
|
|
1836
1832
|
});
|
|
1837
1833
|
last = next;
|
|
1838
1834
|
if (throttle) run(next);
|
|
1839
|
-
else id = startTimer(() =>
|
|
1840
|
-
run(next);
|
|
1841
|
-
});
|
|
1835
|
+
else id = startTimer(() => run(next));
|
|
1842
1836
|
return next.promise;
|
|
1843
1837
|
};
|
|
1844
|
-
timer
|
|
1845
|
-
clearTimer(id);
|
|
1846
|
-
if (last != null && !last.running) last.reject();
|
|
1847
|
-
};
|
|
1838
|
+
Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
|
|
1848
1839
|
return timer;
|
|
1849
1840
|
}
|
|
1850
1841
|
function getInterval(value) {
|
|
1851
1842
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
1852
1843
|
}
|
|
1853
1844
|
function getTimer(type, callback, time) {
|
|
1845
|
+
function cancel() {
|
|
1846
|
+
clearTimer(id);
|
|
1847
|
+
}
|
|
1854
1848
|
function run() {
|
|
1855
1849
|
const now = performance.now();
|
|
1856
1850
|
start ??= now;
|
|
1857
|
-
if (interval === 0 || now - start >= interval -
|
|
1851
|
+
if (interval === 0 || now - start >= interval - TIMER_OFFSET) {
|
|
1858
1852
|
start = throttle ? now : void 0;
|
|
1859
1853
|
callback(...args);
|
|
1860
1854
|
} else id = startTimer(run);
|
|
@@ -1865,18 +1859,17 @@ function getTimer(type, callback, time) {
|
|
|
1865
1859
|
let id;
|
|
1866
1860
|
let start;
|
|
1867
1861
|
const timer = (...parameters) => {
|
|
1868
|
-
|
|
1862
|
+
cancel();
|
|
1869
1863
|
args = parameters;
|
|
1870
1864
|
if (throttle) run();
|
|
1871
1865
|
else id = startTimer(run);
|
|
1872
1866
|
};
|
|
1873
|
-
timer
|
|
1874
|
-
clearTimer(id);
|
|
1875
|
-
};
|
|
1867
|
+
Object.defineProperty(timer, TIMER_CANCEL, { value: () => cancel() });
|
|
1876
1868
|
return timer;
|
|
1877
1869
|
}
|
|
1878
|
-
const
|
|
1870
|
+
const TIMER_CANCEL = "cancel";
|
|
1879
1871
|
const TIMER_DEBOUNCE = "debounce";
|
|
1872
|
+
const TIMER_OFFSET = 5;
|
|
1880
1873
|
const TIMER_THROTTLE = "throttle";
|
|
1881
1874
|
const TIMER_WAIT = "wait";
|
|
1882
1875
|
// istanbul ignore next
|
|
@@ -1927,7 +1920,6 @@ function asyncThrottle(callback, time) {
|
|
|
1927
1920
|
function debounce(callback, time) {
|
|
1928
1921
|
return getTimer(TIMER_DEBOUNCE, callback, time);
|
|
1929
1922
|
}
|
|
1930
|
-
debounce.async = asyncDebounce;
|
|
1931
1923
|
/**
|
|
1932
1924
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
1933
1925
|
*
|
|
@@ -1938,7 +1930,8 @@ debounce.async = asyncDebounce;
|
|
|
1938
1930
|
function throttle(callback, time) {
|
|
1939
1931
|
return getTimer(TIMER_THROTTLE, callback, time);
|
|
1940
1932
|
}
|
|
1941
|
-
|
|
1933
|
+
Object.defineProperty(debounce, "async", { value: asyncDebounce });
|
|
1934
|
+
Object.defineProperty(throttle, "async", { value: asyncThrottle });
|
|
1942
1935
|
//#endregion
|
|
1943
1936
|
//#region src/function/once.ts
|
|
1944
1937
|
/**
|
|
@@ -1986,6 +1979,7 @@ function asyncOnce(callback) {
|
|
|
1986
1979
|
enumerable: true,
|
|
1987
1980
|
get: () => state.called
|
|
1988
1981
|
},
|
|
1982
|
+
clear: { value: () => clearState(state) },
|
|
1989
1983
|
cleared: {
|
|
1990
1984
|
enumerable: true,
|
|
1991
1985
|
get: () => state.cleared
|
|
@@ -1999,13 +1993,13 @@ function asyncOnce(callback) {
|
|
|
1999
1993
|
get: () => state.finished
|
|
2000
1994
|
}
|
|
2001
1995
|
});
|
|
2002
|
-
fn.clear = () => {
|
|
2003
|
-
if (!state.called || !state.finished || state.cleared) return;
|
|
2004
|
-
state.cleared = true;
|
|
2005
|
-
state.value = void 0;
|
|
2006
|
-
};
|
|
2007
1996
|
return fn;
|
|
2008
1997
|
}
|
|
1998
|
+
function clearState(state) {
|
|
1999
|
+
if (!state.called || state.cleared) return;
|
|
2000
|
+
state.cleared = true;
|
|
2001
|
+
state.value = void 0;
|
|
2002
|
+
}
|
|
2009
2003
|
function handleOnceResult(state, value, error) {
|
|
2010
2004
|
state.error = error;
|
|
2011
2005
|
state.finished = true;
|
|
@@ -2039,19 +2033,21 @@ function once(callback) {
|
|
|
2039
2033
|
return state.value;
|
|
2040
2034
|
};
|
|
2041
2035
|
Object.defineProperties(fn, {
|
|
2042
|
-
called: {
|
|
2043
|
-
|
|
2036
|
+
called: {
|
|
2037
|
+
enumerable: true,
|
|
2038
|
+
get: () => state.called
|
|
2039
|
+
},
|
|
2040
|
+
clear: { value: () => clearState(state) },
|
|
2041
|
+
cleared: {
|
|
2042
|
+
enumerable: true,
|
|
2043
|
+
get: () => state.cleared
|
|
2044
|
+
}
|
|
2044
2045
|
});
|
|
2045
|
-
fn.clear = () => {
|
|
2046
|
-
if (!state.called || state.cleared) return;
|
|
2047
|
-
state.cleared = true;
|
|
2048
|
-
state.value = void 0;
|
|
2049
|
-
};
|
|
2050
2046
|
return fn;
|
|
2051
2047
|
}
|
|
2052
|
-
once.async = asyncOnce;
|
|
2053
2048
|
const ONCE_MESSAGE_CLEARED = "Once has been cleared";
|
|
2054
2049
|
const ONCE_MESSAGE_EXPECTATION = "Once expected a function";
|
|
2050
|
+
Object.defineProperty(once, "async", { value: asyncOnce });
|
|
2055
2051
|
//#endregion
|
|
2056
2052
|
//#region src/function/retry.ts
|
|
2057
2053
|
/**
|
|
@@ -2131,13 +2127,13 @@ function retry(callback, options) {
|
|
|
2131
2127
|
}
|
|
2132
2128
|
throw new RetryError(RETRY_MESSAGE_FAILED, last);
|
|
2133
2129
|
}
|
|
2134
|
-
retry.async = asyncRetry;
|
|
2135
2130
|
function shouldRetry() {
|
|
2136
2131
|
return true;
|
|
2137
2132
|
}
|
|
2138
2133
|
const RETRY_ERROR_NAME = "RetryError";
|
|
2139
2134
|
const RETRY_MESSAGE_EXPECTATION = "Retry expected a function";
|
|
2140
2135
|
const RETRY_MESSAGE_FAILED = "Retry failed";
|
|
2136
|
+
Object.defineProperty(retry, "async", { value: asyncRetry });
|
|
2141
2137
|
//#endregion
|
|
2142
2138
|
//#region src/function/work.ts
|
|
2143
2139
|
function asyncFlow(...fns) {
|
|
@@ -2154,7 +2150,6 @@ function flow(...fns) {
|
|
|
2154
2150
|
return isOk(value) ? value.value : value;
|
|
2155
2151
|
}), fns, true);
|
|
2156
2152
|
}
|
|
2157
|
-
flow.async = asyncFlow;
|
|
2158
2153
|
async function asyncPipe(value, ...pipes) {
|
|
2159
2154
|
assertPipeFunctions(pipes);
|
|
2160
2155
|
return asyncWork(value, pipes, false);
|
|
@@ -2163,7 +2158,6 @@ function pipe(value, ...pipes) {
|
|
|
2163
2158
|
assertPipeFunctions(pipes);
|
|
2164
2159
|
return work(value, pipes, false);
|
|
2165
2160
|
}
|
|
2166
|
-
pipe.async = asyncPipe;
|
|
2167
2161
|
async function asyncWork(initial, functions, flow) {
|
|
2168
2162
|
const { length } = functions;
|
|
2169
2163
|
let transformed = unwrapValue(initial);
|
|
@@ -2175,10 +2169,10 @@ async function asyncWork(initial, functions, flow) {
|
|
|
2175
2169
|
}
|
|
2176
2170
|
function unwrapValue(value, flow, nested) {
|
|
2177
2171
|
if (typeof value === "function") {
|
|
2178
|
-
if (nested != null) throw new TypeError(
|
|
2172
|
+
if (nested != null) throw new TypeError(WORK_MESSAGE_NESTING);
|
|
2179
2173
|
return unwrapValue(value(), flow, true);
|
|
2180
2174
|
}
|
|
2181
|
-
if (flow != null && value instanceof Promise) throw new TypeError(flow ?
|
|
2175
|
+
if (flow != null && value instanceof Promise) throw new TypeError(flow ? WORK_MESSAGE_FLOW_PROMISE : WORK_MESSAGE_PIPE_PROMISE);
|
|
2182
2176
|
if (isError(value)) throw value.error;
|
|
2183
2177
|
return isOk(value) ? value.value : value;
|
|
2184
2178
|
}
|
|
@@ -2191,114 +2185,42 @@ function work(initial, functions, flow) {
|
|
|
2191
2185
|
}
|
|
2192
2186
|
return transformed;
|
|
2193
2187
|
}
|
|
2194
|
-
const
|
|
2195
|
-
const
|
|
2196
|
-
const
|
|
2197
|
-
const
|
|
2198
|
-
const
|
|
2199
|
-
const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"),
|
|
2200
|
-
const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"),
|
|
2188
|
+
const WORK_MESSAGE_FLOW_ARRAY = "Flow expected to receive an array of functions";
|
|
2189
|
+
const WORK_MESSAGE_FLOW_PROMISE = "Synchronous Flow received a promise. Use `flow.async` instead.";
|
|
2190
|
+
const WORK_MESSAGE_NESTING = "Return values are too deeply nested.";
|
|
2191
|
+
const WORK_MESSAGE_PIPE_ARRAY = "Pipe expected to receive an array of functions";
|
|
2192
|
+
const WORK_MESSAGE_PIPE_PROMISE = "Synchronous Pipe received a promise. Use `pipe.async` instead.";
|
|
2193
|
+
const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_FLOW_ARRAY, TypeError);
|
|
2194
|
+
const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), WORK_MESSAGE_PIPE_ARRAY, TypeError);
|
|
2195
|
+
flow.async = asyncFlow;
|
|
2196
|
+
pipe.async = asyncPipe;
|
|
2197
|
+
Object.defineProperty(flow, "async", { value: asyncFlow });
|
|
2198
|
+
Object.defineProperty(pipe, "async", { value: asyncPipe });
|
|
2201
2199
|
//#endregion
|
|
2202
|
-
//#region src/
|
|
2203
|
-
function
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
value: NAME_EVENTS
|
|
2211
|
-
});
|
|
2212
|
-
return Object.freeze(events);
|
|
2213
|
-
}
|
|
2214
|
-
/**
|
|
2215
|
-
* Create a _Herald_ for announcing named events
|
|
2216
|
-
*
|
|
2217
|
-
* @param names Event names
|
|
2218
|
-
* @returns _Herald_ instance
|
|
2219
|
-
*/
|
|
2220
|
-
function herald(names) {
|
|
2221
|
-
if (!Array.isArray(names) || names.length === 0 || !names.every((name) => typeof name === "string")) throw new Error(MESSAGE);
|
|
2222
|
-
const set = new Set(names);
|
|
2223
|
-
const subscribers = /* @__PURE__ */ new Map();
|
|
2224
|
-
const instance = {
|
|
2225
|
-
clear() {
|
|
2226
|
-
subscribers.clear();
|
|
2227
|
-
},
|
|
2228
|
-
emit(event, ...parameters) {
|
|
2229
|
-
const callbacks = subscribers.get(event);
|
|
2230
|
-
if (callbacks == null) return;
|
|
2231
|
-
for (const callback of callbacks) callback(...parameters);
|
|
2232
|
-
},
|
|
2233
|
-
subscribe(event, callback) {
|
|
2234
|
-
if (!set.has(event) || typeof callback !== "function") return noop;
|
|
2235
|
-
let eventSubscribers = subscribers.get(event);
|
|
2236
|
-
if (eventSubscribers == null) {
|
|
2237
|
-
eventSubscribers = /* @__PURE__ */ new Set();
|
|
2238
|
-
subscribers.set(event, eventSubscribers);
|
|
2239
|
-
}
|
|
2240
|
-
eventSubscribers.add(callback);
|
|
2241
|
-
return () => {
|
|
2242
|
-
eventSubscribers?.delete(callback);
|
|
2243
|
-
};
|
|
2244
|
-
},
|
|
2245
|
-
unsubscribe(event, callback) {
|
|
2246
|
-
if (!set.has(event) || (callback != null ? typeof callback !== "function" : false)) return;
|
|
2247
|
-
const eventSubscribers = subscribers.get(event);
|
|
2248
|
-
if (eventSubscribers == null) return;
|
|
2249
|
-
if (callback == null) eventSubscribers.clear();
|
|
2250
|
-
else eventSubscribers.delete(callback);
|
|
2251
|
-
if (callback == null || eventSubscribers.size === 0) subscribers.delete(event);
|
|
2252
|
-
}
|
|
2200
|
+
//#region src/internal/abort.ts
|
|
2201
|
+
function createAborter(value, onAbort) {
|
|
2202
|
+
if (!(value instanceof AbortSignal)) return;
|
|
2203
|
+
value.addEventListener(ABORT_EVENT, onAbort, ABORT_OPTIONS);
|
|
2204
|
+
return {
|
|
2205
|
+
callback: onAbort,
|
|
2206
|
+
signal: value,
|
|
2207
|
+
cancel: () => value.removeEventListener(ABORT_EVENT, onAbort)
|
|
2253
2208
|
};
|
|
2254
|
-
Object.defineProperties(instance, {
|
|
2255
|
-
[KEY_HERALD]: {
|
|
2256
|
-
enumerable: false,
|
|
2257
|
-
value: NAME_HERALD
|
|
2258
|
-
},
|
|
2259
|
-
events: {
|
|
2260
|
-
enumerable: true,
|
|
2261
|
-
value: getEvents(instance)
|
|
2262
|
-
}
|
|
2263
|
-
});
|
|
2264
|
-
return Object.freeze(instance);
|
|
2265
|
-
}
|
|
2266
|
-
/**
|
|
2267
|
-
* Is the value events for a herald?
|
|
2268
|
-
*
|
|
2269
|
-
* @param value Value to check
|
|
2270
|
-
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
2271
|
-
*/
|
|
2272
|
-
function isEvents(value) {
|
|
2273
|
-
return isHeraldInstance(NAME_EVENTS, value);
|
|
2274
2209
|
}
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
*
|
|
2278
|
-
* @param value Value to check
|
|
2279
|
-
* @returns `true` if the value is a herald, otherwise `false`
|
|
2280
|
-
*/
|
|
2281
|
-
function isHerald(value) {
|
|
2282
|
-
return isHeraldInstance(NAME_HERALD, value);
|
|
2283
|
-
}
|
|
2284
|
-
function isHeraldInstance(name, value) {
|
|
2285
|
-
return typeof value === "object" && value != null && KEY_HERALD in value && value[KEY_HERALD] === name;
|
|
2286
|
-
}
|
|
2287
|
-
const KEY_HERALD = "$herald";
|
|
2288
|
-
const MESSAGE = "Herald requires an array of event names.";
|
|
2289
|
-
const NAME_EVENTS = "events";
|
|
2290
|
-
const NAME_HERALD = "herald";
|
|
2210
|
+
const ABORT_EVENT = "abort";
|
|
2211
|
+
const ABORT_OPTIONS = { once: true };
|
|
2291
2212
|
//#endregion
|
|
2292
2213
|
//#region src/internal/subscription.ts
|
|
2293
|
-
function addSubscription(subscription, state) {
|
|
2294
|
-
const { key,
|
|
2295
|
-
const { items, values } = subscriptions;
|
|
2296
|
-
if (key
|
|
2214
|
+
function addSubscription(subscriptions, subscription, state) {
|
|
2215
|
+
const { key, value } = state.parameters;
|
|
2216
|
+
const { items, values } = subscriptions.state;
|
|
2217
|
+
if (!isKey(key)) {
|
|
2297
2218
|
items.any.add(subscription);
|
|
2298
2219
|
values.from.any.set(value, subscription);
|
|
2299
2220
|
values.to.any.set(subscription, value);
|
|
2300
2221
|
return;
|
|
2301
2222
|
}
|
|
2223
|
+
/* istanbul ignore if */
|
|
2302
2224
|
if (items.keyed == null || values.from.keyed == null || values.to.keyed == null)
|
|
2303
2225
|
// istanbul ignore next
|
|
2304
2226
|
return;
|
|
@@ -2325,41 +2247,31 @@ function clearSubscriptions(store) {
|
|
|
2325
2247
|
for (const susbcription of store.values.to.any.keys()) susbcription.unsubscribe();
|
|
2326
2248
|
if (store.values.to.keyed != null) for (const values of store.values.to.keyed.values()) for (const susbcription of values.keys()) susbcription.unsubscribe();
|
|
2327
2249
|
}
|
|
2328
|
-
function
|
|
2329
|
-
|
|
2330
|
-
const { values } = parameters.subscriptions;
|
|
2331
|
-
return parameters.key == null ? values.from.any.get(parameters.value) : values.from.keyed?.get(parameters.key)?.get(parameters.value);
|
|
2332
|
-
}
|
|
2333
|
-
function getSubscription(parameters) {
|
|
2250
|
+
function createSubscription(subscriptions, property, parameters) {
|
|
2251
|
+
if (parameters.signal?.aborted ?? false) throw new Error(parameters.signal?.reason);
|
|
2334
2252
|
const state = {
|
|
2335
2253
|
parameters,
|
|
2336
2254
|
active: true
|
|
2337
2255
|
};
|
|
2338
|
-
const existing = getExistingSubscription(state);
|
|
2339
|
-
if (existing != null) return existing;
|
|
2340
|
-
if (
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
} };
|
|
2256
|
+
const existing = getExistingSubscription(subscriptions, state);
|
|
2257
|
+
if (existing != null) return [existing, true];
|
|
2258
|
+
if (subscriptions.state.keys != null && isKey(parameters.key) && !subscriptions.state.keys.has(parameters.key)) throw new Error(SUBSCRIPTION_INVALID_KEY);
|
|
2259
|
+
state.aborter = createAborter(parameters.signal, () => unsubscribe(subscriptions, instance, state));
|
|
2260
|
+
const instance = { unsubscribe: () => unsubscribe(subscriptions, instance, state) };
|
|
2344
2261
|
Object.defineProperties(instance, {
|
|
2345
|
-
[
|
|
2346
|
-
|
|
2347
|
-
value: true
|
|
2348
|
-
},
|
|
2349
|
-
[parameters.property.key]: {
|
|
2350
|
-
enumerable: false,
|
|
2351
|
-
value: parameters.property.value
|
|
2352
|
-
},
|
|
2262
|
+
[SUBSCRIPTION_PROPERTY]: { value: true },
|
|
2263
|
+
[property.key]: { value: property.value ?? "subscription" },
|
|
2353
2264
|
active: {
|
|
2354
2265
|
enumerable: true,
|
|
2355
2266
|
get: () => (parameters.isActive?.() ?? true) && state.active
|
|
2356
2267
|
}
|
|
2357
2268
|
});
|
|
2358
|
-
addSubscription(instance, state);
|
|
2359
|
-
return Object.freeze(instance);
|
|
2269
|
+
addSubscription(subscriptions, instance, state);
|
|
2270
|
+
return [Object.freeze(instance), false];
|
|
2360
2271
|
}
|
|
2361
|
-
function
|
|
2362
|
-
|
|
2272
|
+
function createSubscriptions(parameters) {
|
|
2273
|
+
const { keys, property } = parameters;
|
|
2274
|
+
const state = {
|
|
2363
2275
|
keys,
|
|
2364
2276
|
items: {
|
|
2365
2277
|
any: /* @__PURE__ */ new Set(),
|
|
@@ -2376,6 +2288,17 @@ function getSubscriptions(keys) {
|
|
|
2376
2288
|
}
|
|
2377
2289
|
}
|
|
2378
2290
|
};
|
|
2291
|
+
const instance = {
|
|
2292
|
+
state,
|
|
2293
|
+
clear: () => clearSubscriptions(state),
|
|
2294
|
+
create: (parameters) => createSubscription(instance, property, parameters)
|
|
2295
|
+
};
|
|
2296
|
+
return instance;
|
|
2297
|
+
}
|
|
2298
|
+
function getExistingSubscription(subscriptions, state) {
|
|
2299
|
+
const { parameters } = state;
|
|
2300
|
+
const { values } = subscriptions.state;
|
|
2301
|
+
return isKey(parameters.key) ? values.from.keyed?.get(parameters.key)?.get(parameters.value) : values.from.any.get(parameters.value);
|
|
2379
2302
|
}
|
|
2380
2303
|
/**
|
|
2381
2304
|
* Is the value a subscription?
|
|
@@ -2384,7 +2307,7 @@ function getSubscriptions(keys) {
|
|
|
2384
2307
|
* @returns `true` if the value is a subscription, otherwise `false`
|
|
2385
2308
|
*/
|
|
2386
2309
|
function isSubscription(value) {
|
|
2387
|
-
return isPlainObject(value) &&
|
|
2310
|
+
return isPlainObject(value) && SUBSCRIPTION_PROPERTY in value && value[SUBSCRIPTION_PROPERTY] === true;
|
|
2388
2311
|
}
|
|
2389
2312
|
function removeFromStore(items, values, subscription, value, key) {
|
|
2390
2313
|
items.delete(subscription);
|
|
@@ -2396,23 +2319,138 @@ function removeFromStore(items, values, subscription, value, key) {
|
|
|
2396
2319
|
values.to.keyed?.get(key)?.delete(subscription);
|
|
2397
2320
|
}
|
|
2398
2321
|
}
|
|
2399
|
-
function removeSubscription(subscription, state) {
|
|
2322
|
+
function removeSubscription(subscriptions, subscription, state) {
|
|
2400
2323
|
if (!state.active) return;
|
|
2324
|
+
state.aborter = void 0;
|
|
2401
2325
|
state.active = false;
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2326
|
+
state.parameters.isActive = void 0;
|
|
2327
|
+
state.parameters.signal = void 0;
|
|
2328
|
+
const { key, value } = state.parameters;
|
|
2329
|
+
const { items, values } = subscriptions.state;
|
|
2330
|
+
if (!isKey(key)) {
|
|
2405
2331
|
removeFromStore(items.any, values, subscription, value);
|
|
2406
2332
|
return;
|
|
2407
2333
|
}
|
|
2408
2334
|
const keyed = items.keyed?.get(key);
|
|
2335
|
+
/* istanbul ignore if */
|
|
2409
2336
|
if (items.keyed == null || keyed == null)
|
|
2410
2337
|
// istanbul ignore next
|
|
2411
2338
|
return;
|
|
2412
2339
|
removeFromStore(keyed, values, subscription, value, key);
|
|
2413
2340
|
if (keyed.size === 0) items.keyed.delete(key);
|
|
2414
2341
|
}
|
|
2415
|
-
|
|
2342
|
+
function unsubscribe(subscriptions, subscription, state) {
|
|
2343
|
+
state.aborter?.cancel();
|
|
2344
|
+
removeSubscription(subscriptions, subscription, state);
|
|
2345
|
+
}
|
|
2346
|
+
const SUBSCRIPTION_INVALID_KEY = "Invalid key for subscription";
|
|
2347
|
+
const SUBSCRIPTION_PROPERTY = "$subscription";
|
|
2348
|
+
//#endregion
|
|
2349
|
+
//#region src/herald.ts
|
|
2350
|
+
function createEvents(herald, state) {
|
|
2351
|
+
const events = {};
|
|
2352
|
+
Object.defineProperty(events, HERALD_PROPERTY, { value: HERALD_NAME_EVENTS });
|
|
2353
|
+
for (const key of state.keys) events[key] = (callback, signal) => herald.subscribe(key, callback, signal);
|
|
2354
|
+
return Object.freeze(events);
|
|
2355
|
+
}
|
|
2356
|
+
function emitForHerald(state, event, ...parameters) {
|
|
2357
|
+
const items = state.subscriptions.state.values.from.keyed?.get(event);
|
|
2358
|
+
if (items == null || items.size === 0) return;
|
|
2359
|
+
for (const [callback] of items) callback(...parameters);
|
|
2360
|
+
}
|
|
2361
|
+
function getHeraldSubscriptionProperty(input) {
|
|
2362
|
+
if (input == null) return heraldSubscription;
|
|
2363
|
+
const property = isPlainObject(input) ? input : {};
|
|
2364
|
+
if (typeof property.key !== "string" || property.value != null && typeof property.value !== "string") throw new Error(HERALD_MESSAGE_PROPERTY);
|
|
2365
|
+
return {
|
|
2366
|
+
key: property.key,
|
|
2367
|
+
value: property.value
|
|
2368
|
+
};
|
|
2369
|
+
}
|
|
2370
|
+
function getHeraldState(input) {
|
|
2371
|
+
const options = isPlainObject(input) ? input : {};
|
|
2372
|
+
if (!Array.isArray(options.names) || options.names.length === 0 || !options.names.every((name) => typeof name === "string")) throw new Error(HERALD_MESSAGE_ARRAY);
|
|
2373
|
+
if (options.onCreate != null && typeof options.onCreate !== "function") throw new Error(HERALD_MESSAGE_ONCREATE);
|
|
2374
|
+
const keys = new Set(options.names);
|
|
2375
|
+
return {
|
|
2376
|
+
keys,
|
|
2377
|
+
subscriptions: createSubscriptions({
|
|
2378
|
+
keys,
|
|
2379
|
+
property: getHeraldSubscriptionProperty(options.property)
|
|
2380
|
+
}),
|
|
2381
|
+
onCreate: options.onCreate
|
|
2382
|
+
};
|
|
2383
|
+
}
|
|
2384
|
+
/**
|
|
2385
|
+
* Create a _Herald_ for announcing named events
|
|
2386
|
+
*
|
|
2387
|
+
* @param names Event names
|
|
2388
|
+
* @param property Optional property for subscription identification _(defaults to `$herald`)_
|
|
2389
|
+
* @returns _Herald_ instance
|
|
2390
|
+
*/
|
|
2391
|
+
function herald(options) {
|
|
2392
|
+
const state = getHeraldState(options);
|
|
2393
|
+
const instance = {
|
|
2394
|
+
clear: () => state.subscriptions.clear(),
|
|
2395
|
+
emit: (event, ...parameters) => emitForHerald(state, event, ...parameters),
|
|
2396
|
+
observed: (event) => (state.subscriptions.state.items.keyed?.get(event)?.size ?? 0) > 0,
|
|
2397
|
+
subscribe: (key, callback, signal) => subscribeToHerald(state, key, callback, signal)
|
|
2398
|
+
};
|
|
2399
|
+
Object.defineProperties(instance, {
|
|
2400
|
+
[HERALD_PROPERTY]: { value: HERALD_NAME_HERALD },
|
|
2401
|
+
events: {
|
|
2402
|
+
enumerable: true,
|
|
2403
|
+
value: createEvents(instance, state)
|
|
2404
|
+
}
|
|
2405
|
+
});
|
|
2406
|
+
return Object.freeze(instance);
|
|
2407
|
+
}
|
|
2408
|
+
/**
|
|
2409
|
+
* Is the value events for a herald?
|
|
2410
|
+
*
|
|
2411
|
+
* @param value Value to check
|
|
2412
|
+
* @returns `true` if the value is events for a herald, otherwise `false`
|
|
2413
|
+
*/
|
|
2414
|
+
function isHeraldEvents(value) {
|
|
2415
|
+
return isHeraldInstance(HERALD_NAME_EVENTS, value);
|
|
2416
|
+
}
|
|
2417
|
+
/**
|
|
2418
|
+
* Is the value a herald?
|
|
2419
|
+
*
|
|
2420
|
+
* @param value Value to check
|
|
2421
|
+
* @returns `true` if the value is a herald, otherwise `false`
|
|
2422
|
+
*/
|
|
2423
|
+
function isHerald(value) {
|
|
2424
|
+
return isHeraldInstance(HERALD_NAME_HERALD, value);
|
|
2425
|
+
}
|
|
2426
|
+
function isHeraldInstance(name, value) {
|
|
2427
|
+
return isPlainObject(value) && HERALD_PROPERTY in value && value[HERALD_PROPERTY] === name;
|
|
2428
|
+
}
|
|
2429
|
+
/**
|
|
2430
|
+
* Is the value a herald subscription?
|
|
2431
|
+
*
|
|
2432
|
+
* @param value Value to check
|
|
2433
|
+
* @returns `true` if the value is a herald subscription, otherwise `false`
|
|
2434
|
+
*/
|
|
2435
|
+
function isHeraldSubscription(value) {
|
|
2436
|
+
return isSubscription(value) && value[HERALD_PROPERTY] === "subscription";
|
|
2437
|
+
}
|
|
2438
|
+
function subscribeToHerald(state, key, callback, signal) {
|
|
2439
|
+
const [subscription, existing] = state.subscriptions.create({
|
|
2440
|
+
key,
|
|
2441
|
+
signal,
|
|
2442
|
+
value: callback
|
|
2443
|
+
});
|
|
2444
|
+
if (!existing) state.onCreate?.(key, callback, subscription);
|
|
2445
|
+
return subscription;
|
|
2446
|
+
}
|
|
2447
|
+
const HERALD_PROPERTY = "$herald";
|
|
2448
|
+
const HERALD_MESSAGE_ARRAY = "Herald requires an array of event names.";
|
|
2449
|
+
const HERALD_MESSAGE_ONCREATE = `Herald requires a valid onCreate callback for subscription creation`;
|
|
2450
|
+
const HERALD_MESSAGE_PROPERTY = `Herald requires valid property information for subscription identification`;
|
|
2451
|
+
const HERALD_NAME_EVENTS = "events";
|
|
2452
|
+
const HERALD_NAME_HERALD = "herald";
|
|
2453
|
+
const heraldSubscription = { key: HERALD_PROPERTY };
|
|
2416
2454
|
//#endregion
|
|
2417
2455
|
//#region src/internal/value/equal.ts
|
|
2418
2456
|
/**
|
|
@@ -2474,7 +2512,7 @@ function equalPlainObject(first, second, options) {
|
|
|
2474
2512
|
firstKeys = firstKeys.filter((key) => filterKey(key, options));
|
|
2475
2513
|
secondKeys = secondKeys.filter((key) => filterKey(key, options));
|
|
2476
2514
|
}
|
|
2477
|
-
const secondSet = secondKeys.length >=
|
|
2515
|
+
const secondSet = secondKeys.length >= EQUAL_MINIMUM_LENGTH_FOR_SET ? new Set(secondKeys) : void 0;
|
|
2478
2516
|
const { length } = firstKeys;
|
|
2479
2517
|
if (length !== secondKeys.length) return false;
|
|
2480
2518
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -2519,9 +2557,9 @@ function equalValue(first, second, options) {
|
|
|
2519
2557
|
case first instanceof ArrayBuffer && second instanceof ArrayBuffer: return equalArrayBuffer(first, second, options);
|
|
2520
2558
|
case first instanceof Date && second instanceof Date: return Object.is(first.getTime(), second.getTime());
|
|
2521
2559
|
case first instanceof DataView && second instanceof DataView: return equalDataView(first, second, options);
|
|
2522
|
-
case first instanceof Error && second instanceof Error: return equalProperties(first, second,
|
|
2560
|
+
case first instanceof Error && second instanceof Error: return equalProperties(first, second, EQUAL_ERROR_PROPERTIES, options);
|
|
2523
2561
|
case first instanceof Map && second instanceof Map: return equalMap(first, second, options);
|
|
2524
|
-
case first instanceof RegExp && second instanceof RegExp: return equalProperties(first, second,
|
|
2562
|
+
case first instanceof RegExp && second instanceof RegExp: return equalProperties(first, second, EQUAL_EXPRESSION_PROPERTIES, options);
|
|
2525
2563
|
case first instanceof Set && second instanceof Set: return equalSet(first, second, options);
|
|
2526
2564
|
case Array.isArray(first) && Array.isArray(second): return equalArray(first, second, options);
|
|
2527
2565
|
case isPlainObject(first) && isPlainObject(second): return equalPlainObject(first, second, options);
|
|
@@ -2583,9 +2621,9 @@ function registerEqualizer(constructor, handler) {
|
|
|
2583
2621
|
}
|
|
2584
2622
|
const EQUAL_ARRAY_PEEK_PERCENTAGE = 10;
|
|
2585
2623
|
const EQUAL_ARRAY_THRESHOLD = 100;
|
|
2586
|
-
const
|
|
2587
|
-
const
|
|
2588
|
-
const
|
|
2624
|
+
const EQUAL_ERROR_PROPERTIES = ["name", "message"];
|
|
2625
|
+
const EQUAL_EXPRESSION_PROPERTIES = ["source", "flags"];
|
|
2626
|
+
const EQUAL_MINIMUM_LENGTH_FOR_SET = 16;
|
|
2589
2627
|
//#endregion
|
|
2590
2628
|
//#region src/internal/value/get.ts
|
|
2591
2629
|
function getValue(data, path, ignoreCase) {
|
|
@@ -2613,7 +2651,7 @@ function setValue(data, path, value, ignoreCase) {
|
|
|
2613
2651
|
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase).value;
|
|
2614
2652
|
if (typeof next !== "object" || next === null) {
|
|
2615
2653
|
const nextPath = paths[index + 1];
|
|
2616
|
-
if (
|
|
2654
|
+
if (SET_EXPRESSION_INDEX.test(nextPath)) next = Array.from({ length: Number(nextPath) + 1 }, () => void 0);
|
|
2617
2655
|
else next = {};
|
|
2618
2656
|
target[currentPath] = next;
|
|
2619
2657
|
}
|
|
@@ -2621,7 +2659,7 @@ function setValue(data, path, value, ignoreCase) {
|
|
|
2621
2659
|
}
|
|
2622
2660
|
return data;
|
|
2623
2661
|
}
|
|
2624
|
-
const
|
|
2662
|
+
const SET_EXPRESSION_INDEX = /^\d+$/;
|
|
2625
2663
|
//#endregion
|
|
2626
2664
|
//#region src/string/case.ts
|
|
2627
2665
|
/**
|
|
@@ -2631,7 +2669,7 @@ const EXPRESSION_INDEX = /^\d+$/;
|
|
|
2631
2669
|
* @returns Camel-cased string
|
|
2632
2670
|
*/
|
|
2633
2671
|
function camelCase(value) {
|
|
2634
|
-
return toCase(
|
|
2672
|
+
return toCase(STRING_CASE_CAMEL, value, true, false);
|
|
2635
2673
|
}
|
|
2636
2674
|
/**
|
|
2637
2675
|
* Capitalize the first letter of a string _(and lowercase the rest)_
|
|
@@ -2651,7 +2689,7 @@ function capitalize(value) {
|
|
|
2651
2689
|
* @returns Kebab-cased string
|
|
2652
2690
|
*/
|
|
2653
2691
|
function kebabCase(value) {
|
|
2654
|
-
return toCase(
|
|
2692
|
+
return toCase(STRING_CASE_KEBAB, value, false, false);
|
|
2655
2693
|
}
|
|
2656
2694
|
/**
|
|
2657
2695
|
* Convert a string to lower case
|
|
@@ -2671,7 +2709,7 @@ function lowerCase(value) {
|
|
|
2671
2709
|
* @returns Pascal-cased string
|
|
2672
2710
|
*/
|
|
2673
2711
|
function pascalCase(value) {
|
|
2674
|
-
return toCase(
|
|
2712
|
+
return toCase(STRING_CASE_PASCAL, value, true, true);
|
|
2675
2713
|
}
|
|
2676
2714
|
/**
|
|
2677
2715
|
* Convert a string to snake case _(this_is_snake_case)_
|
|
@@ -2680,7 +2718,7 @@ function pascalCase(value) {
|
|
|
2680
2718
|
* @returns Snake-cased string
|
|
2681
2719
|
*/
|
|
2682
2720
|
function snakeCase(value) {
|
|
2683
|
-
return toCase(
|
|
2721
|
+
return toCase(STRING_CASE_SNAKE, value, false, false);
|
|
2684
2722
|
}
|
|
2685
2723
|
/**
|
|
2686
2724
|
* Convert a string to title case _(Capitalizing Every Word)_
|
|
@@ -2709,7 +2747,7 @@ function toCaseCallback(value) {
|
|
|
2709
2747
|
const partsLength = parts.length;
|
|
2710
2748
|
const cased = [];
|
|
2711
2749
|
for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
|
|
2712
|
-
const items = parts[partIndex].replace(
|
|
2750
|
+
const items = parts[partIndex].replace(STRING_EXPRESSION_ACRONYM, (full, one, two, three) => three === STRING_S ? full : `${one}-${two}${three}`).replace(STRING_EXPRESSION_CAMEL_CASE, STRING_REPLACEMENT_CAMEL_CASE).split("-");
|
|
2713
2751
|
const itemsLength = items.length;
|
|
2714
2752
|
const partResult = [];
|
|
2715
2753
|
let itemCount = 0;
|
|
@@ -2735,23 +2773,23 @@ function upperCase(value) {
|
|
|
2735
2773
|
memoizedUpperCase ??= memoize((v) => v.toLocaleUpperCase());
|
|
2736
2774
|
return memoizedUpperCase.run(value);
|
|
2737
2775
|
}
|
|
2738
|
-
const
|
|
2739
|
-
const
|
|
2740
|
-
const
|
|
2741
|
-
const
|
|
2742
|
-
const
|
|
2743
|
-
const
|
|
2744
|
-
const
|
|
2745
|
-
const
|
|
2746
|
-
const
|
|
2747
|
-
const
|
|
2748
|
-
const
|
|
2776
|
+
const STRING_CASE_CAMEL = "camel";
|
|
2777
|
+
const STRING_CASE_KEBAB = "kebab";
|
|
2778
|
+
const STRING_CASE_PASCAL = "pascal";
|
|
2779
|
+
const STRING_CASE_SNAKE = "snake";
|
|
2780
|
+
const STRING_DELIMTER_EMPTY = "";
|
|
2781
|
+
const STRING_DELIMITER_HYPHEN = "-";
|
|
2782
|
+
const STRING_DELIMITER_UNDERSCORE = "_";
|
|
2783
|
+
const STRING_EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
|
|
2784
|
+
const STRING_EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
|
|
2785
|
+
const STRING_REPLACEMENT_CAMEL_CASE = "$1-$2";
|
|
2786
|
+
const STRING_S = "s";
|
|
2749
2787
|
const caseMemoizers = {};
|
|
2750
2788
|
const delimiters = {
|
|
2751
|
-
[
|
|
2752
|
-
[
|
|
2753
|
-
[
|
|
2754
|
-
[
|
|
2789
|
+
[STRING_CASE_CAMEL]: STRING_DELIMTER_EMPTY,
|
|
2790
|
+
[STRING_CASE_KEBAB]: STRING_DELIMITER_HYPHEN,
|
|
2791
|
+
[STRING_CASE_PASCAL]: STRING_DELIMTER_EMPTY,
|
|
2792
|
+
[STRING_CASE_SNAKE]: STRING_DELIMITER_UNDERSCORE
|
|
2755
2793
|
};
|
|
2756
2794
|
let memoizedCapitalize;
|
|
2757
2795
|
let memoizedLowerCase;
|
|
@@ -2813,10 +2851,10 @@ function getHandler(input) {
|
|
|
2813
2851
|
case "string": return (item) => item[input];
|
|
2814
2852
|
default:
|
|
2815
2853
|
if (isPlainObject(input)) return getHandler(input.key ?? input.handler);
|
|
2816
|
-
throw new TypeError(
|
|
2854
|
+
throw new TypeError(FUZZY_MESSAGE_HANDLER);
|
|
2817
2855
|
}
|
|
2818
2856
|
}
|
|
2819
|
-
function
|
|
2857
|
+
function getFuzzyItems(items) {
|
|
2820
2858
|
return items.sort((first, second) => first.haystack.localeCompare(second.haystack)).map(({ item }) => item);
|
|
2821
2859
|
}
|
|
2822
2860
|
function getFuzzyOptions(input, state) {
|
|
@@ -2840,22 +2878,22 @@ function getFuzzyState(items, input) {
|
|
|
2840
2878
|
}
|
|
2841
2879
|
function getTolerance(input, state) {
|
|
2842
2880
|
if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 : Math.floor(input);
|
|
2843
|
-
return state?.tolerance ??
|
|
2881
|
+
return state?.tolerance ?? FUZZY_PROXIMITY_THRESHOLD;
|
|
2844
2882
|
}
|
|
2845
2883
|
function fuzzy(items, configuration) {
|
|
2846
|
-
if (!Array.isArray(items)) throw new TypeError(
|
|
2884
|
+
if (!Array.isArray(items)) throw new TypeError(FUZZY_MESSAGE_ARRAY);
|
|
2847
2885
|
const state = getFuzzyState(items, configuration);
|
|
2848
2886
|
const instance = { search: (value, options) => search(state.items, state.strings, value, options == null ? state : getFuzzyOptions(options, state)) };
|
|
2849
2887
|
Object.defineProperties(instance, {
|
|
2850
2888
|
items: {
|
|
2889
|
+
enumerable: true,
|
|
2851
2890
|
get: () => state.items.slice(),
|
|
2852
|
-
set: (
|
|
2853
|
-
if (!Array.isArray(items)) throw new TypeError(MESSAGE_ARRAY);
|
|
2854
|
-
state.items = items.slice();
|
|
2855
|
-
state.strings = items.map(state.handler);
|
|
2856
|
-
}
|
|
2891
|
+
set: (value) => setItems(state, value)
|
|
2857
2892
|
},
|
|
2858
|
-
strings: {
|
|
2893
|
+
strings: {
|
|
2894
|
+
enumerable: true,
|
|
2895
|
+
get: () => state.strings.slice()
|
|
2896
|
+
}
|
|
2859
2897
|
});
|
|
2860
2898
|
return Object.freeze(instance);
|
|
2861
2899
|
}
|
|
@@ -2899,14 +2937,14 @@ function getScore(haystack, needle) {
|
|
|
2899
2937
|
if (haystackIndex === 0) score += 1;
|
|
2900
2938
|
if (previousMatchIndex !== -1) {
|
|
2901
2939
|
const gap = haystackIndex - previousMatchIndex - 1;
|
|
2902
|
-
score += Math.max(0,
|
|
2940
|
+
score += Math.max(0, FUZZY_PROXIMITY_THRESHOLD - gap);
|
|
2903
2941
|
}
|
|
2904
2942
|
previousMatchIndex = haystackIndex;
|
|
2905
2943
|
needleIndex += 1;
|
|
2906
2944
|
}
|
|
2907
2945
|
if (needleIndex === needleLength) break;
|
|
2908
2946
|
}
|
|
2909
|
-
score -= Math.floor(lowerCaseHaystack.length /
|
|
2947
|
+
score -= Math.floor(lowerCaseHaystack.length / FUZZY_LENGTH_DIVISOR);
|
|
2910
2948
|
return Math.max(0, score);
|
|
2911
2949
|
}
|
|
2912
2950
|
function search(items, strings, input, options) {
|
|
@@ -2949,18 +2987,23 @@ function search(items, strings, input, options) {
|
|
|
2949
2987
|
for (let index = 0; index < length; index += 1) {
|
|
2950
2988
|
const key = keys[index];
|
|
2951
2989
|
if (maxScore - key > options.tolerance) break;
|
|
2952
|
-
similar.push(...
|
|
2990
|
+
similar.push(...getFuzzyItems(scored[key]));
|
|
2953
2991
|
}
|
|
2954
2992
|
}
|
|
2955
|
-
result.exact =
|
|
2993
|
+
result.exact = getFuzzyItems(options.limit == null ? exact : exact.slice(0, options.limit));
|
|
2956
2994
|
if (options.limit == null) result.similar = similar;
|
|
2957
2995
|
else result.similar = similar.slice(0, options.limit - result.exact.length);
|
|
2958
2996
|
return result;
|
|
2959
2997
|
}
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2998
|
+
function setItems(state, value) {
|
|
2999
|
+
if (!Array.isArray(value)) throw new TypeError(FUZZY_MESSAGE_ARRAY);
|
|
3000
|
+
state.items = value.slice();
|
|
3001
|
+
state.strings = value.map(state.handler);
|
|
3002
|
+
}
|
|
3003
|
+
const FUZZY_LENGTH_DIVISOR = 3;
|
|
3004
|
+
const FUZZY_MESSAGE_ARRAY = "Fuzzy requires an array of items";
|
|
3005
|
+
const FUZZY_MESSAGE_HANDLER = "Fuzzy requires a key or function to stringify items";
|
|
3006
|
+
const FUZZY_PROXIMITY_THRESHOLD = 5;
|
|
2964
3007
|
//#endregion
|
|
2965
3008
|
//#region src/string/index.ts
|
|
2966
3009
|
function dedent(value, ...values) {
|
|
@@ -2999,16 +3042,16 @@ function getUuid(html) {
|
|
|
2999
3042
|
crypto.getRandomValues(bytes);
|
|
3000
3043
|
bytes[6] = bytes[6] & 15 | 64;
|
|
3001
3044
|
bytes[8] = bytes[8] & 63 | 128;
|
|
3002
|
-
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2,
|
|
3045
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, STRING_ZERO)).join("");
|
|
3003
3046
|
const first = hex.substring(0, 8);
|
|
3004
|
-
if (forHTML &&
|
|
3047
|
+
if (forHTML && STRING_NUMERICAL_PREFIX_PATTERN.test(first)) continue;
|
|
3005
3048
|
return [
|
|
3006
3049
|
first,
|
|
3007
3050
|
hex.substring(8, 12),
|
|
3008
3051
|
hex.substring(12, 16),
|
|
3009
3052
|
hex.substring(16, 20),
|
|
3010
3053
|
hex.substring(20, 32)
|
|
3011
|
-
].join(forHTML ?
|
|
3054
|
+
].join(forHTML ? STRING_DELIMITER_UUID_HTML : STRING_DELIMITER_UUID_DEFAULT);
|
|
3012
3055
|
}
|
|
3013
3056
|
}
|
|
3014
3057
|
/**
|
|
@@ -3051,10 +3094,10 @@ function truncate(value, length, suffix) {
|
|
|
3051
3094
|
const truncatedLength = length - actualSuffixLength;
|
|
3052
3095
|
return `${value.slice(0, truncatedLength)}${actualSuffix}`;
|
|
3053
3096
|
}
|
|
3054
|
-
const
|
|
3055
|
-
const
|
|
3056
|
-
const
|
|
3057
|
-
const
|
|
3097
|
+
const STRING_DELIMITER_UUID_DEFAULT = "-";
|
|
3098
|
+
const STRING_DELIMITER_UUID_HTML = "_";
|
|
3099
|
+
const STRING_NUMERICAL_PREFIX_PATTERN = /^\d/;
|
|
3100
|
+
const STRING_ZERO = "0";
|
|
3058
3101
|
//#endregion
|
|
3059
3102
|
//#region src/string/normalize.ts
|
|
3060
3103
|
/**
|
|
@@ -3066,8 +3109,8 @@ const ZERO = "0";
|
|
|
3066
3109
|
function deburr(value) {
|
|
3067
3110
|
if (typeof value !== "string") return "";
|
|
3068
3111
|
deburrMemoizer ??= memoize((value) => {
|
|
3069
|
-
let deburred = value.normalize(
|
|
3070
|
-
deburred = deburred.replace(
|
|
3112
|
+
let deburred = value.normalize(NORMALIZE_DEBURR_NORMALIZATION).replace(NORMALIZE_DEBURR_PATTERN_SIMPLE, "");
|
|
3113
|
+
deburred = deburred.replace(NORMALIZE_DEBURR_PATTERN_CHARACTERS, (_, character) => NORMALIZE_DEBURR_CHARACTERS[character]);
|
|
3071
3114
|
return deburred;
|
|
3072
3115
|
});
|
|
3073
3116
|
return deburrMemoizer.run(value);
|
|
@@ -3111,13 +3154,13 @@ function normalizeString(value, options) {
|
|
|
3111
3154
|
if (typeof value !== "string") return "";
|
|
3112
3155
|
let result = value;
|
|
3113
3156
|
if (options.trim) result = result.trim();
|
|
3114
|
-
if (options.whitespace) result = result.replace(
|
|
3157
|
+
if (options.whitespace) result = result.replace(NORMALIZE_WHITESPACE_PATTERN, NORMALIZE_WHITESPACE_REPLACEMENT);
|
|
3115
3158
|
if (options.deburr) result = deburr(result);
|
|
3116
3159
|
if (options.lowerCase) result = lowerCase(result);
|
|
3117
|
-
if (options.special) result = result.replace(
|
|
3118
|
-
return result.normalize(
|
|
3160
|
+
if (options.special) result = result.replace(NORMALIZE_SPECIAL_PATTERN, NORMALIZE_SPECIAL_REPLACEMENT);
|
|
3161
|
+
return result.normalize(NORMALIZE_NORMALIZATION_NORMALIZATION);
|
|
3119
3162
|
}
|
|
3120
|
-
const
|
|
3163
|
+
const NORMALIZE_DEBURR_CHARACTERS = {
|
|
3121
3164
|
Æ: "AE",
|
|
3122
3165
|
æ: "ae",
|
|
3123
3166
|
Ð: "D",
|
|
@@ -3149,14 +3192,14 @@ const DEBURR_CHARACTERS = {
|
|
|
3149
3192
|
Ŧ: "T",
|
|
3150
3193
|
ŧ: "t"
|
|
3151
3194
|
};
|
|
3152
|
-
const
|
|
3153
|
-
const
|
|
3154
|
-
const
|
|
3155
|
-
const
|
|
3156
|
-
const
|
|
3157
|
-
const
|
|
3158
|
-
const
|
|
3159
|
-
const
|
|
3195
|
+
const NORMALIZE_DEBURR_NORMALIZATION = "NFD";
|
|
3196
|
+
const NORMALIZE_DEBURR_PATTERN_CHARACTERS = new RegExp(`(${Object.keys(NORMALIZE_DEBURR_CHARACTERS).join("|")})`, "g");
|
|
3197
|
+
const NORMALIZE_DEBURR_PATTERN_SIMPLE = /[\u0300-\u036f]/g;
|
|
3198
|
+
const NORMALIZE_NORMALIZATION_NORMALIZATION = "NFC";
|
|
3199
|
+
const NORMALIZE_SPECIAL_PATTERN = /[\p{P}\p{S}]/gu;
|
|
3200
|
+
const NORMALIZE_SPECIAL_REPLACEMENT = "";
|
|
3201
|
+
const NORMALIZE_WHITESPACE_PATTERN = /\s+/g;
|
|
3202
|
+
const NORMALIZE_WHITESPACE_REPLACEMENT = " ";
|
|
3160
3203
|
let deburrMemoizer;
|
|
3161
3204
|
//#endregion
|
|
3162
3205
|
//#region src/string/template.ts
|
|
@@ -3169,7 +3212,7 @@ function getTemplateOptions(input) {
|
|
|
3169
3212
|
const options = isPlainObject(input) ? input : {};
|
|
3170
3213
|
return {
|
|
3171
3214
|
ignoreCase: options.ignoreCase === true,
|
|
3172
|
-
pattern: options.pattern instanceof RegExp ? options.pattern :
|
|
3215
|
+
pattern: options.pattern instanceof RegExp ? options.pattern : TEMPLATE_EXPRESSION_VARIABLE
|
|
3173
3216
|
};
|
|
3174
3217
|
}
|
|
3175
3218
|
function handleTemplate(value, pattern, ignoreCase, variables) {
|
|
@@ -3204,7 +3247,7 @@ function template(value, ...parameters) {
|
|
|
3204
3247
|
return handleTemplate(value, pattern, ignoreCase, parameters[0]);
|
|
3205
3248
|
}
|
|
3206
3249
|
template.initialize = initializeTemplater;
|
|
3207
|
-
const
|
|
3250
|
+
const TEMPLATE_EXPRESSION_VARIABLE = /{{([\s\S]+?)}}/g;
|
|
3208
3251
|
//#endregion
|
|
3209
3252
|
//#region src/value/clone.ts
|
|
3210
3253
|
const CLONE_NAME = "clone";
|
|
@@ -3218,21 +3261,21 @@ clone.handlers = getSelfHandlers(clone, {
|
|
|
3218
3261
|
clone.deregister = deregisterCloner;
|
|
3219
3262
|
clone.register = registerCloner;
|
|
3220
3263
|
function cloneArrayBuffer(value, depth, references) {
|
|
3221
|
-
if (typeof depth === "number" && depth >=
|
|
3264
|
+
if (typeof depth === "number" && depth >= CLONE_MAX_DEPTH) return value;
|
|
3222
3265
|
const cloned = new ArrayBuffer(value.byteLength);
|
|
3223
3266
|
new Uint8Array(cloned).set(new Uint8Array(value));
|
|
3224
3267
|
references?.set(value, cloned);
|
|
3225
3268
|
return cloned;
|
|
3226
3269
|
}
|
|
3227
3270
|
function cloneDataView(value, depth, references) {
|
|
3228
|
-
if (depth >=
|
|
3271
|
+
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
3229
3272
|
const buffer = cloneArrayBuffer(value.buffer);
|
|
3230
3273
|
const cloned = new DataView(buffer, value.byteOffset, value.byteLength);
|
|
3231
3274
|
references.set(value, cloned);
|
|
3232
3275
|
return cloned;
|
|
3233
3276
|
}
|
|
3234
3277
|
function cloneMap(map, depth, references, flat) {
|
|
3235
|
-
if (depth >=
|
|
3278
|
+
if (depth >= CLONE_MAX_DEPTH) return map;
|
|
3236
3279
|
const cloned = /* @__PURE__ */ new Map();
|
|
3237
3280
|
const entries = map.entries();
|
|
3238
3281
|
for (const entry of entries) cloned.set(flat ? entry[0] : cloneValue(entry[0], depth + 1, references, false), flat ? entry[1] : cloneValue(entry[1], depth + 1, references, false));
|
|
@@ -3240,13 +3283,13 @@ function cloneMap(map, depth, references, flat) {
|
|
|
3240
3283
|
return cloned;
|
|
3241
3284
|
}
|
|
3242
3285
|
function cloneNode(node, depth, references) {
|
|
3243
|
-
if (depth >=
|
|
3286
|
+
if (depth >= CLONE_MAX_DEPTH) return node;
|
|
3244
3287
|
const cloned = node.cloneNode(true);
|
|
3245
3288
|
references.set(node, cloned);
|
|
3246
3289
|
return cloned;
|
|
3247
3290
|
}
|
|
3248
3291
|
function cloneObject(value, depth, references, flat) {
|
|
3249
|
-
if (flat || depth >=
|
|
3292
|
+
if (flat || depth >= CLONE_MAX_DEPTH) return Array.isArray(value) ? value.slice() : { ...value };
|
|
3250
3293
|
const cloned = Array.isArray(value) ? [] : {};
|
|
3251
3294
|
const keys = [...Object.keys(value), ...Object.getOwnPropertySymbols(value)];
|
|
3252
3295
|
const { length } = keys;
|
|
@@ -3258,14 +3301,14 @@ function cloneObject(value, depth, references, flat) {
|
|
|
3258
3301
|
return cloned;
|
|
3259
3302
|
}
|
|
3260
3303
|
function cloneRegularExpression(value, depth, references) {
|
|
3261
|
-
if (depth >=
|
|
3304
|
+
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
3262
3305
|
const cloned = new RegExp(value.source, value.flags);
|
|
3263
3306
|
cloned.lastIndex = value.lastIndex;
|
|
3264
3307
|
references.set(value, cloned);
|
|
3265
3308
|
return cloned;
|
|
3266
3309
|
}
|
|
3267
3310
|
function cloneSet(set, depth, references, flat) {
|
|
3268
|
-
if (depth >=
|
|
3311
|
+
if (depth >= CLONE_MAX_DEPTH) return set;
|
|
3269
3312
|
const cloned = /* @__PURE__ */ new Set();
|
|
3270
3313
|
const values = [...set.values()];
|
|
3271
3314
|
const { length } = values;
|
|
@@ -3274,7 +3317,7 @@ function cloneSet(set, depth, references, flat) {
|
|
|
3274
3317
|
return cloned;
|
|
3275
3318
|
}
|
|
3276
3319
|
function cloneTypedArray(value, depth, references) {
|
|
3277
|
-
if (depth >=
|
|
3320
|
+
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
3278
3321
|
const cloned = new value.constructor(value);
|
|
3279
3322
|
references.set(value, cloned);
|
|
3280
3323
|
return cloned;
|
|
@@ -3335,7 +3378,7 @@ function registerCloner(constructor, handler) {
|
|
|
3335
3378
|
clone.handlers.register(constructor, handler);
|
|
3336
3379
|
}
|
|
3337
3380
|
function tryStructuredClone(value, depth, references) {
|
|
3338
|
-
if (depth >=
|
|
3381
|
+
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
3339
3382
|
try {
|
|
3340
3383
|
const cloned = structuredClone(value);
|
|
3341
3384
|
references.set(value, cloned);
|
|
@@ -3345,7 +3388,7 @@ function tryStructuredClone(value, depth, references) {
|
|
|
3345
3388
|
return value;
|
|
3346
3389
|
}
|
|
3347
3390
|
}
|
|
3348
|
-
const
|
|
3391
|
+
const CLONE_MAX_DEPTH = 100;
|
|
3349
3392
|
//#endregion
|
|
3350
3393
|
//#region src/value/collection.ts
|
|
3351
3394
|
function inMap(map, value, key) {
|
|
@@ -3609,7 +3652,7 @@ function shake(value) {
|
|
|
3609
3652
|
//#endregion
|
|
3610
3653
|
//#region src/value/smush.ts
|
|
3611
3654
|
function flattenObject(value, depth, smushed, prefix) {
|
|
3612
|
-
if (depth >=
|
|
3655
|
+
if (depth >= SMUSH_MAX_DEPTH) return {};
|
|
3613
3656
|
if (smushed.has(value)) return smushed.get(value);
|
|
3614
3657
|
const keys = Object.keys(value);
|
|
3615
3658
|
const { length } = keys;
|
|
@@ -3641,7 +3684,7 @@ function flattenObject(value, depth, smushed, prefix) {
|
|
|
3641
3684
|
function smush(value) {
|
|
3642
3685
|
return typeof value === "object" && value !== null ? flattenObject(value, 0, /* @__PURE__ */ new WeakMap()) : {};
|
|
3643
3686
|
}
|
|
3644
|
-
const
|
|
3687
|
+
const SMUSH_MAX_DEPTH = 100;
|
|
3645
3688
|
//#endregion
|
|
3646
3689
|
//#region src/value/unsmush.ts
|
|
3647
3690
|
function getKeys(value) {
|
|
@@ -3827,38 +3870,23 @@ function beacon(value, options) {
|
|
|
3827
3870
|
active: true,
|
|
3828
3871
|
observable: void 0,
|
|
3829
3872
|
options: getBeaconOptions(options),
|
|
3830
|
-
subscriptions:
|
|
3873
|
+
subscriptions: createSubscriptions({ property: beaconSubscription })
|
|
3831
3874
|
};
|
|
3832
3875
|
const instance = {
|
|
3833
|
-
deactivate: () =>
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
update$1(TYPE_NEXT, state, value, finish);
|
|
3838
|
-
},
|
|
3839
|
-
error: (value, finish) => {
|
|
3840
|
-
update$1(TYPE_ERROR, state, value, finish);
|
|
3841
|
-
},
|
|
3842
|
-
finish: () => {
|
|
3843
|
-
finishBeacon(state, true);
|
|
3844
|
-
}
|
|
3876
|
+
deactivate: () => finishBeacon(state, false),
|
|
3877
|
+
emit: (value, finish) => updateBeacon(BEACON_TYPE_NEXT, state, value, finish),
|
|
3878
|
+
error: (value, finish) => updateBeacon(BEACON_TYPE_ERROR, state, value, finish),
|
|
3879
|
+
finish: () => finishBeacon(state, true)
|
|
3845
3880
|
};
|
|
3846
3881
|
Object.defineProperties(instance, {
|
|
3847
|
-
[
|
|
3848
|
-
enumerable: false,
|
|
3849
|
-
value: NAME_BEACON
|
|
3850
|
-
},
|
|
3882
|
+
[BEACON_PROPERTY]: { value: BEACON_NAME },
|
|
3851
3883
|
active: {
|
|
3852
3884
|
enumerable: true,
|
|
3853
3885
|
get: () => state.active
|
|
3854
3886
|
},
|
|
3855
3887
|
observable: {
|
|
3856
3888
|
enumerable: true,
|
|
3857
|
-
get: () =>
|
|
3858
|
-
if (!state.active) throw new Error(MESSAGE_BEACON);
|
|
3859
|
-
state.observable ??= observe(state);
|
|
3860
|
-
return state.observable;
|
|
3861
|
-
}
|
|
3889
|
+
get: () => getObservable(state)
|
|
3862
3890
|
},
|
|
3863
3891
|
value: {
|
|
3864
3892
|
enumerable: true,
|
|
@@ -3870,8 +3898,8 @@ function beacon(value, options) {
|
|
|
3870
3898
|
function finishBeacon(state, emit) {
|
|
3871
3899
|
if (!state.active) return;
|
|
3872
3900
|
state.active = false;
|
|
3873
|
-
for (const [, observer] of state.subscriptions.values.to.any) if (emit) observer.complete?.();
|
|
3874
|
-
|
|
3901
|
+
for (const [, observer] of state.subscriptions.state.values.to.any) if (emit) observer.complete?.();
|
|
3902
|
+
state.subscriptions.clear();
|
|
3875
3903
|
state.observable?.deactivate();
|
|
3876
3904
|
state.observable = void 0;
|
|
3877
3905
|
}
|
|
@@ -3880,6 +3908,11 @@ function getBeaconOptions(input) {
|
|
|
3880
3908
|
options.equal = typeof options.equal === "function" ? options.equal : Object.is;
|
|
3881
3909
|
return options;
|
|
3882
3910
|
}
|
|
3911
|
+
function getObservable(state) {
|
|
3912
|
+
if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
|
|
3913
|
+
state.observable ??= observeBeacon(state);
|
|
3914
|
+
return state.observable;
|
|
3915
|
+
}
|
|
3883
3916
|
function getObservableCallback(value) {
|
|
3884
3917
|
return typeof value === "function" ? value : noop;
|
|
3885
3918
|
}
|
|
@@ -3904,10 +3937,19 @@ function getObserver(first, second, third) {
|
|
|
3904
3937
|
* @returns `true` if the value is a beacon, otherwise `false`
|
|
3905
3938
|
*/
|
|
3906
3939
|
function isBeacon(value) {
|
|
3907
|
-
return isBeaconInstance(
|
|
3940
|
+
return isBeaconInstance(BEACON_NAME, value);
|
|
3908
3941
|
}
|
|
3909
3942
|
function isBeaconInstance(name, value) {
|
|
3910
|
-
return isPlainObject(value) && value[
|
|
3943
|
+
return isPlainObject(value) && value[BEACON_PROPERTY] === name;
|
|
3944
|
+
}
|
|
3945
|
+
/**
|
|
3946
|
+
* Is the value a beacon subscription?
|
|
3947
|
+
*
|
|
3948
|
+
* @param value Value to check
|
|
3949
|
+
* @returns `true` if the value is a beacon subscription, otherwise `false`
|
|
3950
|
+
*/
|
|
3951
|
+
function isBeaconSubscription(value) {
|
|
3952
|
+
return isSubscription(value) && value[BEACON_PROPERTY] === "subscription";
|
|
3911
3953
|
}
|
|
3912
3954
|
/**
|
|
3913
3955
|
* Is the value an observable?
|
|
@@ -3916,9 +3958,9 @@ function isBeaconInstance(name, value) {
|
|
|
3916
3958
|
* @returns `true` if the value is an observable, otherwise `false`
|
|
3917
3959
|
*/
|
|
3918
3960
|
function isObservable(value) {
|
|
3919
|
-
return isBeaconInstance(
|
|
3961
|
+
return isBeaconInstance(BEACON_OBSERVABLE, value);
|
|
3920
3962
|
}
|
|
3921
|
-
function
|
|
3963
|
+
function observeBeacon(beacon) {
|
|
3922
3964
|
const state = {
|
|
3923
3965
|
beacon,
|
|
3924
3966
|
active: beacon.active
|
|
@@ -3927,24 +3969,10 @@ function observe(beacon) {
|
|
|
3927
3969
|
deactivate: () => {
|
|
3928
3970
|
state.active = false;
|
|
3929
3971
|
},
|
|
3930
|
-
subscribe(first, second, third)
|
|
3931
|
-
if (!beacon.active || !state.active) throw new Error(MESSAGE_OBSERVABLE);
|
|
3932
|
-
const observer = getObserver(first, second, third);
|
|
3933
|
-
const subscription = getSubscription({
|
|
3934
|
-
isActive: () => beacon.active && state.active,
|
|
3935
|
-
property: beaconSubscription,
|
|
3936
|
-
subscriptions: beacon.subscriptions,
|
|
3937
|
-
value: observer
|
|
3938
|
-
});
|
|
3939
|
-
observer.next?.(beacon.value);
|
|
3940
|
-
return subscription;
|
|
3941
|
-
}
|
|
3972
|
+
subscribe: (first, second, third) => subscribeToObservable(state, first, second, third)
|
|
3942
3973
|
};
|
|
3943
3974
|
Object.defineProperties(instance, {
|
|
3944
|
-
[
|
|
3945
|
-
enumerable: false,
|
|
3946
|
-
value: NAME_OBSERVABLE
|
|
3947
|
-
},
|
|
3975
|
+
[BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
|
|
3948
3976
|
active: {
|
|
3949
3977
|
enumerable: true,
|
|
3950
3978
|
get: () => beacon.active && state.active
|
|
@@ -3952,26 +3980,33 @@ function observe(beacon) {
|
|
|
3952
3980
|
});
|
|
3953
3981
|
return Object.freeze(instance);
|
|
3954
3982
|
}
|
|
3955
|
-
function
|
|
3983
|
+
function subscribeToObservable(state, first, second, third) {
|
|
3984
|
+
if (!state.beacon.active || !state.active) throw new Error(BEACON_MESSAGE_SUBSCRIBE);
|
|
3985
|
+
const observer = getObserver(first, second, third);
|
|
3986
|
+
const [subscription] = state.beacon.subscriptions.create({
|
|
3987
|
+
isActive: () => state.beacon.active && state.active,
|
|
3988
|
+
value: observer
|
|
3989
|
+
});
|
|
3990
|
+
observer.next?.(state.beacon.value);
|
|
3991
|
+
return subscription;
|
|
3992
|
+
}
|
|
3993
|
+
function updateBeacon(type, state, value, finish) {
|
|
3956
3994
|
if (!state.active) return;
|
|
3957
|
-
if (type ===
|
|
3995
|
+
if (type === BEACON_TYPE_NEXT) {
|
|
3958
3996
|
if (state.options.equal(state.value, value)) return;
|
|
3959
3997
|
state.value = value;
|
|
3960
3998
|
}
|
|
3961
|
-
for (const [, observer] of state.subscriptions.values.to.any) observer[type]?.(value);
|
|
3999
|
+
for (const [, observer] of state.subscriptions.state.values.to.any) observer[type]?.(value);
|
|
3962
4000
|
if (finish === true) finishBeacon(state, true);
|
|
3963
4001
|
}
|
|
3964
|
-
const
|
|
3965
|
-
const
|
|
3966
|
-
const
|
|
3967
|
-
const
|
|
3968
|
-
const
|
|
3969
|
-
const
|
|
3970
|
-
const
|
|
3971
|
-
const beaconSubscription = {
|
|
3972
|
-
key: KEY_BEACON,
|
|
3973
|
-
value: "subscription"
|
|
3974
|
-
};
|
|
4002
|
+
const BEACON_MESSAGE_RETRIEVE = "Cannot retrieve observable from a closed beacon";
|
|
4003
|
+
const BEACON_MESSAGE_SUBSCRIBE = "Cannot subscribe to a closed observable";
|
|
4004
|
+
const BEACON_PROPERTY = "$beacon";
|
|
4005
|
+
const BEACON_NAME = "beacon";
|
|
4006
|
+
const BEACON_OBSERVABLE = "observable";
|
|
4007
|
+
const BEACON_TYPE_ERROR = "error";
|
|
4008
|
+
const BEACON_TYPE_NEXT = "next";
|
|
4009
|
+
const beaconSubscription = { key: BEACON_PROPERTY };
|
|
3975
4010
|
//#endregion
|
|
3976
4011
|
//#region src/color/constants.ts
|
|
3977
4012
|
const COLOR_ALPHA = {
|
|
@@ -4042,10 +4077,7 @@ const COLOR_MAX = {
|
|
|
4042
4077
|
hex: 255,
|
|
4043
4078
|
percent: 100
|
|
4044
4079
|
};
|
|
4045
|
-
const COLOR_PROPERTY = {
|
|
4046
|
-
name: "$color",
|
|
4047
|
-
subscription: "subscription"
|
|
4048
|
-
};
|
|
4080
|
+
const COLOR_PROPERTY = { name: "$color" };
|
|
4049
4081
|
const COLOR_TYPE = {
|
|
4050
4082
|
all: void 0,
|
|
4051
4083
|
hex: "hex",
|
|
@@ -4055,7 +4087,8 @@ const COLOR_TYPE = {
|
|
|
4055
4087
|
hwb: "hwb",
|
|
4056
4088
|
hwba: "hwba",
|
|
4057
4089
|
rgb: "rgb",
|
|
4058
|
-
rgba: "rgba"
|
|
4090
|
+
rgba: "rgba",
|
|
4091
|
+
wildcard: "*"
|
|
4059
4092
|
};
|
|
4060
4093
|
COLOR_TYPE.all = /* @__PURE__ */ new Set([
|
|
4061
4094
|
COLOR_ALPHA.name,
|
|
@@ -4967,7 +5000,7 @@ function getDefaultColorState() {
|
|
|
4967
5000
|
origin: COLOR_TYPE.hex
|
|
4968
5001
|
};
|
|
4969
5002
|
}
|
|
4970
|
-
function setColorValue(color, state,
|
|
5003
|
+
function setColorValue(color, state, changes, type, value, alpha) {
|
|
4971
5004
|
state.hex = void 0;
|
|
4972
5005
|
state.hsl = void 0;
|
|
4973
5006
|
state.hwb = void 0;
|
|
@@ -4975,36 +5008,33 @@ function setColorValue(color, state, subscriptions, type, value, alpha) {
|
|
|
4975
5008
|
state.origin = type;
|
|
4976
5009
|
state[type] = value;
|
|
4977
5010
|
if (alpha != null) state.alpha = getAlpha(alpha, type === COLOR_TYPE.hex);
|
|
4978
|
-
for (const type of COLOR_TYPE.all)
|
|
4979
|
-
|
|
4980
|
-
if (callbacks != null) for (const callback of callbacks.values()) callback(color[type]);
|
|
4981
|
-
}
|
|
4982
|
-
for (const callback of subscriptions.values.to.any.values()) callback(color);
|
|
5011
|
+
for (const type of COLOR_TYPE.all) if (changes.observed(type)) changes.emit(type, color[type]);
|
|
5012
|
+
changes.emit(COLOR_TYPE.wildcard, color);
|
|
4983
5013
|
}
|
|
4984
|
-
function setHexColor(color, state,
|
|
5014
|
+
function setHexColor(color, state, changes, value, alpha) {
|
|
4985
5015
|
if (!isHexColor(value) || !alpha && value === state.hex) return;
|
|
4986
5016
|
const normalized = getNormalizedHex(value, true);
|
|
4987
|
-
setColorValue(color, state,
|
|
5017
|
+
setColorValue(color, state, changes, COLOR_TYPE.hex, normalized.slice(0, COLOR_LENGTHS.hexLong), alpha ? normalized.slice(COLOR_LENGTHS.hexLong) : void 0);
|
|
4988
5018
|
}
|
|
4989
|
-
function setHSLColor(color, state,
|
|
5019
|
+
function setHSLColor(color, state, changes, value, alpha) {
|
|
4990
5020
|
if (!isHslLike(value)) return;
|
|
4991
|
-
setColorValue(color, state,
|
|
5021
|
+
setColorValue(color, state, changes, COLOR_TYPE.hsl, {
|
|
4992
5022
|
hue: getDegrees(value.hue),
|
|
4993
5023
|
saturation: getPercentage(value.saturation),
|
|
4994
5024
|
lightness: getPercentage(value.lightness)
|
|
4995
5025
|
}, alpha ? value.alpha : void 0);
|
|
4996
5026
|
}
|
|
4997
|
-
function setHWBColor(color, state,
|
|
5027
|
+
function setHWBColor(color, state, changes, value, alpha) {
|
|
4998
5028
|
if (!isHwbLike(value)) return;
|
|
4999
|
-
setColorValue(color, state,
|
|
5029
|
+
setColorValue(color, state, changes, COLOR_TYPE.hwb, {
|
|
5000
5030
|
hue: getDegrees(value.hue),
|
|
5001
5031
|
whiteness: getPercentage(value.whiteness),
|
|
5002
5032
|
blackness: getPercentage(value.blackness)
|
|
5003
5033
|
}, alpha ? value.alpha : void 0);
|
|
5004
5034
|
}
|
|
5005
|
-
function setRGBColor(color, state,
|
|
5035
|
+
function setRGBColor(color, state, changes, value, alpha) {
|
|
5006
5036
|
if (!isRgbLike(value)) return;
|
|
5007
|
-
setColorValue(color, state,
|
|
5037
|
+
setColorValue(color, state, changes, COLOR_TYPE.rgb, {
|
|
5008
5038
|
red: getHexValue(value.red),
|
|
5009
5039
|
green: getHexValue(value.green),
|
|
5010
5040
|
blue: getHexValue(value.blue)
|
|
@@ -5018,6 +5048,9 @@ const getters = {
|
|
|
5018
5048
|
};
|
|
5019
5049
|
//#endregion
|
|
5020
5050
|
//#region src/color/misc/format.ts
|
|
5051
|
+
function formatHexColor(color, alpha) {
|
|
5052
|
+
return `#${alpha === true ? color.hexa : color.hex}`;
|
|
5053
|
+
}
|
|
5021
5054
|
function formatHslColor(state, alpha) {
|
|
5022
5055
|
const { hue, lightness, saturation } = getColorFromState(state, COLOR_TYPE.hsl);
|
|
5023
5056
|
return `hsl(${hue}deg ${saturation}% ${lightness}%${getSuffix(state, alpha)})`;
|
|
@@ -5036,51 +5069,56 @@ function getSuffix(state, alpha) {
|
|
|
5036
5069
|
//#endregion
|
|
5037
5070
|
//#region src/color/instance.ts
|
|
5038
5071
|
function color(value) {
|
|
5072
|
+
const changes = herald({
|
|
5073
|
+
names: [COLOR_TYPE.wildcard, ...COLOR_TYPE.all],
|
|
5074
|
+
property: colorSubscription,
|
|
5075
|
+
onCreate: (event, callback) => onCreateSubscription(instance, event, callback)
|
|
5076
|
+
});
|
|
5039
5077
|
const state = getColorState(value);
|
|
5040
|
-
const subscriptions = getSubscriptions(COLOR_TYPE.all);
|
|
5041
5078
|
const instance = {
|
|
5042
|
-
subscribe: (
|
|
5043
|
-
toHexString: (alpha) =>
|
|
5079
|
+
subscribe: (callback, signal) => changes.subscribe(COLOR_TYPE.wildcard, callback, signal),
|
|
5080
|
+
toHexString: (alpha) => formatHexColor(instance, alpha),
|
|
5044
5081
|
toHslString: (alpha) => formatHslColor(state, alpha),
|
|
5045
5082
|
toHwbString: (alpha) => formatHwbColor(state, alpha),
|
|
5046
5083
|
toRgbString: (alpha) => formatRgbColor(state, alpha),
|
|
5047
5084
|
toString: () => instance.toHexString(),
|
|
5048
|
-
unsubscribe: () =>
|
|
5085
|
+
unsubscribe: () => changes.clear()
|
|
5049
5086
|
};
|
|
5050
5087
|
Object.defineProperties(instance, {
|
|
5051
|
-
[COLOR_PROPERTY.name]: {
|
|
5052
|
-
enumerable: false,
|
|
5053
|
-
value: true
|
|
5054
|
-
},
|
|
5088
|
+
[COLOR_PROPERTY.name]: { value: true },
|
|
5055
5089
|
alpha: {
|
|
5056
5090
|
enumerable: true,
|
|
5057
5091
|
get: () => state.alpha.value,
|
|
5058
5092
|
set: (value) => setAlphaValue(state, value)
|
|
5059
5093
|
},
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
|
|
5065
|
-
|
|
5094
|
+
changes: {
|
|
5095
|
+
enumerable: true,
|
|
5096
|
+
value: changes.events
|
|
5097
|
+
},
|
|
5098
|
+
hex: getColorProperty(COLOR_TYPE.hex, instance, state, changes, false),
|
|
5099
|
+
hexa: getColorProperty(COLOR_TYPE.hex, instance, state, changes, true),
|
|
5100
|
+
hsl: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, false),
|
|
5101
|
+
hsla: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, true),
|
|
5102
|
+
hwb: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, false),
|
|
5103
|
+
hwba: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, true),
|
|
5066
5104
|
origin: {
|
|
5067
5105
|
enumerable: true,
|
|
5068
5106
|
get: () => state.origin
|
|
5069
5107
|
},
|
|
5070
|
-
rgb:
|
|
5071
|
-
rgba:
|
|
5108
|
+
rgb: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, false),
|
|
5109
|
+
rgba: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, true)
|
|
5072
5110
|
});
|
|
5073
5111
|
return Object.freeze(instance);
|
|
5074
5112
|
}
|
|
5075
|
-
function
|
|
5113
|
+
function getColorProperty(space, color, state, changes, alpha) {
|
|
5076
5114
|
const setValue = setters[space];
|
|
5077
5115
|
return {
|
|
5078
5116
|
enumerable: true,
|
|
5079
|
-
get: () =>
|
|
5080
|
-
set: (value) => setValue(color, state,
|
|
5117
|
+
get: () => getColorValue(space, state, alpha),
|
|
5118
|
+
set: (value) => setValue(color, state, changes, value, alpha)
|
|
5081
5119
|
};
|
|
5082
5120
|
}
|
|
5083
|
-
function
|
|
5121
|
+
function getColorValue(space, state, alpha) {
|
|
5084
5122
|
const value = getColorFromState(state, space);
|
|
5085
5123
|
if (typeof value === "string") return alpha ? `${value}${state.alpha.hex}` : value;
|
|
5086
5124
|
return alpha ? {
|
|
@@ -5088,28 +5126,16 @@ function getValue$1(space, state, alpha) {
|
|
|
5088
5126
|
alpha: state.alpha.value
|
|
5089
5127
|
} : { ...value };
|
|
5090
5128
|
}
|
|
5129
|
+
function onCreateSubscription(instance, event, callback) {
|
|
5130
|
+
let value;
|
|
5131
|
+
if (event === COLOR_TYPE.wildcard) value = instance;
|
|
5132
|
+
else value = instance[event];
|
|
5133
|
+
callback(value);
|
|
5134
|
+
}
|
|
5091
5135
|
function setAlphaValue(state, value) {
|
|
5092
5136
|
if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
|
|
5093
5137
|
}
|
|
5094
|
-
|
|
5095
|
-
let key;
|
|
5096
|
-
let value;
|
|
5097
|
-
if (typeof first === "string") {
|
|
5098
|
-
key = first;
|
|
5099
|
-
value = second;
|
|
5100
|
-
} else if (typeof first === "function") value = first;
|
|
5101
|
-
if (typeof value !== "function") throw new Error();
|
|
5102
|
-
return getSubscription({
|
|
5103
|
-
key,
|
|
5104
|
-
subscriptions,
|
|
5105
|
-
value,
|
|
5106
|
-
property: colorSubscription
|
|
5107
|
-
});
|
|
5108
|
-
}
|
|
5109
|
-
const colorSubscription = {
|
|
5110
|
-
key: COLOR_PROPERTY.name,
|
|
5111
|
-
value: COLOR_PROPERTY.subscription
|
|
5112
|
-
};
|
|
5138
|
+
const colorSubscription = { key: COLOR_PROPERTY.name };
|
|
5113
5139
|
const setters = {
|
|
5114
5140
|
hex: setHexColor,
|
|
5115
5141
|
hsl: setHSLColor,
|
|
@@ -5252,13 +5278,13 @@ const EXPRESSION_WHITESPACE = /^\s*$/;
|
|
|
5252
5278
|
//#endregion
|
|
5253
5279
|
//#region src/logger.ts
|
|
5254
5280
|
function isLogger(value) {
|
|
5255
|
-
return isLoggerInstance(
|
|
5281
|
+
return isLoggerInstance(LOGGER_NAME, value);
|
|
5256
5282
|
}
|
|
5257
5283
|
function isLoggerInstance(name, value) {
|
|
5258
|
-
return typeof value === "object" && value !== null && value[
|
|
5284
|
+
return typeof value === "object" && value !== null && value[LOGGER_PROPERTY] === name;
|
|
5259
5285
|
}
|
|
5260
5286
|
function isTimedLogger(value) {
|
|
5261
|
-
return isLoggerInstance(
|
|
5287
|
+
return isLoggerInstance(LOGGER_NAME_TIMED, value);
|
|
5262
5288
|
}
|
|
5263
5289
|
function timedLogger(label) {
|
|
5264
5290
|
function stop() {
|
|
@@ -5277,35 +5303,20 @@ function timedLogger(label) {
|
|
|
5277
5303
|
};
|
|
5278
5304
|
const instance = {};
|
|
5279
5305
|
Object.defineProperties(instance, {
|
|
5280
|
-
[
|
|
5281
|
-
enumerable: false,
|
|
5282
|
-
value: NAME_TIMED
|
|
5283
|
-
},
|
|
5306
|
+
[LOGGER_PROPERTY]: { value: LOGGER_NAME_TIMED },
|
|
5284
5307
|
active: {
|
|
5285
5308
|
enumerable: true,
|
|
5286
|
-
get()
|
|
5287
|
-
return state.started && !state.stopped && enabled;
|
|
5288
|
-
}
|
|
5309
|
+
get: () => state.started && !state.stopped && enabled
|
|
5289
5310
|
},
|
|
5290
|
-
log: {
|
|
5291
|
-
|
|
5292
|
-
get() {
|
|
5293
|
-
return this.active ? logger : noop;
|
|
5294
|
-
}
|
|
5295
|
-
},
|
|
5296
|
-
stop: {
|
|
5297
|
-
enumerable: true,
|
|
5298
|
-
get() {
|
|
5299
|
-
return instance.active ? stop() : noop;
|
|
5300
|
-
}
|
|
5301
|
-
}
|
|
5311
|
+
log: { get: () => instance.active ? logger : noop },
|
|
5312
|
+
stop: { get: () => instance.active ? stop() : noop }
|
|
5302
5313
|
});
|
|
5303
5314
|
if (state.started) console.time(label);
|
|
5304
5315
|
return Object.freeze(instance);
|
|
5305
5316
|
}
|
|
5306
|
-
const
|
|
5307
|
-
const
|
|
5308
|
-
const
|
|
5317
|
+
const LOGGER_NAME = "Logger";
|
|
5318
|
+
const LOGGER_NAME_TIMED = "TimedLogger";
|
|
5319
|
+
const LOGGER_PROPERTY = "$logger";
|
|
5309
5320
|
const methods = [
|
|
5310
5321
|
"debug",
|
|
5311
5322
|
"dir",
|
|
@@ -5324,25 +5335,15 @@ const methods = [
|
|
|
5324
5335
|
const Logger = (() => {
|
|
5325
5336
|
const instance = {};
|
|
5326
5337
|
Object.defineProperties(instance, {
|
|
5327
|
-
[
|
|
5328
|
-
enumerable: false,
|
|
5329
|
-
value: NAME_LOGGER
|
|
5330
|
-
},
|
|
5338
|
+
[LOGGER_PROPERTY]: { value: LOGGER_NAME },
|
|
5331
5339
|
enabled: {
|
|
5332
5340
|
enumerable: true,
|
|
5333
|
-
get()
|
|
5334
|
-
|
|
5335
|
-
},
|
|
5336
|
-
set(value) {
|
|
5341
|
+
get: () => enabled,
|
|
5342
|
+
set: (value) => {
|
|
5337
5343
|
enabled = typeof value === "boolean" ? value : enabled;
|
|
5338
5344
|
}
|
|
5339
5345
|
},
|
|
5340
|
-
time: {
|
|
5341
|
-
enumerable: true,
|
|
5342
|
-
value(label) {
|
|
5343
|
-
return timedLogger(label);
|
|
5344
|
-
}
|
|
5345
|
-
}
|
|
5346
|
+
time: { value: (label) => timedLogger(getString(label)) }
|
|
5346
5347
|
});
|
|
5347
5348
|
for (const method of methods) Object.defineProperty(instance, method, {
|
|
5348
5349
|
enumerable: true,
|
|
@@ -5414,6 +5415,17 @@ function sum(array, key) {
|
|
|
5414
5415
|
return getAggregated("sum", array, key);
|
|
5415
5416
|
}
|
|
5416
5417
|
//#endregion
|
|
5418
|
+
//#region src/promise/constants.ts
|
|
5419
|
+
const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
5420
|
+
const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
5421
|
+
const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
5422
|
+
const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
5423
|
+
const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
5424
|
+
const PROMISE_STRATEGY_ALL = /* @__PURE__ */ new Set(["complete", "first"]);
|
|
5425
|
+
const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
5426
|
+
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
5427
|
+
const PROMISE_TYPE_REJECTED = "rejected";
|
|
5428
|
+
//#endregion
|
|
5417
5429
|
//#region src/promise/models.ts
|
|
5418
5430
|
/**
|
|
5419
5431
|
* A _Promise_ that can be canceled
|
|
@@ -5434,7 +5446,8 @@ var CancelablePromise = class extends Promise {
|
|
|
5434
5446
|
* @param reason Optional reason for canceling the _Promise_
|
|
5435
5447
|
*/
|
|
5436
5448
|
cancel(reason) {
|
|
5437
|
-
this.#rejector(reason);
|
|
5449
|
+
this.#rejector?.(reason);
|
|
5450
|
+
this.#rejector = void 0;
|
|
5438
5451
|
}
|
|
5439
5452
|
};
|
|
5440
5453
|
/**
|
|
@@ -5446,17 +5459,6 @@ var PromiseTimeoutError = class extends Error {
|
|
|
5446
5459
|
this.name = PROMISE_ERROR_NAME;
|
|
5447
5460
|
}
|
|
5448
5461
|
};
|
|
5449
|
-
const PROMISE_ABORT_EVENT = "abort";
|
|
5450
|
-
const PROMISE_ABORT_OPTIONS = { once: true };
|
|
5451
|
-
const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
5452
|
-
const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
5453
|
-
const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
5454
|
-
const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
5455
|
-
const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
5456
|
-
const PROMISE_STRATEGY_ALL = /* @__PURE__ */ new Set(["complete", "first"]);
|
|
5457
|
-
const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
5458
|
-
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
5459
|
-
const PROMISE_TYPE_REJECTED = "rejected";
|
|
5460
5462
|
//#endregion
|
|
5461
5463
|
//#region src/promise/helpers.ts
|
|
5462
5464
|
function getPromiseOptions(input) {
|
|
@@ -5522,10 +5524,10 @@ function cancelable(executor) {
|
|
|
5522
5524
|
return new CancelablePromise(executor);
|
|
5523
5525
|
}
|
|
5524
5526
|
function handleResult(status, parameters) {
|
|
5525
|
-
const {
|
|
5526
|
-
if (signal
|
|
5527
|
+
const { aborter, complete, data, handlers, index, value } = parameters;
|
|
5528
|
+
if (aborter?.signal.aborted ?? false) return;
|
|
5527
5529
|
if (!complete && status === "rejected") {
|
|
5528
|
-
settlePromise(
|
|
5530
|
+
settlePromise(handlers.reject, value, aborter);
|
|
5529
5531
|
return;
|
|
5530
5532
|
}
|
|
5531
5533
|
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
@@ -5535,10 +5537,10 @@ function handleResult(status, parameters) {
|
|
|
5535
5537
|
status,
|
|
5536
5538
|
reason: value
|
|
5537
5539
|
};
|
|
5538
|
-
if (index === data.last) settlePromise(
|
|
5540
|
+
if (index === data.last) settlePromise(handlers.resolve, data.result, aborter);
|
|
5539
5541
|
}
|
|
5540
|
-
function settlePromise(
|
|
5541
|
-
|
|
5542
|
+
function settlePromise(settler, value, aborter) {
|
|
5543
|
+
aborter?.cancel();
|
|
5542
5544
|
settler(value);
|
|
5543
5545
|
}
|
|
5544
5546
|
async function toResult(value) {
|
|
@@ -5550,50 +5552,44 @@ async function toResult(value) {
|
|
|
5550
5552
|
//#region src/promise/delay.ts
|
|
5551
5553
|
function delay(options) {
|
|
5552
5554
|
const { signal, time } = getPromiseOptions(options);
|
|
5553
|
-
if (signal?.aborted ?? false) return Promise.reject(signal
|
|
5554
|
-
|
|
5555
|
+
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
5556
|
+
const aborter = createAborter(signal, () => {
|
|
5555
5557
|
timer.cancel();
|
|
5556
|
-
rejector(signal
|
|
5557
|
-
}
|
|
5558
|
-
const timer = getTimer(TIMER_WAIT, () =>
|
|
5559
|
-
settlePromise(abort, resolver, void 0, signal);
|
|
5560
|
-
}, time);
|
|
5561
|
-
signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
|
|
5558
|
+
rejector(signal?.reason);
|
|
5559
|
+
});
|
|
5560
|
+
const timer = getTimer(TIMER_WAIT, () => settlePromise(resolver, void 0, aborter), time);
|
|
5562
5561
|
let rejector;
|
|
5563
5562
|
let resolver;
|
|
5564
5563
|
return new Promise((resolve, reject) => {
|
|
5565
5564
|
rejector = reject;
|
|
5566
5565
|
resolver = resolve;
|
|
5567
|
-
if (time === 0) settlePromise(
|
|
5566
|
+
if (time === 0) settlePromise(resolver, void 0, aborter);
|
|
5568
5567
|
else timer();
|
|
5569
5568
|
});
|
|
5570
5569
|
}
|
|
5571
5570
|
//#endregion
|
|
5572
5571
|
//#region src/promise/timed.ts
|
|
5573
5572
|
async function getTimedPromise(promise, time, signal) {
|
|
5574
|
-
|
|
5573
|
+
const aborter = createAborter(signal, () => {
|
|
5575
5574
|
timer.cancel();
|
|
5576
|
-
rejector(signal
|
|
5577
|
-
}
|
|
5578
|
-
|
|
5579
|
-
const timer = getTimer(TIMER_WAIT, () => {
|
|
5580
|
-
settlePromise(abort, rejector, new PromiseTimeoutError(), signal);
|
|
5581
|
-
}, time);
|
|
5575
|
+
rejector(signal?.reason);
|
|
5576
|
+
});
|
|
5577
|
+
const timer = getTimer(TIMER_WAIT, () => settlePromise(rejector, new PromiseTimeoutError(), aborter), time);
|
|
5582
5578
|
let rejector;
|
|
5583
5579
|
return Promise.race([promise, new Promise((_, reject) => {
|
|
5584
5580
|
rejector = reject;
|
|
5585
5581
|
timer();
|
|
5586
5582
|
})]).then((value) => {
|
|
5583
|
+
aborter?.cancel();
|
|
5587
5584
|
timer.cancel();
|
|
5588
5585
|
rejector(void 0);
|
|
5589
|
-
signal?.removeEventListener(PROMISE_ABORT_EVENT, abort);
|
|
5590
5586
|
return value;
|
|
5591
5587
|
});
|
|
5592
5588
|
}
|
|
5593
5589
|
async function timed(promise, options) {
|
|
5594
5590
|
if (!(promise instanceof Promise)) return Promise.reject(/* @__PURE__ */ new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
5595
5591
|
const { signal, time } = getPromiseOptions(options);
|
|
5596
|
-
if (signal?.aborted ?? false) return Promise.reject(signal
|
|
5592
|
+
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
5597
5593
|
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
5598
5594
|
}
|
|
5599
5595
|
//#endregion
|
|
@@ -5602,21 +5598,20 @@ async function attemptPromise(value, options) {
|
|
|
5602
5598
|
const isFunction = typeof value === "function";
|
|
5603
5599
|
if (!isFunction && !(value instanceof Promise)) return Promise.reject(/* @__PURE__ */ new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
5604
5600
|
const { signal, time } = getPromiseOptions(options);
|
|
5605
|
-
if (signal?.aborted ?? false) return Promise.reject(signal
|
|
5606
|
-
|
|
5607
|
-
rejector(signal
|
|
5608
|
-
}
|
|
5601
|
+
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
5602
|
+
const aborter = createAborter(signal, () => {
|
|
5603
|
+
rejector(signal?.reason);
|
|
5604
|
+
});
|
|
5609
5605
|
async function handler(resolve, reject) {
|
|
5610
5606
|
try {
|
|
5611
5607
|
let result = isFunction ? value() : await value;
|
|
5612
5608
|
if (result instanceof Promise) result = await result;
|
|
5613
|
-
settlePromise(
|
|
5609
|
+
settlePromise(resolve, result, aborter);
|
|
5614
5610
|
} catch (error) {
|
|
5615
|
-
settlePromise(
|
|
5611
|
+
settlePromise(reject, error, aborter);
|
|
5616
5612
|
}
|
|
5617
5613
|
}
|
|
5618
5614
|
let rejector;
|
|
5619
|
-
signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
|
|
5620
5615
|
const promise = new Promise((resolve, reject) => {
|
|
5621
5616
|
rejector = reject;
|
|
5622
5617
|
handler(resolve, reject);
|
|
@@ -5625,16 +5620,15 @@ async function attemptPromise(value, options) {
|
|
|
5625
5620
|
}
|
|
5626
5621
|
async function promises(items, options) {
|
|
5627
5622
|
const { signal, strategy } = getPromisesOptions(options);
|
|
5628
|
-
if (signal?.aborted ?? false) return Promise.reject(signal
|
|
5623
|
+
if (signal?.aborted ?? false) return Promise.reject(signal?.reason);
|
|
5629
5624
|
if (!Array.isArray(items)) return Promise.resolve([]);
|
|
5630
5625
|
const actual = items.map((item) => typeof item === "function" ? item() : item).filter((item) => item instanceof Promise);
|
|
5631
5626
|
const { length } = actual;
|
|
5632
5627
|
if (length === 0) return Promise.resolve([]);
|
|
5633
5628
|
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
5634
|
-
|
|
5635
|
-
handlers.reject(signal
|
|
5636
|
-
}
|
|
5637
|
-
signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
|
|
5629
|
+
const aborter = createAborter(signal, () => {
|
|
5630
|
+
handlers.reject(signal?.reason);
|
|
5631
|
+
});
|
|
5638
5632
|
const data = {
|
|
5639
5633
|
last: length - 1,
|
|
5640
5634
|
result: []
|
|
@@ -5646,20 +5640,18 @@ async function promises(items, options) {
|
|
|
5646
5640
|
resolve
|
|
5647
5641
|
};
|
|
5648
5642
|
for (let index = 0; index < length; index += 1) actual[index].then((value) => handleResult(PROMISE_TYPE_FULFILLED, {
|
|
5649
|
-
|
|
5643
|
+
aborter,
|
|
5650
5644
|
complete,
|
|
5651
5645
|
data,
|
|
5652
5646
|
handlers,
|
|
5653
5647
|
index,
|
|
5654
|
-
signal,
|
|
5655
5648
|
value
|
|
5656
5649
|
})).catch((reason) => handleResult(PROMISE_TYPE_REJECTED, {
|
|
5657
|
-
|
|
5650
|
+
aborter,
|
|
5658
5651
|
complete,
|
|
5659
5652
|
data,
|
|
5660
5653
|
handlers,
|
|
5661
5654
|
index,
|
|
5662
|
-
signal,
|
|
5663
5655
|
value: reason
|
|
5664
5656
|
}));
|
|
5665
5657
|
});
|
|
@@ -5678,15 +5670,15 @@ async function resultPromises(items, signal) {
|
|
|
5678
5670
|
*/
|
|
5679
5671
|
function fromQuery(query) {
|
|
5680
5672
|
if (typeof query !== "string" || query.trim().length === 0) return {};
|
|
5681
|
-
const parts = query.split(
|
|
5673
|
+
const parts = query.split(QUERY_AMPERSAND).map((part) => part.split(QUERY_EQUAL).map(tryDecode)).sort(([first], [second]) => first.localeCompare(second));
|
|
5682
5674
|
const { length } = parts;
|
|
5683
5675
|
const parameters = {};
|
|
5684
5676
|
let position = 0;
|
|
5685
5677
|
let array;
|
|
5686
5678
|
for (let index = 0; index < length; index += 1) {
|
|
5687
5679
|
const [key, value] = parts[index];
|
|
5688
|
-
if (
|
|
5689
|
-
const named = key.replace(
|
|
5680
|
+
if (QUERY_EXPRESSION_ARRAY_SUFFIX.test(key)) {
|
|
5681
|
+
const named = key.replace(QUERY_EXPRESSION_ARRAY_SUFFIX, "");
|
|
5690
5682
|
if (named !== array) {
|
|
5691
5683
|
array = named;
|
|
5692
5684
|
position = 0;
|
|
@@ -5708,9 +5700,9 @@ function getParts(value, fromArray, prefix) {
|
|
|
5708
5700
|
for (let index = 0; index < length; index += 1) {
|
|
5709
5701
|
const key = keys[index];
|
|
5710
5702
|
const val = value[key];
|
|
5711
|
-
const fullKey = join([prefix, fromArray ? void 0 : key],
|
|
5703
|
+
const fullKey = join([prefix, fromArray ? void 0 : key], QUERY_DOT);
|
|
5712
5704
|
const encodedKey = getString(tryEncode(fullKey));
|
|
5713
|
-
const prefixedKey = fromArray ? join([encodedKey, index],
|
|
5705
|
+
const prefixedKey = fromArray ? join([encodedKey, index], QUERY_DOT) : encodedKey;
|
|
5714
5706
|
if (Array.isArray(val)) parts.push(...getParts(val, true, fullKey));
|
|
5715
5707
|
else if (isPlainObject(val)) parts.push(...getParts(val, false, fullKey));
|
|
5716
5708
|
else if (isDecodable(val)) parts.push(`${prefixedKey}=${getString(tryEncode(val))}`);
|
|
@@ -5719,7 +5711,7 @@ function getParts(value, fromArray, prefix) {
|
|
|
5719
5711
|
return parts;
|
|
5720
5712
|
}
|
|
5721
5713
|
function getQueryValue(value) {
|
|
5722
|
-
if (
|
|
5714
|
+
if (QUERY_EXPRESSION_BOOLEAN.test(value)) return value === QUERY_TRUE;
|
|
5723
5715
|
const asNumber = getNumber(value);
|
|
5724
5716
|
if (!Number.isNaN(asNumber)) return asNumber;
|
|
5725
5717
|
const parsed = Date.parse(value);
|
|
@@ -5728,7 +5720,7 @@ function getQueryValue(value) {
|
|
|
5728
5720
|
return date.toJSON() === value ? date : value;
|
|
5729
5721
|
}
|
|
5730
5722
|
function isDecodable(value) {
|
|
5731
|
-
return
|
|
5723
|
+
return QUERY_TYPES.has(typeof value);
|
|
5732
5724
|
}
|
|
5733
5725
|
/**
|
|
5734
5726
|
* Convert a plain _(nested)_ object to a query string
|
|
@@ -5737,15 +5729,15 @@ function isDecodable(value) {
|
|
|
5737
5729
|
* @returns Query string representation of the object
|
|
5738
5730
|
*/
|
|
5739
5731
|
function toQuery(parameters) {
|
|
5740
|
-
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0),
|
|
5741
|
-
}
|
|
5742
|
-
const
|
|
5743
|
-
const
|
|
5744
|
-
const
|
|
5745
|
-
const
|
|
5746
|
-
const
|
|
5747
|
-
const
|
|
5748
|
-
const
|
|
5732
|
+
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0), QUERY_AMPERSAND) : "";
|
|
5733
|
+
}
|
|
5734
|
+
const QUERY_AMPERSAND = "&";
|
|
5735
|
+
const QUERY_DOT = ".";
|
|
5736
|
+
const QUERY_EQUAL = "=";
|
|
5737
|
+
const QUERY_EXPRESSION_ARRAY_SUFFIX = /\[\]$/;
|
|
5738
|
+
const QUERY_EXPRESSION_BOOLEAN = /^(false|true)$/;
|
|
5739
|
+
const QUERY_TRUE = "true";
|
|
5740
|
+
const QUERY_TYPES = /* @__PURE__ */ new Set([
|
|
5749
5741
|
"boolean",
|
|
5750
5742
|
"number",
|
|
5751
5743
|
"string"
|
|
@@ -5758,11 +5750,47 @@ const TYPES = /* @__PURE__ */ new Set([
|
|
|
5758
5750
|
var QueueError = class extends Error {
|
|
5759
5751
|
constructor(message) {
|
|
5760
5752
|
super(message);
|
|
5761
|
-
this.name =
|
|
5753
|
+
this.name = QUEUE_ERROR_NAME;
|
|
5762
5754
|
}
|
|
5763
5755
|
};
|
|
5756
|
+
function addToQueue(instance, state, parameters, signal) {
|
|
5757
|
+
if (instance.full) throw new QueueError(QUEUE_MESSAGE_MAXIMUM);
|
|
5758
|
+
const abortSignal = signal instanceof AbortSignal ? signal : void 0;
|
|
5759
|
+
if (abortSignal?.aborted ?? false) throw new Error(abortSignal?.reason);
|
|
5760
|
+
const id = identify(state);
|
|
5761
|
+
let rejector;
|
|
5762
|
+
let resolver;
|
|
5763
|
+
const promise = new Promise((resolve, reject) => {
|
|
5764
|
+
rejector = reject;
|
|
5765
|
+
resolver = resolve;
|
|
5766
|
+
});
|
|
5767
|
+
const aborter = createAborter(abortSignal, () => rejector(abortSignal?.reason));
|
|
5768
|
+
state.items.push({
|
|
5769
|
+
aborter,
|
|
5770
|
+
id,
|
|
5771
|
+
parameters,
|
|
5772
|
+
promise,
|
|
5773
|
+
key: state.key,
|
|
5774
|
+
reject: rejector,
|
|
5775
|
+
resolve: resolver
|
|
5776
|
+
});
|
|
5777
|
+
if (state.options.autostart) run(state);
|
|
5778
|
+
return {
|
|
5779
|
+
id,
|
|
5780
|
+
promise
|
|
5781
|
+
};
|
|
5782
|
+
}
|
|
5783
|
+
function clearQueue(state) {
|
|
5784
|
+
const items = state.items.splice(0);
|
|
5785
|
+
const { length } = items;
|
|
5786
|
+
for (let index = 0; index < length; index += 1) {
|
|
5787
|
+
const { aborter, reject } = items[index];
|
|
5788
|
+
aborter?.cancel();
|
|
5789
|
+
reject(new QueueError(QUEUE_MESSAGE_CLEAR));
|
|
5790
|
+
}
|
|
5791
|
+
}
|
|
5764
5792
|
function createQueue(callback, options, key) {
|
|
5765
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
5793
|
+
if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
|
|
5766
5794
|
const state = {
|
|
5767
5795
|
callback,
|
|
5768
5796
|
key,
|
|
@@ -5774,71 +5802,16 @@ function createQueue(callback, options, key) {
|
|
|
5774
5802
|
runners: 0
|
|
5775
5803
|
};
|
|
5776
5804
|
const instance = {
|
|
5777
|
-
add: (parameters, signal) =>
|
|
5778
|
-
|
|
5779
|
-
const abortSignal = signal instanceof AbortSignal ? signal : void 0;
|
|
5780
|
-
if (abortSignal?.aborted ?? false) throw new Error(abortSignal.reason);
|
|
5781
|
-
const id = identify(state);
|
|
5782
|
-
let rejector;
|
|
5783
|
-
let resolver;
|
|
5784
|
-
const promise = new Promise((resolve, reject) => {
|
|
5785
|
-
rejector = reject;
|
|
5786
|
-
resolver = resolve;
|
|
5787
|
-
});
|
|
5788
|
-
const aborter = abortSignal == null ? void 0 : () => rejector(abortSignal.reason);
|
|
5789
|
-
signal?.addEventListener(EVENT_NAME, aborter, EVENT_OPTIONS);
|
|
5790
|
-
state.items.push({
|
|
5791
|
-
id,
|
|
5792
|
-
parameters,
|
|
5793
|
-
promise,
|
|
5794
|
-
abort: aborter,
|
|
5795
|
-
key: state.key,
|
|
5796
|
-
reject: rejector,
|
|
5797
|
-
resolve: resolver,
|
|
5798
|
-
signal: abortSignal
|
|
5799
|
-
});
|
|
5800
|
-
if (state.options.autostart) run(state);
|
|
5801
|
-
return {
|
|
5802
|
-
id,
|
|
5803
|
-
promise
|
|
5804
|
-
};
|
|
5805
|
-
},
|
|
5806
|
-
clear: () => {
|
|
5807
|
-
const items = state.items.splice(0);
|
|
5808
|
-
const { length } = items;
|
|
5809
|
-
for (let index = 0; index < length; index += 1) {
|
|
5810
|
-
const { abort, reject, signal } = items[index];
|
|
5811
|
-
reject(new QueueError(MESSAGE_CLEAR));
|
|
5812
|
-
signal?.removeEventListener(EVENT_NAME, abort);
|
|
5813
|
-
}
|
|
5814
|
-
},
|
|
5805
|
+
add: (parameters, signal) => addToQueue(instance, state, parameters, signal),
|
|
5806
|
+
clear: () => clearQueue(state),
|
|
5815
5807
|
pause: () => {
|
|
5816
5808
|
state.paused = true;
|
|
5817
5809
|
},
|
|
5818
|
-
remove: (id) =>
|
|
5819
|
-
|
|
5820
|
-
if (index > -1) {
|
|
5821
|
-
const { abort, reject, signal } = state.items.splice(index, 1)[0];
|
|
5822
|
-
reject(new QueueError(MESSAGE_REMOVE));
|
|
5823
|
-
signal?.removeEventListener(EVENT_NAME, abort);
|
|
5824
|
-
}
|
|
5825
|
-
},
|
|
5826
|
-
resume: () => {
|
|
5827
|
-
if (state.paused) {
|
|
5828
|
-
const handled = state.handled.splice(0);
|
|
5829
|
-
const { length } = handled;
|
|
5830
|
-
for (let index = 0; index < length; index += 1) handled[index]();
|
|
5831
|
-
}
|
|
5832
|
-
state.paused = false;
|
|
5833
|
-
const length = Math.min(state.options.concurrency, state.items.length);
|
|
5834
|
-
for (let index = 0; index < length; index += 1) run(state);
|
|
5835
|
-
}
|
|
5810
|
+
remove: (id) => removeQueued(state, id),
|
|
5811
|
+
resume: () => resumeQueue(state)
|
|
5836
5812
|
};
|
|
5837
5813
|
Object.defineProperties(instance, {
|
|
5838
|
-
[
|
|
5839
|
-
enumerable: false,
|
|
5840
|
-
value: NAME_QUEUE
|
|
5841
|
-
},
|
|
5814
|
+
[QUEUE_PROPERTY]: { value: QUEUE_NAME_QUEUE },
|
|
5842
5815
|
active: {
|
|
5843
5816
|
enumerable: true,
|
|
5844
5817
|
get: () => state.runners > 0
|
|
@@ -5877,12 +5850,6 @@ function createQueue(callback, options, key) {
|
|
|
5877
5850
|
function getBooleanOrDefault(value, defaultValue) {
|
|
5878
5851
|
return typeof value === "boolean" ? value : defaultValue;
|
|
5879
5852
|
}
|
|
5880
|
-
function getItems(state) {
|
|
5881
|
-
const size = {};
|
|
5882
|
-
const queues = state.queues.entries();
|
|
5883
|
-
for (const [key, queue] of queues) size[key] = queue.size;
|
|
5884
|
-
return size;
|
|
5885
|
-
}
|
|
5886
5853
|
function getOptions(input) {
|
|
5887
5854
|
const options = typeof input === "object" && input != null ? input : {};
|
|
5888
5855
|
return {
|
|
@@ -5892,7 +5859,7 @@ function getOptions(input) {
|
|
|
5892
5859
|
};
|
|
5893
5860
|
}
|
|
5894
5861
|
function getQueue(state, key, add) {
|
|
5895
|
-
if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(
|
|
5862
|
+
if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(QUEUE_MESSAGE_KEY);
|
|
5896
5863
|
let queue = state.queues.get(key);
|
|
5897
5864
|
if (queue == null && add === true) {
|
|
5898
5865
|
queue = createQueue(state.callback, state.options, key);
|
|
@@ -5900,6 +5867,12 @@ function getQueue(state, key, add) {
|
|
|
5900
5867
|
}
|
|
5901
5868
|
return queue;
|
|
5902
5869
|
}
|
|
5870
|
+
function getQueueItems(state) {
|
|
5871
|
+
const size = {};
|
|
5872
|
+
const queues = state.queues.entries();
|
|
5873
|
+
for (const [key, queue] of queues) size[key] = queue.size;
|
|
5874
|
+
return size;
|
|
5875
|
+
}
|
|
5903
5876
|
function getStatus(state, status) {
|
|
5904
5877
|
const queues = state.queues.entries();
|
|
5905
5878
|
const result = [];
|
|
@@ -5907,8 +5880,8 @@ function getStatus(state, status) {
|
|
|
5907
5880
|
return result;
|
|
5908
5881
|
}
|
|
5909
5882
|
function handleQueuedResult(item, error, result) {
|
|
5910
|
-
item.
|
|
5911
|
-
if (item.signal?.aborted ?? false) {
|
|
5883
|
+
item.aborter?.cancel();
|
|
5884
|
+
if (item.aborter?.signal?.aborted ?? false) {
|
|
5912
5885
|
item.reject();
|
|
5913
5886
|
return;
|
|
5914
5887
|
}
|
|
@@ -5937,7 +5910,7 @@ function identify(state) {
|
|
|
5937
5910
|
* @returns `true` if the value is a keyed queue, otherwise `false`
|
|
5938
5911
|
*/
|
|
5939
5912
|
function isKeyedQueue(value) {
|
|
5940
|
-
return isQueueInstance(
|
|
5913
|
+
return isQueueInstance(QUEUE_NAME_KEYED, value);
|
|
5941
5914
|
}
|
|
5942
5915
|
/**
|
|
5943
5916
|
* Is the value a queue?
|
|
@@ -5946,13 +5919,13 @@ function isKeyedQueue(value) {
|
|
|
5946
5919
|
* @returns `true` if the value is a queue, otherwise `false`
|
|
5947
5920
|
*/
|
|
5948
5921
|
function isQueue(value) {
|
|
5949
|
-
return isQueueInstance(
|
|
5922
|
+
return isQueueInstance(QUEUE_NAME_QUEUE, value);
|
|
5950
5923
|
}
|
|
5951
5924
|
function isQueueInstance(name, value) {
|
|
5952
|
-
return typeof value === "object" && value != null && value[
|
|
5925
|
+
return typeof value === "object" && value != null && value[QUEUE_PROPERTY] === name;
|
|
5953
5926
|
}
|
|
5954
5927
|
function keyedQueue(callback, options) {
|
|
5955
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
5928
|
+
if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
|
|
5956
5929
|
const state = {
|
|
5957
5930
|
callback,
|
|
5958
5931
|
options: getOptions(options),
|
|
@@ -5960,26 +5933,17 @@ function keyedQueue(callback, options) {
|
|
|
5960
5933
|
};
|
|
5961
5934
|
const instance = {
|
|
5962
5935
|
add: (key, parameters, signal) => getQueue(state, key, true).add(parameters, signal),
|
|
5963
|
-
clear: (key) =>
|
|
5964
|
-
handleQueues(state, HANDLE_CLEAR, key);
|
|
5965
|
-
},
|
|
5936
|
+
clear: (key) => handleQueues(state, QUEUE_HANDLE_CLEAR, key),
|
|
5966
5937
|
get: (key) => getQueue(state, key),
|
|
5967
|
-
pause: (key) =>
|
|
5968
|
-
handleQueues(state, HANDLE_PAUSE, key);
|
|
5969
|
-
},
|
|
5938
|
+
pause: (key) => handleQueues(state, QUEUE_HANDLE_PAUSE, key),
|
|
5970
5939
|
remove: (key, id) => removeQueue(state, key, id),
|
|
5971
|
-
resume: (key) =>
|
|
5972
|
-
handleQueues(state, HANDLE_RESUME, key);
|
|
5973
|
-
}
|
|
5940
|
+
resume: (key) => handleQueues(state, QUEUE_HANDLE_RESUME, key)
|
|
5974
5941
|
};
|
|
5975
5942
|
Object.defineProperties(instance, {
|
|
5976
|
-
[
|
|
5977
|
-
enumerable: false,
|
|
5978
|
-
value: NAME_KEYED
|
|
5979
|
-
},
|
|
5943
|
+
[QUEUE_PROPERTY]: { value: QUEUE_NAME_KEYED },
|
|
5980
5944
|
active: {
|
|
5981
5945
|
enumerable: true,
|
|
5982
|
-
get: () => getStatus(state,
|
|
5946
|
+
get: () => getStatus(state, QUEUE_STATUS_ACTIVE)
|
|
5983
5947
|
},
|
|
5984
5948
|
autostart: {
|
|
5985
5949
|
enumerable: true,
|
|
@@ -5991,15 +5955,15 @@ function keyedQueue(callback, options) {
|
|
|
5991
5955
|
},
|
|
5992
5956
|
empty: {
|
|
5993
5957
|
enumerable: true,
|
|
5994
|
-
get: () => getStatus(state,
|
|
5958
|
+
get: () => getStatus(state, QUEUE_STATUS_EMPTY)
|
|
5995
5959
|
},
|
|
5996
5960
|
full: {
|
|
5997
5961
|
enumerable: true,
|
|
5998
|
-
get: () => getStatus(state,
|
|
5962
|
+
get: () => getStatus(state, QUEUE_STATUS_FULL)
|
|
5999
5963
|
},
|
|
6000
5964
|
items: {
|
|
6001
5965
|
enumerable: true,
|
|
6002
|
-
get: () =>
|
|
5966
|
+
get: () => getQueueItems(state)
|
|
6003
5967
|
},
|
|
6004
5968
|
keys: {
|
|
6005
5969
|
enumerable: true,
|
|
@@ -6011,7 +5975,7 @@ function keyedQueue(callback, options) {
|
|
|
6011
5975
|
},
|
|
6012
5976
|
paused: {
|
|
6013
5977
|
enumerable: true,
|
|
6014
|
-
get: () => getStatus(state,
|
|
5978
|
+
get: () => getStatus(state, QUEUE_STATUS_PAUSED)
|
|
6015
5979
|
},
|
|
6016
5980
|
queues: {
|
|
6017
5981
|
enumerable: true,
|
|
@@ -6026,7 +5990,7 @@ function queue(callback, options) {
|
|
|
6026
5990
|
queue.keyed = keyedQueue;
|
|
6027
5991
|
function removeQueue(state, key, id) {
|
|
6028
5992
|
if (key == null) {
|
|
6029
|
-
handleQueues(state,
|
|
5993
|
+
handleQueues(state, QUEUE_HANDLE_CLEAR);
|
|
6030
5994
|
state.queues.clear();
|
|
6031
5995
|
return;
|
|
6032
5996
|
}
|
|
@@ -6039,6 +6003,24 @@ function removeQueue(state, key, id) {
|
|
|
6039
6003
|
queue.clear();
|
|
6040
6004
|
state.queues.delete(key);
|
|
6041
6005
|
}
|
|
6006
|
+
function removeQueued(state, id) {
|
|
6007
|
+
const index = state.items.findIndex((item) => item.id === id);
|
|
6008
|
+
if (index > -1) {
|
|
6009
|
+
const { aborter, reject } = state.items.splice(index, 1)[0];
|
|
6010
|
+
aborter?.cancel();
|
|
6011
|
+
reject(new QueueError(QUEUE_MESSAGE_REMOVE));
|
|
6012
|
+
}
|
|
6013
|
+
}
|
|
6014
|
+
function resumeQueue(state) {
|
|
6015
|
+
if (state.paused) {
|
|
6016
|
+
const handled = state.handled.splice(0);
|
|
6017
|
+
const { length } = handled;
|
|
6018
|
+
for (let index = 0; index < length; index += 1) handled[index]();
|
|
6019
|
+
}
|
|
6020
|
+
state.paused = false;
|
|
6021
|
+
const length = Math.min(state.options.concurrency, state.items.length);
|
|
6022
|
+
for (let index = 0; index < length; index += 1) run(state);
|
|
6023
|
+
}
|
|
6042
6024
|
async function run(state) {
|
|
6043
6025
|
if (state.paused || state.runners >= state.options.concurrency) return;
|
|
6044
6026
|
state.runners += 1;
|
|
@@ -6047,7 +6029,7 @@ async function run(state) {
|
|
|
6047
6029
|
let error = false;
|
|
6048
6030
|
let result;
|
|
6049
6031
|
try {
|
|
6050
|
-
if (!(item.signal?.aborted ?? false)) {
|
|
6032
|
+
if (!(item.aborter?.signal?.aborted ?? false)) {
|
|
6051
6033
|
const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
|
|
6052
6034
|
result = await state.callback(...parameters);
|
|
6053
6035
|
}
|
|
@@ -6057,9 +6039,7 @@ async function run(state) {
|
|
|
6057
6039
|
}
|
|
6058
6040
|
if (state.paused) {
|
|
6059
6041
|
const paused = item;
|
|
6060
|
-
state.handled.push(() =>
|
|
6061
|
-
handleQueuedResult(paused, error, result);
|
|
6062
|
-
});
|
|
6042
|
+
state.handled.push(() => handleQueuedResult(paused, error, result));
|
|
6063
6043
|
break;
|
|
6064
6044
|
}
|
|
6065
6045
|
handleQueuedResult(item, error, result);
|
|
@@ -6067,24 +6047,22 @@ async function run(state) {
|
|
|
6067
6047
|
}
|
|
6068
6048
|
state.runners -= 1;
|
|
6069
6049
|
}
|
|
6070
|
-
const
|
|
6071
|
-
const
|
|
6072
|
-
const
|
|
6073
|
-
const
|
|
6074
|
-
const
|
|
6075
|
-
const
|
|
6076
|
-
const
|
|
6077
|
-
const
|
|
6078
|
-
const
|
|
6079
|
-
const
|
|
6080
|
-
const
|
|
6081
|
-
const
|
|
6082
|
-
const
|
|
6083
|
-
const
|
|
6084
|
-
const
|
|
6085
|
-
const
|
|
6086
|
-
const STATUS_FULL = "full";
|
|
6087
|
-
const STATUS_PAUSED = "paused";
|
|
6050
|
+
const QUEUE_ERROR_NAME = "QueueError";
|
|
6051
|
+
const QUEUE_PROPERTY = "$queue";
|
|
6052
|
+
const QUEUE_HANDLE_CLEAR = "clear";
|
|
6053
|
+
const QUEUE_HANDLE_PAUSE = "pause";
|
|
6054
|
+
const QUEUE_HANDLE_RESUME = "resume";
|
|
6055
|
+
const QUEUE_MESSAGE_CALLBACK = "A Queue requires a callback function";
|
|
6056
|
+
const QUEUE_MESSAGE_CLEAR = "Queue was cleared";
|
|
6057
|
+
const QUEUE_MESSAGE_KEY = "Key must be a non-empty string";
|
|
6058
|
+
const QUEUE_MESSAGE_MAXIMUM = "Queue has reached its maximum size";
|
|
6059
|
+
const QUEUE_MESSAGE_REMOVE = "Item removed from queue";
|
|
6060
|
+
const QUEUE_NAME_KEYED = "keyedQueue";
|
|
6061
|
+
const QUEUE_NAME_QUEUE = "queue";
|
|
6062
|
+
const QUEUE_STATUS_ACTIVE = "active";
|
|
6063
|
+
const QUEUE_STATUS_EMPTY = "empty";
|
|
6064
|
+
const QUEUE_STATUS_FULL = "full";
|
|
6065
|
+
const QUEUE_STATUS_PAUSED = "paused";
|
|
6088
6066
|
//#endregion
|
|
6089
6067
|
//#region src/random.ts
|
|
6090
6068
|
/**
|
|
@@ -6093,7 +6071,7 @@ const STATUS_PAUSED = "paused";
|
|
|
6093
6071
|
* @returns Random boolean
|
|
6094
6072
|
*/
|
|
6095
6073
|
function getRandomBoolean() {
|
|
6096
|
-
return Math.random() >
|
|
6074
|
+
return Math.random() > RANDOM_BOOLEAN_MODIFIER;
|
|
6097
6075
|
}
|
|
6098
6076
|
/**
|
|
6099
6077
|
* Get a random string of characters with a specified length
|
|
@@ -6104,7 +6082,7 @@ function getRandomBoolean() {
|
|
|
6104
6082
|
*/
|
|
6105
6083
|
function getRandomCharacters(length, selection) {
|
|
6106
6084
|
if (typeof length !== "number" || length <= 0) return "";
|
|
6107
|
-
const actual = typeof selection === "string" && selection.length > 0 ? selection :
|
|
6085
|
+
const actual = typeof selection === "string" && selection.length > 0 ? selection : RANDOM_ALPHABET;
|
|
6108
6086
|
let characters = "";
|
|
6109
6087
|
for (let index = 0; index < length; index += 1) characters += actual.charAt(getRandomInteger(0, actual.length - 1));
|
|
6110
6088
|
return characters;
|
|
@@ -6124,7 +6102,7 @@ function getRandomColor(prefix) {
|
|
|
6124
6102
|
* @returns Random hexadecimal character from `0-9` and `A-F`
|
|
6125
6103
|
*/
|
|
6126
6104
|
function getRandomHex() {
|
|
6127
|
-
return
|
|
6105
|
+
return RANDOM_HEX_CHARACTERS[getRandomInteger(0, RANDOM_HEX_MAXIMUM)];
|
|
6128
6106
|
}
|
|
6129
6107
|
/**
|
|
6130
6108
|
* Get a random item from an array
|
|
@@ -6142,10 +6120,10 @@ function getRandomItems(array, amount) {
|
|
|
6142
6120
|
if (amount === 1) return [array[getRandomInteger(0, array.length - 1)]];
|
|
6143
6121
|
return typeof amount !== "number" || amount <= 0 || amount >= array.length ? shuffle(array) : shuffle(array).slice(0, amount);
|
|
6144
6122
|
}
|
|
6145
|
-
const
|
|
6146
|
-
const
|
|
6147
|
-
const
|
|
6148
|
-
const
|
|
6123
|
+
const RANDOM_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
6124
|
+
const RANDOM_BOOLEAN_MODIFIER = .5;
|
|
6125
|
+
const RANDOM_HEX_CHARACTERS = "0123456789ABCDEF";
|
|
6126
|
+
const RANDOM_HEX_MAXIMUM = 15;
|
|
6149
6127
|
//#endregion
|
|
6150
6128
|
//#region src/result/index.ts
|
|
6151
6129
|
async function asyncAttempt(value, err) {
|
|
@@ -6164,7 +6142,7 @@ function attempt(callback, err) {
|
|
|
6164
6142
|
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
6165
6143
|
}
|
|
6166
6144
|
}
|
|
6167
|
-
attempt
|
|
6145
|
+
Object.defineProperty(attempt, "async", { value: asyncAttempt });
|
|
6168
6146
|
//#endregion
|
|
6169
6147
|
//#region src/result/match.ts
|
|
6170
6148
|
async function asyncMatchResult(result, first, error) {
|
|
@@ -6172,7 +6150,7 @@ async function asyncMatchResult(result, first, error) {
|
|
|
6172
6150
|
if (typeof result === "function") value = await result();
|
|
6173
6151
|
else if (result instanceof Promise) value = await result;
|
|
6174
6152
|
else value = result;
|
|
6175
|
-
if (!isResult(value)) throw new Error(
|
|
6153
|
+
if (!isResult(value)) throw new Error(MATCH_MESSAGE);
|
|
6176
6154
|
const hasObj = typeof first === "object" && first !== null;
|
|
6177
6155
|
const okHandler = hasObj ? first.ok : first;
|
|
6178
6156
|
const errorHandler = hasObj ? first.error : error;
|
|
@@ -6181,21 +6159,21 @@ async function asyncMatchResult(result, first, error) {
|
|
|
6181
6159
|
}
|
|
6182
6160
|
function matchResult(result, first, error) {
|
|
6183
6161
|
const value = typeof result === "function" ? result() : result;
|
|
6184
|
-
if (!isResult(value)) throw new Error(
|
|
6162
|
+
if (!isResult(value)) throw new Error(MATCH_MESSAGE);
|
|
6185
6163
|
const hasObj = typeof first === "object" && first !== null;
|
|
6186
6164
|
const okHandler = hasObj ? first.ok : first;
|
|
6187
6165
|
const errorHandler = hasObj ? first.error : error;
|
|
6188
6166
|
if (isOk(value)) return okHandler(value.value);
|
|
6189
6167
|
return errorHandler(value.error, value.original);
|
|
6190
6168
|
}
|
|
6191
|
-
|
|
6192
|
-
|
|
6169
|
+
const MATCH_MESSAGE = "`result.match` expected a Result or a function that returns a Result";
|
|
6170
|
+
Object.defineProperty(matchResult, "async", { value: asyncMatchResult });
|
|
6193
6171
|
//#endregion
|
|
6194
6172
|
//#region src/result/work/flow.ts
|
|
6195
6173
|
function attemptAsyncFlow(...fns) {
|
|
6196
6174
|
let Flow;
|
|
6197
|
-
return (...args) =>
|
|
6198
|
-
Flow ??=
|
|
6175
|
+
return (...args) => asyncAttempt(() => {
|
|
6176
|
+
Flow ??= asyncFlow(...fns);
|
|
6199
6177
|
return Flow(...args.map((value) => {
|
|
6200
6178
|
if (isError(value)) throw value.error;
|
|
6201
6179
|
return isOk(value) ? value.value : value;
|
|
@@ -6212,11 +6190,11 @@ function attemptFlow(...fns) {
|
|
|
6212
6190
|
}));
|
|
6213
6191
|
});
|
|
6214
6192
|
}
|
|
6215
|
-
attemptFlow
|
|
6193
|
+
Object.defineProperty(attemptFlow, "async", { value: attemptAsyncFlow });
|
|
6216
6194
|
//#endregion
|
|
6217
6195
|
//#region src/result/work/pipe.ts
|
|
6218
6196
|
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
6219
|
-
return
|
|
6197
|
+
return asyncAttempt(() => {
|
|
6220
6198
|
if (isError(initial)) throw initial.error;
|
|
6221
6199
|
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
6222
6200
|
if (first == null) return value;
|
|
@@ -6231,7 +6209,7 @@ function attemptPipe(initial, first, ...seconds) {
|
|
|
6231
6209
|
return pipe(value, ...[first, ...seconds]);
|
|
6232
6210
|
});
|
|
6233
6211
|
}
|
|
6234
|
-
attemptPipe
|
|
6212
|
+
Object.defineProperty(attemptPipe, "async", { value: attemptAsyncPipe });
|
|
6235
6213
|
//#endregion
|
|
6236
6214
|
//#region src/sized/set.ts
|
|
6237
6215
|
/**
|
|
@@ -6289,4 +6267,4 @@ var SizedSet = class extends Set {
|
|
|
6289
6267
|
}
|
|
6290
6268
|
};
|
|
6291
6269
|
//#endregion
|
|
6292
|
-
export { CancelablePromise, Logger,
|
|
6270
|
+
export { CancelablePromise, Logger, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isBeacon, isBeaconSubscription, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isHerald, isHeraldEvents, isHeraldSubscription, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isPlainObject, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isTimedLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|