@ls-stack/utils 3.18.0 → 3.20.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/README.md +1 -15
- package/docs/README.md +72 -0
- package/docs/_media/modules.md +52 -0
- package/docs/arrayUtils/-internal-.md +17 -0
- package/docs/arrayUtils/README.md +423 -0
- package/docs/assertions/-internal-.md +63 -0
- package/docs/assertions/README.md +565 -0
- package/docs/asyncQueue/-internal-.md +815 -0
- package/docs/asyncQueue/README.md +75 -0
- package/docs/awaitDebounce.md +66 -0
- package/docs/cache/-internal-.md +168 -0
- package/docs/cache/README.md +360 -0
- package/docs/castValues.md +47 -0
- package/docs/concurrentCalls/-internal-.md +416 -0
- package/docs/concurrentCalls/README.md +77 -0
- package/docs/consoleFmt.md +91 -0
- package/docs/conversions.md +27 -0
- package/docs/createThrottleController/-internal-.md +73 -0
- package/docs/createThrottleController/README.md +31 -0
- package/docs/debounce.md +188 -0
- package/docs/dedent/-internal-.md +17 -0
- package/docs/dedent/README.md +204 -0
- package/docs/deepEqual.md +94 -0
- package/docs/enhancedMap.md +358 -0
- package/docs/exhaustiveMatch/-internal-.md +39 -0
- package/docs/exhaustiveMatch/README.md +146 -0
- package/docs/getAutoIncrementId.md +93 -0
- package/docs/getCompositeKey.md +39 -0
- package/docs/getValueStableKey.md +57 -0
- package/docs/hash.md +31 -0
- package/docs/interpolate/-internal-.md +61 -0
- package/docs/interpolate/README.md +62 -0
- package/docs/levenshtein.md +93 -0
- package/docs/main.md +21 -0
- package/docs/mathUtils.md +137 -0
- package/docs/modules.md +52 -0
- package/docs/objUtils.md +237 -0
- package/docs/parallelAsyncCalls/-internal-.md +347 -0
- package/docs/parallelAsyncCalls/README.md +45 -0
- package/docs/promiseUtils/-internal-.md +69 -0
- package/docs/promiseUtils/README.md +31 -0
- package/docs/retryOnError.md +67 -0
- package/docs/runShellCmd/-internal-.md +111 -0
- package/docs/runShellCmd/README.md +201 -0
- package/docs/safeJson.md +51 -0
- package/docs/saferTyping.md +228 -0
- package/docs/serializeXML.md +100 -0
- package/docs/shallowEqual.md +33 -0
- package/docs/sleep.md +27 -0
- package/docs/stringUtils/-internal-.md +17 -0
- package/docs/stringUtils/README.md +166 -0
- package/docs/testUtils.md +315 -0
- package/docs/throttle/-internal-.md +47 -0
- package/docs/throttle/README.md +178 -0
- package/docs/time.md +274 -0
- package/docs/timers.md +256 -0
- package/docs/tsResult/-internal-.md +327 -0
- package/docs/tsResult/README.md +696 -0
- package/docs/typeGuards.md +399 -0
- package/docs/typingFnUtils/-internal-.md +27 -0
- package/docs/typingFnUtils/README.md +293 -0
- package/docs/typingTestUtils.md +172 -0
- package/docs/typingUtils.md +111 -0
- package/docs/yamlStringify.md +45 -0
- package/lib/arrayUtils.js +3 -3
- package/lib/assertions.js +2 -2
- package/lib/awaitDebounce.cjs +106 -0
- package/lib/awaitDebounce.d.cts +38 -0
- package/lib/awaitDebounce.d.ts +38 -0
- package/lib/awaitDebounce.js +28 -0
- package/lib/cache.js +2 -2
- package/lib/{chunk-NH2LCAQS.js → chunk-6FIBVC2P.js} +1 -1
- package/lib/{chunk-GKOTKAIV.js → chunk-7CQPOM5I.js} +1 -1
- package/lib/{chunk-WS4WEVHU.js → chunk-C2SVCIWE.js} +1 -1
- package/lib/{chunk-SSKW673U.js → chunk-JF2MDHOJ.js} +5 -1
- package/lib/chunk-NW5H5EW7.js +100 -0
- package/lib/{chunk-DMW5Q4T2.js → chunk-SRVMMYSW.js} +1 -1
- package/lib/concurrentCalls.js +3 -3
- package/lib/createThrottleController.js +3 -3
- package/lib/debounce.js +4 -95
- package/lib/dedent.cjs +31 -3
- package/lib/dedent.d.cts +61 -1
- package/lib/dedent.d.ts +61 -1
- package/lib/dedent.js +31 -3
- package/lib/enhancedMap.js +3 -3
- package/lib/getAutoIncrementId.cjs +44 -0
- package/lib/getAutoIncrementId.d.cts +44 -0
- package/lib/getAutoIncrementId.d.ts +44 -0
- package/lib/getAutoIncrementId.js +18 -0
- package/lib/getCompositeKey.js +3 -3
- package/lib/getValueStableKey.js +3 -3
- package/lib/interpolate.js +2 -2
- package/lib/parallelAsyncCalls.js +2 -2
- package/lib/runShellCmd.d.cts +50 -0
- package/lib/runShellCmd.d.ts +50 -0
- package/lib/serializeXML.cjs +7 -2
- package/lib/serializeXML.d.cts +1 -1
- package/lib/serializeXML.d.ts +1 -1
- package/lib/serializeXML.js +7 -5
- package/lib/testUtils.js +3 -3
- package/lib/throttle.cjs +250 -0
- package/lib/throttle.d.cts +89 -0
- package/lib/throttle.d.ts +89 -0
- package/lib/throttle.js +38 -0
- package/lib/tsResult.js +2 -2
- package/lib/typeGuards.cjs +7 -2
- package/lib/typeGuards.d.cts +2 -1
- package/lib/typeGuards.d.ts +2 -1
- package/lib/typeGuards.js +5 -3
- package/lib/typingFnUtils.cjs +5 -1
- package/lib/typingFnUtils.d.cts +1 -1
- package/lib/typingFnUtils.d.ts +1 -1
- package/lib/typingFnUtils.js +5 -1
- package/lib/yamlStringify.js +5 -5
- package/package.json +22 -12
package/lib/getCompositeKey.js
CHANGED
package/lib/getValueStableKey.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getCompositeKey
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
5
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-6FIBVC2P.js";
|
|
4
|
+
import "./chunk-C2SVCIWE.js";
|
|
5
|
+
import "./chunk-JF2MDHOJ.js";
|
|
6
6
|
|
|
7
7
|
// src/getValueStableKey.ts
|
|
8
8
|
var getValueStableKey = getCompositeKey;
|
package/lib/interpolate.js
CHANGED
package/lib/runShellCmd.d.cts
CHANGED
|
@@ -12,12 +12,62 @@ type RunCmdOptions = {
|
|
|
12
12
|
throwOnError?: boolean;
|
|
13
13
|
noCiColorForce?: boolean;
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
17
|
+
* ```
|
|
18
|
+
* // Old (deprecated)
|
|
19
|
+
* import { runCmd } from '@ls-stack/utils/runShellCmd';
|
|
20
|
+
*
|
|
21
|
+
* // New (preferred)
|
|
22
|
+
* import { runCmd } from '@ls-stack/node-utils/runShellCmd';
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
15
25
|
declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
28
|
+
* ```
|
|
29
|
+
* // Old (deprecated)
|
|
30
|
+
* import { concurrentCmd } from '@ls-stack/utils/runShellCmd';
|
|
31
|
+
*
|
|
32
|
+
* // New (preferred)
|
|
33
|
+
* import { concurrentCmd } from '@ls-stack/node-utils/runShellCmd';
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
16
36
|
declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
39
|
+
* ```
|
|
40
|
+
* // Old (deprecated)
|
|
41
|
+
* import { runCmdUnwrap } from '@ls-stack/utils/runShellCmd';
|
|
42
|
+
*
|
|
43
|
+
* // New (preferred)
|
|
44
|
+
* import { runCmdUnwrap } from '@ls-stack/node-utils/runShellCmd';
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
17
47
|
declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
|
|
18
48
|
silent?: boolean | 'timeOnly';
|
|
19
49
|
}): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
52
|
+
* ```
|
|
53
|
+
* // Old (deprecated)
|
|
54
|
+
* import { runCmdSilent } from '@ls-stack/utils/runShellCmd';
|
|
55
|
+
*
|
|
56
|
+
* // New (preferred)
|
|
57
|
+
* import { runCmdSilent } from '@ls-stack/node-utils/runShellCmd';
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
20
60
|
declare function runCmdSilent(command: string | string[]): Promise<CmdResult>;
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
63
|
+
* ```
|
|
64
|
+
* // Old (deprecated)
|
|
65
|
+
* import { runCmdSilentUnwrap } from '@ls-stack/utils/runShellCmd';
|
|
66
|
+
*
|
|
67
|
+
* // New (preferred)
|
|
68
|
+
* import { runCmdSilentUnwrap } from '@ls-stack/node-utils/runShellCmd';
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
21
71
|
declare function runCmdSilentUnwrap(command: string | string[]): Promise<string>;
|
|
22
72
|
|
|
23
73
|
export { concurrentCmd, runCmd, runCmdSilent, runCmdSilentUnwrap, runCmdUnwrap };
|
package/lib/runShellCmd.d.ts
CHANGED
|
@@ -12,12 +12,62 @@ type RunCmdOptions = {
|
|
|
12
12
|
throwOnError?: boolean;
|
|
13
13
|
noCiColorForce?: boolean;
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
17
|
+
* ```
|
|
18
|
+
* // Old (deprecated)
|
|
19
|
+
* import { runCmd } from '@ls-stack/utils/runShellCmd';
|
|
20
|
+
*
|
|
21
|
+
* // New (preferred)
|
|
22
|
+
* import { runCmd } from '@ls-stack/node-utils/runShellCmd';
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
15
25
|
declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
28
|
+
* ```
|
|
29
|
+
* // Old (deprecated)
|
|
30
|
+
* import { concurrentCmd } from '@ls-stack/utils/runShellCmd';
|
|
31
|
+
*
|
|
32
|
+
* // New (preferred)
|
|
33
|
+
* import { concurrentCmd } from '@ls-stack/node-utils/runShellCmd';
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
16
36
|
declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
39
|
+
* ```
|
|
40
|
+
* // Old (deprecated)
|
|
41
|
+
* import { runCmdUnwrap } from '@ls-stack/utils/runShellCmd';
|
|
42
|
+
*
|
|
43
|
+
* // New (preferred)
|
|
44
|
+
* import { runCmdUnwrap } from '@ls-stack/node-utils/runShellCmd';
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
17
47
|
declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
|
|
18
48
|
silent?: boolean | 'timeOnly';
|
|
19
49
|
}): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
52
|
+
* ```
|
|
53
|
+
* // Old (deprecated)
|
|
54
|
+
* import { runCmdSilent } from '@ls-stack/utils/runShellCmd';
|
|
55
|
+
*
|
|
56
|
+
* // New (preferred)
|
|
57
|
+
* import { runCmdSilent } from '@ls-stack/node-utils/runShellCmd';
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
20
60
|
declare function runCmdSilent(command: string | string[]): Promise<CmdResult>;
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated This utility has been moved to @ls-stack/node-utils. Please update your imports:
|
|
63
|
+
* ```
|
|
64
|
+
* // Old (deprecated)
|
|
65
|
+
* import { runCmdSilentUnwrap } from '@ls-stack/utils/runShellCmd';
|
|
66
|
+
*
|
|
67
|
+
* // New (preferred)
|
|
68
|
+
* import { runCmdSilentUnwrap } from '@ls-stack/node-utils/runShellCmd';
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
21
71
|
declare function runCmdSilentUnwrap(command: string | string[]): Promise<string>;
|
|
22
72
|
|
|
23
73
|
export { concurrentCmd, runCmd, runCmdSilent, runCmdSilentUnwrap, runCmdUnwrap };
|
package/lib/serializeXML.cjs
CHANGED
|
@@ -24,6 +24,11 @@ __export(serializeXML_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(serializeXML_exports);
|
|
26
26
|
|
|
27
|
+
// src/typeGuards.ts
|
|
28
|
+
function isTruthy(value) {
|
|
29
|
+
return !!value;
|
|
30
|
+
}
|
|
31
|
+
|
|
27
32
|
// src/arrayUtils.ts
|
|
28
33
|
function filterAndMap(array, mapFilter) {
|
|
29
34
|
const result = [];
|
|
@@ -52,12 +57,12 @@ var XML_ESCAPE_REGEX = /[&<>"']/g;
|
|
|
52
57
|
function serializeXML(node, options) {
|
|
53
58
|
if (Array.isArray(node)) {
|
|
54
59
|
const EMPTY_LINE_MARKER = "\0EMPTY_LINE\0";
|
|
55
|
-
return node.map((n) => {
|
|
60
|
+
return node.filter(isTruthy).map((n) => {
|
|
56
61
|
if (n.type === "emptyLine") {
|
|
57
62
|
return EMPTY_LINE_MARKER;
|
|
58
63
|
}
|
|
59
64
|
return serializeWithLevel(n, options, 0);
|
|
60
|
-
}).filter(
|
|
65
|
+
}).filter(isTruthy).join(options?.indent ? "\n" : "").replace(new RegExp(EMPTY_LINE_MARKER, "g"), "");
|
|
61
66
|
}
|
|
62
67
|
return serializeWithLevel(node, options, 0);
|
|
63
68
|
}
|
package/lib/serializeXML.d.cts
CHANGED
|
@@ -17,6 +17,6 @@ type SerializeOptions = {
|
|
|
17
17
|
validateTagName?: boolean;
|
|
18
18
|
invalidNodes?: 'throw' | 'reject';
|
|
19
19
|
};
|
|
20
|
-
declare function serializeXML(node: XMLNode | XMLNode[], options?: SerializeOptions): string;
|
|
20
|
+
declare function serializeXML(node: XMLNode | (XMLNode | null | undefined | false)[], options?: SerializeOptions): string;
|
|
21
21
|
|
|
22
22
|
export { type SerializeOptions, type XMLNode, serializeXML };
|
package/lib/serializeXML.d.ts
CHANGED
|
@@ -17,6 +17,6 @@ type SerializeOptions = {
|
|
|
17
17
|
validateTagName?: boolean;
|
|
18
18
|
invalidNodes?: 'throw' | 'reject';
|
|
19
19
|
};
|
|
20
|
-
declare function serializeXML(node: XMLNode | XMLNode[], options?: SerializeOptions): string;
|
|
20
|
+
declare function serializeXML(node: XMLNode | (XMLNode | null | undefined | false)[], options?: SerializeOptions): string;
|
|
21
21
|
|
|
22
22
|
export { type SerializeOptions, type XMLNode, serializeXML };
|
package/lib/serializeXML.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
filterAndMap
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
5
|
-
import
|
|
3
|
+
} from "./chunk-SRVMMYSW.js";
|
|
4
|
+
import "./chunk-C2SVCIWE.js";
|
|
5
|
+
import {
|
|
6
|
+
isTruthy
|
|
7
|
+
} from "./chunk-JF2MDHOJ.js";
|
|
6
8
|
|
|
7
9
|
// src/serializeXML.ts
|
|
8
10
|
var XML_TAG_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9._-]*$/;
|
|
@@ -18,12 +20,12 @@ var XML_ESCAPE_REGEX = /[&<>"']/g;
|
|
|
18
20
|
function serializeXML(node, options) {
|
|
19
21
|
if (Array.isArray(node)) {
|
|
20
22
|
const EMPTY_LINE_MARKER = "\0EMPTY_LINE\0";
|
|
21
|
-
return node.map((n) => {
|
|
23
|
+
return node.filter(isTruthy).map((n) => {
|
|
22
24
|
if (n.type === "emptyLine") {
|
|
23
25
|
return EMPTY_LINE_MARKER;
|
|
24
26
|
}
|
|
25
27
|
return serializeWithLevel(n, options, 0);
|
|
26
|
-
}).filter(
|
|
28
|
+
}).filter(isTruthy).join(options?.indent ? "\n" : "").replace(new RegExp(EMPTY_LINE_MARKER, "g"), "");
|
|
27
29
|
}
|
|
28
30
|
return serializeWithLevel(node, options, 0);
|
|
29
31
|
}
|
package/lib/testUtils.js
CHANGED
|
@@ -14,11 +14,11 @@ import {
|
|
|
14
14
|
import {
|
|
15
15
|
arrayWithPrevAndIndex,
|
|
16
16
|
filterAndMap
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-SRVMMYSW.js";
|
|
18
18
|
import {
|
|
19
19
|
isObject
|
|
20
|
-
} from "./chunk-
|
|
21
|
-
import "./chunk-
|
|
20
|
+
} from "./chunk-C2SVCIWE.js";
|
|
21
|
+
import "./chunk-JF2MDHOJ.js";
|
|
22
22
|
|
|
23
23
|
// src/testUtils.ts
|
|
24
24
|
function createLoggerStore({
|
package/lib/throttle.cjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/throttle.ts
|
|
21
|
+
var throttle_exports = {};
|
|
22
|
+
__export(throttle_exports, {
|
|
23
|
+
createThrottledFunctionFactory: () => createThrottledFunctionFactory,
|
|
24
|
+
throttle: () => throttle
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(throttle_exports);
|
|
27
|
+
|
|
28
|
+
// src/debounce.ts
|
|
29
|
+
function debounce(func, wait, options) {
|
|
30
|
+
let lastArgs;
|
|
31
|
+
let lastThis;
|
|
32
|
+
let maxWait;
|
|
33
|
+
let result;
|
|
34
|
+
let timerId;
|
|
35
|
+
let lastCallTime;
|
|
36
|
+
let lastInvokeTime = 0;
|
|
37
|
+
let leading = false;
|
|
38
|
+
let maxing = false;
|
|
39
|
+
let trailing = true;
|
|
40
|
+
if (options) {
|
|
41
|
+
leading = !!options.leading;
|
|
42
|
+
maxing = "maxWait" in options;
|
|
43
|
+
maxWait = maxing ? Math.max(options.maxWait || 0, wait) : maxWait;
|
|
44
|
+
trailing = "trailing" in options ? !!options.trailing : trailing;
|
|
45
|
+
}
|
|
46
|
+
function invokeFunc(time) {
|
|
47
|
+
const args = lastArgs;
|
|
48
|
+
const thisArg = lastThis;
|
|
49
|
+
lastArgs = lastThis = void 0;
|
|
50
|
+
lastInvokeTime = time;
|
|
51
|
+
result = func.apply(thisArg, args);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
function leadingEdge(time) {
|
|
55
|
+
lastInvokeTime = time;
|
|
56
|
+
timerId = setTimeout(timerExpired, wait);
|
|
57
|
+
return leading ? invokeFunc(time) : result;
|
|
58
|
+
}
|
|
59
|
+
function remainingWait(time) {
|
|
60
|
+
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
61
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
62
|
+
const timeWaiting = wait - timeSinceLastCall;
|
|
63
|
+
return maxing ? Math.min(timeWaiting, (maxWait ?? 0) - timeSinceLastInvoke) : timeWaiting;
|
|
64
|
+
}
|
|
65
|
+
function shouldInvoke(time) {
|
|
66
|
+
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
67
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
68
|
+
return lastCallTime === void 0 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= (maxWait ?? 0);
|
|
69
|
+
}
|
|
70
|
+
function timerExpired() {
|
|
71
|
+
const time = Date.now();
|
|
72
|
+
if (shouldInvoke(time)) {
|
|
73
|
+
return trailingEdge(time);
|
|
74
|
+
}
|
|
75
|
+
timerId = setTimeout(timerExpired, remainingWait(time));
|
|
76
|
+
}
|
|
77
|
+
function trailingEdge(time) {
|
|
78
|
+
timerId = void 0;
|
|
79
|
+
if (trailing && lastArgs) {
|
|
80
|
+
return invokeFunc(time);
|
|
81
|
+
}
|
|
82
|
+
lastArgs = lastThis = void 0;
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
function cancel() {
|
|
86
|
+
if (timerId !== void 0) {
|
|
87
|
+
clearTimeout(timerId);
|
|
88
|
+
}
|
|
89
|
+
lastInvokeTime = 0;
|
|
90
|
+
lastArgs = lastCallTime = lastThis = timerId = void 0;
|
|
91
|
+
}
|
|
92
|
+
function flush() {
|
|
93
|
+
return timerId === void 0 ? result : trailingEdge(Date.now());
|
|
94
|
+
}
|
|
95
|
+
function debounced() {
|
|
96
|
+
const time = Date.now();
|
|
97
|
+
const isInvoking = shouldInvoke(time);
|
|
98
|
+
lastArgs = arguments;
|
|
99
|
+
lastThis = this;
|
|
100
|
+
lastCallTime = time;
|
|
101
|
+
if (isInvoking) {
|
|
102
|
+
if (timerId === void 0) {
|
|
103
|
+
return leadingEdge(lastCallTime);
|
|
104
|
+
}
|
|
105
|
+
if (maxing) {
|
|
106
|
+
clearTimeout(timerId);
|
|
107
|
+
timerId = setTimeout(timerExpired, wait);
|
|
108
|
+
return invokeFunc(lastCallTime);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (timerId === void 0) {
|
|
112
|
+
timerId = setTimeout(timerExpired, wait);
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
debounced.cancel = cancel;
|
|
117
|
+
debounced.flush = flush;
|
|
118
|
+
return debounced;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/typeGuards.ts
|
|
122
|
+
function isFunction(value) {
|
|
123
|
+
return typeof value === "function";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/assertions.ts
|
|
127
|
+
var isFunction2 = isFunction;
|
|
128
|
+
|
|
129
|
+
// src/enhancedMap.ts
|
|
130
|
+
var enhancedMapReject = Symbol();
|
|
131
|
+
var EnhancedMap = class _EnhancedMap extends Map {
|
|
132
|
+
find(predicate) {
|
|
133
|
+
for (const [key, value] of this) {
|
|
134
|
+
if (predicate(value, key)) {
|
|
135
|
+
return { key, value };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return void 0;
|
|
139
|
+
}
|
|
140
|
+
setMultiple(...values) {
|
|
141
|
+
if (Array.isArray(values[0])) {
|
|
142
|
+
for (const [key, value] of values) {
|
|
143
|
+
this.set(key, value);
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
for (const [key, value] of Object.entries(values[0])) {
|
|
147
|
+
this.set(key, value);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
getOrThrow(key) {
|
|
153
|
+
const value = this.get(key);
|
|
154
|
+
if (value === void 0) {
|
|
155
|
+
throw new Error(`Key ${key} not found in EnhancedMap`);
|
|
156
|
+
}
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
getOrInsert(key, fallback) {
|
|
160
|
+
if (!this.has(key)) {
|
|
161
|
+
this.set(key, fallback());
|
|
162
|
+
}
|
|
163
|
+
return this.getOrThrow(key);
|
|
164
|
+
}
|
|
165
|
+
toFilteredValues(predicate) {
|
|
166
|
+
const values = [];
|
|
167
|
+
for (const [key, value] of this) {
|
|
168
|
+
if (predicate(value, key)) {
|
|
169
|
+
values.push(value);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return values;
|
|
173
|
+
}
|
|
174
|
+
toMap(mapFunction) {
|
|
175
|
+
const values = [];
|
|
176
|
+
for (const [key, value] of this) {
|
|
177
|
+
const result = mapFunction(value, key, enhancedMapReject);
|
|
178
|
+
if (result !== enhancedMapReject) {
|
|
179
|
+
values.push(result);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return values;
|
|
183
|
+
}
|
|
184
|
+
toObjMap(mapFunction) {
|
|
185
|
+
const values = {};
|
|
186
|
+
for (const [key, value] of this) {
|
|
187
|
+
const result = mapFunction(value, key);
|
|
188
|
+
if (result) {
|
|
189
|
+
values[result[0]] = result[1];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return values;
|
|
193
|
+
}
|
|
194
|
+
toValues() {
|
|
195
|
+
return [...this.values()];
|
|
196
|
+
}
|
|
197
|
+
toKeys() {
|
|
198
|
+
return [...this.keys()];
|
|
199
|
+
}
|
|
200
|
+
static from(array, mapFunction) {
|
|
201
|
+
const map = new _EnhancedMap();
|
|
202
|
+
if (!array) return map;
|
|
203
|
+
const isFn = isFunction2(mapFunction);
|
|
204
|
+
for (const item of array) {
|
|
205
|
+
if (isFn) {
|
|
206
|
+
const result = mapFunction(item);
|
|
207
|
+
if (result) {
|
|
208
|
+
map.set(result[0], result[1]);
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
const key = item[mapFunction];
|
|
212
|
+
if (key !== void 0) {
|
|
213
|
+
map.set(key, item);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return map;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/throttle.ts
|
|
222
|
+
function throttle(func, wait, options) {
|
|
223
|
+
let leading = true;
|
|
224
|
+
let trailing = true;
|
|
225
|
+
if (options) {
|
|
226
|
+
leading = "leading" in options ? !!options.leading : leading;
|
|
227
|
+
trailing = "trailing" in options ? !!options.trailing : trailing;
|
|
228
|
+
}
|
|
229
|
+
return debounce(func, wait, { leading, maxWait: wait, trailing });
|
|
230
|
+
}
|
|
231
|
+
function createThrottledFunctionFactory(wait, callback, options) {
|
|
232
|
+
const cache = new EnhancedMap();
|
|
233
|
+
return {
|
|
234
|
+
call: (...args) => {
|
|
235
|
+
const key = args.map(
|
|
236
|
+
(arg) => arg === void 0 ? "__UNDEFINED__" : JSON.stringify(arg)
|
|
237
|
+
).join(",");
|
|
238
|
+
const cachedFunction = cache.getOrInsert(
|
|
239
|
+
key,
|
|
240
|
+
() => throttle(callback, wait, options)
|
|
241
|
+
);
|
|
242
|
+
return cachedFunction(...args);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
247
|
+
0 && (module.exports = {
|
|
248
|
+
createThrottledFunctionFactory,
|
|
249
|
+
throttle
|
|
250
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { DebouncedFunc } from './debounce.cjs';
|
|
2
|
+
import { __LEGIT_ANY_FUNCTION__ } from './saferTyping.cjs';
|
|
3
|
+
|
|
4
|
+
interface ThrottleSettings {
|
|
5
|
+
/**
|
|
6
|
+
* Specify invoking on the leading edge of the timeout.
|
|
7
|
+
* @default true
|
|
8
|
+
*/
|
|
9
|
+
leading?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Specify invoking on the trailing edge of the timeout.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
trailing?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a throttled function that only invokes the provided function at most once per every `wait` milliseconds.
|
|
18
|
+
* The throttled function comes with a `cancel` method to cancel delayed invocations and a `flush` method to immediately invoke them.
|
|
19
|
+
*
|
|
20
|
+
* Throttling is useful for rate-limiting events that fire frequently, like scroll or resize handlers.
|
|
21
|
+
* Unlike debouncing, throttling guarantees the function is called at regular intervals.
|
|
22
|
+
*
|
|
23
|
+
* @template T - The type of the function to throttle
|
|
24
|
+
* @param func - The function to throttle
|
|
25
|
+
* @param wait - The number of milliseconds to throttle invocations to
|
|
26
|
+
* @param options - The options object
|
|
27
|
+
* @param options.leading - Specify invoking on the leading edge of the timeout (default: true)
|
|
28
|
+
* @param options.trailing - Specify invoking on the trailing edge of the timeout (default: true)
|
|
29
|
+
* @returns Returns the new throttled function
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const throttledSave = throttle(saveData, 1000);
|
|
34
|
+
*
|
|
35
|
+
* // Will only call saveData at most once per second
|
|
36
|
+
* throttledSave();
|
|
37
|
+
* throttledSave();
|
|
38
|
+
* throttledSave();
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Only invoke on trailing edge
|
|
44
|
+
* const throttledHandler = throttle(handleScroll, 100, { leading: false });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare function throttle<T extends __LEGIT_ANY_FUNCTION__>(func: T, wait: number, options?: ThrottleSettings): DebouncedFunc<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a factory for throttled functions that caches throttled instances based on function arguments.
|
|
50
|
+
* Each unique set of arguments gets its own throttled function instance, allowing for fine-grained
|
|
51
|
+
* throttling control per argument combination.
|
|
52
|
+
*
|
|
53
|
+
* This is useful when you want to throttle calls to the same function but with different parameters
|
|
54
|
+
* independently. For example, throttling API calls per user ID or throttling UI updates per component.
|
|
55
|
+
*
|
|
56
|
+
* @template T - The type of arguments the callback function accepts
|
|
57
|
+
* @template R - The return type of the callback function
|
|
58
|
+
* @param wait - The number of milliseconds to throttle invocations to
|
|
59
|
+
* @param callback - The function to throttle
|
|
60
|
+
* @param options - The throttle options (leading/trailing behavior)
|
|
61
|
+
* @returns An object with a `call` method that accepts the callback arguments
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const apiThrottle = createThrottledFunctionFactory(
|
|
66
|
+
* 1000,
|
|
67
|
+
* (userId: string, action: string) => callAPI(userId, action)
|
|
68
|
+
* );
|
|
69
|
+
*
|
|
70
|
+
* // Each user gets their own throttled instance
|
|
71
|
+
* apiThrottle.call('user1', 'update'); // Executes immediately
|
|
72
|
+
* apiThrottle.call('user2', 'update'); // Executes immediately (different user)
|
|
73
|
+
* apiThrottle.call('user1', 'update'); // Throttled (same user)
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* // Throttle UI updates per component
|
|
79
|
+
* const updateThrottle = createThrottledFunctionFactory(
|
|
80
|
+
* 100,
|
|
81
|
+
* (componentId: string, data: any) => updateComponent(componentId, data)
|
|
82
|
+
* );
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function createThrottledFunctionFactory<T extends string | number | null | undefined | boolean, R>(wait: number, callback: (...args: T[]) => R, options?: ThrottleSettings): {
|
|
86
|
+
call: (...args: T[]) => R | undefined;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { createThrottledFunctionFactory, throttle };
|