@oscarpalmer/atoms 0.205.0 → 0.206.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/reverse.mjs +2 -1
- package/dist/array/single.d.mts +3 -3
- package/dist/array/sort.mjs +3 -2
- package/dist/color/misc/alpha.mjs +1 -1
- package/dist/color/misc/get.mjs +1 -1
- package/dist/color/space/hsl.mjs +7 -2
- package/dist/color/space/hwb.mjs +2 -1
- package/dist/color/space/rgb.mjs +22 -13
- package/dist/function/retry.mjs +12 -14
- package/dist/index.d.mts +53 -53
- package/dist/index.mjs +258 -258
- package/dist/internal/array/insert.mjs +2 -1
- package/dist/internal/function/limit.mjs +9 -9
- package/dist/internal/math/aggregate.d.mts +41 -0
- package/dist/internal/math/aggregate.mjs +4 -1
- package/dist/internal/math/misc.d.mts +8 -0
- package/dist/internal/math/misc.mjs +11 -1
- package/dist/internal/random.mjs +2 -1
- package/dist/internal/result/attempt.d.mts +59 -0
- package/dist/internal/result/attempt.mjs +24 -0
- package/dist/internal/string/misc.mjs +4 -5
- package/dist/internal/value/equal.mjs +2 -1
- package/dist/internal/value/misc.mjs +1 -1
- package/dist/math.d.mts +3 -52
- package/dist/math.mjs +3 -16
- package/dist/promise/constants.d.mts +0 -1
- package/dist/promise/constants.mjs +1 -2
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.mjs +9 -9
- package/dist/promise/misc.mjs +1 -1
- package/dist/queue.mjs +11 -6
- package/dist/result/index.d.mts +2 -59
- package/dist/result/index.mjs +1 -23
- package/dist/result/work/flow.mjs +2 -1
- package/dist/result/work/pipe.mjs +2 -1
- package/dist/string/fuzzy.mjs +7 -5
- package/dist/string/index.mjs +2 -1
- package/dist/value/clone.mjs +5 -8
- package/dist/value/diff.mjs +3 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -462,6 +462,34 @@ const outside = [-1, MATCH_COMPARISON_OUTSIDE];
|
|
|
462
462
|
const outsides = /* @__PURE__ */ new Set([MATCH_COMPARISON_INVALID, MATCH_COMPARISON_OUTSIDE]);
|
|
463
463
|
const starts = /* @__PURE__ */ new Set([MATCH_COMPARISON_START, MATCH_COMPARISON_SAME]);
|
|
464
464
|
//#endregion
|
|
465
|
+
//#region src/internal/math/misc.ts
|
|
466
|
+
/**
|
|
467
|
+
* Round a number down
|
|
468
|
+
*
|
|
469
|
+
* @param value Number to round down
|
|
470
|
+
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
471
|
+
* @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
|
|
472
|
+
*/
|
|
473
|
+
function floor(value, decimals) {
|
|
474
|
+
return roundNumber(Math.floor, value, decimals);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Round a number
|
|
478
|
+
*
|
|
479
|
+
* @param value Number to round
|
|
480
|
+
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
481
|
+
* @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
|
|
482
|
+
*/
|
|
483
|
+
function round(value, decimals) {
|
|
484
|
+
return roundNumber(Math.round, value, decimals);
|
|
485
|
+
}
|
|
486
|
+
function roundNumber(callback, value, decimals) {
|
|
487
|
+
if (typeof value !== "number") return NaN;
|
|
488
|
+
if (typeof decimals !== "number" || decimals < 1) return callback(value);
|
|
489
|
+
const mod = 10 ** decimals;
|
|
490
|
+
return callback((value + Number.EPSILON) * mod) / mod;
|
|
491
|
+
}
|
|
492
|
+
//#endregion
|
|
465
493
|
//#region src/internal/random.ts
|
|
466
494
|
function getRandomFloatingNumberValue(inclusive, minimum, maximum) {
|
|
467
495
|
let maxFloat = isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
|
|
@@ -488,7 +516,7 @@ function getRandomFloatingNumber(minimum, maximum) {
|
|
|
488
516
|
* @returns Random integer
|
|
489
517
|
*/
|
|
490
518
|
function getRandomInteger(minimum, maximum) {
|
|
491
|
-
return
|
|
519
|
+
return floor(getRandomFloatingNumberValue(true, minimum, maximum));
|
|
492
520
|
}
|
|
493
521
|
//#endregion
|
|
494
522
|
//#region src/internal/array/shuffle.ts
|
|
@@ -586,10 +614,81 @@ function times(length, value) {
|
|
|
586
614
|
return values;
|
|
587
615
|
}
|
|
588
616
|
//#endregion
|
|
617
|
+
//#region src/internal/math/aggregate.ts
|
|
618
|
+
function aggregate(type, array, key, first) {
|
|
619
|
+
const length = Array.isArray(array) ? array.length : 0;
|
|
620
|
+
const aggregation = {
|
|
621
|
+
array: false,
|
|
622
|
+
count: 0,
|
|
623
|
+
first: first === true,
|
|
624
|
+
value: NaN
|
|
625
|
+
};
|
|
626
|
+
if (length === 0) return aggregation;
|
|
627
|
+
const aggregator = aggregators[type];
|
|
628
|
+
const callback = getAggregateCallback(key);
|
|
629
|
+
aggregation.array = callback != null && (type === "max" || type === "min");
|
|
630
|
+
let notNumber = true;
|
|
631
|
+
for (let index = 0; index < length; index += 1) {
|
|
632
|
+
const item = array[index];
|
|
633
|
+
const value = callback == null ? item : callback(item, index, array);
|
|
634
|
+
if (isNonNumber(value)) continue;
|
|
635
|
+
aggregation.value = aggregator(aggregation, value, notNumber, item);
|
|
636
|
+
aggregation.count += 1;
|
|
637
|
+
notNumber = false;
|
|
638
|
+
}
|
|
639
|
+
return aggregation;
|
|
640
|
+
}
|
|
641
|
+
function calculateSum(aggregation, value, notNumber) {
|
|
642
|
+
return notNumber ? value : aggregation.value + value;
|
|
643
|
+
}
|
|
644
|
+
function getAbsoluteValue(condition, aggregation, value, notNumber, item) {
|
|
645
|
+
if (notNumber || condition(aggregation, value)) {
|
|
646
|
+
if (aggregation.array) {
|
|
647
|
+
aggregation.items ??= {};
|
|
648
|
+
aggregation.items[value] ??= [];
|
|
649
|
+
aggregation.items[value].push(item);
|
|
650
|
+
}
|
|
651
|
+
return value;
|
|
652
|
+
}
|
|
653
|
+
return aggregation.value;
|
|
654
|
+
}
|
|
655
|
+
function getAggregateCallback(key) {
|
|
656
|
+
if (key == null) return;
|
|
657
|
+
return typeof key === "function" ? key : (item) => item[key];
|
|
658
|
+
}
|
|
659
|
+
function getAggregated(type, array, key, first) {
|
|
660
|
+
const aggregation = aggregate(type, array, key, first);
|
|
661
|
+
if (aggregation.count === 0) return aggregation.array ? aggregation.first ? void 0 : [] : NaN;
|
|
662
|
+
if (aggregation.array) {
|
|
663
|
+
const array = aggregation.items[aggregation.value];
|
|
664
|
+
return aggregation.first ? array[0] : array;
|
|
665
|
+
}
|
|
666
|
+
return aggregation.value;
|
|
667
|
+
}
|
|
668
|
+
function isMaxValue(aggregation, value) {
|
|
669
|
+
return aggregation.array ? value >= aggregation.value : value > aggregation.value;
|
|
670
|
+
}
|
|
671
|
+
function isMinValue(aggregation, value) {
|
|
672
|
+
return aggregation.array ? value <= aggregation.value : value < aggregation.value;
|
|
673
|
+
}
|
|
674
|
+
function max(array, key, first) {
|
|
675
|
+
return getAggregated("max", array, key, first);
|
|
676
|
+
}
|
|
677
|
+
function min(array, key, first) {
|
|
678
|
+
return getAggregated("min", array, key, first);
|
|
679
|
+
}
|
|
680
|
+
const AGGREGATION_AVERAGE = "average";
|
|
681
|
+
const aggregators = {
|
|
682
|
+
average: calculateSum,
|
|
683
|
+
max: getAbsoluteValue.bind(void 0, isMaxValue),
|
|
684
|
+
min: getAbsoluteValue.bind(void 0, isMinValue),
|
|
685
|
+
sum: calculateSum
|
|
686
|
+
};
|
|
687
|
+
//#endregion
|
|
589
688
|
//#region src/internal/array/insert.ts
|
|
590
689
|
function insertChunkedValues(type, array, items, start, deleteCount) {
|
|
591
690
|
const actualDeleteCount = deleteCount < 0 ? 0 : deleteCount;
|
|
592
|
-
const actualStart =
|
|
691
|
+
const actualStart = min([max([0, start]), array.length]);
|
|
593
692
|
const chunked = chunk(items);
|
|
594
693
|
const lastIndex = chunked.length - 1;
|
|
595
694
|
let index = chunked.length;
|
|
@@ -658,7 +757,7 @@ function reverse(array) {
|
|
|
658
757
|
if (!Array.isArray(array)) return [];
|
|
659
758
|
const { length } = array;
|
|
660
759
|
if (length < 2) return array;
|
|
661
|
-
const half =
|
|
760
|
+
const half = floor(length / 2);
|
|
662
761
|
for (let firstIndex = 0; firstIndex < half; firstIndex += 1) {
|
|
663
762
|
const temporaryItem = array[firstIndex];
|
|
664
763
|
const secondIndex = length - 1 - firstIndex;
|
|
@@ -907,73 +1006,92 @@ function selectValues(array, parameters) {
|
|
|
907
1006
|
}
|
|
908
1007
|
filterAndMap.reverse = mapAndFilter;
|
|
909
1008
|
//#endregion
|
|
910
|
-
//#region src/internal/
|
|
911
|
-
function
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
1009
|
+
//#region src/internal/result/misc.ts
|
|
1010
|
+
function error(value, original) {
|
|
1011
|
+
return getError(value, original);
|
|
1012
|
+
}
|
|
1013
|
+
function getError(value, original) {
|
|
1014
|
+
const errorResult = {
|
|
1015
|
+
error: value,
|
|
1016
|
+
ok: false
|
|
918
1017
|
};
|
|
919
|
-
if (
|
|
920
|
-
|
|
921
|
-
const callback = getAggregateCallback(key);
|
|
922
|
-
aggregation.array = callback != null && (type === "max" || type === "min");
|
|
923
|
-
let notNumber = true;
|
|
924
|
-
for (let index = 0; index < length; index += 1) {
|
|
925
|
-
const item = array[index];
|
|
926
|
-
const value = callback == null ? item : callback(item, index, array);
|
|
927
|
-
if (isNonNumber(value)) continue;
|
|
928
|
-
aggregation.value = aggregator(aggregation, value, notNumber, item);
|
|
929
|
-
aggregation.count += 1;
|
|
930
|
-
notNumber = false;
|
|
931
|
-
}
|
|
932
|
-
return aggregation;
|
|
1018
|
+
if (original instanceof Error) errorResult.original = original;
|
|
1019
|
+
return errorResult;
|
|
933
1020
|
}
|
|
934
|
-
function
|
|
935
|
-
return
|
|
1021
|
+
function isError(value, extended) {
|
|
1022
|
+
return isResultValue(value, false) && (extended === true ? value.original instanceof Error : true);
|
|
936
1023
|
}
|
|
937
|
-
function
|
|
938
|
-
|
|
939
|
-
if (aggregation.array) {
|
|
940
|
-
aggregation.items ??= {};
|
|
941
|
-
aggregation.items[value] ??= [];
|
|
942
|
-
aggregation.items[value].push(item);
|
|
943
|
-
}
|
|
944
|
-
return value;
|
|
945
|
-
}
|
|
946
|
-
return aggregation.value;
|
|
1024
|
+
function isOk(value) {
|
|
1025
|
+
return isResultValue(value, true);
|
|
947
1026
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
1027
|
+
/**
|
|
1028
|
+
* Is the value a _Result_?
|
|
1029
|
+
*
|
|
1030
|
+
* @param value Value to check
|
|
1031
|
+
* @returns `true` if the value is a _Result_, otherwise `false`
|
|
1032
|
+
*/
|
|
1033
|
+
function isResult(value) {
|
|
1034
|
+
return isResultValue(value, true) || isResultValue(value, false);
|
|
951
1035
|
}
|
|
952
|
-
function
|
|
953
|
-
|
|
954
|
-
if (aggregation.count === 0) return aggregation.array ? aggregation.first ? void 0 : [] : NaN;
|
|
955
|
-
if (aggregation.array) {
|
|
956
|
-
const array = aggregation.items[aggregation.value];
|
|
957
|
-
return aggregation.first ? array[0] : array;
|
|
958
|
-
}
|
|
959
|
-
return aggregation.value;
|
|
1036
|
+
function isResultValue(value, okValue) {
|
|
1037
|
+
return isPlainObject(value) && value.ok === okValue && (okValue ? RESULT_PROPERTY_VALUE : RESULT_PROPERTY_ERROR) in value;
|
|
960
1038
|
}
|
|
961
|
-
|
|
962
|
-
|
|
1039
|
+
/**
|
|
1040
|
+
* Creates an ok result
|
|
1041
|
+
*
|
|
1042
|
+
* @param value Value
|
|
1043
|
+
* @returns Ok result
|
|
1044
|
+
*/
|
|
1045
|
+
function ok(value) {
|
|
1046
|
+
return {
|
|
1047
|
+
ok: true,
|
|
1048
|
+
value
|
|
1049
|
+
};
|
|
963
1050
|
}
|
|
964
|
-
function
|
|
965
|
-
return
|
|
1051
|
+
function unwrap(value, defaultValue) {
|
|
1052
|
+
return isOk(value) ? value.value : defaultValue;
|
|
966
1053
|
}
|
|
967
|
-
|
|
968
|
-
|
|
1054
|
+
const RESULT_PROPERTY_ERROR = "error";
|
|
1055
|
+
const RESULT_PROPERTY_VALUE = "value";
|
|
1056
|
+
error.is = isError;
|
|
1057
|
+
ok.is = isOk;
|
|
1058
|
+
//#endregion
|
|
1059
|
+
//#region src/result/misc.ts
|
|
1060
|
+
/**
|
|
1061
|
+
* Converts a _Result_ to a _Promise_
|
|
1062
|
+
*
|
|
1063
|
+
* Resolves if ok, rejects for error
|
|
1064
|
+
*
|
|
1065
|
+
* @param result _Result_ to convert
|
|
1066
|
+
* @returns Promised result
|
|
1067
|
+
*/
|
|
1068
|
+
async function toPromise(result) {
|
|
1069
|
+
const actual = typeof result === "function" ? result() : result;
|
|
1070
|
+
if (!isResult(actual)) return Promise.reject(new Error(RESULT_MESSAGE_PROMISE));
|
|
1071
|
+
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
969
1072
|
}
|
|
970
|
-
const
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1073
|
+
const RESULT_MESSAGE_PROMISE = "toPromise expected to receive a Result";
|
|
1074
|
+
//#endregion
|
|
1075
|
+
//#region src/internal/result/attempt.ts
|
|
1076
|
+
async function asyncAttempt(value, err) {
|
|
1077
|
+
try {
|
|
1078
|
+
let result;
|
|
1079
|
+
if (typeof value === "function") result = value();
|
|
1080
|
+
else result = value;
|
|
1081
|
+
if (result instanceof Promise) result = await result;
|
|
1082
|
+
return ok(result);
|
|
1083
|
+
} catch (thrown) {
|
|
1084
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
function attempt(callback, err) {
|
|
1088
|
+
try {
|
|
1089
|
+
return ok(callback());
|
|
1090
|
+
} catch (thrown) {
|
|
1091
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
attempt.async = asyncAttempt;
|
|
977
1095
|
//#endregion
|
|
978
1096
|
//#region src/internal/string/misc.ts
|
|
979
1097
|
/**
|
|
@@ -1034,11 +1152,7 @@ function join(array, delimiter) {
|
|
|
1034
1152
|
return values.join(typeof delimiter === "string" ? delimiter : "");
|
|
1035
1153
|
}
|
|
1036
1154
|
function tryCallback(value, callback) {
|
|
1037
|
-
|
|
1038
|
-
return callback(value);
|
|
1039
|
-
} catch {
|
|
1040
|
-
return value;
|
|
1041
|
-
}
|
|
1155
|
+
return unwrap(attempt(() => callback(value)), value);
|
|
1042
1156
|
}
|
|
1043
1157
|
function tryDecode(value) {
|
|
1044
1158
|
return tryCallback(value, decodeURIComponent);
|
|
@@ -1244,7 +1358,7 @@ function getSortedArrayIndex(array, item) {
|
|
|
1244
1358
|
let low = 0;
|
|
1245
1359
|
let high = length - 1;
|
|
1246
1360
|
while (low <= high) {
|
|
1247
|
-
const mid =
|
|
1361
|
+
const mid = floor((low + high) / 2);
|
|
1248
1362
|
if (getComparisonValue(item, array[mid], sorters, sortersLength) < 0) high = mid - 1;
|
|
1249
1363
|
else low = mid + 1;
|
|
1250
1364
|
}
|
|
@@ -1299,7 +1413,7 @@ function isSortedArray(array) {
|
|
|
1299
1413
|
const sortersLength = sorters.length;
|
|
1300
1414
|
let offset = 0;
|
|
1301
1415
|
if (length >= SORT_THRESHOLD) {
|
|
1302
|
-
offset =
|
|
1416
|
+
offset = round(length / SORT_PEEK_PERCENTAGE);
|
|
1303
1417
|
offset = offset > SORT_THRESHOLD ? SORT_THRESHOLD : offset;
|
|
1304
1418
|
for (let index = 0; index < offset; index += 1) {
|
|
1305
1419
|
const [firstItem, firstOffset] = [array[index], array[index + 1]];
|
|
@@ -1505,15 +1619,14 @@ async function handleAsyncLimiter(state, item) {
|
|
|
1505
1619
|
if (state.interval === 0 || now - state.start >= state.interval - LIMITER_OFFSET) {
|
|
1506
1620
|
state.start = state.throttle ? now : void 0;
|
|
1507
1621
|
item.running = true;
|
|
1508
|
-
|
|
1509
|
-
let
|
|
1510
|
-
if (
|
|
1511
|
-
|
|
1512
|
-
}
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
}
|
|
1622
|
+
const result = await asyncAttempt(async () => {
|
|
1623
|
+
let value = state.callback(...item.parameters);
|
|
1624
|
+
if (value instanceof Promise) value = await value;
|
|
1625
|
+
return value;
|
|
1626
|
+
});
|
|
1627
|
+
if (result.ok) item.resolve(result.value);
|
|
1628
|
+
else item.reject(result.error);
|
|
1629
|
+
item.running = false;
|
|
1517
1630
|
} else state.timer = startTimer(() => handleAsyncLimiter(state, item));
|
|
1518
1631
|
}
|
|
1519
1632
|
function handleLimiter(state) {
|
|
@@ -1613,56 +1726,6 @@ function throttle(callback, time) {
|
|
|
1613
1726
|
debounce.async = asyncDebounce;
|
|
1614
1727
|
throttle.async = asyncThrottle;
|
|
1615
1728
|
//#endregion
|
|
1616
|
-
//#region src/internal/result/misc.ts
|
|
1617
|
-
function error(value, original) {
|
|
1618
|
-
return getError(value, original);
|
|
1619
|
-
}
|
|
1620
|
-
function getError(value, original) {
|
|
1621
|
-
const errorResult = {
|
|
1622
|
-
error: value,
|
|
1623
|
-
ok: false
|
|
1624
|
-
};
|
|
1625
|
-
if (original instanceof Error) errorResult.original = original;
|
|
1626
|
-
return errorResult;
|
|
1627
|
-
}
|
|
1628
|
-
function isError(value, extended) {
|
|
1629
|
-
return isResultValue(value, false) && (extended === true ? value.original instanceof Error : true);
|
|
1630
|
-
}
|
|
1631
|
-
function isOk(value) {
|
|
1632
|
-
return isResultValue(value, true);
|
|
1633
|
-
}
|
|
1634
|
-
/**
|
|
1635
|
-
* Is the value a _Result_?
|
|
1636
|
-
*
|
|
1637
|
-
* @param value Value to check
|
|
1638
|
-
* @returns `true` if the value is a _Result_, otherwise `false`
|
|
1639
|
-
*/
|
|
1640
|
-
function isResult(value) {
|
|
1641
|
-
return isResultValue(value, true) || isResultValue(value, false);
|
|
1642
|
-
}
|
|
1643
|
-
function isResultValue(value, okValue) {
|
|
1644
|
-
return isPlainObject(value) && value.ok === okValue && (okValue ? RESULT_PROPERTY_VALUE : RESULT_PROPERTY_ERROR) in value;
|
|
1645
|
-
}
|
|
1646
|
-
/**
|
|
1647
|
-
* Creates an ok result
|
|
1648
|
-
*
|
|
1649
|
-
* @param value Value
|
|
1650
|
-
* @returns Ok result
|
|
1651
|
-
*/
|
|
1652
|
-
function ok(value) {
|
|
1653
|
-
return {
|
|
1654
|
-
ok: true,
|
|
1655
|
-
value
|
|
1656
|
-
};
|
|
1657
|
-
}
|
|
1658
|
-
function unwrap(value, defaultValue) {
|
|
1659
|
-
return isOk(value) ? value.value : defaultValue;
|
|
1660
|
-
}
|
|
1661
|
-
const RESULT_PROPERTY_ERROR = "error";
|
|
1662
|
-
const RESULT_PROPERTY_VALUE = "value";
|
|
1663
|
-
error.is = isError;
|
|
1664
|
-
ok.is = isOk;
|
|
1665
|
-
//#endregion
|
|
1666
1729
|
//#region src/internal/value/misc.ts
|
|
1667
1730
|
function findKey(needle, haystack) {
|
|
1668
1731
|
const keys = Object.keys(haystack);
|
|
@@ -2005,15 +2068,12 @@ var RetryError = class extends Error {
|
|
|
2005
2068
|
async function asyncRetry(callback, options) {
|
|
2006
2069
|
if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
2007
2070
|
async function handle() {
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
attempts += 1;
|
|
2015
|
-
limiter.run();
|
|
2016
|
-
}
|
|
2071
|
+
const result = await asyncAttempt(async () => callback());
|
|
2072
|
+
if (result.ok) resolver(result.value);
|
|
2073
|
+
else if (attempts >= times || !when(result.error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, result.error));
|
|
2074
|
+
else {
|
|
2075
|
+
attempts += 1;
|
|
2076
|
+
limiter.run();
|
|
2017
2077
|
}
|
|
2018
2078
|
}
|
|
2019
2079
|
const { delay, times, when } = createRetryOptions(options);
|
|
@@ -2046,11 +2106,11 @@ function retry(callback, options) {
|
|
|
2046
2106
|
if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
2047
2107
|
const { times, when } = createRetryOptions(options);
|
|
2048
2108
|
let last;
|
|
2049
|
-
for (let index = 0; index <= times; index += 1)
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
if (index >= times || !when(error)) {
|
|
2053
|
-
last = error;
|
|
2109
|
+
for (let index = 0; index <= times; index += 1) {
|
|
2110
|
+
const result = attempt(callback);
|
|
2111
|
+
if (result.ok) return result.value;
|
|
2112
|
+
if (index >= times || !when(result.error)) {
|
|
2113
|
+
last = result.error;
|
|
2054
2114
|
break;
|
|
2055
2115
|
}
|
|
2056
2116
|
}
|
|
@@ -2531,7 +2591,7 @@ function equalArray(first, second, options) {
|
|
|
2531
2591
|
if (length !== second.length) return false;
|
|
2532
2592
|
let offset = 0;
|
|
2533
2593
|
if (length >= EQUAL_ARRAY_THRESHOLD) {
|
|
2534
|
-
offset =
|
|
2594
|
+
offset = round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
|
|
2535
2595
|
offset = offset > EQUAL_ARRAY_THRESHOLD ? EQUAL_ARRAY_THRESHOLD : offset;
|
|
2536
2596
|
for (let index = 0; index < offset; index += 1) if (!(equalValue(first[index], second[index], options) && equalValue(first[length - index - 1], second[length - index - 1], options))) return false;
|
|
2537
2597
|
}
|
|
@@ -2703,7 +2763,6 @@ const ABORT_OPTIONS = { once: true };
|
|
|
2703
2763
|
//#region src/promise/constants.ts
|
|
2704
2764
|
const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
2705
2765
|
const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
2706
|
-
const PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY = "promises expected at least one promise-function or promise in the array or object";
|
|
2707
2766
|
const PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE = "promises expected an array or object holding promise-functions or promises";
|
|
2708
2767
|
const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
2709
2768
|
const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
@@ -2808,22 +2867,6 @@ function isType(value, type) {
|
|
|
2808
2867
|
return isPlainObject(value) && "status" in value && value.status === type;
|
|
2809
2868
|
}
|
|
2810
2869
|
//#endregion
|
|
2811
|
-
//#region src/result/misc.ts
|
|
2812
|
-
/**
|
|
2813
|
-
* Converts a _Result_ to a _Promise_
|
|
2814
|
-
*
|
|
2815
|
-
* Resolves if ok, rejects for error
|
|
2816
|
-
*
|
|
2817
|
-
* @param result _Result_ to convert
|
|
2818
|
-
* @returns Promised result
|
|
2819
|
-
*/
|
|
2820
|
-
async function toPromise(result) {
|
|
2821
|
-
const actual = typeof result === "function" ? result() : result;
|
|
2822
|
-
if (!isResult(actual)) return Promise.reject(new Error(RESULT_MESSAGE_PROMISE));
|
|
2823
|
-
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
2824
|
-
}
|
|
2825
|
-
const RESULT_MESSAGE_PROMISE = "toPromise expected to receive a Result";
|
|
2826
|
-
//#endregion
|
|
2827
2870
|
//#region src/promise/misc.ts
|
|
2828
2871
|
/**
|
|
2829
2872
|
* Create a cancelable _Promise_
|
|
@@ -2914,13 +2957,13 @@ async function attemptPromise(value, options) {
|
|
|
2914
2957
|
rejector(signal?.reason);
|
|
2915
2958
|
});
|
|
2916
2959
|
async function handler(resolve, reject) {
|
|
2917
|
-
|
|
2960
|
+
const result = await asyncAttempt(async () => {
|
|
2918
2961
|
let result = isFunction ? value() : await value;
|
|
2919
2962
|
if (result instanceof Promise) result = await result;
|
|
2920
|
-
|
|
2921
|
-
}
|
|
2922
|
-
|
|
2923
|
-
|
|
2963
|
+
return result;
|
|
2964
|
+
});
|
|
2965
|
+
if (result.ok) settlePromise(resolve, result.value, aborter);
|
|
2966
|
+
else settlePromise(reject, result.error, aborter);
|
|
2924
2967
|
}
|
|
2925
2968
|
let rejector;
|
|
2926
2969
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -2935,12 +2978,11 @@ async function promises(items, options) {
|
|
|
2935
2978
|
if (!isArrayOrPlainObject(items)) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
|
|
2936
2979
|
const isArray = Array.isArray(items);
|
|
2937
2980
|
const actual = (isArray ? items.map((item, index) => [index, item]) : Object.entries(items)).map(([key, value]) => [key, typeof value === "function" ? value() : value]).filter(([, value]) => value instanceof Promise);
|
|
2938
|
-
const { length } = actual;
|
|
2939
|
-
if (length === 0) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY);
|
|
2940
|
-
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
2941
2981
|
const aborter = createAborter(signal, () => {
|
|
2942
2982
|
handlers.reject(signal?.reason);
|
|
2943
2983
|
});
|
|
2984
|
+
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
2985
|
+
const { length } = actual;
|
|
2944
2986
|
const data = {
|
|
2945
2987
|
last: length - 1,
|
|
2946
2988
|
result: isArray ? [] : {}
|
|
@@ -2976,27 +3018,6 @@ async function resultPromises(items, signal) {
|
|
|
2976
3018
|
}
|
|
2977
3019
|
promises.result = resultPromises;
|
|
2978
3020
|
//#endregion
|
|
2979
|
-
//#region src/result/index.ts
|
|
2980
|
-
async function asyncAttempt(value, err) {
|
|
2981
|
-
try {
|
|
2982
|
-
let result;
|
|
2983
|
-
if (typeof value === "function") result = value();
|
|
2984
|
-
else result = value;
|
|
2985
|
-
if (result instanceof Promise) result = await result;
|
|
2986
|
-
return ok(result);
|
|
2987
|
-
} catch (thrown) {
|
|
2988
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
2989
|
-
}
|
|
2990
|
-
}
|
|
2991
|
-
function attempt(callback, err) {
|
|
2992
|
-
try {
|
|
2993
|
-
return ok(callback());
|
|
2994
|
-
} catch (thrown) {
|
|
2995
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
2996
|
-
}
|
|
2997
|
-
}
|
|
2998
|
-
attempt.async = asyncAttempt;
|
|
2999
|
-
//#endregion
|
|
3000
3021
|
//#region src/result/match.ts
|
|
3001
3022
|
async function asyncMatchResult(result, first, error) {
|
|
3002
3023
|
let value;
|
|
@@ -3136,7 +3157,7 @@ function createFuzzyItems(items) {
|
|
|
3136
3157
|
function createFuzzyOptions(input, state) {
|
|
3137
3158
|
const options = isPlainObject(input) ? input : {};
|
|
3138
3159
|
const limit = typeof input === "number" ? input : options.limit;
|
|
3139
|
-
if (typeof limit === "number" && !Number.isNaN(limit) && limit >= 1) options.limit =
|
|
3160
|
+
if (typeof limit === "number" && !Number.isNaN(limit) && limit >= 1) options.limit = floor(limit);
|
|
3140
3161
|
else options.limit = state?.limit;
|
|
3141
3162
|
options.tolerance = getTolerance(options.tolerance, state);
|
|
3142
3163
|
return options;
|
|
@@ -3201,18 +3222,18 @@ function getScore(haystack, needle) {
|
|
|
3201
3222
|
if (haystackIndex === 0) score += 1;
|
|
3202
3223
|
if (previousMatchIndex !== -1) {
|
|
3203
3224
|
const gap = haystackIndex - previousMatchIndex - 1;
|
|
3204
|
-
score +=
|
|
3225
|
+
score += max([0, FUZZY_PROXIMITY_THRESHOLD - gap]);
|
|
3205
3226
|
}
|
|
3206
3227
|
previousMatchIndex = haystackIndex;
|
|
3207
3228
|
needleIndex += 1;
|
|
3208
3229
|
}
|
|
3209
3230
|
if (needleIndex === needleLength) break;
|
|
3210
3231
|
}
|
|
3211
|
-
score -=
|
|
3212
|
-
return
|
|
3232
|
+
score -= floor(lowerCaseHaystack.length / FUZZY_LENGTH_DIVISOR);
|
|
3233
|
+
return max([0, score]);
|
|
3213
3234
|
}
|
|
3214
3235
|
function getTolerance(input, state) {
|
|
3215
|
-
if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 :
|
|
3236
|
+
if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 : floor(input);
|
|
3216
3237
|
return state?.tolerance ?? FUZZY_PROXIMITY_THRESHOLD;
|
|
3217
3238
|
}
|
|
3218
3239
|
/**
|
|
@@ -3317,7 +3338,7 @@ function dedent(value, ...values) {
|
|
|
3317
3338
|
if (indentation != null) lengths.push(indentation.length);
|
|
3318
3339
|
}
|
|
3319
3340
|
if (lengths.length === 0) return actual.trim();
|
|
3320
|
-
const minimum =
|
|
3341
|
+
const minimum = min(lengths);
|
|
3321
3342
|
const pattern = new RegExp(`^\\s{0,${minimum}}`);
|
|
3322
3343
|
let result = "";
|
|
3323
3344
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -3711,14 +3732,9 @@ function registerCloner(constructor, handler) {
|
|
|
3711
3732
|
}
|
|
3712
3733
|
function tryStructuredClone(value, parameters, depth) {
|
|
3713
3734
|
if (depth >= CLONE_MAX_DEPTH) return value;
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
return cloned;
|
|
3718
|
-
} catch {
|
|
3719
|
-
parameters.references.set(value, value);
|
|
3720
|
-
return value;
|
|
3721
|
-
}
|
|
3735
|
+
const cloned = unwrap(attempt(() => structuredClone(value)), value);
|
|
3736
|
+
parameters.references.set(value, cloned);
|
|
3737
|
+
return cloned;
|
|
3722
3738
|
}
|
|
3723
3739
|
const CLONE_COPY_OPTIONS = {
|
|
3724
3740
|
copyFunctions: true,
|
|
@@ -3841,8 +3857,8 @@ function getChanges(changes, first, second, options, prefix) {
|
|
|
3841
3857
|
function getDiffs(first, second, options, prefix) {
|
|
3842
3858
|
const changes = [];
|
|
3843
3859
|
if (Array.isArray(first) && Array.isArray(second)) {
|
|
3844
|
-
const maximumLength =
|
|
3845
|
-
const minimumLength =
|
|
3860
|
+
const maximumLength = max([first.length, second.length]);
|
|
3861
|
+
const minimumLength = min([first.length, second.length]);
|
|
3846
3862
|
for (let index = minimumLength; index < maximumLength; index += 1) {
|
|
3847
3863
|
const key = join([prefix, index], ".");
|
|
3848
3864
|
changes.push({
|
|
@@ -4868,24 +4884,6 @@ const colorSubscription = {
|
|
|
4868
4884
|
value: SUBSCRIPTION_NAME
|
|
4869
4885
|
};
|
|
4870
4886
|
//#endregion
|
|
4871
|
-
//#region src/internal/math/misc.ts
|
|
4872
|
-
/**
|
|
4873
|
-
* Round a number
|
|
4874
|
-
*
|
|
4875
|
-
* @param value Number to round
|
|
4876
|
-
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
4877
|
-
* @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
|
|
4878
|
-
*/
|
|
4879
|
-
function round(value, decimals) {
|
|
4880
|
-
return roundNumber(Math.round, value, decimals);
|
|
4881
|
-
}
|
|
4882
|
-
function roundNumber(callback, value, decimals) {
|
|
4883
|
-
if (typeof value !== "number") return NaN;
|
|
4884
|
-
if (typeof decimals !== "number" || decimals < 1) return callback(value);
|
|
4885
|
-
const mod = 10 ** decimals;
|
|
4886
|
-
return callback((value + Number.EPSILON) * mod) / mod;
|
|
4887
|
-
}
|
|
4888
|
-
//#endregion
|
|
4889
4887
|
//#region src/color/misc/alpha.ts
|
|
4890
4888
|
function getAlpha(value, hex) {
|
|
4891
4889
|
if (typeof value === "number") return getAlphaFromValue(value);
|
|
@@ -5211,12 +5209,12 @@ function convertRgbToHex(rgb, alpha) {
|
|
|
5211
5209
|
}
|
|
5212
5210
|
function convertRgbToHsla(value) {
|
|
5213
5211
|
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
5214
|
-
const { delta,
|
|
5215
|
-
const lightness = (
|
|
5212
|
+
const { delta, maximum, minimum } = values;
|
|
5213
|
+
const lightness = (minimum + maximum) / 2;
|
|
5216
5214
|
let hue = 0;
|
|
5217
5215
|
let saturation = 0;
|
|
5218
5216
|
if (delta !== 0) {
|
|
5219
|
-
saturation = (
|
|
5217
|
+
saturation = (maximum - lightness) / min([lightness, 1 - lightness]);
|
|
5220
5218
|
hue = getRgbHue(values);
|
|
5221
5219
|
hue *= 60;
|
|
5222
5220
|
}
|
|
@@ -5229,7 +5227,7 @@ function convertRgbToHsla(value) {
|
|
|
5229
5227
|
}
|
|
5230
5228
|
function convertRgbToHwba(value) {
|
|
5231
5229
|
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
5232
|
-
const { delta,
|
|
5230
|
+
const { delta, maximum, minimum } = values;
|
|
5233
5231
|
let hue = 0;
|
|
5234
5232
|
if (delta !== 0) {
|
|
5235
5233
|
hue = getRgbHue(values);
|
|
@@ -5237,8 +5235,8 @@ function convertRgbToHwba(value) {
|
|
|
5237
5235
|
}
|
|
5238
5236
|
return {
|
|
5239
5237
|
hue,
|
|
5240
|
-
whiteness:
|
|
5241
|
-
blackness: (1 -
|
|
5238
|
+
whiteness: minimum * COLOR_MAX.percent,
|
|
5239
|
+
blackness: (1 - maximum) * COLOR_MAX.percent,
|
|
5242
5240
|
alpha: getAlphaValue(value?.alpha ?? COLOR_MAX.percent)
|
|
5243
5241
|
};
|
|
5244
5242
|
}
|
|
@@ -5258,14 +5256,22 @@ function getRgbCalculationValues(rgb) {
|
|
|
5258
5256
|
const blue = rgb.blue / COLOR_MAX.hex;
|
|
5259
5257
|
const green = rgb.green / COLOR_MAX.hex;
|
|
5260
5258
|
const red = rgb.red / COLOR_MAX.hex;
|
|
5261
|
-
const
|
|
5262
|
-
|
|
5259
|
+
const maximum = max([
|
|
5260
|
+
blue,
|
|
5261
|
+
green,
|
|
5262
|
+
red
|
|
5263
|
+
]);
|
|
5264
|
+
const minimum = min([
|
|
5265
|
+
blue,
|
|
5266
|
+
green,
|
|
5267
|
+
red
|
|
5268
|
+
]);
|
|
5263
5269
|
return {
|
|
5264
5270
|
blue,
|
|
5265
|
-
delta:
|
|
5271
|
+
delta: maximum - minimum,
|
|
5266
5272
|
green,
|
|
5267
|
-
|
|
5268
|
-
|
|
5273
|
+
maximum,
|
|
5274
|
+
minimum,
|
|
5269
5275
|
red
|
|
5270
5276
|
};
|
|
5271
5277
|
}
|
|
@@ -5276,8 +5282,8 @@ function getRgbaColor$1() {
|
|
|
5276
5282
|
return getStateValue(this, COLOR_TYPE.rgb, true);
|
|
5277
5283
|
}
|
|
5278
5284
|
function getRgbHue(values) {
|
|
5279
|
-
const { blue, delta, green,
|
|
5280
|
-
switch (
|
|
5285
|
+
const { blue, delta, green, maximum, red } = values;
|
|
5286
|
+
switch (maximum) {
|
|
5281
5287
|
case blue: return (red - green) / delta + 4;
|
|
5282
5288
|
case green: return (blue - red) / delta + 2;
|
|
5283
5289
|
default: return (green - blue) / delta + (green < blue ? 6 : 0);
|
|
@@ -5526,7 +5532,7 @@ function convertHslToHwba(input) {
|
|
|
5526
5532
|
let { hue, lightness, saturation } = isHslLike(input) ? getHslValues(input) : { ...COLOR_DEFAULTS.hsl };
|
|
5527
5533
|
lightness /= COLOR_MAX.percent;
|
|
5528
5534
|
saturation /= COLOR_MAX.percent;
|
|
5529
|
-
const value = lightness + saturation *
|
|
5535
|
+
const value = lightness + saturation * min([lightness, 1 - lightness]);
|
|
5530
5536
|
if (value === 0) saturation = 0;
|
|
5531
5537
|
else saturation = 2 * (1 - lightness / value);
|
|
5532
5538
|
const blackness = 1 - value;
|
|
@@ -5563,7 +5569,11 @@ function getColorFromHsl(state, type) {
|
|
|
5563
5569
|
}
|
|
5564
5570
|
function getHexyValue(hue, lightness, saturation, value) {
|
|
5565
5571
|
const part = (value + hue / 30) % 12;
|
|
5566
|
-
return (lightness - saturation *
|
|
5572
|
+
return (lightness - saturation * min([lightness, 1 - lightness]) * max([-1, min([
|
|
5573
|
+
part - 3,
|
|
5574
|
+
9 - part,
|
|
5575
|
+
1
|
|
5576
|
+
])])) * COLOR_MAX.hex;
|
|
5567
5577
|
}
|
|
5568
5578
|
function getHslValue() {
|
|
5569
5579
|
return getStateValue(this, COLOR_TYPE.hsl, false);
|
|
@@ -5696,7 +5706,7 @@ function getColorFromHwb(state, type) {
|
|
|
5696
5706
|
function getHwbSaturation(blackness, whiteness, lightness) {
|
|
5697
5707
|
const hue = 1 - blackness - whiteness;
|
|
5698
5708
|
if (hue <= 0) return 0;
|
|
5699
|
-
return hue /
|
|
5709
|
+
return hue / min([2 * lightness, 2 - 2 * lightness]);
|
|
5700
5710
|
}
|
|
5701
5711
|
function getHwbValue() {
|
|
5702
5712
|
return getStateValue(this, COLOR_TYPE.hwb, false);
|
|
@@ -6235,16 +6245,6 @@ function count(array, key, value) {
|
|
|
6235
6245
|
}
|
|
6236
6246
|
return counted;
|
|
6237
6247
|
}
|
|
6238
|
-
/**
|
|
6239
|
-
* Round a number down
|
|
6240
|
-
*
|
|
6241
|
-
* @param value Number to round down
|
|
6242
|
-
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
6243
|
-
* @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
|
|
6244
|
-
*/
|
|
6245
|
-
function floor(value, decimals) {
|
|
6246
|
-
return roundNumber(Math.floor, value, decimals);
|
|
6247
|
-
}
|
|
6248
6248
|
function median(array, key) {
|
|
6249
6249
|
let length = Array.isArray(array) ? array.length : 0;
|
|
6250
6250
|
if (!Array.isArray(array) || length === 0) return NaN;
|
|
@@ -6259,10 +6259,7 @@ function median(array, key) {
|
|
|
6259
6259
|
const second = length / 2;
|
|
6260
6260
|
return (numbers[first] + numbers[second]) / 2;
|
|
6261
6261
|
}
|
|
6262
|
-
return numbers[
|
|
6263
|
-
}
|
|
6264
|
-
function min(array, key, first) {
|
|
6265
|
-
return getAggregated("min", array, key, first);
|
|
6262
|
+
return numbers[floor(length / 2)];
|
|
6266
6263
|
}
|
|
6267
6264
|
function sum(array, key) {
|
|
6268
6265
|
return getAggregated("sum", array, key);
|
|
@@ -6683,7 +6680,7 @@ function resumeQueue() {
|
|
|
6683
6680
|
for (let index = 0; index < length; index += 1) handled[index]();
|
|
6684
6681
|
}
|
|
6685
6682
|
state.paused = false;
|
|
6686
|
-
const length =
|
|
6683
|
+
const length = min([state.options.concurrency, state.items.length]);
|
|
6687
6684
|
for (let index = 0; index < length; index += 1) run(state);
|
|
6688
6685
|
}
|
|
6689
6686
|
function resumeQueues(key) {
|
|
@@ -6696,14 +6693,17 @@ async function run(state) {
|
|
|
6696
6693
|
while (item != null) {
|
|
6697
6694
|
let error = false;
|
|
6698
6695
|
let result;
|
|
6699
|
-
|
|
6700
|
-
if (!(item.aborter?.signal?.aborted ?? false)) {
|
|
6696
|
+
const attempted = await asyncAttempt(async () => {
|
|
6697
|
+
if (item != null && !(item.aborter?.signal?.aborted ?? false)) {
|
|
6701
6698
|
const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
|
|
6702
|
-
|
|
6699
|
+
return await state.callback(...parameters);
|
|
6703
6700
|
}
|
|
6704
|
-
|
|
6701
|
+
throw new Error();
|
|
6702
|
+
});
|
|
6703
|
+
if (attempted.ok) result = attempted.value;
|
|
6704
|
+
else {
|
|
6705
6705
|
error = true;
|
|
6706
|
-
result =
|
|
6706
|
+
result = attempted.error;
|
|
6707
6707
|
}
|
|
6708
6708
|
if (state.paused) {
|
|
6709
6709
|
const paused = item;
|