@oscarpalmer/atoms 0.202.0 → 0.204.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array/difference.d.mts +4 -5
- package/dist/array/exists.d.mts +5 -6
- package/dist/array/filter.d.mts +10 -11
- package/dist/array/filter.mjs +0 -1
- package/dist/array/find.d.mts +10 -11
- package/dist/array/find.mjs +0 -1
- package/dist/array/first.d.mts +10 -11
- package/dist/array/first.mjs +0 -1
- package/dist/array/flatten.d.mts +2 -3
- package/dist/array/from.d.mts +7 -8
- package/dist/array/group-by.d.mts +7 -8
- package/dist/array/group-by.mjs +0 -1
- package/dist/array/insert.d.mts +3 -4
- package/dist/array/intersection.d.mts +4 -5
- package/dist/array/last.d.mts +10 -11
- package/dist/array/last.mjs +0 -1
- package/dist/array/move.d.mts +9 -10
- package/dist/array/move.mjs +0 -4
- package/dist/array/partition.d.mts +5 -6
- package/dist/array/push.d.mts +2 -3
- package/dist/array/reverse.d.mts +2 -3
- package/dist/array/select.d.mts +11 -12
- package/dist/array/select.mjs +0 -1
- package/dist/array/single.d.mts +4 -5
- package/dist/array/slice.d.mts +12 -13
- package/dist/array/sort.d.mts +31 -32
- package/dist/array/sort.mjs +38 -39
- package/dist/array/splice.d.mts +5 -6
- package/dist/array/swap.d.mts +8 -9
- package/dist/array/swap.mjs +0 -1
- package/dist/array/to-map.d.mts +7 -8
- package/dist/array/to-map.mjs +0 -1
- package/dist/array/to-record.d.mts +7 -8
- package/dist/array/to-record.mjs +0 -1
- package/dist/array/to-set.d.mts +4 -5
- package/dist/array/toggle.d.mts +4 -5
- package/dist/array/union.d.mts +4 -5
- package/dist/array/unique.d.mts +4 -5
- package/dist/array/update.d.mts +4 -5
- package/dist/beacon.d.mts +8 -9
- package/dist/beacon.mjs +96 -71
- package/dist/color/constants.d.mts +14 -13
- package/dist/color/constants.mjs +9 -2
- package/dist/color/index.d.mts +2 -2
- package/dist/color/instance.d.mts +3 -3
- package/dist/color/instance.mjs +91 -69
- package/dist/color/misc/alpha.d.mts +7 -6
- package/dist/color/misc/alpha.mjs +8 -2
- package/dist/color/misc/format.d.mts +6 -7
- package/dist/color/misc/format.mjs +15 -12
- package/dist/color/misc/get.d.mts +14 -15
- package/dist/color/misc/is.d.mts +16 -17
- package/dist/color/misc/is.mjs +7 -7
- package/dist/color/misc/state.d.mts +33 -10
- package/dist/color/misc/state.mjs +28 -48
- package/dist/color/models.d.mts +25 -19
- package/dist/color/space/hex.d.mts +14 -11
- package/dist/color/space/hex.mjs +21 -2
- package/dist/color/space/hsl.d.mts +13 -10
- package/dist/color/space/hsl.mjs +25 -4
- package/dist/color/space/hwb.d.mts +14 -11
- package/dist/color/space/hwb.mjs +24 -3
- package/dist/color/space/rgb.d.mts +16 -13
- package/dist/color/space/rgb.mjs +41 -20
- package/dist/function/limit.d.mts +9 -8
- package/dist/function/limit.mjs +5 -7
- package/dist/function/once.d.mts +26 -6
- package/dist/function/once.mjs +129 -76
- package/dist/function/retry.d.mts +5 -6
- package/dist/function/retry.mjs +3 -4
- package/dist/index.d.mts +184 -60
- package/dist/index.mjs +1292 -970
- package/dist/internal/aborter.d.mts +9 -0
- package/dist/internal/{abort.mjs → aborter.mjs} +1 -1
- package/dist/internal/array/callbacks.d.mts +4 -5
- package/dist/internal/array/chunk.d.mts +2 -3
- package/dist/internal/array/compact.d.mts +3 -4
- package/dist/internal/array/find.d.mts +10 -11
- package/dist/internal/array/get.d.mts +7 -8
- package/dist/internal/array/get.mjs +3 -2
- package/dist/internal/array/group.d.mts +2 -3
- package/dist/internal/array/index-of.d.mts +10 -11
- package/dist/internal/array/index-of.mjs +0 -1
- package/dist/internal/array/insert.d.mts +5 -6
- package/dist/internal/array/match.d.mts +17 -18
- package/dist/internal/array/overlap.d.mts +2 -3
- package/dist/internal/array/sets.d.mts +5 -6
- package/dist/internal/array/shuffle.d.mts +2 -3
- package/dist/internal/array/update.d.mts +2 -3
- package/dist/internal/defaults.d.mts +3 -4
- package/dist/internal/function/assert.d.mts +10 -11
- package/dist/internal/function/assert.mjs +0 -7
- package/dist/internal/function/limit.d.mts +41 -0
- package/dist/internal/function/limit.mjs +106 -0
- package/dist/internal/function/memoize.d.mts +4 -5
- package/dist/internal/function/memoize.mjs +55 -31
- package/dist/internal/function/misc.d.mts +2 -3
- package/dist/internal/function/work.d.mts +53 -54
- package/dist/internal/function/work.mjs +0 -2
- package/dist/internal/herald.d.mts +8 -9
- package/dist/internal/herald.mjs +38 -27
- package/dist/internal/is.d.mts +19 -20
- package/dist/internal/math/aggregate.d.mts +14 -15
- package/dist/internal/math/misc.d.mts +3 -4
- package/dist/internal/number.d.mts +4 -5
- package/dist/internal/random.d.mts +3 -4
- package/dist/internal/result/misc.d.mts +29 -14
- package/dist/internal/result/misc.mjs +4 -3
- package/dist/internal/result/models.d.mts +9 -10
- package/dist/internal/sized/map.d.mts +2 -3
- package/dist/internal/sized/misc.d.mts +2 -3
- package/dist/internal/string/case.d.mts +9 -10
- package/dist/internal/string/misc.d.mts +8 -9
- package/dist/internal/subscription.d.mts +10 -11
- package/dist/internal/subscription.mjs +72 -58
- package/dist/internal/value/compare.d.mts +17 -9
- package/dist/internal/value/compare.mjs +6 -11
- package/dist/internal/value/equal.d.mts +34 -15
- package/dist/internal/value/equal.mjs +20 -13
- package/dist/internal/value/get.d.mts +3 -4
- package/dist/internal/value/handlers.d.mts +19 -9
- package/dist/internal/value/handlers.mjs +48 -50
- package/dist/internal/value/has.d.mts +6 -7
- package/dist/internal/value/has.mjs +0 -1
- package/dist/internal/value/misc.d.mts +6 -7
- package/dist/internal/value/misc.mjs +4 -3
- package/dist/internal/value/partial.d.mts +3 -4
- package/dist/internal/value/partial.mjs +2 -1
- package/dist/internal/value/set.d.mts +4 -5
- package/dist/internal/value/set.mjs +2 -1
- package/dist/is.d.mts +13 -13
- package/dist/logger.d.mts +5 -4
- package/dist/logger.mjs +72 -64
- package/dist/math.d.mts +20 -20
- package/dist/models.d.mts +45 -49
- package/dist/promise/constants.d.mts +14 -15
- package/dist/promise/delay.d.mts +3 -4
- package/dist/promise/delay.mjs +5 -5
- package/dist/promise/helpers.d.mts +7 -8
- package/dist/promise/helpers.mjs +4 -3
- package/dist/promise/index.d.mts +13 -14
- package/dist/promise/index.mjs +1 -2
- package/dist/promise/misc.d.mts +7 -7
- package/dist/promise/models.d.mts +17 -18
- package/dist/promise/timed.d.mts +4 -5
- package/dist/promise/timed.mjs +6 -6
- package/dist/query.d.mts +3 -4
- package/dist/queue.d.mts +14 -15
- package/dist/queue.mjs +198 -136
- package/dist/random.d.mts +8 -8
- package/dist/result/index.d.mts +8 -9
- package/dist/result/index.mjs +0 -1
- package/dist/result/match.d.mts +6 -7
- package/dist/result/match.mjs +0 -1
- package/dist/result/misc.d.mts +3 -3
- package/dist/result/work/flow.d.mts +28 -29
- package/dist/result/work/flow.mjs +0 -1
- package/dist/result/work/pipe.d.mts +24 -25
- package/dist/result/work/pipe.mjs +0 -1
- package/dist/sized/set.d.mts +2 -3
- package/dist/string/fuzzy.d.mts +21 -12
- package/dist/string/fuzzy.mjs +77 -52
- package/dist/string/index.d.mts +8 -8
- package/dist/string/match.d.mts +4 -5
- package/dist/string/normalize.d.mts +8 -9
- package/dist/string/normalize.mjs +9 -5
- package/dist/string/template.d.mts +9 -9
- package/dist/string/template.mjs +11 -5
- package/dist/value/clone.d.mts +21 -13
- package/dist/value/clone.mjs +19 -25
- package/dist/value/collection.d.mts +4 -5
- package/dist/value/diff.d.mts +5 -6
- package/dist/value/freeze.d.mts +18 -19
- package/dist/value/freeze.mjs +1 -5
- package/dist/value/merge.d.mts +13 -14
- package/dist/value/merge.mjs +21 -7
- package/dist/value/omit.d.mts +2 -3
- package/dist/value/pick.d.mts +2 -3
- package/dist/value/shake.d.mts +3 -4
- package/dist/value/smush.d.mts +3 -4
- package/dist/value/transform.d.mts +15 -10
- package/dist/value/transform.mjs +12 -8
- package/dist/value/unsmush.d.mts +3 -4
- package/package.json +2 -2
- package/dist/internal/abort.d.mts +0 -10
- package/dist/internal/function/timer.d.mts +0 -10
- package/dist/internal/function/timer.mjs +0 -87
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { getNumberOrDefault } from "../defaults.mjs";
|
|
2
|
+
//#region src/internal/function/limit.ts
|
|
3
|
+
const LIMITER_NAME_ASYNC = "asyncLimiter";
|
|
4
|
+
const LIMITER_NAME_SYNC = "limiter";
|
|
5
|
+
const LIMITER_PROPERTY = "$limiter";
|
|
6
|
+
function AsyncLimiter(type, callback, time) {
|
|
7
|
+
this[LIMITER_SYMBOL] = {
|
|
8
|
+
callback,
|
|
9
|
+
type,
|
|
10
|
+
interval: getNumberOrDefault(time, 0),
|
|
11
|
+
parameters: [],
|
|
12
|
+
throttle: type === LIMITER_THROTTLE
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
AsyncLimiter.prototype[LIMITER_PROPERTY] = LIMITER_NAME_ASYNC;
|
|
16
|
+
AsyncLimiter.prototype.cancel = cancelAsyncLimiter;
|
|
17
|
+
AsyncLimiter.prototype.run = runAsyncLimiter;
|
|
18
|
+
function Limiter(type, callback, time) {
|
|
19
|
+
this[LIMITER_SYMBOL] = {
|
|
20
|
+
callback,
|
|
21
|
+
type,
|
|
22
|
+
interval: getNumberOrDefault(time, 0),
|
|
23
|
+
parameters: [],
|
|
24
|
+
throttle: type === LIMITER_THROTTLE
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
Limiter.prototype[LIMITER_PROPERTY] = LIMITER_NAME_SYNC;
|
|
28
|
+
Limiter.prototype.cancel = cancelLimiter;
|
|
29
|
+
Limiter.prototype.run = runLimiter;
|
|
30
|
+
function cancelAsyncLimiter() {
|
|
31
|
+
const state = this[LIMITER_SYMBOL];
|
|
32
|
+
if (state.timer != null) clearTimer(state.timer);
|
|
33
|
+
if (state.last != null && !state.last.running) {
|
|
34
|
+
state.last.reject();
|
|
35
|
+
state.last = void 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function cancelLimiter() {
|
|
39
|
+
const { timer } = this[LIMITER_SYMBOL];
|
|
40
|
+
if (timer != null) clearTimer(timer);
|
|
41
|
+
}
|
|
42
|
+
function getAsyncLimiter(type, callback, time) {
|
|
43
|
+
return new AsyncLimiter(type, callback, time);
|
|
44
|
+
}
|
|
45
|
+
function getLimiter(type, callback, time) {
|
|
46
|
+
return new Limiter(type, callback, time);
|
|
47
|
+
}
|
|
48
|
+
async function handleAsyncLimiter(state, item) {
|
|
49
|
+
const now = performance.now();
|
|
50
|
+
state.start ??= now;
|
|
51
|
+
if (state.interval === 0 || now - state.start >= state.interval - LIMITER_OFFSET) {
|
|
52
|
+
state.start = state.throttle ? now : void 0;
|
|
53
|
+
item.running = true;
|
|
54
|
+
try {
|
|
55
|
+
let result = state.callback(...item.parameters);
|
|
56
|
+
if (result instanceof Promise) result = await result;
|
|
57
|
+
item.resolve(result);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
item.reject(error);
|
|
60
|
+
} finally {
|
|
61
|
+
item.running = false;
|
|
62
|
+
}
|
|
63
|
+
} else state.timer = startTimer(() => handleAsyncLimiter(state, item));
|
|
64
|
+
}
|
|
65
|
+
function handleLimiter(state) {
|
|
66
|
+
const now = performance.now();
|
|
67
|
+
state.start ??= now;
|
|
68
|
+
if (state.interval === 0 || now - state.start >= state.interval - LIMITER_OFFSET) {
|
|
69
|
+
state.start = state.throttle ? now : void 0;
|
|
70
|
+
state.callback(...state.parameters);
|
|
71
|
+
} else state.timer = startTimer(() => handleLimiter(state));
|
|
72
|
+
}
|
|
73
|
+
function runAsyncLimiter() {
|
|
74
|
+
const state = this[LIMITER_SYMBOL];
|
|
75
|
+
cancelAsyncLimiter.call(this);
|
|
76
|
+
const next = {
|
|
77
|
+
parameters: state.parameters,
|
|
78
|
+
running: false
|
|
79
|
+
};
|
|
80
|
+
next.promise = new Promise((resolve, reject) => {
|
|
81
|
+
next.reject = reject;
|
|
82
|
+
next.resolve = resolve;
|
|
83
|
+
});
|
|
84
|
+
state.last = next;
|
|
85
|
+
if (state.throttle) handleAsyncLimiter(state, next);
|
|
86
|
+
else state.timer = startTimer(() => handleAsyncLimiter(state, next));
|
|
87
|
+
return next.promise;
|
|
88
|
+
}
|
|
89
|
+
function runLimiter(...parameters) {
|
|
90
|
+
const state = this[LIMITER_SYMBOL];
|
|
91
|
+
cancelLimiter.call(this);
|
|
92
|
+
state.parameters = parameters;
|
|
93
|
+
if (state.throttle) handleLimiter(state);
|
|
94
|
+
else state.timer = startTimer(() => handleLimiter(state));
|
|
95
|
+
}
|
|
96
|
+
const LIMITER_DEBOUNCE = "debounce";
|
|
97
|
+
const LIMITER_OFFSET = 5;
|
|
98
|
+
const LIMITER_SYMBOL = Symbol("limiter");
|
|
99
|
+
const LIMITER_THROTTLE = "throttle";
|
|
100
|
+
const LIMITER_WAIT = "wait";
|
|
101
|
+
// istanbul ignore next
|
|
102
|
+
const clearTimer = typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : clearTimeout;
|
|
103
|
+
// istanbul ignore next
|
|
104
|
+
const startTimer = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout;
|
|
105
|
+
//#endregion
|
|
106
|
+
export { LIMITER_DEBOUNCE, LIMITER_THROTTLE, LIMITER_WAIT, getAsyncLimiter, getLimiter };
|
|
@@ -3,7 +3,7 @@ import { GenericCallback } from "../../models.mjs";
|
|
|
3
3
|
/**
|
|
4
4
|
* A _Memoized_ function instance, caching and retrieving results based on the its parameters _(or a custom cache key)_
|
|
5
5
|
*/
|
|
6
|
-
type Memoized<Callback extends GenericCallback> = {
|
|
6
|
+
export type Memoized<Callback extends GenericCallback> = {
|
|
7
7
|
/**
|
|
8
8
|
* Maximum cache size
|
|
9
9
|
*
|
|
@@ -58,7 +58,7 @@ type Memoized<Callback extends GenericCallback> = {
|
|
|
58
58
|
/**
|
|
59
59
|
* Options for a _Memoized_ function
|
|
60
60
|
*/
|
|
61
|
-
type MemoizedOptions<Callback extends GenericCallback> = {
|
|
61
|
+
export type MemoizedOptions<Callback extends GenericCallback> = {
|
|
62
62
|
/**
|
|
63
63
|
* Callback for getting a cache key for the provided parameters
|
|
64
64
|
*/
|
|
@@ -75,6 +75,5 @@ type MemoizedOptions<Callback extends GenericCallback> = {
|
|
|
75
75
|
* @param options Memoization options
|
|
76
76
|
* @returns _Memoized_ instance
|
|
77
77
|
*/
|
|
78
|
-
declare function memoize<Callback extends GenericCallback>(callback: Callback, options?: MemoizedOptions<Callback>): Memoized<Callback>;
|
|
79
|
-
//#endregion
|
|
80
|
-
export { Memoized, MemoizedOptions, memoize };
|
|
78
|
+
export declare function memoize<Callback extends GenericCallback>(callback: Callback, options?: MemoizedOptions<Callback>): Memoized<Callback>;
|
|
79
|
+
//#endregion
|
|
@@ -3,6 +3,44 @@ import { getString, join } from "../string/misc.mjs";
|
|
|
3
3
|
import { getNumberOrDefault } from "../defaults.mjs";
|
|
4
4
|
import { SizedMap } from "../sized/map.mjs";
|
|
5
5
|
//#region src/internal/function/memoize.ts
|
|
6
|
+
function Memoized(callback, options) {
|
|
7
|
+
this[MEMOIZED_SYMBOL] = {
|
|
8
|
+
options,
|
|
9
|
+
cache: new SizedMap(options.cacheSize),
|
|
10
|
+
getter: void 0
|
|
11
|
+
};
|
|
12
|
+
this[MEMOIZED_SYMBOL].getter = createGetter(this[MEMOIZED_SYMBOL], callback);
|
|
13
|
+
}
|
|
14
|
+
Memoized.prototype.clear = clearMemoized;
|
|
15
|
+
Memoized.prototype.delete = deleteMemoizedValue;
|
|
16
|
+
Memoized.prototype.get = getMemoizedValue;
|
|
17
|
+
Memoized.prototype.has = hasMemoizedValue;
|
|
18
|
+
Memoized.prototype.run = runMemoized;
|
|
19
|
+
Object.defineProperties(Memoized.prototype, {
|
|
20
|
+
maximum: {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: getMemoizedMaximum
|
|
23
|
+
},
|
|
24
|
+
size: {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: getMemoizedSize
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
function clearMemoized() {
|
|
30
|
+
this[MEMOIZED_SYMBOL].cache.clear();
|
|
31
|
+
}
|
|
32
|
+
function deleteMemoizedValue(key) {
|
|
33
|
+
return this[MEMOIZED_SYMBOL].cache.delete(key);
|
|
34
|
+
}
|
|
35
|
+
function createGetter(state, callback) {
|
|
36
|
+
return (...parameters) => {
|
|
37
|
+
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), MEMOIZED_KEY_SEPARATOR));
|
|
38
|
+
if (state.cache.has(key)) return state.cache.get(key);
|
|
39
|
+
const value = callback(...parameters);
|
|
40
|
+
state.cache.set(key, value);
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
6
44
|
function createMemoizationOptions(input) {
|
|
7
45
|
const { cacheKey, cacheSize } = isPlainObject(input) ? input : {};
|
|
8
46
|
return {
|
|
@@ -10,6 +48,18 @@ function createMemoizationOptions(input) {
|
|
|
10
48
|
cacheSize: getNumberOrDefault(cacheSize, MEMOIZED_CACHE_SIZE_DEFAULT)
|
|
11
49
|
};
|
|
12
50
|
}
|
|
51
|
+
function getMemoizedMaximum() {
|
|
52
|
+
return this[MEMOIZED_SYMBOL].cache.maximum;
|
|
53
|
+
}
|
|
54
|
+
function getMemoizedSize() {
|
|
55
|
+
return this[MEMOIZED_SYMBOL].cache.size;
|
|
56
|
+
}
|
|
57
|
+
function getMemoizedValue(key) {
|
|
58
|
+
return this[MEMOIZED_SYMBOL].cache.get(key);
|
|
59
|
+
}
|
|
60
|
+
function hasMemoizedValue(key) {
|
|
61
|
+
return this[MEMOIZED_SYMBOL].cache.has(key);
|
|
62
|
+
}
|
|
13
63
|
/**
|
|
14
64
|
* Memoize a function, caching and retrieving results based on the first parameter
|
|
15
65
|
*
|
|
@@ -19,40 +69,14 @@ function createMemoizationOptions(input) {
|
|
|
19
69
|
*/
|
|
20
70
|
function memoize(callback, options) {
|
|
21
71
|
if (typeof callback !== "function") throw new TypeError(MEMOIZED_CALLBACK);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
state.cache = new SizedMap(state.options.cacheSize);
|
|
28
|
-
state.getter = (...parameters) => {
|
|
29
|
-
const key = state.options.cacheKey?.(...parameters) ?? (parameters.length === 1 ? parameters[0] : join(parameters.map(getString), MEMOIZED_KEY_SEPARATOR));
|
|
30
|
-
if (state.cache.has(key)) return state.cache.get(key);
|
|
31
|
-
const value = callback(...parameters);
|
|
32
|
-
state.cache.set(key, value);
|
|
33
|
-
return value;
|
|
34
|
-
};
|
|
35
|
-
const instance = {
|
|
36
|
-
clear: () => state.cache.clear(),
|
|
37
|
-
delete: (key) => state.cache.delete(key),
|
|
38
|
-
get: (key) => state.cache.get(key),
|
|
39
|
-
has: (key) => state.cache.has(key),
|
|
40
|
-
run: (...parameters) => state.getter(...parameters)
|
|
41
|
-
};
|
|
42
|
-
Object.defineProperties(instance, {
|
|
43
|
-
maximum: {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
get: () => state.cache.maximum
|
|
46
|
-
},
|
|
47
|
-
size: {
|
|
48
|
-
enumerable: true,
|
|
49
|
-
get: () => state.cache.size
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
return Object.freeze(instance);
|
|
72
|
+
return new Memoized(callback, createMemoizationOptions(options));
|
|
73
|
+
}
|
|
74
|
+
function runMemoized(...parameters) {
|
|
75
|
+
return this[MEMOIZED_SYMBOL].getter(...parameters);
|
|
53
76
|
}
|
|
54
77
|
const MEMOIZED_CACHE_SIZE_DEFAULT = 1024;
|
|
55
78
|
const MEMOIZED_CALLBACK = "Memoized requires a callback function";
|
|
56
79
|
const MEMOIZED_KEY_SEPARATOR = "_";
|
|
80
|
+
const MEMOIZED_SYMBOL = Symbol("memoized");
|
|
57
81
|
//#endregion
|
|
58
82
|
export { memoize };
|