@oscarpalmer/atoms 0.217.0 → 0.218.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/insert.mjs +1 -1
- package/dist/array/slice.mjs +27 -14
- package/dist/array/sort.mjs +2 -1
- package/dist/color/misc/get.mjs +3 -1
- package/dist/color/space/hex.mjs +1 -1
- package/dist/function/retry.d.mts +1 -1
- package/dist/function/retry.mjs +5 -2
- package/dist/index.d.mts +163 -154
- package/dist/index.mjs +227 -145
- package/dist/internal/array/find.mjs +57 -34
- package/dist/internal/array/insert.mjs +1 -1
- package/dist/internal/array/match.mjs +21 -17
- package/dist/internal/array/sets.mjs +7 -1
- package/dist/internal/math/aggregate.mjs +4 -1
- package/dist/internal/number.d.mts +1 -1
- package/dist/internal/number.mjs +4 -2
- package/dist/internal/plan/plan.generate.mjs +3 -3
- package/dist/internal/random.d.mts +6 -0
- package/dist/internal/random.mjs +13 -2
- package/dist/internal/result/attempt.mjs +4 -4
- package/dist/internal/string/misc.mjs +2 -1
- package/dist/internal/subscription.mjs +1 -1
- package/dist/internal/value/equal.mjs +2 -1
- package/dist/internal/value/partial.mjs +3 -1
- package/dist/models/plan.model.d.mts +2 -0
- package/dist/models/plan.model.mjs +3 -1
- package/dist/models/string/string.fuzzy.model.d.mts +1 -1
- package/dist/models/string/string.misc.model.mjs +1 -1
- package/dist/models/value/value.merge.model.d.mts +1 -1
- package/dist/plan/index.d.mts +1 -4
- package/dist/plan/index.mjs +2 -3
- package/dist/plan/use.mjs +4 -4
- package/dist/promise/index.d.mts +6 -4
- package/dist/promise/index.mjs +4 -4
- package/dist/promise/misc.mjs +5 -2
- package/dist/promise/timed.mjs +2 -2
- package/dist/queue.mjs +4 -5
- package/dist/random.d.mts +2 -2
- package/dist/random.mjs +3 -3
- package/dist/result/match.mjs +2 -2
- package/dist/result/misc.mjs +3 -10
- package/dist/result/work/pipe.d.mts +24 -22
- package/dist/result/work/pipe.mjs +7 -3
- package/dist/string/fuzzy.mjs +1 -1
- package/dist/string/index.mjs +1 -1
- package/dist/value/clone.mjs +1 -1
- package/dist/value/merge.mjs +17 -4
- package/package.json +1 -1
|
@@ -9,15 +9,47 @@ function createFindParameters(original) {
|
|
|
9
9
|
value: length === 1 && typeof original[0] !== "function" ? original[0] : original[1]
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
+
function findComplexValues(parameters) {
|
|
13
|
+
const { array, keyCallback, length, mapAfter, mapBefore, result, type, value } = parameters;
|
|
14
|
+
const keys = /* @__PURE__ */ new Set();
|
|
15
|
+
for (let index = 0; index < length; index += 1) {
|
|
16
|
+
const item = array[index];
|
|
17
|
+
let keyed;
|
|
18
|
+
let transformed;
|
|
19
|
+
if (mapBefore == null) {
|
|
20
|
+
keyed = keyCallback?.(item, index, array) ?? item;
|
|
21
|
+
transformed = item;
|
|
22
|
+
} else {
|
|
23
|
+
transformed = mapBefore(item, index, array);
|
|
24
|
+
keyed = keyCallback?.(transformed, index, array) ?? transformed;
|
|
25
|
+
}
|
|
26
|
+
if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
|
|
27
|
+
keys.add(keyed);
|
|
28
|
+
result.matched.push(mapAfter?.(item, index, array) ?? transformed);
|
|
29
|
+
} else result.notMatched.push(item);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function findSimpleValue(array, value, findIndex, reversed) {
|
|
34
|
+
if (findIndex) return reversed ? array.lastIndexOf(value) : array.indexOf(value);
|
|
35
|
+
return reversed ? array.findLast((item) => Object.is(item, value)) : array.find((item) => Object.is(item, value));
|
|
36
|
+
}
|
|
37
|
+
function findSimpleValues(array, length, value, boolCallback, mapBefore, mapAfter, result) {
|
|
38
|
+
const callback = boolCallback ?? ((item) => Object.is(item, value));
|
|
39
|
+
for (let index = 0; index < length; index += 1) {
|
|
40
|
+
const item = array[index];
|
|
41
|
+
const transformed = mapBefore?.(item, index, array) ?? item;
|
|
42
|
+
if (callback(transformed, index, array)) result.matched.push(mapAfter?.(item, index, array) ?? transformed);
|
|
43
|
+
else result.notMatched.push(item);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
12
47
|
function findValue(type, array, parameters, reversed) {
|
|
13
48
|
const findIndex = type === ARRAY_FIND_VALUE_INDEX;
|
|
14
49
|
if (!Array.isArray(array) || array.length === 0) return findIndex ? -1 : void 0;
|
|
15
50
|
const { bool, key, value } = createFindParameters(parameters);
|
|
16
51
|
const callbacks = getArrayCallbacks(bool, key);
|
|
17
|
-
if (callbacks?.bool == null && callbacks?.keyed == null)
|
|
18
|
-
if (findIndex) return reversed ? array.lastIndexOf(value) : array.indexOf(value);
|
|
19
|
-
return reversed ? array.findLast((item) => Object.is(item, value)) : array.find((item) => Object.is(item, value));
|
|
20
|
-
}
|
|
52
|
+
if (callbacks?.bool == null && callbacks?.keyed == null) return findSimpleValue(array, value, findIndex, reversed);
|
|
21
53
|
if (callbacks.bool != null) {
|
|
22
54
|
const index = reversed ? array.findLastIndex(callbacks.bool) : array.findIndex(callbacks.bool);
|
|
23
55
|
return findIndex ? index : array[index];
|
|
@@ -38,7 +70,8 @@ function findAbsoluteValueOrDefault(array, parameters, defaultValue, useDefaultV
|
|
|
38
70
|
return useDefaultValue ? defaultValue : void 0;
|
|
39
71
|
}
|
|
40
72
|
const index = findValue(ARRAY_FIND_VALUE_INDEX, array, parameters, reversed);
|
|
41
|
-
|
|
73
|
+
if (index === -1) return useDefaultValue ? defaultValue : void 0;
|
|
74
|
+
return array[index];
|
|
42
75
|
}
|
|
43
76
|
function findValues(type, array, parameters, mapper) {
|
|
44
77
|
const result = {
|
|
@@ -55,36 +88,26 @@ function findValues(type, array, parameters, mapper) {
|
|
|
55
88
|
}
|
|
56
89
|
const mapCallback = getArrayCallback(mapper?.callback);
|
|
57
90
|
const mapReverse = mapper?.reverse ?? false;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
else result.notMatched.push(item);
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
91
|
+
let mapAfter;
|
|
92
|
+
let mapBefore;
|
|
93
|
+
if (mapCallback == null) {
|
|
94
|
+
mapAfter = void 0;
|
|
95
|
+
mapBefore = void 0;
|
|
96
|
+
} else {
|
|
97
|
+
mapAfter = mapReverse ? void 0 : mapCallback;
|
|
98
|
+
mapBefore = mapReverse ? mapCallback : void 0;
|
|
69
99
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
|
|
83
|
-
keys.add(keyed);
|
|
84
|
-
result.matched.push(mapAfter?.(item, index, array) ?? transformed);
|
|
85
|
-
} else result.notMatched.push(item);
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
100
|
+
if (callbacks?.bool != null || type === "all" && key == null) return findSimpleValues(array, length, value, callbacks?.bool, mapBefore, mapAfter, result);
|
|
101
|
+
return findComplexValues({
|
|
102
|
+
array,
|
|
103
|
+
length,
|
|
104
|
+
mapAfter,
|
|
105
|
+
mapBefore,
|
|
106
|
+
result,
|
|
107
|
+
type,
|
|
108
|
+
value,
|
|
109
|
+
keyCallback: callbacks?.keyed
|
|
110
|
+
});
|
|
88
111
|
}
|
|
89
112
|
//#endregion
|
|
90
113
|
export { createFindParameters, findAbsoluteValueOrDefault, findValue, findValues };
|
|
@@ -3,7 +3,7 @@ import { chunk } from "./chunk.mjs";
|
|
|
3
3
|
import { max, min } from "../math/aggregate.mjs";
|
|
4
4
|
//#region src/internal/array/insert.ts
|
|
5
5
|
function insertChunkedValues(type, array, items, start, deleteCount) {
|
|
6
|
-
const actualDeleteCount = deleteCount
|
|
6
|
+
const actualDeleteCount = max([deleteCount, 0]);
|
|
7
7
|
const actualStart = min([max([0, start]), array.length]);
|
|
8
8
|
const chunked = chunk(items);
|
|
9
9
|
const lastIndex = chunked.length - 1;
|
|
@@ -4,6 +4,21 @@ import { ARRAY_MATCH_COMPARISON_INSIDE, ARRAY_MATCH_COMPARISON_SAME, ARRAY_MATCH
|
|
|
4
4
|
function endsWithArray(haystack, needle, key) {
|
|
5
5
|
return arrayMatchEndings.has(getPosition(haystack, needle, key)[1]);
|
|
6
6
|
}
|
|
7
|
+
function evaluatePosition(haystack, haystackLength, needle, needleLength, key) {
|
|
8
|
+
const callback = getArrayCallback(key);
|
|
9
|
+
const limit = haystackLength - needleLength + 1;
|
|
10
|
+
let needleValues = callback == null ? needle : needle.slice();
|
|
11
|
+
if (callback != null) for (let needleIndex = 0; needleIndex < needleLength; needleIndex += 1) needleValues[needleIndex] = callback(needle[needleIndex], needleIndex, needle);
|
|
12
|
+
for (let haystackIndex = 0; haystackIndex < limit; haystackIndex += 1) {
|
|
13
|
+
if (!inHaystack(haystack, haystackIndex, needleValues, 0, callback)) continue;
|
|
14
|
+
if (needleLength === 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
|
|
15
|
+
for (let needleIndex = 1; needleIndex < needleLength; needleIndex += 1) {
|
|
16
|
+
if (!inHaystack(haystack, haystackIndex, needleValues, needleIndex, callback)) break;
|
|
17
|
+
if (needleIndex === needleLength - 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return arrayMatchOutside;
|
|
21
|
+
}
|
|
7
22
|
function getArrayComparison(haystack, needle, key) {
|
|
8
23
|
return getPosition(haystack, needle, key)[1];
|
|
9
24
|
}
|
|
@@ -17,23 +32,12 @@ function getPosition(haystack, needle, key) {
|
|
|
17
32
|
const needleLength = needle.length;
|
|
18
33
|
if (haystackLength === 0 || needleLength === 0) return arrayMatchOutside;
|
|
19
34
|
if (needleLength > haystackLength) return arrayMatchOutside;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
let haystackValue = callback?.(haystackItem, haystackIndex, haystack) ?? haystackItem;
|
|
27
|
-
if (!Object.is(haystackValue, needleValues[0])) continue;
|
|
28
|
-
if (needleLength === 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
|
|
29
|
-
for (let needleIndex = 1; needleIndex < needleLength; needleIndex += 1) {
|
|
30
|
-
haystackItem = haystack[haystackIndex + needleIndex];
|
|
31
|
-
haystackValue = callback?.(haystackItem, haystackIndex + needleIndex, haystack) ?? haystackItem;
|
|
32
|
-
if (!Object.is(haystackValue, needleValues[needleIndex])) break;
|
|
33
|
-
if (needleIndex === needleLength - 1) return [haystackIndex, getName(haystackIndex, haystackLength, needleLength)];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return arrayMatchOutside;
|
|
35
|
+
return evaluatePosition(haystack, haystackLength, needle, needleLength, key);
|
|
36
|
+
}
|
|
37
|
+
function inHaystack(haystack, haystackIndex, needleValues, needleIndex, callback) {
|
|
38
|
+
const haystackItem = haystack[haystackIndex + needleIndex];
|
|
39
|
+
const haystackValue = callback?.(haystackItem, haystackIndex + needleIndex, haystack) ?? haystackItem;
|
|
40
|
+
return Object.is(haystackValue, needleValues[needleIndex]);
|
|
37
41
|
}
|
|
38
42
|
function includesArray(haystack, needle, key) {
|
|
39
43
|
return !arrayMatchOutsides.has(getPosition(haystack, needle, key)[1]);
|
|
@@ -6,8 +6,14 @@ function compareSets(type, first, second, key) {
|
|
|
6
6
|
const isDifference = type === ARRAY_SETS_COMPARE_DIFFERENCE;
|
|
7
7
|
const isIntersection = type === ARRAY_SETS_COMPARE_INTERSECTION;
|
|
8
8
|
const isUnion = type === ARRAY_SETS_COMPARE_UNION;
|
|
9
|
-
if (first.length === 0)
|
|
9
|
+
if (first.length === 0) {
|
|
10
|
+
if (isDifference) return first.slice();
|
|
11
|
+
return isIntersection ? [] : second.slice();
|
|
12
|
+
}
|
|
10
13
|
if (!Array.isArray(second) || second.length === 0) return isIntersection ? [] : first.slice();
|
|
14
|
+
return getComparison(first, second, isIntersection, isUnion, key);
|
|
15
|
+
}
|
|
16
|
+
function getComparison(first, second, isIntersection, isUnion, key) {
|
|
11
17
|
const callback = getArrayCallback(key);
|
|
12
18
|
const values = isUnion ? first : second;
|
|
13
19
|
let { length } = values;
|
|
@@ -44,7 +44,10 @@ function getAggregateCallback(key) {
|
|
|
44
44
|
}
|
|
45
45
|
function getAggregated(type, array, key, first) {
|
|
46
46
|
const aggregation = aggregate(type, array, key, first);
|
|
47
|
-
if (aggregation.count === 0)
|
|
47
|
+
if (aggregation.count === 0) {
|
|
48
|
+
if (aggregation.array) return aggregation.first ? void 0 : [];
|
|
49
|
+
return NaN;
|
|
50
|
+
}
|
|
48
51
|
if (aggregation.array) {
|
|
49
52
|
const array = aggregation.items[aggregation.value];
|
|
50
53
|
return aggregation.first ? array[0] : array;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @param value Value to check
|
|
6
6
|
* @param minimum Minimum value
|
|
7
|
-
* @param maximum Maximum value
|
|
7
|
+
* @param maximum Maximum value _(inclusive)_
|
|
8
8
|
* @returns `true` if the value is between the minimum and maximum, otherwise `false`
|
|
9
9
|
*/
|
|
10
10
|
export declare function between(value: number, minimum: number, maximum: number): boolean;
|
package/dist/internal/number.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isNumber } from "./is.mjs";
|
|
2
|
+
import { max, min } from "./math/aggregate.mjs";
|
|
2
3
|
import { EXPRESSION_NUMBER_BINARY, EXPRESSION_NUMBER_OCTAL, EXPRESSION_NUMBER_ZEROISH } from "../models/misc.model.mjs";
|
|
3
4
|
//#region src/internal/number.ts
|
|
4
5
|
/**
|
|
@@ -6,7 +7,7 @@ import { EXPRESSION_NUMBER_BINARY, EXPRESSION_NUMBER_OCTAL, EXPRESSION_NUMBER_ZE
|
|
|
6
7
|
*
|
|
7
8
|
* @param value Value to check
|
|
8
9
|
* @param minimum Minimum value
|
|
9
|
-
* @param maximum Maximum value
|
|
10
|
+
* @param maximum Maximum value _(inclusive)_
|
|
10
11
|
* @returns `true` if the value is between the minimum and maximum, otherwise `false`
|
|
11
12
|
*/
|
|
12
13
|
function between(value, minimum, maximum) {
|
|
@@ -16,7 +17,8 @@ function between(value, minimum, maximum) {
|
|
|
16
17
|
maximum
|
|
17
18
|
].every(isNumber)) return false;
|
|
18
19
|
if (Object.is(minimum, maximum)) return Object.is(value, minimum);
|
|
19
|
-
|
|
20
|
+
const maximumValue = max([maximum, minimum]);
|
|
21
|
+
return value >= min([maximum, minimum]) && value <= maximumValue;
|
|
20
22
|
}
|
|
21
23
|
/**
|
|
22
24
|
* Clamp a number between a minimum and maximum value
|
|
@@ -4,7 +4,7 @@ import { isError, isOk } from "../result/misc.mjs";
|
|
|
4
4
|
//#region src/internal/plan/plan.generate.ts
|
|
5
5
|
async function asyncGenerate(input, unwrap, first, second) {
|
|
6
6
|
const aborter = createAborter(second ?? first, noop);
|
|
7
|
-
if (aborter
|
|
7
|
+
if (aborter?.signal.aborted) throw aborter.signal.reason;
|
|
8
8
|
const parameters = Array.isArray(first) ? first : [];
|
|
9
9
|
let generator;
|
|
10
10
|
if (typeof input === "function") generator = input(...parameters);
|
|
@@ -13,7 +13,7 @@ async function asyncGenerate(input, unwrap, first, second) {
|
|
|
13
13
|
let lastValue;
|
|
14
14
|
try {
|
|
15
15
|
while (true) {
|
|
16
|
-
if (aborter
|
|
16
|
+
if (aborter?.signal.aborted) await generator.throw(aborter.signal.reason);
|
|
17
17
|
const next = await generator.next(lastValue);
|
|
18
18
|
const done = next.done === true;
|
|
19
19
|
const { value } = next;
|
|
@@ -29,7 +29,7 @@ async function asyncGenerate(input, unwrap, first, second) {
|
|
|
29
29
|
aborter?.cancel();
|
|
30
30
|
await generator.return(lastValue);
|
|
31
31
|
}
|
|
32
|
-
if (aborter
|
|
32
|
+
if (aborter?.signal.aborted) throw aborter.signal.reason;
|
|
33
33
|
if (success) return unwrap && isOk(lastValue) ? lastValue.value : lastValue;
|
|
34
34
|
throw lastValue;
|
|
35
35
|
}
|
package/dist/internal/random.mjs
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { isNumber } from "./is.mjs";
|
|
2
2
|
import { floor } from "./math/misc.mjs";
|
|
3
3
|
//#region src/internal/random.ts
|
|
4
|
+
/**
|
|
5
|
+
* Get a random number between 0 _(inclusive)_ and 1 _(exclusive)_
|
|
6
|
+
*
|
|
7
|
+
* @returns Random number
|
|
8
|
+
*/
|
|
9
|
+
function getRandom() {
|
|
10
|
+
/* istanbul ignore if */
|
|
11
|
+
if (globalThis.crypto == null) return Math.random();
|
|
12
|
+
return globalThis.crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] / RANDOM_DIVISOR;
|
|
13
|
+
}
|
|
4
14
|
function getRandomFloatingNumberValue(inclusive, minimum, maximum) {
|
|
5
15
|
let maxFloat = isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
|
|
6
16
|
let minFloat = isNumber(minimum) && minimum >= Number.MIN_SAFE_INTEGER ? minimum : Number.MIN_SAFE_INTEGER;
|
|
7
17
|
if (minFloat === maxFloat) return minFloat;
|
|
8
18
|
if (minFloat > maxFloat) [minFloat, maxFloat] = [maxFloat, minFloat];
|
|
9
|
-
return
|
|
19
|
+
return getRandom() * (maxFloat + (inclusive ? 1 : 0) - minFloat) + minFloat;
|
|
10
20
|
}
|
|
11
21
|
/**
|
|
12
22
|
* Get a random floating-point number
|
|
@@ -28,5 +38,6 @@ function getRandomFloatingNumber(minimum, maximum) {
|
|
|
28
38
|
function getRandomInteger(minimum, maximum) {
|
|
29
39
|
return floor(getRandomFloatingNumberValue(true, minimum, maximum));
|
|
30
40
|
}
|
|
41
|
+
const RANDOM_DIVISOR = 2 ** 32;
|
|
31
42
|
//#endregion
|
|
32
|
-
export { getRandomFloatingNumber, getRandomInteger };
|
|
43
|
+
export { getRandom, getRandomFloatingNumber, getRandomInteger };
|
|
@@ -7,16 +7,16 @@ async function asyncAttempt(value, err) {
|
|
|
7
7
|
else result = value;
|
|
8
8
|
if (result instanceof Promise) result = await result;
|
|
9
9
|
return ok(result);
|
|
10
|
-
} catch (
|
|
11
|
-
return getError(err ??
|
|
10
|
+
} catch (error) {
|
|
11
|
+
return getError(err ?? error, err == null ? void 0 : error);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
function attempt(callback, err) {
|
|
15
15
|
try {
|
|
16
16
|
const value = callback();
|
|
17
17
|
return ok(value);
|
|
18
|
-
} catch (
|
|
19
|
-
return getError(err ??
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return getError(err ?? error, err == null ? void 0 : error);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
attempt.async = asyncAttempt;
|
|
@@ -75,7 +75,8 @@ function tryEncode(value) {
|
|
|
75
75
|
* @returns Array of words found in the string
|
|
76
76
|
*/
|
|
77
77
|
function words(value) {
|
|
78
|
-
|
|
78
|
+
if (typeof value === "string") return value.match(STRING_EXPRESSION_WORDS) ?? [];
|
|
79
|
+
return [];
|
|
79
80
|
}
|
|
80
81
|
//#endregion
|
|
81
82
|
export { getString, ignoreKey, interpolate, join, tryDecode, tryEncode, words };
|
|
@@ -125,7 +125,7 @@ function createSubscriptionsProperty(input) {
|
|
|
125
125
|
const values = isPlainObject(input) ? input : {};
|
|
126
126
|
return {
|
|
127
127
|
key: typeof values.key === "string" ? values.key : SUBSCRIPTION_PROPERTY,
|
|
128
|
-
value: values.value
|
|
128
|
+
value: values.value ?? "subscription"
|
|
129
129
|
};
|
|
130
130
|
}
|
|
131
131
|
function createSubscriptionsState(property, keys) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isNonPlainObject, isPlainObject, isPrimitive, isTypedArray } from "../is.mjs";
|
|
2
2
|
import { round } from "../math/misc.mjs";
|
|
3
|
+
import { min } from "../math/aggregate.mjs";
|
|
3
4
|
import { createCompareHandler } from "./handlers.mjs";
|
|
4
5
|
import { EQUAL_ERROR_PROPERTIES, EQUAL_EXPRESSION_PROPERTIES, EQUAL_SYMBOL } from "../../models/value/value.equal.model.mjs";
|
|
5
6
|
//#region src/internal/value/equal.ts
|
|
@@ -63,7 +64,7 @@ function equalArray(first, second, options) {
|
|
|
63
64
|
let offset = 0;
|
|
64
65
|
if (length >= 100) {
|
|
65
66
|
offset = round(length / 10);
|
|
66
|
-
offset = offset
|
|
67
|
+
offset = min([offset, 100]);
|
|
67
68
|
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;
|
|
68
69
|
}
|
|
69
70
|
const end = length - offset;
|
|
@@ -2,7 +2,9 @@ import { isPlainObject } from "../is.mjs";
|
|
|
2
2
|
//#region src/internal/value/partial.ts
|
|
3
3
|
function partial(value, providedKeys, omit) {
|
|
4
4
|
if (!isPlainObject(value)) return {};
|
|
5
|
-
|
|
5
|
+
let keys;
|
|
6
|
+
if (omit) keys = Object.keys(value);
|
|
7
|
+
else keys = Array.isArray(providedKeys) ? providedKeys : [];
|
|
6
8
|
const { length } = keys;
|
|
7
9
|
if (length === 0) return omit ? value : {};
|
|
8
10
|
const partials = {};
|
|
@@ -233,6 +233,8 @@ export declare const PLAN_FAILURE_ASYNC: "asyncFailure";
|
|
|
233
233
|
export declare const PLAN_FAILURE_SYNC: "failure";
|
|
234
234
|
export declare const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
|
|
235
235
|
export declare const PLAN_MESSAGE_RUN_INPUT = "run requires a plan or a generator function";
|
|
236
|
+
export declare const PLAN_MESSAGE_USE_NESTED_FN = "Cannot use nested functions with 'use'";
|
|
237
|
+
export declare const PLAN_MESSAGE_USE_SYNC_ASYNC = "Cannot use asynchronous values with 'use'";
|
|
236
238
|
export declare const PLAN_STEP_ASYNC: "asyncStep";
|
|
237
239
|
export declare const PLAN_STEP_SYNC: "step";
|
|
238
240
|
export declare const PLAN_SUCCESS_ASYNC: "asyncSuccess";
|
|
@@ -7,6 +7,8 @@ const PLAN_FAILURE_ASYNC = "asyncFailure";
|
|
|
7
7
|
const PLAN_FAILURE_SYNC = "failure";
|
|
8
8
|
const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
|
|
9
9
|
const PLAN_MESSAGE_RUN_INPUT = "run requires a plan or a generator function";
|
|
10
|
+
const PLAN_MESSAGE_USE_NESTED_FN = "Cannot use nested functions with 'use'";
|
|
11
|
+
const PLAN_MESSAGE_USE_SYNC_ASYNC = "Cannot use asynchronous values with 'use'";
|
|
10
12
|
const PLAN_STEP_ASYNC = "asyncStep";
|
|
11
13
|
const PLAN_STEP_SYNC = "step";
|
|
12
14
|
const PLAN_SUCCESS_ASYNC = "asyncSuccess";
|
|
@@ -15,4 +17,4 @@ const PLAN_SYMBOL = Symbol("plan");
|
|
|
15
17
|
const PLAN_TYPE_ASYNC = "asyncPlan";
|
|
16
18
|
const PLAN_TYPE_SYNC = "plan";
|
|
17
19
|
//#endregion
|
|
18
|
-
export { GENERATOR_FUNCTION_TYPE_ASYNC, GENERATOR_FUNCTION_TYPE_SYNC, GENERATOR_TYPE_ASYNC, GENERATOR_TYPE_SYNC, PLAN_FAILURE_ASYNC, PLAN_FAILURE_SYNC, PLAN_MESSAGE_PLAN_INPUT, PLAN_MESSAGE_RUN_INPUT, PLAN_STEP_ASYNC, PLAN_STEP_SYNC, PLAN_SUCCESS_ASYNC, PLAN_SUCCESS_SYNC, PLAN_SYMBOL, PLAN_TYPE_ASYNC, PLAN_TYPE_SYNC };
|
|
20
|
+
export { GENERATOR_FUNCTION_TYPE_ASYNC, GENERATOR_FUNCTION_TYPE_SYNC, GENERATOR_TYPE_ASYNC, GENERATOR_TYPE_SYNC, PLAN_FAILURE_ASYNC, PLAN_FAILURE_SYNC, PLAN_MESSAGE_PLAN_INPUT, PLAN_MESSAGE_RUN_INPUT, PLAN_MESSAGE_USE_NESTED_FN, PLAN_MESSAGE_USE_SYNC_ASYNC, PLAN_STEP_ASYNC, PLAN_STEP_SYNC, PLAN_SUCCESS_ASYNC, PLAN_SUCCESS_SYNC, PLAN_SYMBOL, PLAN_TYPE_ASYNC, PLAN_TYPE_SYNC };
|
|
@@ -88,7 +88,7 @@ export type FuzzyResult<Item> = {
|
|
|
88
88
|
/**
|
|
89
89
|
* Options for fuzzy searching
|
|
90
90
|
*/
|
|
91
|
-
export type FuzzySearchOptions = FuzzyOptions;
|
|
91
|
+
export type FuzzySearchOptions = { [Key in keyof FuzzyOptions]: FuzzyOptions[Key]; };
|
|
92
92
|
export type FuzzyState<Item> = {
|
|
93
93
|
handler(item: Item): string;
|
|
94
94
|
items: Item[];
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const STRING_DELIMITER_UUID_DEFAULT = "-";
|
|
3
3
|
const STRING_DELIMITER_UUID_HTML = "_";
|
|
4
4
|
const STRING_EXPRESSION_IGNORED = /(^|\.)(__proto__|constructor|prototype)(\.|$)/i;
|
|
5
|
-
const STRING_EXPRESSION_WORDS = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]
|
|
5
|
+
const STRING_EXPRESSION_WORDS = /([^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+)/g;
|
|
6
6
|
const STRING_EXPRESSION_WHITESPACE_PREFIX = /^(\s+)/;
|
|
7
7
|
const STRING_MATCH_ENDS_WITH = "endsWith";
|
|
8
8
|
const STRING_MATCH_INCLUDES = "includes";
|
|
@@ -3,7 +3,7 @@ import { ArrayOrPlainObject, NestedPartial, PlainObject, UnionToIntersection } f
|
|
|
3
3
|
/**
|
|
4
4
|
* Options for assigning values
|
|
5
5
|
*/
|
|
6
|
-
export type AssignOptions = BaseOptions;
|
|
6
|
+
export type AssignOptions = { [Key in keyof BaseOptions]: BaseOptions[Key]; };
|
|
7
7
|
type BaseOptions = {
|
|
8
8
|
/**
|
|
9
9
|
* Key _(or key epxressions)_ for values that should be replaced
|
package/dist/plan/index.d.mts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { AsyncPlan, Plan } from "../models/plan.model.mjs";
|
|
2
|
-
import { asyncAttemptRun, attemptRun } from "./result.mjs";
|
|
3
|
-
import { asyncRun, run } from "./run.mjs";
|
|
4
2
|
//#region src/plan/index.d.ts
|
|
5
3
|
/**
|
|
6
4
|
* Create a plan for an asynchronous generator function
|
|
@@ -16,5 +14,4 @@ export declare function plan<Yielded, Returned, Parameters extends unknown[]>(ge
|
|
|
16
14
|
* @returns Generator plan
|
|
17
15
|
*/
|
|
18
16
|
export declare function plan<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned>): Plan<Yielded, Returned, Parameters>;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { asyncAttemptRun, asyncRun, attemptRun, run };
|
|
17
|
+
//#endregion
|
package/dist/plan/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { PLAN_MESSAGE_PLAN_INPUT, PLAN_SYMBOL, PLAN_TYPE_ASYNC, PLAN_TYPE_SYNC } from "../models/plan.model.mjs";
|
|
2
2
|
import { attemptRunPlan, getGenerator, runAsyncPlan, runPlan } from "../internal/plan/index.mjs";
|
|
3
3
|
import { isAsyncGeneratorFunction, isGeneratorFunction } from "./is.mjs";
|
|
4
|
-
import { asyncAttemptRun
|
|
5
|
-
import { asyncRun, run } from "./run.mjs";
|
|
4
|
+
import { asyncAttemptRun } from "./result.mjs";
|
|
6
5
|
//#region src/plan/index.ts
|
|
7
6
|
function AsyncPlan(generator) {
|
|
8
7
|
this[PLAN_SYMBOL] = {
|
|
@@ -31,4 +30,4 @@ function plan(generator) {
|
|
|
31
30
|
throw new Error(PLAN_MESSAGE_PLAN_INPUT);
|
|
32
31
|
}
|
|
33
32
|
//#endregion
|
|
34
|
-
export {
|
|
33
|
+
export { plan };
|
package/dist/plan/use.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isError, isOk } from "../internal/result/misc.mjs";
|
|
2
|
-
import { PLAN_STEP_ASYNC, PLAN_STEP_SYNC, PLAN_SYMBOL } from "../models/plan.model.mjs";
|
|
2
|
+
import { PLAN_MESSAGE_USE_NESTED_FN, PLAN_MESSAGE_USE_SYNC_ASYNC, PLAN_STEP_ASYNC, PLAN_STEP_SYNC, PLAN_SYMBOL } from "../models/plan.model.mjs";
|
|
3
3
|
import { getGenerator } from "../internal/plan/index.mjs";
|
|
4
4
|
import { isAsyncFailure, isAsyncGenerator, isAsyncGeneratorFunction, isAsyncPlan, isAsyncStep, isAsyncSuccess, isGenerator } from "./is.mjs";
|
|
5
5
|
import { fail } from "./misc.mjs";
|
|
@@ -23,7 +23,7 @@ function asyncUse(value) {
|
|
|
23
23
|
let actual = value;
|
|
24
24
|
if (typeof actual === "function") actual = actual();
|
|
25
25
|
actual = await actual;
|
|
26
|
-
if (typeof actual === "function") throw new
|
|
26
|
+
if (typeof actual === "function") throw new TypeError(PLAN_MESSAGE_USE_NESTED_FN);
|
|
27
27
|
if (typeof actual === "object" && actual !== null && PLAN_SYMBOL in actual) return yield* actual;
|
|
28
28
|
if (isAsyncGenerator(actual) || isGenerator(actual)) return yield* actual;
|
|
29
29
|
if (isError(actual)) {
|
|
@@ -61,8 +61,8 @@ function use(value) {
|
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
function validateUseInput(value, resolved) {
|
|
64
|
-
if (value instanceof Promise || isAsyncFailure(value) || isAsyncGenerator(value) || isAsyncGeneratorFunction(value) || isAsyncPlan(value) || isAsyncStep(value) || isAsyncSuccess(value)) throw new Error();
|
|
65
|
-
if (resolved && typeof value === "function") throw new Error();
|
|
64
|
+
if (value instanceof Promise || isAsyncFailure(value) || isAsyncGenerator(value) || isAsyncGeneratorFunction(value) || isAsyncPlan(value) || isAsyncStep(value) || isAsyncSuccess(value)) throw new Error(PLAN_MESSAGE_USE_SYNC_ASYNC);
|
|
65
|
+
if (resolved && typeof value === "function") throw new Error(PLAN_MESSAGE_USE_NESTED_FN);
|
|
66
66
|
}
|
|
67
67
|
use.async = asyncUse;
|
|
68
68
|
//#endregion
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PlainObject } from "../models/index.mjs";
|
|
2
2
|
import { PromiseOptions, PromiseStrategy, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValues } from "../models/promise.model.mjs";
|
|
3
3
|
//#region src/promise/index.d.ts
|
|
4
|
+
type AttemptPromiseOptions = PromiseOptions | AbortSignal | number;
|
|
4
5
|
/**
|
|
5
6
|
* Wrap a _Promise_ with safety handlers
|
|
6
7
|
*
|
|
@@ -8,7 +9,7 @@ import { PromiseOptions, PromiseStrategy, PromisesItems, PromisesOptions, Promis
|
|
|
8
9
|
* @param options Options for the _Promise_
|
|
9
10
|
* @returns Wrapped _Promise_
|
|
10
11
|
*/
|
|
11
|
-
export declare function attemptPromise<Value>(promise: Promise<Value>, options?:
|
|
12
|
+
export declare function attemptPromise<Value>(promise: Promise<Value>, options?: AttemptPromiseOptions): Promise<Value>;
|
|
12
13
|
/**
|
|
13
14
|
* Wrap a _Promise_-returning callback with safety handlers
|
|
14
15
|
*
|
|
@@ -16,7 +17,7 @@ export declare function attemptPromise<Value>(promise: Promise<Value>, options?:
|
|
|
16
17
|
* @param options Options for the _Promise_
|
|
17
18
|
* @returns _Promise_-wrapped callback
|
|
18
19
|
*/
|
|
19
|
-
export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?:
|
|
20
|
+
export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: AttemptPromiseOptions): Promise<Value>;
|
|
20
21
|
/**
|
|
21
22
|
* Wrap a callback with a _Promise_ and safety handlers
|
|
22
23
|
*
|
|
@@ -24,7 +25,7 @@ export declare function attemptPromise<Value>(callback: () => Promise<Value>, op
|
|
|
24
25
|
* @param options Options for the _Promise_
|
|
25
26
|
* @returns _Promise_-wrapped callback
|
|
26
27
|
*/
|
|
27
|
-
export declare function attemptPromise<Value>(callback: () => Value, options?:
|
|
28
|
+
export declare function attemptPromise<Value>(callback: () => Value, options?: AttemptPromiseOptions): Promise<Value>;
|
|
28
29
|
/**
|
|
29
30
|
* Handle a list of _Promises_, returning their results in an ordered array
|
|
30
31
|
*
|
|
@@ -106,4 +107,5 @@ export declare function resultPromises<Items extends PlainObject>(items: Items,
|
|
|
106
107
|
export declare namespace promises {
|
|
107
108
|
export var result: typeof resultPromises;
|
|
108
109
|
}
|
|
109
|
-
//#endregion
|
|
110
|
+
//#endregion
|
|
111
|
+
export {};
|
package/dist/promise/index.mjs
CHANGED
|
@@ -8,9 +8,9 @@ import { getTimedPromise } from "./timed.mjs";
|
|
|
8
8
|
//#region src/promise/index.ts
|
|
9
9
|
async function attemptPromise(value, options) {
|
|
10
10
|
const isFunction = typeof value === "function";
|
|
11
|
-
if (!isFunction && !(value instanceof Promise))
|
|
11
|
+
if (!isFunction && !(value instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT);
|
|
12
12
|
const { signal, time } = createPromiseOptions(options);
|
|
13
|
-
if (signal?.aborted ?? false)
|
|
13
|
+
if (signal?.aborted ?? false) throw signal?.reason;
|
|
14
14
|
const aborter = createAborter(signal, () => {
|
|
15
15
|
rejector(signal?.reason);
|
|
16
16
|
});
|
|
@@ -32,8 +32,8 @@ async function attemptPromise(value, options) {
|
|
|
32
32
|
}
|
|
33
33
|
async function promises(items, options) {
|
|
34
34
|
const { signal, strategy } = createPromisesOptions(options);
|
|
35
|
-
if (signal?.aborted ?? false)
|
|
36
|
-
if (!isArrayOrPlainObject(items))
|
|
35
|
+
if (signal?.aborted ?? false) throw signal?.reason;
|
|
36
|
+
if (!isArrayOrPlainObject(items)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
|
|
37
37
|
const isArray = Array.isArray(items);
|
|
38
38
|
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);
|
|
39
39
|
const aborter = createAborter(signal, () => {
|
package/dist/promise/misc.mjs
CHANGED
|
@@ -20,13 +20,16 @@ function handleResult(status, parameters) {
|
|
|
20
20
|
handlers.reject(value);
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
let resultValue;
|
|
24
|
+
if (complete) resultValue = status === "fulfilled" ? {
|
|
24
25
|
status,
|
|
25
26
|
value
|
|
26
27
|
} : {
|
|
27
28
|
status,
|
|
28
29
|
reason: value
|
|
29
30
|
};
|
|
31
|
+
else resultValue = value;
|
|
32
|
+
data.result[key] = resultValue;
|
|
30
33
|
if (index === data.last) {
|
|
31
34
|
aborter?.cancel();
|
|
32
35
|
handlers.resolve(data.result);
|
|
@@ -38,7 +41,7 @@ function settlePromise(settler, value, aborter) {
|
|
|
38
41
|
}
|
|
39
42
|
async function toResult(value) {
|
|
40
43
|
const actual = typeof value === "function" ? value() : value;
|
|
41
|
-
if (!(actual instanceof Promise))
|
|
44
|
+
if (!(actual instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT);
|
|
42
45
|
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
43
46
|
}
|
|
44
47
|
//#endregion
|
package/dist/promise/timed.mjs
CHANGED
|
@@ -23,9 +23,9 @@ async function getTimedPromise(promise, time, signal) {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
async function timed(promise, options) {
|
|
26
|
-
if (!(promise instanceof Promise))
|
|
26
|
+
if (!(promise instanceof Promise)) throw new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED);
|
|
27
27
|
const { signal, time } = createPromiseOptions(options);
|
|
28
|
-
if (signal?.aborted ?? false)
|
|
28
|
+
if (signal?.aborted ?? false) throw signal?.reason;
|
|
29
29
|
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
30
30
|
}
|
|
31
31
|
//#endregion
|
package/dist/queue.mjs
CHANGED
|
@@ -335,14 +335,13 @@ async function run(state) {
|
|
|
335
335
|
state.runners += 1;
|
|
336
336
|
let item = state.items.shift();
|
|
337
337
|
while (item != null) {
|
|
338
|
+
const next = item;
|
|
338
339
|
let error = false;
|
|
339
340
|
let result;
|
|
340
341
|
const attempted = await asyncAttempt(async () => {
|
|
341
|
-
if (
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}
|
|
345
|
-
throw new Error();
|
|
342
|
+
if (next.aborter?.signal?.aborted) throw next.aborter.signal.reason;
|
|
343
|
+
const parameters = next.key == null ? next.parameters : [next.key, ...next.parameters];
|
|
344
|
+
return await state.callback(...parameters);
|
|
346
345
|
});
|
|
347
346
|
if (attempted.ok) result = attempted.value;
|
|
348
347
|
else {
|