@oscarpalmer/atoms 0.143.0 → 0.145.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/filter.js +5 -1
- package/dist/array/partition.js +6 -0
- package/dist/array/range.js +6 -0
- package/dist/array/select.js +5 -0
- package/dist/array/toggle.js +5 -0
- package/dist/array/unique.js +1 -1
- package/dist/array/update.js +5 -0
- package/dist/atoms.full.js +204 -58
- package/dist/function/assert.js +19 -0
- package/dist/function/work.js +42 -0
- package/dist/index.js +9 -3
- package/dist/internal/array/callbacks.js +1 -1
- package/dist/internal/array/find.js +23 -11
- package/dist/internal/array/update.js +22 -0
- package/dist/is.js +10 -1
- package/dist/math.js +24 -3
- package/dist/promise.js +25 -25
- package/dist/result.js +18 -16
- package/dist/value/merge.js +3 -1
- package/package.json +25 -1
- package/src/array/exists.ts +7 -8
- package/src/array/filter.ts +32 -9
- package/src/array/find.ts +6 -6
- package/src/array/index-of.ts +7 -8
- package/src/array/partition.ts +56 -0
- package/src/array/range.ts +35 -0
- package/src/array/select.ts +92 -0
- package/src/array/toggle.ts +40 -0
- package/src/array/unique.ts +6 -6
- package/src/array/update.ts +40 -0
- package/src/function/assert.ts +43 -0
- package/src/function/work.ts +831 -0
- package/src/index.ts +6 -0
- package/src/internal/array/callbacks.ts +1 -1
- package/src/internal/array/find.ts +35 -29
- package/src/internal/array/update.ts +52 -0
- package/src/internal/math/aggregate.ts +2 -2
- package/src/is.ts +15 -2
- package/src/math.ts +33 -5
- package/src/promise.ts +83 -83
- package/src/result.ts +89 -80
- package/src/value/merge.ts +10 -6
- package/types/array/exists.d.ts +2 -2
- package/types/array/filter.d.ts +10 -2
- package/types/array/find.d.ts +2 -2
- package/types/array/index-of.d.ts +2 -2
- package/types/array/partition.d.ts +31 -0
- package/types/array/range.d.ts +20 -0
- package/types/array/select.d.ts +43 -0
- package/types/array/toggle.d.ts +24 -0
- package/types/array/unique.d.ts +2 -2
- package/types/array/update.d.ts +24 -0
- package/types/function/assert.d.ts +8 -0
- package/types/function/work.d.ts +280 -0
- package/types/index.d.ts +6 -0
- package/types/internal/array/callbacks.d.ts +1 -0
- package/types/internal/array/find.d.ts +5 -2
- package/types/internal/array/update.d.ts +1 -0
- package/types/internal/math/aggregate.d.ts +1 -1
- package/types/is.d.ts +8 -1
- package/types/math.d.ts +15 -1
- package/types/promise.d.ts +21 -21
- package/types/result.d.ts +47 -43
- package/types/value/merge.d.ts +5 -4
package/dist/array/filter.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { findValues } from "../internal/array/find.js";
|
|
2
2
|
function filter(array, ...parameters) {
|
|
3
|
-
return findValues("all", array, parameters);
|
|
3
|
+
return findValues("all", array, parameters).matched;
|
|
4
|
+
}
|
|
5
|
+
filter.remove = removeFiltered;
|
|
6
|
+
function removeFiltered(array, ...parameters) {
|
|
7
|
+
return findValues("all", array, parameters).notMatched;
|
|
4
8
|
}
|
|
5
9
|
export { filter };
|
package/dist/array/unique.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { findValues } from "../internal/array/find.js";
|
|
2
2
|
function unique(array, key) {
|
|
3
3
|
if (!Array.isArray(array)) return [];
|
|
4
|
-
return array.length > 1 ? findValues("unique", array, [key, void 0]) : array;
|
|
4
|
+
return array.length > 1 ? findValues("unique", array, [key, void 0]).matched : array;
|
|
5
5
|
}
|
|
6
6
|
export { unique };
|
package/dist/atoms.full.js
CHANGED
|
@@ -57,27 +57,39 @@ function findValueInArray(array, callback, value, findIndex) {
|
|
|
57
57
|
}
|
|
58
58
|
return findIndex ? -1 : void 0;
|
|
59
59
|
}
|
|
60
|
-
function findValues(type, array, parameters) {
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
function findValues(type, array, parameters, mapper) {
|
|
61
|
+
const result = {
|
|
62
|
+
matched: [],
|
|
63
|
+
notMatched: []
|
|
64
|
+
};
|
|
65
|
+
if (!Array.isArray(array) || array.length === 0) return result;
|
|
63
66
|
const { length } = array;
|
|
64
67
|
const { bool, key, value } = getParameters(parameters);
|
|
65
68
|
const callbacks = getArrayCallbacks(bool, key);
|
|
66
|
-
if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
|
|
70
|
+
result.matched = [...new Set(array)];
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
const mapCallback = typeof mapper === "function" ? mapper : void 0;
|
|
74
|
+
if (callbacks?.bool != null || type === "all" && key == null) {
|
|
75
|
+
const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
|
|
76
|
+
for (let index = 0; index < length; index += 1) {
|
|
77
|
+
const item = array[index];
|
|
78
|
+
if (callback(item, index, array)) result.matched.push(mapCallback?.(item, index, array) ?? item);
|
|
79
|
+
else result.notMatched.push(item);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
69
83
|
const keys = /* @__PURE__ */ new Set();
|
|
70
|
-
const values = [];
|
|
71
84
|
for (let index = 0; index < length; index += 1) {
|
|
72
85
|
const item = array[index];
|
|
73
86
|
const keyed = callbacks?.keyed?.(item, index, array) ?? item;
|
|
74
87
|
if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
|
|
75
88
|
keys.add(keyed);
|
|
76
|
-
|
|
77
|
-
}
|
|
89
|
+
result.matched.push(mapCallback?.(item, index, array) ?? item);
|
|
90
|
+
} else result.notMatched.push(item);
|
|
78
91
|
}
|
|
79
|
-
|
|
80
|
-
return values;
|
|
92
|
+
return result;
|
|
81
93
|
}
|
|
82
94
|
function getParameters(original) {
|
|
83
95
|
const { length } = original;
|
|
@@ -93,7 +105,11 @@ function exists(array, ...parameters) {
|
|
|
93
105
|
return findValue("index", array, parameters) > -1;
|
|
94
106
|
}
|
|
95
107
|
function filter(array, ...parameters) {
|
|
96
|
-
return findValues("all", array, parameters);
|
|
108
|
+
return findValues("all", array, parameters).matched;
|
|
109
|
+
}
|
|
110
|
+
filter.remove = removeFiltered;
|
|
111
|
+
function removeFiltered(array, ...parameters) {
|
|
112
|
+
return findValues("all", array, parameters).notMatched;
|
|
97
113
|
}
|
|
98
114
|
function find(array, ...parameters) {
|
|
99
115
|
return findValue("value", array, parameters);
|
|
@@ -257,6 +273,10 @@ function insertValues(type, array, items, start, deleteCount) {
|
|
|
257
273
|
function insert(array, indexOrItems, items) {
|
|
258
274
|
return insertValues("insert", array, items == null ? indexOrItems : items, typeof indexOrItems === "number" ? indexOrItems : array?.length, 0);
|
|
259
275
|
}
|
|
276
|
+
function partition(array, ...parameters) {
|
|
277
|
+
const { matched, notMatched } = findValues("all", array, parameters);
|
|
278
|
+
return [matched, notMatched];
|
|
279
|
+
}
|
|
260
280
|
/**
|
|
261
281
|
* Push items into an array _(at the end)_
|
|
262
282
|
* @param array Original array
|
|
@@ -266,6 +286,14 @@ function insert(array, indexOrItems, items) {
|
|
|
266
286
|
function push(array, pushed) {
|
|
267
287
|
return insertValues("push", array, pushed, array.length, 0);
|
|
268
288
|
}
|
|
289
|
+
function range(length, value) {
|
|
290
|
+
if (typeof length !== "number" || length <= 0) return [];
|
|
291
|
+
const isFunction = typeof value === "function";
|
|
292
|
+
return Array.from({ length }, (_, index) => isFunction ? value(index) : value ?? index);
|
|
293
|
+
}
|
|
294
|
+
function select(array, ...parameters) {
|
|
295
|
+
return findValues("all", array, parameters, parameters.pop()).matched;
|
|
296
|
+
}
|
|
269
297
|
function aggregate(type, array, key) {
|
|
270
298
|
const length = Array.isArray(array) ? array.length : 0;
|
|
271
299
|
if (length === 0) return {
|
|
@@ -590,9 +618,35 @@ function toSet(array, value) {
|
|
|
590
618
|
for (let index = 0; index < length; index += 1) set.add(callbacks.value(array[index], index, array));
|
|
591
619
|
return set;
|
|
592
620
|
}
|
|
621
|
+
function updateInArray(array, items, key, replace) {
|
|
622
|
+
if (!Array.isArray(array)) return [];
|
|
623
|
+
const itemsIsArray = Array.isArray(items);
|
|
624
|
+
if (array.length === 0 || !itemsIsArray) {
|
|
625
|
+
if (itemsIsArray) array.push(...items);
|
|
626
|
+
return array;
|
|
627
|
+
}
|
|
628
|
+
const { length } = items;
|
|
629
|
+
if (length === 0) return array;
|
|
630
|
+
const callback = getArrayCallback(key);
|
|
631
|
+
for (let valuesIndex = 0; valuesIndex < length; valuesIndex += 1) {
|
|
632
|
+
const item = items[valuesIndex];
|
|
633
|
+
const value = callback?.(item) ?? item;
|
|
634
|
+
const arrayIndex = callback == null ? array.indexOf(value) : array.findIndex((arrayItem, arrayIndex) => callback(arrayItem, arrayIndex, array) === value);
|
|
635
|
+
if (arrayIndex === -1) array.push(item);
|
|
636
|
+
else if (replace) array[arrayIndex] = item;
|
|
637
|
+
else array.splice(arrayIndex, 1);
|
|
638
|
+
}
|
|
639
|
+
return array;
|
|
640
|
+
}
|
|
641
|
+
function toggle(array, values, key) {
|
|
642
|
+
return updateInArray(array, values, key, false);
|
|
643
|
+
}
|
|
593
644
|
function unique(array, key) {
|
|
594
645
|
if (!Array.isArray(array)) return [];
|
|
595
|
-
return array.length > 1 ? findValues("unique", array, [key, void 0]) : array;
|
|
646
|
+
return array.length > 1 ? findValues("unique", array, [key, void 0]).matched : array;
|
|
647
|
+
}
|
|
648
|
+
function update(array, values, key) {
|
|
649
|
+
return updateInArray(array, values, key, true);
|
|
596
650
|
}
|
|
597
651
|
function getLimiter(callback, throttler, time) {
|
|
598
652
|
const interval = typeof time === "number" && time >= frame_rate_default ? time : frame_rate_default;
|
|
@@ -847,6 +901,64 @@ const DEFAULT_CACHE_SIZE = 1024;
|
|
|
847
901
|
function throttle(callback, time) {
|
|
848
902
|
return getLimiter(callback, true, time);
|
|
849
903
|
}
|
|
904
|
+
function assert(condition, message, error) {
|
|
905
|
+
if (!condition()) throw new (error ?? Error)(message);
|
|
906
|
+
}
|
|
907
|
+
assert.condition = (condition, message, error) => {
|
|
908
|
+
return (value) => {
|
|
909
|
+
assert(() => condition(value), message, error);
|
|
910
|
+
};
|
|
911
|
+
};
|
|
912
|
+
assert.instanceOf = (constructor, message, error) => {
|
|
913
|
+
return (value) => {
|
|
914
|
+
assert(() => value instanceof constructor, message, error);
|
|
915
|
+
};
|
|
916
|
+
};
|
|
917
|
+
assert.is = (condition, message, error) => {
|
|
918
|
+
return (value) => {
|
|
919
|
+
assert(() => condition(value), message, error);
|
|
920
|
+
};
|
|
921
|
+
};
|
|
922
|
+
function asyncFlow(...fns) {
|
|
923
|
+
assertFlowFunctions(fns);
|
|
924
|
+
return (...args) => asyncWork(args, fns, true);
|
|
925
|
+
}
|
|
926
|
+
function flow(...fns) {
|
|
927
|
+
assertFlowFunctions(fns);
|
|
928
|
+
return (...args) => work(args, fns, true);
|
|
929
|
+
}
|
|
930
|
+
flow.async = asyncFlow;
|
|
931
|
+
async function asyncPipe(value, ...pipes) {
|
|
932
|
+
assertPipeFunctions(pipes);
|
|
933
|
+
return asyncWork(value, pipes, false);
|
|
934
|
+
}
|
|
935
|
+
function pipe(value, ...pipes) {
|
|
936
|
+
assertPipeFunctions(pipes);
|
|
937
|
+
return work(value, pipes, false);
|
|
938
|
+
}
|
|
939
|
+
pipe.async = asyncPipe;
|
|
940
|
+
async function asyncWork(initial, functions, flow) {
|
|
941
|
+
const { length } = functions;
|
|
942
|
+
let transformed = initial;
|
|
943
|
+
for (let index = 0; index < length; index += 1) {
|
|
944
|
+
const fn = functions[index];
|
|
945
|
+
transformed = flow && index === 0 && Array.isArray(initial) ? await fn(...initial) : await fn(transformed);
|
|
946
|
+
}
|
|
947
|
+
return transformed;
|
|
948
|
+
}
|
|
949
|
+
function work(initial, functions, flow) {
|
|
950
|
+
const { length } = functions;
|
|
951
|
+
let transformed = initial;
|
|
952
|
+
for (let index = 0; index < length; index += 1) {
|
|
953
|
+
const fn = functions[index];
|
|
954
|
+
transformed = flow && index === 0 && Array.isArray(initial) ? fn(...initial) : fn(transformed);
|
|
955
|
+
}
|
|
956
|
+
return transformed;
|
|
957
|
+
}
|
|
958
|
+
const MESSAGE_FLOW = "Flow expected to receive an array of functions";
|
|
959
|
+
const MESSAGE_PIPE = "Pipe expected to receive an array of functions";
|
|
960
|
+
const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_FLOW, TypeError);
|
|
961
|
+
const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_PIPE, TypeError);
|
|
850
962
|
function _getRandomFloat(inclusive, minimum, maximum) {
|
|
851
963
|
let maxFloat = isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
|
|
852
964
|
let minFloat = isNumber(minimum) && minimum >= Number.MIN_SAFE_INTEGER ? minimum : Number.MIN_SAFE_INTEGER;
|
|
@@ -1583,10 +1695,12 @@ function setChanges(parameters) {
|
|
|
1583
1695
|
function getMergeOptions(options) {
|
|
1584
1696
|
const actual = {
|
|
1585
1697
|
replaceableObjects: void 0,
|
|
1698
|
+
skipNullableAny: false,
|
|
1586
1699
|
skipNullableInArrays: false
|
|
1587
1700
|
};
|
|
1588
1701
|
if (typeof options !== "object" || options == null) return actual;
|
|
1589
1702
|
actual.replaceableObjects = getReplaceableObjects(options.replaceableObjects);
|
|
1703
|
+
actual.skipNullableAny = options.skipNullableAny === true;
|
|
1590
1704
|
actual.skipNullableInArrays = options.skipNullableInArrays === true;
|
|
1591
1705
|
return actual;
|
|
1592
1706
|
}
|
|
@@ -1629,7 +1743,7 @@ function mergeObjects(values, options, prefix) {
|
|
|
1629
1743
|
const full = join([prefix, key], ".");
|
|
1630
1744
|
const next = item[key];
|
|
1631
1745
|
const previous = merged[key];
|
|
1632
|
-
if (
|
|
1746
|
+
if (next == null && (options.skipNullableAny || isArray && options.skipNullableInArrays)) continue;
|
|
1633
1747
|
if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous) && !(options.replaceableObjects?.(full) ?? false)) merged[key] = mergeValues([previous, next], options, false, full);
|
|
1634
1748
|
else merged[key] = next;
|
|
1635
1749
|
}
|
|
@@ -2574,6 +2688,15 @@ function isEmpty(value) {
|
|
|
2574
2688
|
return true;
|
|
2575
2689
|
}
|
|
2576
2690
|
/**
|
|
2691
|
+
* Is the value an instance of the constructor?
|
|
2692
|
+
* @param constructor Class constructor
|
|
2693
|
+
* @param value Value to check
|
|
2694
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
2695
|
+
*/
|
|
2696
|
+
function isInstanceOf(constructor, value) {
|
|
2697
|
+
return isConstructor(constructor) && value instanceof constructor;
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2577
2700
|
* Is the value not `undefined` or `null`?
|
|
2578
2701
|
* @param value Value to check
|
|
2579
2702
|
* @returns `true` if the value is not `undefined` or `null`, otherwise `false`
|
|
@@ -2746,6 +2869,15 @@ function average(array, key) {
|
|
|
2746
2869
|
const aggregated = aggregate("average", array, key);
|
|
2747
2870
|
return aggregated.count > 0 ? aggregated.value / aggregated.count : NaN;
|
|
2748
2871
|
}
|
|
2872
|
+
/**
|
|
2873
|
+
* Round a number up
|
|
2874
|
+
* @param value Number to round up
|
|
2875
|
+
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
2876
|
+
* @returns Rounded number, or `NaN` if the value if unable to be rounded
|
|
2877
|
+
*/
|
|
2878
|
+
function ceil(value, decimals) {
|
|
2879
|
+
return roundNumber(Math.ceil, value, decimals);
|
|
2880
|
+
}
|
|
2749
2881
|
function count(array, key, value) {
|
|
2750
2882
|
if (!Array.isArray(array)) return NaN;
|
|
2751
2883
|
const { length } = array;
|
|
@@ -2758,6 +2890,15 @@ function count(array, key, value) {
|
|
|
2758
2890
|
}
|
|
2759
2891
|
return counted;
|
|
2760
2892
|
}
|
|
2893
|
+
/**
|
|
2894
|
+
* Round a number down
|
|
2895
|
+
* @param value Number to round down
|
|
2896
|
+
* @param decimals Number of decimal places to round to _(defaults to `0`)_
|
|
2897
|
+
* @returns Rounded number, or `NaN` if the value if unable to be rounded
|
|
2898
|
+
*/
|
|
2899
|
+
function floor(value, decimals) {
|
|
2900
|
+
return roundNumber(Math.floor, value, decimals);
|
|
2901
|
+
}
|
|
2761
2902
|
function median(array, key) {
|
|
2762
2903
|
let length = Array.isArray(array) ? array.length : 0;
|
|
2763
2904
|
if (!Array.isArray(array) || length === 0) return NaN;
|
|
@@ -2784,10 +2925,13 @@ function min(array, key) {
|
|
|
2784
2925
|
* @returns Rounded number, or `NaN` if the value if unable to be rounded
|
|
2785
2926
|
*/
|
|
2786
2927
|
function round(value, decimals) {
|
|
2928
|
+
return roundNumber(Math.round, value, decimals);
|
|
2929
|
+
}
|
|
2930
|
+
function roundNumber(callback, value, decimals) {
|
|
2787
2931
|
if (typeof value !== "number") return NaN;
|
|
2788
|
-
if (typeof decimals !== "number" || decimals < 1) return
|
|
2932
|
+
if (typeof decimals !== "number" || decimals < 1) return callback(value);
|
|
2789
2933
|
const mod = 10 ** decimals;
|
|
2790
|
-
return
|
|
2934
|
+
return callback((value + Number.EPSILON) * mod) / mod;
|
|
2791
2935
|
}
|
|
2792
2936
|
function sum(array, key) {
|
|
2793
2937
|
return getAggregated("sum", array, key);
|
|
@@ -2816,6 +2960,31 @@ var PromiseTimeoutError = class extends Error {
|
|
|
2816
2960
|
this.name = ERROR_NAME$1;
|
|
2817
2961
|
}
|
|
2818
2962
|
};
|
|
2963
|
+
async function attemptPromise(value, options) {
|
|
2964
|
+
const isFunction = typeof value === "function";
|
|
2965
|
+
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
|
|
2966
|
+
const { signal, time } = getPromiseOptions(options);
|
|
2967
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
2968
|
+
function abort() {
|
|
2969
|
+
rejector(signal.reason);
|
|
2970
|
+
}
|
|
2971
|
+
async function handler(resolve, reject) {
|
|
2972
|
+
try {
|
|
2973
|
+
let result = isFunction ? value() : await value;
|
|
2974
|
+
if (result instanceof Promise) result = await result;
|
|
2975
|
+
settlePromise(abort, resolve, result, signal);
|
|
2976
|
+
} catch (error) {
|
|
2977
|
+
settlePromise(abort, reject, error, signal);
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
let rejector;
|
|
2981
|
+
signal?.addEventListener(EVENT_NAME$1, abort, ABORT_OPTIONS);
|
|
2982
|
+
const promise = new Promise((resolve, reject) => {
|
|
2983
|
+
rejector = reject;
|
|
2984
|
+
handler(resolve, reject);
|
|
2985
|
+
});
|
|
2986
|
+
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
2987
|
+
}
|
|
2819
2988
|
/**
|
|
2820
2989
|
* Create a cancelable promise
|
|
2821
2990
|
* @param executor Executor function for the promise
|
|
@@ -2976,31 +3145,6 @@ async function timed(promise, options) {
|
|
|
2976
3145
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
2977
3146
|
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
2978
3147
|
}
|
|
2979
|
-
async function attemptPromise(value, options) {
|
|
2980
|
-
const isFunction = typeof value === "function";
|
|
2981
|
-
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
|
|
2982
|
-
const { signal, time } = getPromiseOptions(options);
|
|
2983
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
2984
|
-
function abort() {
|
|
2985
|
-
rejector(signal.reason);
|
|
2986
|
-
}
|
|
2987
|
-
async function handler(resolve, reject) {
|
|
2988
|
-
try {
|
|
2989
|
-
let result = isFunction ? value() : await value;
|
|
2990
|
-
if (result instanceof Promise) result = await result;
|
|
2991
|
-
settlePromise(abort, resolve, result, signal);
|
|
2992
|
-
} catch (error) {
|
|
2993
|
-
settlePromise(abort, reject, error, signal);
|
|
2994
|
-
}
|
|
2995
|
-
}
|
|
2996
|
-
let rejector;
|
|
2997
|
-
signal?.addEventListener(EVENT_NAME$1, abort, ABORT_OPTIONS);
|
|
2998
|
-
const promise = new Promise((resolve, reject) => {
|
|
2999
|
-
rejector = reject;
|
|
3000
|
-
handler(resolve, reject);
|
|
3001
|
-
});
|
|
3002
|
-
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
3003
|
-
}
|
|
3004
3148
|
const ABORT_OPTIONS = { once: true };
|
|
3005
3149
|
const DEFAULT_STRATEGY = "complete";
|
|
3006
3150
|
const ERROR_NAME$1 = "PromiseTimeoutError";
|
|
@@ -3343,6 +3487,24 @@ function _isResult(value, okValue) {
|
|
|
3343
3487
|
if (!isPlainObject(value)) return false;
|
|
3344
3488
|
return value.ok === okValue && (okValue ? "value" : "error") in value;
|
|
3345
3489
|
}
|
|
3490
|
+
async function asyncAttempt(value, err) {
|
|
3491
|
+
try {
|
|
3492
|
+
let result = typeof value === "function" ? value() : await value;
|
|
3493
|
+
if (result instanceof Promise) result = await result;
|
|
3494
|
+
return ok(result);
|
|
3495
|
+
} catch (thrown) {
|
|
3496
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3497
|
+
}
|
|
3498
|
+
}
|
|
3499
|
+
function attempt(callback, err) {
|
|
3500
|
+
try {
|
|
3501
|
+
return ok(callback());
|
|
3502
|
+
} catch (thrown) {
|
|
3503
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3504
|
+
}
|
|
3505
|
+
}
|
|
3506
|
+
attempt.async = asyncAttempt;
|
|
3507
|
+
attempt.promise = attemptPromise;
|
|
3346
3508
|
function error(value, original) {
|
|
3347
3509
|
return getError(value, original);
|
|
3348
3510
|
}
|
|
@@ -3384,22 +3546,6 @@ function ok(value) {
|
|
|
3384
3546
|
value
|
|
3385
3547
|
};
|
|
3386
3548
|
}
|
|
3387
|
-
function attempt(callback, err) {
|
|
3388
|
-
try {
|
|
3389
|
-
return ok(callback());
|
|
3390
|
-
} catch (thrown) {
|
|
3391
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3392
|
-
}
|
|
3393
|
-
}
|
|
3394
|
-
attempt.async = asyncAttempt;
|
|
3395
|
-
attempt.promise = attemptPromise;
|
|
3396
|
-
async function asyncAttempt(value, err) {
|
|
3397
|
-
try {
|
|
3398
|
-
return ok(await (typeof value === "function" ? value() : value));
|
|
3399
|
-
} catch (thrown) {
|
|
3400
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3401
|
-
}
|
|
3402
|
-
}
|
|
3403
3549
|
function unwrap(value, defaultValue) {
|
|
3404
3550
|
return isOk(value) ? value.value : defaultValue;
|
|
3405
3551
|
}
|
|
@@ -3455,4 +3601,4 @@ var SizedSet = class extends Set {
|
|
|
3455
3601
|
}
|
|
3456
3602
|
}
|
|
3457
3603
|
};
|
|
3458
|
-
export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, pascalCase, pick, promises, push, queue, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, upperCase, words };
|
|
3604
|
+
export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function assert(condition, message, error) {
|
|
2
|
+
if (!condition()) throw new (error ?? Error)(message);
|
|
3
|
+
}
|
|
4
|
+
assert.condition = (condition, message, error) => {
|
|
5
|
+
return (value) => {
|
|
6
|
+
assert(() => condition(value), message, error);
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
assert.instanceOf = (constructor, message, error) => {
|
|
10
|
+
return (value) => {
|
|
11
|
+
assert(() => value instanceof constructor, message, error);
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
assert.is = (condition, message, error) => {
|
|
15
|
+
return (value) => {
|
|
16
|
+
assert(() => condition(value), message, error);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export { assert };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { assert } from "./assert.js";
|
|
2
|
+
function asyncFlow(...fns) {
|
|
3
|
+
assertFlowFunctions(fns);
|
|
4
|
+
return (...args) => asyncWork(args, fns, true);
|
|
5
|
+
}
|
|
6
|
+
function flow(...fns) {
|
|
7
|
+
assertFlowFunctions(fns);
|
|
8
|
+
return (...args) => work(args, fns, true);
|
|
9
|
+
}
|
|
10
|
+
flow.async = asyncFlow;
|
|
11
|
+
async function asyncPipe(value, ...pipes) {
|
|
12
|
+
assertPipeFunctions(pipes);
|
|
13
|
+
return asyncWork(value, pipes, false);
|
|
14
|
+
}
|
|
15
|
+
function pipe(value, ...pipes) {
|
|
16
|
+
assertPipeFunctions(pipes);
|
|
17
|
+
return work(value, pipes, false);
|
|
18
|
+
}
|
|
19
|
+
pipe.async = asyncPipe;
|
|
20
|
+
async function asyncWork(initial, functions, flow) {
|
|
21
|
+
const { length } = functions;
|
|
22
|
+
let transformed = initial;
|
|
23
|
+
for (let index = 0; index < length; index += 1) {
|
|
24
|
+
const fn = functions[index];
|
|
25
|
+
transformed = flow && index === 0 && Array.isArray(initial) ? await fn(...initial) : await fn(transformed);
|
|
26
|
+
}
|
|
27
|
+
return transformed;
|
|
28
|
+
}
|
|
29
|
+
function work(initial, functions, flow) {
|
|
30
|
+
const { length } = functions;
|
|
31
|
+
let transformed = initial;
|
|
32
|
+
for (let index = 0; index < length; index += 1) {
|
|
33
|
+
const fn = functions[index];
|
|
34
|
+
transformed = flow && index === 0 && Array.isArray(initial) ? fn(...initial) : fn(transformed);
|
|
35
|
+
}
|
|
36
|
+
return transformed;
|
|
37
|
+
}
|
|
38
|
+
var MESSAGE_FLOW = "Flow expected to receive an array of functions";
|
|
39
|
+
var MESSAGE_PIPE = "Pipe expected to receive an array of functions";
|
|
40
|
+
var assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_FLOW, TypeError);
|
|
41
|
+
var assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_PIPE, TypeError);
|
|
42
|
+
export { flow, pipe };
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,10 @@ import { groupBy } from "./array/group-by.js";
|
|
|
8
8
|
import { indexOf } from "./array/index-of.js";
|
|
9
9
|
import { chunk } from "./internal/array/chunk.js";
|
|
10
10
|
import { insert } from "./array/insert.js";
|
|
11
|
+
import { partition } from "./array/partition.js";
|
|
11
12
|
import { push } from "./array/push.js";
|
|
13
|
+
import { range } from "./array/range.js";
|
|
14
|
+
import { select } from "./array/select.js";
|
|
12
15
|
import { max } from "./internal/math/aggregate.js";
|
|
13
16
|
import { compact } from "./internal/array/compact.js";
|
|
14
17
|
import { getString, ignoreKey, join, tryDecode, tryEncode, words } from "./internal/string.js";
|
|
@@ -18,7 +21,9 @@ import { splice } from "./array/splice.js";
|
|
|
18
21
|
import { toMap } from "./array/to-map.js";
|
|
19
22
|
import { toRecord } from "./array/to-record.js";
|
|
20
23
|
import { toSet } from "./array/to-set.js";
|
|
24
|
+
import { toggle } from "./array/toggle.js";
|
|
21
25
|
import { unique } from "./array/unique.js";
|
|
26
|
+
import { update } from "./array/update.js";
|
|
22
27
|
import { noop } from "./internal/function/misc.js";
|
|
23
28
|
import { beacon } from "./beacon.js";
|
|
24
29
|
import { between, clamp, getNumber } from "./internal/number.js";
|
|
@@ -33,6 +38,7 @@ import { debounce } from "./function/debounce.js";
|
|
|
33
38
|
import { SizedMap } from "./sized/map.js";
|
|
34
39
|
import { memoize } from "./function/memoize.js";
|
|
35
40
|
import { throttle } from "./function/throttle.js";
|
|
41
|
+
import { flow, pipe } from "./function/work.js";
|
|
36
42
|
import { getRandomFloat, getRandomInteger } from "./internal/random.js";
|
|
37
43
|
import { shuffle } from "./internal/array/shuffle.js";
|
|
38
44
|
import { equal } from "./internal/value/equal.js";
|
|
@@ -49,13 +55,13 @@ import { omit } from "./value/omit.js";
|
|
|
49
55
|
import { pick } from "./value/pick.js";
|
|
50
56
|
import { smush } from "./value/smush.js";
|
|
51
57
|
import { unsmush } from "./value/unsmush.js";
|
|
52
|
-
import { isEmpty, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
|
|
58
|
+
import { isEmpty, isInstanceOf, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
|
|
53
59
|
import { logger } from "./logger.js";
|
|
54
|
-
import { average, count, median, min, round, sum } from "./math.js";
|
|
60
|
+
import { average, ceil, count, floor, median, min, round, sum } from "./math.js";
|
|
55
61
|
import { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, isFulfilled, isRejected, promises, timed } from "./promise.js";
|
|
56
62
|
import { fromQuery, toQuery } from "./query.js";
|
|
57
63
|
import { QueueError, queue } from "./queue.js";
|
|
58
64
|
import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
|
|
59
65
|
import { attempt, error, isError, isOk, isResult, ok, unwrap } from "./result.js";
|
|
60
66
|
import { SizedSet } from "./sized/set.js";
|
|
61
|
-
export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, pascalCase, pick, promises, push, queue, rgbToHex, rgbToHsl, rgbToHsla, round, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, upperCase, words };
|
|
67
|
+
export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
|
@@ -19,27 +19,39 @@ function findValueInArray(array, callback, value, findIndex) {
|
|
|
19
19
|
}
|
|
20
20
|
return findIndex ? -1 : void 0;
|
|
21
21
|
}
|
|
22
|
-
function findValues(type, array, parameters) {
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
function findValues(type, array, parameters, mapper) {
|
|
23
|
+
const result = {
|
|
24
|
+
matched: [],
|
|
25
|
+
notMatched: []
|
|
26
|
+
};
|
|
27
|
+
if (!Array.isArray(array) || array.length === 0) return result;
|
|
25
28
|
const { length } = array;
|
|
26
29
|
const { bool, key, value } = getParameters(parameters);
|
|
27
30
|
const callbacks = getArrayCallbacks(bool, key);
|
|
28
|
-
if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
|
|
32
|
+
result.matched = [...new Set(array)];
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
const mapCallback = typeof mapper === "function" ? mapper : void 0;
|
|
36
|
+
if (callbacks?.bool != null || type === "all" && key == null) {
|
|
37
|
+
const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
|
|
38
|
+
for (let index = 0; index < length; index += 1) {
|
|
39
|
+
const item = array[index];
|
|
40
|
+
if (callback(item, index, array)) result.matched.push(mapCallback?.(item, index, array) ?? item);
|
|
41
|
+
else result.notMatched.push(item);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
31
45
|
const keys = /* @__PURE__ */ new Set();
|
|
32
|
-
const values = [];
|
|
33
46
|
for (let index = 0; index < length; index += 1) {
|
|
34
47
|
const item = array[index];
|
|
35
48
|
const keyed = callbacks?.keyed?.(item, index, array) ?? item;
|
|
36
49
|
if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
|
|
37
50
|
keys.add(keyed);
|
|
38
|
-
|
|
39
|
-
}
|
|
51
|
+
result.matched.push(mapCallback?.(item, index, array) ?? item);
|
|
52
|
+
} else result.notMatched.push(item);
|
|
40
53
|
}
|
|
41
|
-
|
|
42
|
-
return values;
|
|
54
|
+
return result;
|
|
43
55
|
}
|
|
44
56
|
function getParameters(original) {
|
|
45
57
|
const { length } = original;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getArrayCallback } from "./callbacks.js";
|
|
2
|
+
function updateInArray(array, items, key, replace) {
|
|
3
|
+
if (!Array.isArray(array)) return [];
|
|
4
|
+
const itemsIsArray = Array.isArray(items);
|
|
5
|
+
if (array.length === 0 || !itemsIsArray) {
|
|
6
|
+
if (itemsIsArray) array.push(...items);
|
|
7
|
+
return array;
|
|
8
|
+
}
|
|
9
|
+
const { length } = items;
|
|
10
|
+
if (length === 0) return array;
|
|
11
|
+
const callback = getArrayCallback(key);
|
|
12
|
+
for (let valuesIndex = 0; valuesIndex < length; valuesIndex += 1) {
|
|
13
|
+
const item = items[valuesIndex];
|
|
14
|
+
const value = callback?.(item) ?? item;
|
|
15
|
+
const arrayIndex = callback == null ? array.indexOf(value) : array.findIndex((arrayItem, arrayIndex) => callback(arrayItem, arrayIndex, array) === value);
|
|
16
|
+
if (arrayIndex === -1) array.push(item);
|
|
17
|
+
else if (replace) array[arrayIndex] = item;
|
|
18
|
+
else array.splice(arrayIndex, 1);
|
|
19
|
+
}
|
|
20
|
+
return array;
|
|
21
|
+
}
|
|
22
|
+
export { updateInArray };
|
package/dist/is.js
CHANGED
|
@@ -15,6 +15,15 @@ function isEmpty(value) {
|
|
|
15
15
|
return true;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
+
* Is the value an instance of the constructor?
|
|
19
|
+
* @param constructor Class constructor
|
|
20
|
+
* @param value Value to check
|
|
21
|
+
* @returns `true` if the value is an instance of the constructor, otherwise `false`
|
|
22
|
+
*/
|
|
23
|
+
function isInstanceOf(constructor, value) {
|
|
24
|
+
return isConstructor(constructor) && value instanceof constructor;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
18
27
|
* Is the value not `undefined` or `null`?
|
|
19
28
|
* @param value Value to check
|
|
20
29
|
* @returns `true` if the value is not `undefined` or `null`, otherwise `false`
|
|
@@ -72,4 +81,4 @@ function isPrimitive(value) {
|
|
|
72
81
|
}
|
|
73
82
|
var EXPRESSION_PRIMITIVE = /^(bigint|boolean|number|string|symbol)$/;
|
|
74
83
|
var EXPRESSION_WHITESPACE = /^\s*$/;
|
|
75
|
-
export { isArrayOrPlainObject, isConstructor, isEmpty, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
|
|
84
|
+
export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
|