@fluixi/utils 1.0.0-alpha.53
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/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/lib/array/advanced.cjs +1 -0
- package/dist/lib/array/advanced.d.ts +1116 -0
- package/dist/lib/array/advanced.d.ts.map +1 -0
- package/dist/lib/array/advanced.js +2429 -0
- package/dist/lib/array/advanced.mjs +1 -0
- package/dist/lib/array/array.cjs +1 -0
- package/dist/lib/array/array.d.ts +6 -0
- package/dist/lib/array/array.d.ts.map +1 -0
- package/dist/lib/array/array.js +31 -0
- package/dist/lib/array/array.mjs +1 -0
- package/dist/lib/array/index.cjs +1 -0
- package/dist/lib/array/index.d.ts +4 -0
- package/dist/lib/array/index.d.ts.map +1 -0
- package/dist/lib/array/index.js +3 -0
- package/dist/lib/array/index.mjs +1 -0
- package/dist/lib/array/utils.cjs +1 -0
- package/dist/lib/array/utils.d.ts +100 -0
- package/dist/lib/array/utils.d.ts.map +1 -0
- package/dist/lib/array/utils.js +165 -0
- package/dist/lib/array/utils.mjs +1 -0
- package/dist/lib/color/color.cjs +1 -0
- package/dist/lib/color/color.d.ts +366 -0
- package/dist/lib/color/color.d.ts.map +1 -0
- package/dist/lib/color/color.js +722 -0
- package/dist/lib/color/color.mjs +1 -0
- package/dist/lib/color/constants.cjs +1 -0
- package/dist/lib/color/constants.d.ts +361 -0
- package/dist/lib/color/constants.d.ts.map +1 -0
- package/dist/lib/color/constants.js +358 -0
- package/dist/lib/color/constants.mjs +1 -0
- package/dist/lib/color/conversions.cjs +1 -0
- package/dist/lib/color/conversions.d.ts +166 -0
- package/dist/lib/color/conversions.d.ts.map +1 -0
- package/dist/lib/color/conversions.js +766 -0
- package/dist/lib/color/conversions.mjs +1 -0
- package/dist/lib/color/helpers.cjs +1 -0
- package/dist/lib/color/helpers.d.ts +75 -0
- package/dist/lib/color/helpers.d.ts.map +1 -0
- package/dist/lib/color/helpers.js +419 -0
- package/dist/lib/color/helpers.mjs +1 -0
- package/dist/lib/color/index.cjs +1 -0
- package/dist/lib/color/index.d.ts +12 -0
- package/dist/lib/color/index.d.ts.map +1 -0
- package/dist/lib/color/index.js +15 -0
- package/dist/lib/color/index.mjs +1 -0
- package/dist/lib/color/types.cjs +1 -0
- package/dist/lib/color/types.d.ts +179 -0
- package/dist/lib/color/types.d.ts.map +1 -0
- package/dist/lib/color/types.js +1 -0
- package/dist/lib/color/types.mjs +0 -0
- package/dist/lib/color/utils.cjs +1 -0
- package/dist/lib/color/utils.d.ts +199 -0
- package/dist/lib/color/utils.d.ts.map +1 -0
- package/dist/lib/color/utils.js +858 -0
- package/dist/lib/color/utils.mjs +1 -0
- package/dist/lib/compare/comparators.cjs +1 -0
- package/dist/lib/compare/comparators.d.ts +65 -0
- package/dist/lib/compare/comparators.d.ts.map +1 -0
- package/dist/lib/compare/comparators.js +210 -0
- package/dist/lib/compare/comparators.mjs +1 -0
- package/dist/lib/compare/index.cjs +1 -0
- package/dist/lib/compare/index.d.ts +2 -0
- package/dist/lib/compare/index.d.ts.map +1 -0
- package/dist/lib/compare/index.js +1 -0
- package/dist/lib/compare/index.mjs +1 -0
- package/dist/lib/crypto/hash.cjs +1 -0
- package/dist/lib/crypto/hash.d.ts +6 -0
- package/dist/lib/crypto/hash.d.ts.map +1 -0
- package/dist/lib/crypto/hash.js +53 -0
- package/dist/lib/crypto/hash.mjs +1 -0
- package/dist/lib/crypto/index.cjs +1 -0
- package/dist/lib/crypto/index.d.ts +2 -0
- package/dist/lib/crypto/index.d.ts.map +1 -0
- package/dist/lib/crypto/index.js +1 -0
- package/dist/lib/crypto/index.mjs +1 -0
- package/dist/lib/dom/events.cjs +1 -0
- package/dist/lib/dom/events.d.ts +159 -0
- package/dist/lib/dom/events.d.ts.map +1 -0
- package/dist/lib/dom/events.js +50 -0
- package/dist/lib/dom/events.mjs +1 -0
- package/dist/lib/dom/index.cjs +1 -0
- package/dist/lib/dom/index.d.ts +3 -0
- package/dist/lib/dom/index.d.ts.map +1 -0
- package/dist/lib/dom/index.js +2 -0
- package/dist/lib/dom/index.mjs +1 -0
- package/dist/lib/dom/utils.cjs +1 -0
- package/dist/lib/dom/utils.d.ts +6 -0
- package/dist/lib/dom/utils.d.ts.map +1 -0
- package/dist/lib/dom/utils.js +37 -0
- package/dist/lib/dom/utils.mjs +1 -0
- package/dist/lib/functions/functions.cjs +1 -0
- package/dist/lib/functions/functions.d.ts +329 -0
- package/dist/lib/functions/functions.d.ts.map +1 -0
- package/dist/lib/functions/functions.js +585 -0
- package/dist/lib/functions/functions.mjs +1 -0
- package/dist/lib/functions/index.cjs +1 -0
- package/dist/lib/functions/index.d.ts +3 -0
- package/dist/lib/functions/index.d.ts.map +1 -0
- package/dist/lib/functions/index.js +1 -0
- package/dist/lib/functions/index.mjs +1 -0
- package/dist/lib/functions/types.cjs +1 -0
- package/dist/lib/functions/types.d.ts +2 -0
- package/dist/lib/functions/types.d.ts.map +1 -0
- package/dist/lib/functions/types.js +1 -0
- package/dist/lib/functions/types.mjs +0 -0
- package/dist/lib/helpers.cjs +1 -0
- package/dist/lib/helpers.d.ts +13 -0
- package/dist/lib/helpers.d.ts.map +1 -0
- package/dist/lib/helpers.js +66 -0
- package/dist/lib/helpers.mjs +1 -0
- package/dist/lib/index.cjs +1 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +8 -0
- package/dist/lib/index.mjs +1 -0
- package/dist/lib/maths/constants.cjs +1 -0
- package/dist/lib/maths/constants.d.ts +84 -0
- package/dist/lib/maths/constants.d.ts.map +1 -0
- package/dist/lib/maths/constants.js +79 -0
- package/dist/lib/maths/constants.mjs +1 -0
- package/dist/lib/maths/fn/advanced.cjs +1 -0
- package/dist/lib/maths/fn/advanced.d.ts +149 -0
- package/dist/lib/maths/fn/advanced.d.ts.map +1 -0
- package/dist/lib/maths/fn/advanced.js +264 -0
- package/dist/lib/maths/fn/advanced.mjs +1 -0
- package/dist/lib/maths/fn/basic.cjs +1 -0
- package/dist/lib/maths/fn/basic.d.ts +182 -0
- package/dist/lib/maths/fn/basic.d.ts.map +1 -0
- package/dist/lib/maths/fn/basic.js +343 -0
- package/dist/lib/maths/fn/basic.mjs +1 -0
- package/dist/lib/maths/fn/complex.cjs +1 -0
- package/dist/lib/maths/fn/complex.d.ts +153 -0
- package/dist/lib/maths/fn/complex.d.ts.map +1 -0
- package/dist/lib/maths/fn/complex.js +383 -0
- package/dist/lib/maths/fn/complex.mjs +1 -0
- package/dist/lib/maths/fn/index.cjs +1 -0
- package/dist/lib/maths/fn/index.d.ts +10 -0
- package/dist/lib/maths/fn/index.d.ts.map +1 -0
- package/dist/lib/maths/fn/index.js +9 -0
- package/dist/lib/maths/fn/index.mjs +1 -0
- package/dist/lib/maths/fn/trigo.cjs +1 -0
- package/dist/lib/maths/fn/trigo.d.ts +147 -0
- package/dist/lib/maths/fn/trigo.d.ts.map +1 -0
- package/dist/lib/maths/fn/trigo.js +227 -0
- package/dist/lib/maths/fn/trigo.mjs +1 -0
- package/dist/lib/maths/fn/vector.cjs +1 -0
- package/dist/lib/maths/fn/vector.d.ts +512 -0
- package/dist/lib/maths/fn/vector.d.ts.map +1 -0
- package/dist/lib/maths/fn/vector.js +1136 -0
- package/dist/lib/maths/fn/vector.mjs +1 -0
- package/dist/lib/maths/formatter.cjs +1 -0
- package/dist/lib/maths/formatter.d.ts +35 -0
- package/dist/lib/maths/formatter.d.ts.map +1 -0
- package/dist/lib/maths/formatter.js +56 -0
- package/dist/lib/maths/formatter.mjs +1 -0
- package/dist/lib/maths/guards.cjs +1 -0
- package/dist/lib/maths/guards.d.ts +166 -0
- package/dist/lib/maths/guards.d.ts.map +1 -0
- package/dist/lib/maths/guards.js +315 -0
- package/dist/lib/maths/guards.mjs +1 -0
- package/dist/lib/maths/index.cjs +1 -0
- package/dist/lib/maths/index.d.ts +24 -0
- package/dist/lib/maths/index.d.ts.map +1 -0
- package/dist/lib/maths/index.js +29 -0
- package/dist/lib/maths/index.mjs +1 -0
- package/dist/lib/maths/parser.cjs +1 -0
- package/dist/lib/maths/parser.d.ts +20 -0
- package/dist/lib/maths/parser.d.ts.map +1 -0
- package/dist/lib/maths/parser.js +78 -0
- package/dist/lib/maths/parser.mjs +1 -0
- package/dist/lib/maths/random.cjs +1 -0
- package/dist/lib/maths/random.d.ts +27 -0
- package/dist/lib/maths/random.d.ts.map +1 -0
- package/dist/lib/maths/random.js +49 -0
- package/dist/lib/maths/random.mjs +1 -0
- package/dist/lib/maths/types/advanced.cjs +1 -0
- package/dist/lib/maths/types/advanced.d.ts +357 -0
- package/dist/lib/maths/types/advanced.d.ts.map +1 -0
- package/dist/lib/maths/types/advanced.js +5 -0
- package/dist/lib/maths/types/advanced.mjs +0 -0
- package/dist/lib/maths/types/basic.cjs +1 -0
- package/dist/lib/maths/types/basic.d.ts +95 -0
- package/dist/lib/maths/types/basic.d.ts.map +1 -0
- package/dist/lib/maths/types/basic.js +5 -0
- package/dist/lib/maths/types/basic.mjs +0 -0
- package/dist/lib/maths/types/index.cjs +1 -0
- package/dist/lib/maths/types/index.d.ts +4 -0
- package/dist/lib/maths/types/index.d.ts.map +1 -0
- package/dist/lib/maths/types/index.js +3 -0
- package/dist/lib/maths/types/index.mjs +0 -0
- package/dist/lib/maths/types/negative.cjs +1 -0
- package/dist/lib/maths/types/negative.d.ts +51 -0
- package/dist/lib/maths/types/negative.d.ts.map +1 -0
- package/dist/lib/maths/types/negative.js +5 -0
- package/dist/lib/maths/types/negative.mjs +0 -0
- package/dist/lib/maths/types/operations.cjs +1 -0
- package/dist/lib/maths/types/operations.d.ts +188 -0
- package/dist/lib/maths/types/operations.d.ts.map +1 -0
- package/dist/lib/maths/types/operations.js +6 -0
- package/dist/lib/maths/types/operations.mjs +0 -0
- package/dist/lib/object/clone.cjs +1 -0
- package/dist/lib/object/clone.d.ts +46 -0
- package/dist/lib/object/clone.d.ts.map +1 -0
- package/dist/lib/object/clone.js +104 -0
- package/dist/lib/object/clone.mjs +1 -0
- package/dist/lib/object/diff.cjs +1 -0
- package/dist/lib/object/diff.d.ts +203 -0
- package/dist/lib/object/diff.d.ts.map +1 -0
- package/dist/lib/object/diff.js +805 -0
- package/dist/lib/object/diff.mjs +1 -0
- package/dist/lib/object/getter-setter.cjs +1 -0
- package/dist/lib/object/getter-setter.d.ts +361 -0
- package/dist/lib/object/getter-setter.d.ts.map +1 -0
- package/dist/lib/object/getter-setter.js +1771 -0
- package/dist/lib/object/getter-setter.mjs +1 -0
- package/dist/lib/object/index.cjs +6 -0
- package/dist/lib/object/index.d.ts +13 -0
- package/dist/lib/object/index.d.ts.map +1 -0
- package/dist/lib/object/index.js +12 -0
- package/dist/lib/object/index.mjs +6 -0
- package/dist/lib/object/merge.cjs +1 -0
- package/dist/lib/object/merge.d.ts +93 -0
- package/dist/lib/object/merge.d.ts.map +1 -0
- package/dist/lib/object/merge.js +474 -0
- package/dist/lib/object/merge.mjs +1 -0
- package/dist/lib/object/object.cjs +1 -0
- package/dist/lib/object/object.d.ts +128 -0
- package/dist/lib/object/object.d.ts.map +1 -0
- package/dist/lib/object/object.js +432 -0
- package/dist/lib/object/object.mjs +1 -0
- package/dist/lib/object/omit.cjs +1 -0
- package/dist/lib/object/omit.d.ts +351 -0
- package/dist/lib/object/omit.d.ts.map +1 -0
- package/dist/lib/object/omit.js +656 -0
- package/dist/lib/object/omit.mjs +1 -0
- package/dist/lib/object/reconcile.cjs +1 -0
- package/dist/lib/object/reconcile.d.ts +7 -0
- package/dist/lib/object/reconcile.d.ts.map +1 -0
- package/dist/lib/object/reconcile.js +122 -0
- package/dist/lib/object/reconcile.mjs +1 -0
- package/dist/lib/object/selector.cjs +1 -0
- package/dist/lib/object/selector.d.ts +50 -0
- package/dist/lib/object/selector.d.ts.map +1 -0
- package/dist/lib/object/selector.js +371 -0
- package/dist/lib/object/selector.mjs +1 -0
- package/dist/lib/object/sort.cjs +1 -0
- package/dist/lib/object/sort.d.ts +149 -0
- package/dist/lib/object/sort.d.ts.map +1 -0
- package/dist/lib/object/sort.js +298 -0
- package/dist/lib/object/sort.mjs +1 -0
- package/dist/lib/object/transform.cjs +1 -0
- package/dist/lib/object/transform.d.ts +620 -0
- package/dist/lib/object/transform.d.ts.map +1 -0
- package/dist/lib/object/transform.js +1058 -0
- package/dist/lib/object/transform.mjs +1 -0
- package/dist/lib/object/types.cjs +1 -0
- package/dist/lib/object/types.d.ts +250 -0
- package/dist/lib/object/types.d.ts.map +1 -0
- package/dist/lib/object/types.js +1 -0
- package/dist/lib/object/types.mjs +0 -0
- package/dist/lib/object/utils.cjs +6 -0
- package/dist/lib/object/utils.d.ts +165 -0
- package/dist/lib/object/utils.d.ts.map +1 -0
- package/dist/lib/object/utils.js +444 -0
- package/dist/lib/object/utils.mjs +6 -0
- package/dist/lib/primitive/boolean/boolean.cjs +1 -0
- package/dist/lib/primitive/boolean/boolean.d.ts +276 -0
- package/dist/lib/primitive/boolean/boolean.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/boolean.js +615 -0
- package/dist/lib/primitive/boolean/boolean.mjs +1 -0
- package/dist/lib/primitive/boolean/index.cjs +1 -0
- package/dist/lib/primitive/boolean/index.d.ts +3 -0
- package/dist/lib/primitive/boolean/index.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/index.js +2 -0
- package/dist/lib/primitive/boolean/index.mjs +1 -0
- package/dist/lib/primitive/boolean/types.cjs +1 -0
- package/dist/lib/primitive/boolean/types.d.ts +14 -0
- package/dist/lib/primitive/boolean/types.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/types.js +1 -0
- package/dist/lib/primitive/boolean/types.mjs +0 -0
- package/dist/lib/primitive/date/date.cjs +1 -0
- package/dist/lib/primitive/date/date.d.ts +641 -0
- package/dist/lib/primitive/date/date.d.ts.map +1 -0
- package/dist/lib/primitive/date/date.js +1998 -0
- package/dist/lib/primitive/date/date.mjs +1 -0
- package/dist/lib/primitive/date/index.cjs +1 -0
- package/dist/lib/primitive/date/index.d.ts +3 -0
- package/dist/lib/primitive/date/index.d.ts.map +1 -0
- package/dist/lib/primitive/date/index.js +2 -0
- package/dist/lib/primitive/date/index.mjs +1 -0
- package/dist/lib/primitive/date/types.cjs +1 -0
- package/dist/lib/primitive/date/types.d.ts +205 -0
- package/dist/lib/primitive/date/types.d.ts.map +1 -0
- package/dist/lib/primitive/date/types.js +7 -0
- package/dist/lib/primitive/date/types.mjs +0 -0
- package/dist/lib/primitive/index.cjs +5 -0
- package/dist/lib/primitive/index.d.ts +5 -0
- package/dist/lib/primitive/index.d.ts.map +1 -0
- package/dist/lib/primitive/index.js +4 -0
- package/dist/lib/primitive/index.mjs +5 -0
- package/dist/lib/primitive/string/index.cjs +5 -0
- package/dist/lib/primitive/string/index.d.ts +3 -0
- package/dist/lib/primitive/string/index.d.ts.map +1 -0
- package/dist/lib/primitive/string/index.js +2 -0
- package/dist/lib/primitive/string/index.mjs +5 -0
- package/dist/lib/primitive/string/parser-2.cjs +8 -0
- package/dist/lib/primitive/string/parser-2.d.ts +17 -0
- package/dist/lib/primitive/string/parser-2.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser-2.js +656 -0
- package/dist/lib/primitive/string/parser-2.mjs +8 -0
- package/dist/lib/primitive/string/parser.cjs +3 -0
- package/dist/lib/primitive/string/parser.d.ts +9 -0
- package/dist/lib/primitive/string/parser.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser.js +775 -0
- package/dist/lib/primitive/string/parser.mjs +3 -0
- package/dist/lib/primitive/string/string.cjs +5 -0
- package/dist/lib/primitive/string/string.d.ts +535 -0
- package/dist/lib/primitive/string/string.d.ts.map +1 -0
- package/dist/lib/primitive/string/string.js +1301 -0
- package/dist/lib/primitive/string/string.mjs +5 -0
- package/dist/lib/primitive/string/types.cjs +1 -0
- package/dist/lib/primitive/string/types.d.ts +318 -0
- package/dist/lib/primitive/string/types.d.ts.map +1 -0
- package/dist/lib/primitive/string/types.js +5 -0
- package/dist/lib/primitive/string/types.mjs +0 -0
- package/dist/lib/primitive/types.cjs +1 -0
- package/dist/lib/primitive/types.d.ts +27 -0
- package/dist/lib/primitive/types.d.ts.map +1 -0
- package/dist/lib/primitive/types.js +1 -0
- package/dist/lib/primitive/types.mjs +0 -0
- package/dist/lib/url/index.cjs +1 -0
- package/dist/lib/url/index.d.ts +3 -0
- package/dist/lib/url/index.d.ts.map +1 -0
- package/dist/lib/url/index.js +2 -0
- package/dist/lib/url/index.mjs +1 -0
- package/dist/lib/url/parser.cjs +1 -0
- package/dist/lib/url/parser.d.ts +216 -0
- package/dist/lib/url/parser.d.ts.map +1 -0
- package/dist/lib/url/parser.js +488 -0
- package/dist/lib/url/parser.mjs +1 -0
- package/dist/lib/url/types.cjs +1 -0
- package/dist/lib/url/types.d.ts +18 -0
- package/dist/lib/url/types.d.ts.map +1 -0
- package/dist/lib/url/types.js +1 -0
- package/dist/lib/url/types.mjs +0 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +150 -0
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import { isClass } from '@fluixi/utils/object';
|
|
2
|
+
/**
|
|
3
|
+
* Check if value is a function
|
|
4
|
+
* @param v - The value to check
|
|
5
|
+
* @returns True if the value is a function, false otherwise
|
|
6
|
+
*/
|
|
7
|
+
export function isFunction(v) {
|
|
8
|
+
return typeof v === 'function' && !isClass(v);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Check if value is a Promise
|
|
12
|
+
* @param v - The value to check
|
|
13
|
+
* @returns True if the value is a Promise, false otherwise
|
|
14
|
+
*/
|
|
15
|
+
export function isPromise(v) {
|
|
16
|
+
return (v instanceof Promise ||
|
|
17
|
+
(!!v && (typeof v === 'object' || typeof v === 'function') && 'then' in v));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if value is an async function
|
|
21
|
+
* @param v - The value to check
|
|
22
|
+
* @returns True if the value is an async function, false otherwise
|
|
23
|
+
*/
|
|
24
|
+
export function isAsyncFunction(v) {
|
|
25
|
+
return isFunction(v) && v.constructor.name === 'AsyncFunction';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Memoizes a function, caching results based on arguments
|
|
29
|
+
* @param fn - Function to memoize
|
|
30
|
+
* @param resolver - Optional function to generate cache key
|
|
31
|
+
* @returns Memoized function with cache property
|
|
32
|
+
* @example
|
|
33
|
+
* const expensive = memoize((n: number) => {
|
|
34
|
+
* return n * n;
|
|
35
|
+
* });
|
|
36
|
+
* expensive(5); // Computed
|
|
37
|
+
* expensive(5); // Cached
|
|
38
|
+
*/
|
|
39
|
+
export function memoize(fn, resolver) {
|
|
40
|
+
const cache = new Map();
|
|
41
|
+
const memoized = function (...args) {
|
|
42
|
+
const key = resolver ? resolver(...args) : JSON.stringify(args);
|
|
43
|
+
if (cache.has(key)) {
|
|
44
|
+
return cache.get(key);
|
|
45
|
+
}
|
|
46
|
+
const result = fn.apply(this, args);
|
|
47
|
+
cache.set(key, result);
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
memoized.cache = cache;
|
|
51
|
+
return memoized;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a debounced function that delays invoking func until after wait milliseconds
|
|
55
|
+
* @param fn - Function to debounce
|
|
56
|
+
* @param wait - Milliseconds to wait
|
|
57
|
+
* @param options - Debounce options
|
|
58
|
+
* @returns Debounced function with cancel method
|
|
59
|
+
* @example
|
|
60
|
+
* const debounced = debounce(() => console.log('Search'), 300);
|
|
61
|
+
* debounced(); // Will execute after 300ms of inactivity
|
|
62
|
+
*/
|
|
63
|
+
export function debounce(fn, wait, options = {}) {
|
|
64
|
+
const { leading = false, trailing = true, maxWait } = options;
|
|
65
|
+
let timerId;
|
|
66
|
+
let maxTimerId;
|
|
67
|
+
let lastCallTime;
|
|
68
|
+
let lastInvokeTime = 0;
|
|
69
|
+
let lastArgs;
|
|
70
|
+
let lastThis;
|
|
71
|
+
let result;
|
|
72
|
+
function invokeFunc(time) {
|
|
73
|
+
const args = lastArgs;
|
|
74
|
+
const thisArg = lastThis;
|
|
75
|
+
lastArgs = lastThis = undefined;
|
|
76
|
+
lastInvokeTime = time;
|
|
77
|
+
result = fn.apply(thisArg, args);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
function startTimer(pendingFunc, wait) {
|
|
81
|
+
return setTimeout(pendingFunc, wait);
|
|
82
|
+
}
|
|
83
|
+
function cancelTimer(id) {
|
|
84
|
+
if (id !== undefined) {
|
|
85
|
+
clearTimeout(id);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function leadingEdge(time) {
|
|
89
|
+
lastInvokeTime = time;
|
|
90
|
+
timerId = startTimer(timerExpired, wait);
|
|
91
|
+
return leading ? invokeFunc(time) : result;
|
|
92
|
+
}
|
|
93
|
+
function remainingWait(time) {
|
|
94
|
+
const timeSinceLastCall = time - lastCallTime;
|
|
95
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
96
|
+
const timeWaiting = wait - timeSinceLastCall;
|
|
97
|
+
return maxWait !== undefined
|
|
98
|
+
? Math.min(timeWaiting, maxWait - timeSinceLastInvoke)
|
|
99
|
+
: timeWaiting;
|
|
100
|
+
}
|
|
101
|
+
function shouldInvoke(time) {
|
|
102
|
+
const timeSinceLastCall = time - lastCallTime;
|
|
103
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
104
|
+
return (lastCallTime === undefined ||
|
|
105
|
+
timeSinceLastCall >= wait ||
|
|
106
|
+
timeSinceLastCall < 0 ||
|
|
107
|
+
(maxWait !== undefined && timeSinceLastInvoke >= maxWait));
|
|
108
|
+
}
|
|
109
|
+
function timerExpired() {
|
|
110
|
+
const time = Date.now();
|
|
111
|
+
if (shouldInvoke(time)) {
|
|
112
|
+
trailingEdge(time);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
timerId = startTimer(timerExpired, remainingWait(time));
|
|
116
|
+
}
|
|
117
|
+
function trailingEdge(time) {
|
|
118
|
+
timerId = undefined;
|
|
119
|
+
if (trailing && lastArgs) {
|
|
120
|
+
return invokeFunc(time);
|
|
121
|
+
}
|
|
122
|
+
lastArgs = lastThis = undefined;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
function cancel() {
|
|
126
|
+
cancelTimer(timerId);
|
|
127
|
+
cancelTimer(maxTimerId);
|
|
128
|
+
lastInvokeTime = 0;
|
|
129
|
+
lastArgs = lastCallTime = lastThis = timerId = maxTimerId = undefined;
|
|
130
|
+
}
|
|
131
|
+
function flush() {
|
|
132
|
+
return timerId === undefined ? result : trailingEdge(Date.now());
|
|
133
|
+
}
|
|
134
|
+
function pending() {
|
|
135
|
+
return timerId !== undefined;
|
|
136
|
+
}
|
|
137
|
+
function debounced(...args) {
|
|
138
|
+
const time = Date.now();
|
|
139
|
+
const isInvoking = shouldInvoke(time);
|
|
140
|
+
lastArgs = args;
|
|
141
|
+
lastThis = this;
|
|
142
|
+
lastCallTime = time;
|
|
143
|
+
if (isInvoking) {
|
|
144
|
+
if (timerId === undefined) {
|
|
145
|
+
return leadingEdge(lastCallTime);
|
|
146
|
+
}
|
|
147
|
+
if (maxWait !== undefined) {
|
|
148
|
+
timerId = startTimer(timerExpired, wait);
|
|
149
|
+
return invokeFunc(lastCallTime);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (timerId === undefined) {
|
|
153
|
+
timerId = startTimer(timerExpired, wait);
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
debounced.cancel = cancel;
|
|
158
|
+
debounced.flush = flush;
|
|
159
|
+
debounced.pending = pending;
|
|
160
|
+
return debounced;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Creates a throttled function that only invokes func at most once per every wait milliseconds
|
|
164
|
+
* @param fn - Function to throttle
|
|
165
|
+
* @param wait - Milliseconds to wait
|
|
166
|
+
* @param options - Throttle options
|
|
167
|
+
* @returns Throttled function with cancel method
|
|
168
|
+
* @example
|
|
169
|
+
* const throttled = throttle(() => console.log('Scroll'), 100);
|
|
170
|
+
* window.addEventListener('scroll', throttled);
|
|
171
|
+
*/
|
|
172
|
+
export function throttle(fn, wait, options = {}) {
|
|
173
|
+
const { leading = true, trailing = true } = options;
|
|
174
|
+
return debounce(fn, wait, {
|
|
175
|
+
leading,
|
|
176
|
+
trailing,
|
|
177
|
+
maxWait: wait,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates a function that is restricted to invoking func once
|
|
182
|
+
* @param fn - Function to restrict
|
|
183
|
+
* @returns Function that invokes func once
|
|
184
|
+
* @example
|
|
185
|
+
* const initialize = once(() => console.log('Initialized'));
|
|
186
|
+
* initialize(); // Logs 'Initialized'
|
|
187
|
+
* initialize(); // Does nothing
|
|
188
|
+
*/
|
|
189
|
+
export function once(fn) {
|
|
190
|
+
let called = false;
|
|
191
|
+
let result;
|
|
192
|
+
const wrapped = function (...args) {
|
|
193
|
+
if (!called) {
|
|
194
|
+
called = true;
|
|
195
|
+
result = fn.apply(this, args);
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
};
|
|
199
|
+
Object.defineProperty(wrapped, 'called', {
|
|
200
|
+
get() {
|
|
201
|
+
return called;
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
return wrapped;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Creates a function that invokes func with arguments arranged according to the specified indexes
|
|
208
|
+
* @param fn - Function to rearrange arguments for
|
|
209
|
+
* @param indexes - Argument indexes
|
|
210
|
+
* @returns New function with rearranged arguments
|
|
211
|
+
* @example
|
|
212
|
+
* const rearg = rearg((a, b, c) => [a, b, c], [2, 0, 1]);
|
|
213
|
+
* rearg('a', 'b', 'c'); // ['c', 'a', 'b']
|
|
214
|
+
*/
|
|
215
|
+
export function rearg(fn, ...indexes) {
|
|
216
|
+
return function (...args) {
|
|
217
|
+
const reordered = indexes.map((i) => args[i]);
|
|
218
|
+
return fn.apply(this, reordered);
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Curries a function
|
|
223
|
+
* @param fn - Function to curry
|
|
224
|
+
* @param arity - Number of arguments (defaults to fn.length)
|
|
225
|
+
* @returns Curried function
|
|
226
|
+
* @example
|
|
227
|
+
* const add = (a: number, b: number, c: number) => a + b + c;
|
|
228
|
+
* const curried = curry(add);
|
|
229
|
+
* curried(1)(2)(3); // 6
|
|
230
|
+
* curried(1, 2)(3); // 6
|
|
231
|
+
*/
|
|
232
|
+
export function curry(fn, arity = fn.length) {
|
|
233
|
+
return function curried(...args) {
|
|
234
|
+
if (args.length >= arity) {
|
|
235
|
+
return fn.apply(this, args);
|
|
236
|
+
}
|
|
237
|
+
return function (...nextArgs) {
|
|
238
|
+
return curried.apply(this, [...args, ...nextArgs]);
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Composes functions from right to left
|
|
244
|
+
* @param fns - Functions to compose
|
|
245
|
+
* @returns Composed function
|
|
246
|
+
* @example
|
|
247
|
+
* const double = (x: number) => x * 2;
|
|
248
|
+
* const addOne = (x: number) => x + 1;
|
|
249
|
+
* const composed = compose(double, addOne);
|
|
250
|
+
* composed(5); // 12 (addOne first, then double)
|
|
251
|
+
*/
|
|
252
|
+
export function compose(...fns) {
|
|
253
|
+
return fns.reduceRight((prevFn, nextFn) => (value) => nextFn(prevFn(value)), (value) => value);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Pipes functions from left to right
|
|
257
|
+
* @param fns - Functions to pipe
|
|
258
|
+
* @returns Piped function
|
|
259
|
+
* @example
|
|
260
|
+
* const double = (x: number) => x * 2;
|
|
261
|
+
* const addOne = (x: number) => x + 1;
|
|
262
|
+
* const piped = pipe(double, addOne);
|
|
263
|
+
* piped(5); // 11 (double first, then addOne)
|
|
264
|
+
*/
|
|
265
|
+
export function pipe(...fns) {
|
|
266
|
+
return fns.reduce((prevFn, nextFn) => (value) => nextFn(prevFn(value)), (value) => value);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Creates a function that negates the result of the predicate func
|
|
270
|
+
* @param predicate - Predicate to negate
|
|
271
|
+
* @returns Negated predicate
|
|
272
|
+
* @example
|
|
273
|
+
* const isEven = (n: number) => n % 2 === 0;
|
|
274
|
+
* const isOdd = negate(isEven);
|
|
275
|
+
* isOdd(3); // true
|
|
276
|
+
*/
|
|
277
|
+
export function negate(predicate) {
|
|
278
|
+
return function (...args) {
|
|
279
|
+
return !predicate.apply(this, args);
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Creates a function that invokes func with its arguments transformed
|
|
284
|
+
* @param fn - Function to wrap
|
|
285
|
+
* @param transforms - Argument transforms
|
|
286
|
+
* @returns New function with transformed arguments
|
|
287
|
+
* @example
|
|
288
|
+
* const doubled = overArgs((x, y) => [x, y], [x => x * 2, x => x * 3]);
|
|
289
|
+
* doubled(1, 1); // [2, 3]
|
|
290
|
+
*/
|
|
291
|
+
export function overArgs(fn, ...transforms) {
|
|
292
|
+
return function (...args) {
|
|
293
|
+
const transformedArgs = args.map((arg, index) => transforms[index] ? transforms[index](arg) : arg);
|
|
294
|
+
return fn.apply(this, transformedArgs);
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Creates a function that invokes func with partials prepended to the arguments
|
|
299
|
+
* @param fn - Function to partially apply arguments to
|
|
300
|
+
* @param partials - Arguments to be partially applied
|
|
301
|
+
* @returns Partially applied function
|
|
302
|
+
* @example
|
|
303
|
+
* const greet = (greeting: string, name: string) => `${greeting}, ${name}!`;
|
|
304
|
+
* const sayHello = partial(greet, 'Hello');
|
|
305
|
+
* sayHello('World'); // 'Hello, World!'
|
|
306
|
+
*/
|
|
307
|
+
export function partial(fn, ...partials) {
|
|
308
|
+
return function (...args) {
|
|
309
|
+
return fn.apply(this, [...partials, ...args]);
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Creates a function that invokes func with partials appended to the arguments
|
|
314
|
+
* @param fn - Function to partially apply arguments to
|
|
315
|
+
* @param partials - Arguments to be partially applied
|
|
316
|
+
* @returns Partially applied function
|
|
317
|
+
* @example
|
|
318
|
+
* const greet = (name: string, greeting: string) => `${greeting}, ${name}!`;
|
|
319
|
+
* const greetWorld = partialRight(greet, 'Hello');
|
|
320
|
+
* greetWorld('World'); // 'Hello, World!'
|
|
321
|
+
*/
|
|
322
|
+
export function partialRight(fn, ...partials) {
|
|
323
|
+
return function (...args) {
|
|
324
|
+
return fn.apply(this, [...args, ...partials]);
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Creates a function that invokes func with the this binding of the created function
|
|
329
|
+
* @param fn - Function to bind
|
|
330
|
+
* @param thisArg - The this binding of func
|
|
331
|
+
* @param partials - Arguments to be partially applied
|
|
332
|
+
* @returns Bound function
|
|
333
|
+
* @example
|
|
334
|
+
* const obj = { x: 42 };
|
|
335
|
+
* const getX = bind(function() { return this.x; }, obj);
|
|
336
|
+
* getX(); // 42
|
|
337
|
+
*/
|
|
338
|
+
export function bind(fn, thisArg, ...partials) {
|
|
339
|
+
return function (...args) {
|
|
340
|
+
return fn.apply(thisArg, [...partials, ...args]);
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Creates a function that invokes func with arguments reversed
|
|
345
|
+
* @param fn - Function to flip arguments for
|
|
346
|
+
* @returns Function with flipped arguments
|
|
347
|
+
* @example
|
|
348
|
+
* const flipped = flip((a, b, c) => [a, b, c]);
|
|
349
|
+
* flipped(1, 2, 3); // [3, 2, 1]
|
|
350
|
+
*/
|
|
351
|
+
export function flip(fn) {
|
|
352
|
+
return function (...args) {
|
|
353
|
+
return fn.apply(this, args.reverse());
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Creates a function that accepts up to n arguments, ignoring any additional arguments
|
|
358
|
+
* @param fn - Function to cap arguments for
|
|
359
|
+
* @param n - Arity cap
|
|
360
|
+
* @returns Function with capped arity
|
|
361
|
+
* @example
|
|
362
|
+
* const capped = ary((a, b, c) => [a, b, c], 2);
|
|
363
|
+
* capped(1, 2, 3, 4); // [1, 2, undefined]
|
|
364
|
+
*/
|
|
365
|
+
export function ary(fn, n) {
|
|
366
|
+
return function (...args) {
|
|
367
|
+
return fn.apply(this, args.slice(0, n));
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Creates a function that accepts up to one argument, ignoring any additional arguments
|
|
372
|
+
* @param fn - Function to cap arguments for
|
|
373
|
+
* @returns Unary function
|
|
374
|
+
* @example
|
|
375
|
+
* const unary = unary(parseInt);
|
|
376
|
+
* ['6', '8', '10'].map(unary); // [6, 8, 10] instead of [6, NaN, 2]
|
|
377
|
+
*/
|
|
378
|
+
export function unary(fn) {
|
|
379
|
+
return function (arg) {
|
|
380
|
+
return fn.call(this, arg);
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Defers invoking the func until the current call stack has cleared
|
|
385
|
+
* @param fn - Function to defer
|
|
386
|
+
* @param args - Arguments to invoke func with
|
|
387
|
+
* @returns Timer id
|
|
388
|
+
* @example
|
|
389
|
+
* defer(console.log, 'Deferred');
|
|
390
|
+
*/
|
|
391
|
+
export function defer(fn, ...args) {
|
|
392
|
+
return setTimeout(fn, 1, ...args);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Delays invoking func until after wait milliseconds
|
|
396
|
+
* @param fn - Function to delay
|
|
397
|
+
* @param wait - Milliseconds to delay invocation
|
|
398
|
+
* @param args - Arguments to invoke func with
|
|
399
|
+
* @returns Timer id
|
|
400
|
+
* @example
|
|
401
|
+
* delay(console.log, 1000, 'Delayed');
|
|
402
|
+
*/
|
|
403
|
+
export function delay(fn, wait, ...args) {
|
|
404
|
+
return setTimeout(fn, wait, ...args);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Creates a function that memoizes the result of func with a Time-To-Live (TTL)
|
|
408
|
+
* @param fn - Function to memoize
|
|
409
|
+
* @param ttl - Time to live in milliseconds
|
|
410
|
+
* @param resolver - Optional function to generate cache key
|
|
411
|
+
* @returns Memoized function with TTL
|
|
412
|
+
* @example
|
|
413
|
+
* const expensiveApi = memoizeWithTTL(fetchData, 60000); // Cache for 1 minute
|
|
414
|
+
*/
|
|
415
|
+
export function memoizeWithTTL(fn, ttl, resolver) {
|
|
416
|
+
const cache = new Map();
|
|
417
|
+
const memoized = function (...args) {
|
|
418
|
+
const key = resolver ? resolver(...args) : JSON.stringify(args);
|
|
419
|
+
const now = Date.now();
|
|
420
|
+
const cached = cache.get(key);
|
|
421
|
+
if (cached && cached.expires > now) {
|
|
422
|
+
return cached.value;
|
|
423
|
+
}
|
|
424
|
+
const result = fn.apply(this, args);
|
|
425
|
+
cache.set(key, { value: result, expires: now + ttl });
|
|
426
|
+
// Cleanup expired entries
|
|
427
|
+
for (const [k, v] of cache.entries()) {
|
|
428
|
+
if (v.expires <= now) {
|
|
429
|
+
cache.delete(k);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return result;
|
|
433
|
+
};
|
|
434
|
+
memoized.cache = cache;
|
|
435
|
+
return memoized;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Creates a function that invokes func at most n times
|
|
439
|
+
* @param fn - Function to restrict
|
|
440
|
+
* @param n - Number of calls at which to stop executing func
|
|
441
|
+
* @returns Restricted function
|
|
442
|
+
* @example
|
|
443
|
+
* const limited = before((n) => console.log(n), 3);
|
|
444
|
+
* limited(1); // Logs 1
|
|
445
|
+
* limited(2); // Logs 2
|
|
446
|
+
* limited(3); // Does nothing
|
|
447
|
+
*/
|
|
448
|
+
export function before(fn, n) {
|
|
449
|
+
let result;
|
|
450
|
+
let callCount = 0;
|
|
451
|
+
return function (...args) {
|
|
452
|
+
if (++callCount < n) {
|
|
453
|
+
result = fn.apply(this, args);
|
|
454
|
+
}
|
|
455
|
+
if (callCount >= n) {
|
|
456
|
+
// @ts-ignore - Allow clearing function reference
|
|
457
|
+
fn = undefined;
|
|
458
|
+
}
|
|
459
|
+
return result;
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Creates a function that invokes func once it's called n or more times
|
|
464
|
+
* @param fn - Function to restrict
|
|
465
|
+
* @param n - Number of calls before func is invoked
|
|
466
|
+
* @returns Restricted function
|
|
467
|
+
* @example
|
|
468
|
+
* const afterThree = after((n) => console.log('Called!'), 3);
|
|
469
|
+
* afterThree(); // Does nothing
|
|
470
|
+
* afterThree(); // Does nothing
|
|
471
|
+
* afterThree(); // Logs 'Called!'
|
|
472
|
+
*/
|
|
473
|
+
export function after(fn, n) {
|
|
474
|
+
let callCount = 0;
|
|
475
|
+
return function (...args) {
|
|
476
|
+
if (++callCount >= n) {
|
|
477
|
+
return fn.apply(this, args);
|
|
478
|
+
}
|
|
479
|
+
return undefined;
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Wraps a function to catch and handle errors
|
|
484
|
+
* @param fn - Function to wrap
|
|
485
|
+
* @param errorHandler - Error handler function
|
|
486
|
+
* @returns Wrapped function
|
|
487
|
+
* @example
|
|
488
|
+
* const safe = tryCatch(
|
|
489
|
+
* () => JSON.parse('invalid'),
|
|
490
|
+
* (error) => ({ error: error.message })
|
|
491
|
+
* );
|
|
492
|
+
*/
|
|
493
|
+
export function tryCatch(fn, errorHandler) {
|
|
494
|
+
return function (...args) {
|
|
495
|
+
try {
|
|
496
|
+
return fn.apply(this, args);
|
|
497
|
+
}
|
|
498
|
+
catch (error) {
|
|
499
|
+
return errorHandler(error, ...args);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Creates a function that returns the result of invoking the given functions with the this binding of the created function,
|
|
505
|
+
* where each successive invocation is supplied the return value of the previous
|
|
506
|
+
* @param fns - Functions to invoke
|
|
507
|
+
* @returns Flow function
|
|
508
|
+
* @example
|
|
509
|
+
* const calculate = flow(
|
|
510
|
+
* (n: number) => n * 2,
|
|
511
|
+
* (n: number) => n + 1,
|
|
512
|
+
* (n: number) => n * 3
|
|
513
|
+
* );
|
|
514
|
+
* calculate(5); // 33
|
|
515
|
+
*/
|
|
516
|
+
export function flow(...fns) {
|
|
517
|
+
return pipe(...fns);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Creates a function that returns the result of invoking the given functions from right to left
|
|
521
|
+
* @param fns - Functions to invoke
|
|
522
|
+
* @returns Flow right function
|
|
523
|
+
* @example
|
|
524
|
+
* const calculate = flowRight(
|
|
525
|
+
* (n: number) => n * 3,
|
|
526
|
+
* (n: number) => n + 1,
|
|
527
|
+
* (n: number) => n * 2
|
|
528
|
+
* );
|
|
529
|
+
* calculate(5); // 33
|
|
530
|
+
*/
|
|
531
|
+
export function flowRight(...fns) {
|
|
532
|
+
return compose(...fns);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Wraps an async function to retry on failure
|
|
536
|
+
* @param fn - Async function to wrap
|
|
537
|
+
* @param options - Retry options
|
|
538
|
+
* @returns Wrapped function with retry logic
|
|
539
|
+
* @example
|
|
540
|
+
* const fetchWithRetry = retry(fetchData, { retries: 3, delay: 1000 });
|
|
541
|
+
*/
|
|
542
|
+
export function retry(fn, options = {}) {
|
|
543
|
+
const { retries = 3, delay = 1000, backoff = 2, onRetry } = options;
|
|
544
|
+
return async function (...args) {
|
|
545
|
+
let lastError;
|
|
546
|
+
let currentDelay = delay;
|
|
547
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
548
|
+
try {
|
|
549
|
+
return await fn.apply(this, args);
|
|
550
|
+
}
|
|
551
|
+
catch (error) {
|
|
552
|
+
lastError = error;
|
|
553
|
+
if (attempt < retries) {
|
|
554
|
+
if (onRetry) {
|
|
555
|
+
onRetry(lastError, attempt + 1);
|
|
556
|
+
}
|
|
557
|
+
await new Promise((resolve) => setTimeout(resolve, currentDelay));
|
|
558
|
+
currentDelay *= backoff;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
throw lastError;
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Creates a function that invokes func with a timeout
|
|
567
|
+
* @param fn - Function to wrap with timeout
|
|
568
|
+
* @param timeout - Timeout in milliseconds
|
|
569
|
+
* @param onTimeout - Optional callback when timeout occurs
|
|
570
|
+
* @returns Function with timeout
|
|
571
|
+
* @example
|
|
572
|
+
* const timedFetch = withTimeout(fetchData, 5000);
|
|
573
|
+
*/
|
|
574
|
+
export function withTimeout(fn, timeout, onTimeout) {
|
|
575
|
+
return async function (...args) {
|
|
576
|
+
return Promise.race([
|
|
577
|
+
fn.apply(this, args),
|
|
578
|
+
new Promise((_, reject) => setTimeout(() => {
|
|
579
|
+
if (onTimeout)
|
|
580
|
+
onTimeout();
|
|
581
|
+
reject(new Error(`Function timed out after ${timeout}ms`));
|
|
582
|
+
}, timeout)),
|
|
583
|
+
]);
|
|
584
|
+
};
|
|
585
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isClass as F}from"@fluixi/utils/object";function I(e){return typeof e=="function"&&!F(e)}function L(e){return e instanceof Promise||!!e&&(typeof e=="object"||typeof e=="function")&&"then"in e}function z(e){return I(e)&&e.constructor.name==="AsyncFunction"}function O(e,n){let t=new Map,r=function(...a){let i=n?n(...a):JSON.stringify(a);if(t.has(i))return t.get(i);let o=e.apply(this,a);return t.set(i,o),o};return r.cache=t,r}function M(e,n,t={}){let{leading:r=!1,trailing:a=!0,maxWait:i}=t,o,c,s,y=0,p,f,l;function x(u){let T=p,d=f;return p=f=void 0,y=u,l=e.apply(d,T),l}function m(u,T){return setTimeout(u,T)}function R(u){u!==void 0&&clearTimeout(u)}function w(u){return y=u,o=m(g,n),r?x(u):l}function k(u){let T=u-s,d=u-y,v=n-T;return i!==void 0?Math.min(v,i-d):v}function b(u){let T=u-s,d=u-y;return s===void 0||T>=n||T<0||i!==void 0&&d>=i}function g(){let u=Date.now();if(b(u)){P(u);return}o=m(g,k(u))}function P(u){return o=void 0,a&&p?x(u):(p=f=void 0,l)}function A(){R(o),R(c),y=0,p=s=f=o=c=void 0}function E(){return o===void 0?l:P(Date.now())}function C(){return o!==void 0}function h(...u){let T=Date.now(),d=b(T);if(p=u,f=this,s=T,d){if(o===void 0)return w(s);if(i!==void 0)return o=m(g,n),x(s)}return o===void 0&&(o=m(g,n)),l}return h.cancel=A,h.flush=E,h.pending=C,h}function j(e,n,t={}){let{leading:r=!0,trailing:a=!0}=t;return M(e,n,{leading:r,trailing:a,maxWait:n})}function J(e){let n=!1,t,r=function(...a){return n||(n=!0,t=e.apply(this,a)),t};return Object.defineProperty(r,"called",{get(){return n}}),r}function N(e,...n){return function(...t){let r=n.map(a=>t[a]);return e.apply(this,r)}}function _(e,n=e.length){return function t(...r){return r.length>=n?e.apply(this,r):function(...a){return t.apply(this,[...r,...a])}}}function S(...e){return e.reduceRight((n,t)=>r=>t(n(r)),n=>n)}function W(...e){return e.reduce((n,t)=>r=>t(n(r)),n=>n)}function $(e){return function(...n){return!e.apply(this,n)}}function q(e,...n){return function(...t){let r=t.map((a,i)=>n[i]?n[i](a):a);return e.apply(this,r)}}function B(e,...n){return function(...t){return e.apply(this,[...n,...t])}}function G(e,...n){return function(...t){return e.apply(this,[...t,...n])}}function H(e,n,...t){return function(...r){return e.apply(n,[...t,...r])}}function K(e){return function(...n){return e.apply(this,n.reverse())}}function Q(e,n){return function(...t){return e.apply(this,t.slice(0,n))}}function U(e){return function(n){return e.call(this,n)}}function V(e,...n){return setTimeout(e,1,...n)}function X(e,n,...t){return setTimeout(e,n,...t)}function Y(e,n,t){let r=new Map,a=function(...i){let o=t?t(...i):JSON.stringify(i),c=Date.now(),s=r.get(o);if(s&&s.expires>c)return s.value;let y=e.apply(this,i);r.set(o,{value:y,expires:c+n});for(let[p,f]of r.entries())f.expires<=c&&r.delete(p);return y};return a.cache=r,a}function Z(e,n){let t,r=0;return function(...a){return++r<n&&(t=e.apply(this,a)),r>=n&&(e=void 0),t}}function ee(e,n){let t=0;return function(...r){if(++t>=n)return e.apply(this,r)}}function ne(e,n){return function(...t){try{return e.apply(this,t)}catch(r){return n(r,...t)}}}function te(...e){return W(...e)}function re(...e){return S(...e)}function ae(e,n={}){let{retries:t=3,delay:r=1e3,backoff:a=2,onRetry:i}=n;return async function(...o){let c,s=r;for(let y=0;y<=t;y++)try{return await e.apply(this,o)}catch(p){c=p,y<t&&(i&&i(c,y+1),await new Promise(f=>setTimeout(f,s)),s*=a)}throw c}}function ue(e,n,t){return async function(...r){return Promise.race([e.apply(this,r),new Promise((a,i)=>setTimeout(()=>{t&&t(),i(new Error(`Function timed out after ${n}ms`))},n))])}}export{ee as after,Q as ary,Z as before,H as bind,S as compose,_ as curry,M as debounce,V as defer,X as delay,K as flip,te as flow,re as flowRight,z as isAsyncFunction,I as isFunction,L as isPromise,O as memoize,Y as memoizeWithTTL,$ as negate,J as once,q as overArgs,B as partial,G as partialRight,W as pipe,N as rearg,ae as retry,j as throttle,ne as tryCatch,U as unary,ue as withTimeout};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var R=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var O=(e,n)=>{for(var t in n)R(e,t,{get:n[t],enumerable:!0})},J=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of z(n))!j.call(e,a)&&a!==t&&R(e,a,{get:()=>n[a],enumerable:!(r=L(n,a))||r.enumerable});return e};var N=e=>J(R({},"__esModule",{value:!0}),e);var pe={};O(pe,{after:()=>ie,ary:()=>ee,before:()=>ue,bind:()=>Y,compose:()=>C,curry:()=>K,debounce:()=>E,defer:()=>te,delay:()=>re,flip:()=>Z,flow:()=>se,flowRight:()=>ye,isAsyncFunction:()=>$,isFunction:()=>A,isPromise:()=>_,memoize:()=>q,memoizeWithTTL:()=>ae,negate:()=>Q,once:()=>G,overArgs:()=>U,partial:()=>V,partialRight:()=>X,pipe:()=>F,rearg:()=>H,retry:()=>Te,throttle:()=>B,tryCatch:()=>oe,unary:()=>ne,withTimeout:()=>ce});module.exports=N(pe);var k=require("@fluixi/utils/object");function A(e){return typeof e=="function"&&!(0,k.isClass)(e)}function _(e){return e instanceof Promise||!!e&&(typeof e=="object"||typeof e=="function")&&"then"in e}function $(e){return A(e)&&e.constructor.name==="AsyncFunction"}function q(e,n){let t=new Map,r=function(...a){let i=n?n(...a):JSON.stringify(a);if(t.has(i))return t.get(i);let o=e.apply(this,a);return t.set(i,o),o};return r.cache=t,r}function E(e,n,t={}){let{leading:r=!1,trailing:a=!0,maxWait:i}=t,o,c,s,y=0,p,f,l;function x(u){let T=p,d=f;return p=f=void 0,y=u,l=e.apply(d,T),l}function m(u,T){return setTimeout(u,T)}function b(u){u!==void 0&&clearTimeout(u)}function I(u){return y=u,o=m(g,n),r?x(u):l}function M(u){let T=u-s,d=u-y,w=n-T;return i!==void 0?Math.min(w,i-d):w}function P(u){let T=u-s,d=u-y;return s===void 0||T>=n||T<0||i!==void 0&&d>=i}function g(){let u=Date.now();if(P(u)){v(u);return}o=m(g,M(u))}function v(u){return o=void 0,a&&p?x(u):(p=f=void 0,l)}function S(){b(o),b(c),y=0,p=s=f=o=c=void 0}function W(){return o===void 0?l:v(Date.now())}function D(){return o!==void 0}function h(...u){let T=Date.now(),d=P(T);if(p=u,f=this,s=T,d){if(o===void 0)return I(s);if(i!==void 0)return o=m(g,n),x(s)}return o===void 0&&(o=m(g,n)),l}return h.cancel=S,h.flush=W,h.pending=D,h}function B(e,n,t={}){let{leading:r=!0,trailing:a=!0}=t;return E(e,n,{leading:r,trailing:a,maxWait:n})}function G(e){let n=!1,t,r=function(...a){return n||(n=!0,t=e.apply(this,a)),t};return Object.defineProperty(r,"called",{get(){return n}}),r}function H(e,...n){return function(...t){let r=n.map(a=>t[a]);return e.apply(this,r)}}function K(e,n=e.length){return function t(...r){return r.length>=n?e.apply(this,r):function(...a){return t.apply(this,[...r,...a])}}}function C(...e){return e.reduceRight((n,t)=>r=>t(n(r)),n=>n)}function F(...e){return e.reduce((n,t)=>r=>t(n(r)),n=>n)}function Q(e){return function(...n){return!e.apply(this,n)}}function U(e,...n){return function(...t){let r=t.map((a,i)=>n[i]?n[i](a):a);return e.apply(this,r)}}function V(e,...n){return function(...t){return e.apply(this,[...n,...t])}}function X(e,...n){return function(...t){return e.apply(this,[...t,...n])}}function Y(e,n,...t){return function(...r){return e.apply(n,[...t,...r])}}function Z(e){return function(...n){return e.apply(this,n.reverse())}}function ee(e,n){return function(...t){return e.apply(this,t.slice(0,n))}}function ne(e){return function(n){return e.call(this,n)}}function te(e,...n){return setTimeout(e,1,...n)}function re(e,n,...t){return setTimeout(e,n,...t)}function ae(e,n,t){let r=new Map,a=function(...i){let o=t?t(...i):JSON.stringify(i),c=Date.now(),s=r.get(o);if(s&&s.expires>c)return s.value;let y=e.apply(this,i);r.set(o,{value:y,expires:c+n});for(let[p,f]of r.entries())f.expires<=c&&r.delete(p);return y};return a.cache=r,a}function ue(e,n){let t,r=0;return function(...a){return++r<n&&(t=e.apply(this,a)),r>=n&&(e=void 0),t}}function ie(e,n){let t=0;return function(...r){if(++t>=n)return e.apply(this,r)}}function oe(e,n){return function(...t){try{return e.apply(this,t)}catch(r){return n(r,...t)}}}function se(...e){return F(...e)}function ye(...e){return C(...e)}function Te(e,n={}){let{retries:t=3,delay:r=1e3,backoff:a=2,onRetry:i}=n;return async function(...o){let c,s=r;for(let y=0;y<=t;y++)try{return await e.apply(this,o)}catch(p){c=p,y<t&&(i&&i(c,y+1),await new Promise(f=>setTimeout(f,s)),s*=a)}throw c}}function ce(e,n,t){return async function(...r){return Promise.race([e.apply(this,r),new Promise((a,i)=>setTimeout(()=>{t&&t(),i(new Error(`Function timed out after ${n}ms`))},n))])}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/functions/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,mBAAmB,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './functions.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isClass as F}from"@fluixi/utils/object";function I(e){return typeof e=="function"&&!F(e)}function L(e){return e instanceof Promise||!!e&&(typeof e=="object"||typeof e=="function")&&"then"in e}function z(e){return I(e)&&e.constructor.name==="AsyncFunction"}function j(e,n){let t=new Map,r=function(...a){let i=n?n(...a):JSON.stringify(a);if(t.has(i))return t.get(i);let o=e.apply(this,a);return t.set(i,o),o};return r.cache=t,r}function M(e,n,t={}){let{leading:r=!1,trailing:a=!0,maxWait:i}=t,o,c,s,y=0,p,f,l;function x(u){let T=p,d=f;return p=f=void 0,y=u,l=e.apply(d,T),l}function m(u,T){return setTimeout(u,T)}function R(u){u!==void 0&&clearTimeout(u)}function w(u){return y=u,o=m(g,n),r?x(u):l}function k(u){let T=u-s,d=u-y,v=n-T;return i!==void 0?Math.min(v,i-d):v}function b(u){let T=u-s,d=u-y;return s===void 0||T>=n||T<0||i!==void 0&&d>=i}function g(){let u=Date.now();if(b(u)){P(u);return}o=m(g,k(u))}function P(u){return o=void 0,a&&p?x(u):(p=f=void 0,l)}function A(){R(o),R(c),y=0,p=s=f=o=c=void 0}function E(){return o===void 0?l:P(Date.now())}function C(){return o!==void 0}function h(...u){let T=Date.now(),d=b(T);if(p=u,f=this,s=T,d){if(o===void 0)return w(s);if(i!==void 0)return o=m(g,n),x(s)}return o===void 0&&(o=m(g,n)),l}return h.cancel=A,h.flush=E,h.pending=C,h}function O(e,n,t={}){let{leading:r=!0,trailing:a=!0}=t;return M(e,n,{leading:r,trailing:a,maxWait:n})}function J(e){let n=!1,t,r=function(...a){return n||(n=!0,t=e.apply(this,a)),t};return Object.defineProperty(r,"called",{get(){return n}}),r}function N(e,...n){return function(...t){let r=n.map(a=>t[a]);return e.apply(this,r)}}function _(e,n=e.length){return function t(...r){return r.length>=n?e.apply(this,r):function(...a){return t.apply(this,[...r,...a])}}}function S(...e){return e.reduceRight((n,t)=>r=>t(n(r)),n=>n)}function W(...e){return e.reduce((n,t)=>r=>t(n(r)),n=>n)}function $(e){return function(...n){return!e.apply(this,n)}}function q(e,...n){return function(...t){let r=t.map((a,i)=>n[i]?n[i](a):a);return e.apply(this,r)}}function B(e,...n){return function(...t){return e.apply(this,[...n,...t])}}function G(e,...n){return function(...t){return e.apply(this,[...t,...n])}}function H(e,n,...t){return function(...r){return e.apply(n,[...t,...r])}}function K(e){return function(...n){return e.apply(this,n.reverse())}}function Q(e,n){return function(...t){return e.apply(this,t.slice(0,n))}}function U(e){return function(n){return e.call(this,n)}}function V(e,...n){return setTimeout(e,1,...n)}function X(e,n,...t){return setTimeout(e,n,...t)}function Y(e,n,t){let r=new Map,a=function(...i){let o=t?t(...i):JSON.stringify(i),c=Date.now(),s=r.get(o);if(s&&s.expires>c)return s.value;let y=e.apply(this,i);r.set(o,{value:y,expires:c+n});for(let[p,f]of r.entries())f.expires<=c&&r.delete(p);return y};return a.cache=r,a}function Z(e,n){let t,r=0;return function(...a){return++r<n&&(t=e.apply(this,a)),r>=n&&(e=void 0),t}}function ee(e,n){let t=0;return function(...r){if(++t>=n)return e.apply(this,r)}}function ne(e,n){return function(...t){try{return e.apply(this,t)}catch(r){return n(r,...t)}}}function te(...e){return W(...e)}function re(...e){return S(...e)}function ae(e,n={}){let{retries:t=3,delay:r=1e3,backoff:a=2,onRetry:i}=n;return async function(...o){let c,s=r;for(let y=0;y<=t;y++)try{return await e.apply(this,o)}catch(p){c=p,y<t&&(i&&i(c,y+1),await new Promise(f=>setTimeout(f,s)),s*=a)}throw c}}function ue(e,n,t){return async function(...r){return Promise.race([e.apply(this,r),new Promise((a,i)=>setTimeout(()=>{t&&t(),i(new Error(`Function timed out after ${n}ms`))},n))])}}export{ee as after,Q as ary,Z as before,H as bind,S as compose,_ as curry,M as debounce,V as defer,X as delay,K as flip,te as flow,re as flowRight,z as isAsyncFunction,I as isFunction,L as isPromise,j as memoize,Y as memoizeWithTTL,$ as negate,J as once,q as overArgs,B as partial,G as partialRight,W as pipe,N as rearg,ae as retry,O as throttle,ne as tryCatch,U as unary,ue as withTimeout};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var d=(o,e,x,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let p of b(e))!c.call(o,p)&&p!==x&&t(o,p,{get:()=>e[p],enumerable:!(r=a(e,p))||r.enumerable});return o};var f=o=>d(t({},"__esModule",{value:!0}),o);var g={};module.exports=f(g);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/functions/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var u=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var a=(n,e)=>{for(var r in e)u(n,r,{get:e[r],enumerable:!0})},m=(n,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of p(e))!c.call(n,t)&&t!==r&&u(n,t,{get:()=>e[t],enumerable:!(o=s(e,t))||o.enumerable});return n};var y=n=>m(u({},"__esModule",{value:!0}),n);var w={};a(w,{generateID:()=>k,generateUUID:()=>U,getKind:()=>T,isDefined:()=>b,isNull:()=>g,isPrimitive:()=>d,isUndefined:()=>v,isUndefinedOrNull:()=>O});module.exports=y(w);var x=require("@fluixi/utils/primitive"),i=require("@fluixi/utils/object"),f=require("@fluixi/utils/array"),l=require("@fluixi/utils/functions");function g(n){return n===null}function v(n){return n===void 0}var O=n=>typeof n>"u"||n===null;function d(n){let e=typeof n;return n===null||e==="string"||e==="number"||e==="boolean"||e==="symbol"||e==="bigint"||e==="undefined"||(0,x.isDate)(n)}function b(n){return n!=null}function T(n){return n===null?"null":n===void 0?"undefined":(0,x.isDate)(n)?"date":(0,i.isClass)(n)?"class":(0,l.isFunction)(n)?"function":(0,f.isArray)(n)?"array":d(n)?"primitive":(0,i.isObject)(n)?"object":"primitive"}var U=()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(n){let e=Math.random()*16|0;return(n==="x"?e:e&3|8).toString(16)}),k=()=>"xxyxxxxxxy-4xx8".replace(/[xy]/g,function(n){let e=Math.random()*16|0;return(n==="x"?e:e&3|8).toString(16)});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ObjectKind, ObjectType, UndefinedOrNullType } from '@fluixi/utils/primitive';
|
|
2
|
+
/** Check if value is null */
|
|
3
|
+
export declare function isNull(v: unknown): v is null;
|
|
4
|
+
/** Check if value is undefined */
|
|
5
|
+
export declare function isUndefined(v: unknown): v is undefined;
|
|
6
|
+
export declare const isUndefinedOrNull: (element: unknown) => element is UndefinedOrNullType;
|
|
7
|
+
/** Check if value is primitive */
|
|
8
|
+
export declare function isPrimitive(v: unknown): v is string | number | boolean | symbol | bigint | null | undefined | Date;
|
|
9
|
+
export declare function isDefined(obj: unknown): obj is ObjectType;
|
|
10
|
+
export declare function getKind<T>(obj: T): ObjectKind;
|
|
11
|
+
export declare const generateUUID: () => string;
|
|
12
|
+
export declare const generateID: () => string;
|
|
13
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/lib/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,UAAU,EACV,mBAAmB,EACpB,MAAM,yBAAyB,CAAC;AAKjC,6BAA6B;AAC7B,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,IAAI,CAE5C;AAED,kCAAkC;AAClC,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,SAAS,CAEtD;AAED,eAAO,MAAM,iBAAiB,GAC5B,SAAS,OAAO,KACf,OAAO,IAAI,mBAGb,CAAC;AACF,kCAAkC;AAClC,wBAAgB,WAAW,CACzB,CAAC,EAAE,OAAO,GACT,CAAC,IAAI,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CAY5E;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,UAAU,CAEzD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,UAAU,CAY7C;AAED,eAAO,MAAM,YAAY,cAMxB,CAAC;AACF,eAAO,MAAM,UAAU,cAMtB,CAAC"}
|