@ls-stack/utils 1.1.0 → 1.2.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.
@@ -0,0 +1,45 @@
1
+ type DebounceOptions = {
2
+ /**
3
+ * @see _.leading
4
+ */
5
+ leading?: boolean;
6
+ /**
7
+ * @see _.maxWait
8
+ */
9
+ maxWait?: number;
10
+ /**
11
+ * @see _.trailing
12
+ */
13
+ trailing?: boolean;
14
+ };
15
+ interface DebouncedFunc<T extends (...args: any[]) => void> {
16
+ /**
17
+ * Call the original function, but applying the debounce rules.
18
+ *
19
+ * If the debounced function can be run immediately, this calls it and returns its return
20
+ * value.
21
+ *
22
+ * Otherwise, it returns the return value of the last invocation, or undefined if the debounced
23
+ * function was not invoked yet.
24
+ */
25
+ (...args: Parameters<T>): ReturnType<T> | undefined;
26
+ /**
27
+ * Throw away any pending invocation of the debounced function.
28
+ */
29
+ cancel: () => void;
30
+ /**
31
+ * If there is a pending invocation of the debounced function, invoke it immediately and return
32
+ * its return value.
33
+ *
34
+ * Otherwise, return the value from the last invocation, or undefined if the debounced function
35
+ * was never invoked.
36
+ */
37
+ flush: () => ReturnType<T> | undefined;
38
+ }
39
+ declare function debounce<T extends (...args: any[]) => void>(func: T, wait: number, options?: DebounceOptions): DebouncedFunc<T>;
40
+ declare function isDebouncedFn<T extends (...args: any[]) => void>(fn: T): fn is T & {
41
+ cancel: () => void;
42
+ flush: () => ReturnType<T> | undefined;
43
+ };
44
+
45
+ export { type DebounceOptions, type DebouncedFunc, debounce, isDebouncedFn };
@@ -0,0 +1,100 @@
1
+ // src/debounce.ts
2
+ function debounce(func, wait, options) {
3
+ let lastArgs;
4
+ let lastThis;
5
+ let maxWait;
6
+ let result;
7
+ let timerId;
8
+ let lastCallTime;
9
+ let lastInvokeTime = 0;
10
+ let leading = false;
11
+ let maxing = false;
12
+ let trailing = true;
13
+ if (options) {
14
+ leading = !!options.leading;
15
+ maxing = "maxWait" in options;
16
+ maxWait = maxing ? Math.max(options.maxWait || 0, wait) : maxWait;
17
+ trailing = "trailing" in options ? !!options.trailing : trailing;
18
+ }
19
+ function invokeFunc(time) {
20
+ const args = lastArgs;
21
+ const thisArg = lastThis;
22
+ lastArgs = lastThis = void 0;
23
+ lastInvokeTime = time;
24
+ result = func.apply(thisArg, args);
25
+ return result;
26
+ }
27
+ function leadingEdge(time) {
28
+ lastInvokeTime = time;
29
+ timerId = setTimeout(timerExpired, wait);
30
+ return leading ? invokeFunc(time) : result;
31
+ }
32
+ function remainingWait(time) {
33
+ const timeSinceLastCall = time - (lastCallTime ?? 0);
34
+ const timeSinceLastInvoke = time - lastInvokeTime;
35
+ const timeWaiting = wait - timeSinceLastCall;
36
+ return maxing ? Math.min(timeWaiting, (maxWait ?? 0) - timeSinceLastInvoke) : timeWaiting;
37
+ }
38
+ function shouldInvoke(time) {
39
+ const timeSinceLastCall = time - (lastCallTime ?? 0);
40
+ const timeSinceLastInvoke = time - lastInvokeTime;
41
+ return lastCallTime === void 0 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= (maxWait ?? 0);
42
+ }
43
+ function timerExpired() {
44
+ const time = Date.now();
45
+ if (shouldInvoke(time)) {
46
+ return trailingEdge(time);
47
+ }
48
+ timerId = setTimeout(timerExpired, remainingWait(time));
49
+ }
50
+ function trailingEdge(time) {
51
+ timerId = void 0;
52
+ if (trailing && lastArgs) {
53
+ return invokeFunc(time);
54
+ }
55
+ lastArgs = lastThis = void 0;
56
+ return result;
57
+ }
58
+ function cancel() {
59
+ if (timerId !== void 0) {
60
+ clearTimeout(timerId);
61
+ }
62
+ lastInvokeTime = 0;
63
+ lastArgs = lastCallTime = lastThis = timerId = void 0;
64
+ }
65
+ function flush() {
66
+ return timerId === void 0 ? result : trailingEdge(Date.now());
67
+ }
68
+ function debounced() {
69
+ const time = Date.now();
70
+ const isInvoking = shouldInvoke(time);
71
+ lastArgs = arguments;
72
+ lastThis = this;
73
+ lastCallTime = time;
74
+ if (isInvoking) {
75
+ if (timerId === void 0) {
76
+ return leadingEdge(lastCallTime);
77
+ }
78
+ if (maxing) {
79
+ clearTimeout(timerId);
80
+ timerId = setTimeout(timerExpired, wait);
81
+ return invokeFunc(lastCallTime);
82
+ }
83
+ }
84
+ if (timerId === void 0) {
85
+ timerId = setTimeout(timerExpired, wait);
86
+ }
87
+ return result;
88
+ }
89
+ debounced.cancel = cancel;
90
+ debounced.flush = flush;
91
+ return debounced;
92
+ }
93
+ function isDebouncedFn(fn) {
94
+ return typeof fn === "function" && "cancel" in fn && "flush" in fn;
95
+ }
96
+ export {
97
+ debounce,
98
+ isDebouncedFn
99
+ };
100
+ //# sourceMappingURL=debounce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/debounce.ts"],"sourcesContent":["export type DebounceOptions = {\n /**\n * @see _.leading\n */\n leading?: boolean;\n /**\n * @see _.maxWait\n */\n maxWait?: number;\n /**\n * @see _.trailing\n */\n trailing?: boolean;\n};\n\nexport interface DebouncedFunc<T extends (...args: any[]) => void> {\n /**\n * Call the original function, but applying the debounce rules.\n *\n * If the debounced function can be run immediately, this calls it and returns its return\n * value.\n *\n * Otherwise, it returns the return value of the last invocation, or undefined if the debounced\n * function was not invoked yet.\n */\n (...args: Parameters<T>): ReturnType<T> | undefined;\n\n /**\n * Throw away any pending invocation of the debounced function.\n */\n cancel: () => void;\n\n /**\n * If there is a pending invocation of the debounced function, invoke it immediately and return\n * its return value.\n *\n * Otherwise, return the value from the last invocation, or undefined if the debounced function\n * was never invoked.\n */\n flush: () => ReturnType<T> | undefined;\n}\n\n// forked from lodash/debounce\nexport function debounce<T extends (...args: any[]) => void>(\n func: T,\n wait: number,\n options?: DebounceOptions,\n): DebouncedFunc<T> {\n let lastArgs: IArguments | undefined;\n let lastThis: undefined;\n let maxWait: number | undefined;\n let result: any;\n let timerId: NodeJS.Timeout | undefined;\n let lastCallTime: number | undefined;\n let lastInvokeTime = 0;\n let leading = false;\n let maxing = false;\n let trailing = true;\n\n if (options) {\n leading = !!options.leading;\n maxing = 'maxWait' in options;\n maxWait = maxing ? Math.max(options.maxWait || 0, wait) : maxWait;\n trailing = 'trailing' in options ? !!options.trailing : trailing;\n }\n\n function invokeFunc(time: number) {\n const args = lastArgs;\n const thisArg = lastThis;\n\n lastArgs = lastThis = undefined;\n lastInvokeTime = time;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n result = func.apply(thisArg, args as any);\n return result;\n }\n\n function leadingEdge(time: number) {\n // Reset any `maxWait` timer.\n lastInvokeTime = time;\n // Start the timer for the trailing edge.\n timerId = setTimeout(timerExpired, wait);\n // Invoke the leading edge.\n return leading ? invokeFunc(time) : result;\n }\n\n function remainingWait(time: number) {\n const timeSinceLastCall = time - (lastCallTime ?? 0);\n const timeSinceLastInvoke = time - lastInvokeTime;\n const timeWaiting = wait - timeSinceLastCall;\n\n return maxing ?\n Math.min(timeWaiting, (maxWait ?? 0) - timeSinceLastInvoke)\n : timeWaiting;\n }\n\n function shouldInvoke(time: number) {\n const timeSinceLastCall = time - (lastCallTime ?? 0);\n const timeSinceLastInvoke = time - lastInvokeTime;\n\n // Either this is the first call, activity has stopped and we're at the\n // trailing edge, the system time has gone backwards and we're treating\n // it as the trailing edge, or we've hit the `maxWait` limit.\n return (\n lastCallTime === undefined ||\n timeSinceLastCall >= wait ||\n timeSinceLastCall < 0 ||\n (maxing && timeSinceLastInvoke >= (maxWait ?? 0))\n );\n }\n\n function timerExpired() {\n const time = Date.now();\n if (shouldInvoke(time)) {\n return trailingEdge(time);\n }\n // Restart the timer.\n timerId = setTimeout(timerExpired, remainingWait(time));\n }\n\n function trailingEdge(time: number) {\n timerId = undefined;\n\n // Only invoke if we have `lastArgs` which means `func` has been\n // debounced at least once.\n if (trailing && lastArgs) {\n return invokeFunc(time);\n }\n lastArgs = lastThis = undefined;\n return result;\n }\n\n function cancel() {\n if (timerId !== undefined) {\n clearTimeout(timerId);\n }\n lastInvokeTime = 0;\n lastArgs = lastCallTime = lastThis = timerId = undefined;\n }\n\n function flush() {\n return timerId === undefined ? result : trailingEdge(Date.now());\n }\n\n function debounced(this: any) {\n const time = Date.now();\n const isInvoking = shouldInvoke(time);\n\n lastArgs = arguments;\n // eslint-disable-next-line @typescript-eslint/no-this-alias -- this is a forked code, so a refactor is not worth it\n lastThis = this;\n lastCallTime = time;\n\n if (isInvoking) {\n if (timerId === undefined) {\n return leadingEdge(lastCallTime);\n }\n if (maxing) {\n // Handle invocations in a tight loop.\n clearTimeout(timerId);\n timerId = setTimeout(timerExpired, wait);\n return invokeFunc(lastCallTime);\n }\n }\n if (timerId === undefined) {\n timerId = setTimeout(timerExpired, wait);\n }\n return result;\n }\n debounced.cancel = cancel;\n debounced.flush = flush;\n\n return debounced;\n}\n\nexport function isDebouncedFn<T extends (...args: any[]) => void>(\n fn: T,\n): fn is T & {\n cancel: () => void;\n flush: () => ReturnType<T> | undefined;\n} {\n return typeof fn === 'function' && 'cancel' in fn && 'flush' in fn;\n}\n"],"mappings":";AA2CO,SAAS,SACd,MACA,MACA,SACkB;AAClB,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,MAAI,iBAAiB;AACrB,MAAI,UAAU;AACd,MAAI,SAAS;AACb,MAAI,WAAW;AAEf,MAAI,SAAS;AACX,cAAU,CAAC,CAAC,QAAQ;AACpB,aAAS,aAAa;AACtB,cAAU,SAAS,KAAK,IAAI,QAAQ,WAAW,GAAG,IAAI,IAAI;AAC1D,eAAW,cAAc,UAAU,CAAC,CAAC,QAAQ,WAAW;AAAA,EAC1D;AAEA,WAAS,WAAW,MAAc;AAChC,UAAM,OAAO;AACb,UAAM,UAAU;AAEhB,eAAW,WAAW;AACtB,qBAAiB;AAEjB,aAAS,KAAK,MAAM,SAAS,IAAW;AACxC,WAAO;AAAA,EACT;AAEA,WAAS,YAAY,MAAc;AAEjC,qBAAiB;AAEjB,cAAU,WAAW,cAAc,IAAI;AAEvC,WA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,QAAI,YAAY,QAAW;AACzB,gBAAU,WAAW,cAAc,IAAI;AAAA,IACzC;AACA,WAAO;AAAA,EACT;AACA,YAAU,SAAS;AACnB,YAAU,QAAQ;AAElB,SAAO;AACT;AAEO,SAAS,cACd,IAIA;AACA,SAAO,OAAO,OAAO,cAAc,YAAY,MAAM,WAAW;AAClE;","names":[]}
package/dist/main.d.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  ///<reference path="consoleFmt.d.ts" />
5
5
  ///<reference path="conversions.d.ts" />
6
6
  ///<reference path="createThrottleController.d.ts" />
7
+ ///<reference path="debounce.d.ts" />
7
8
  ///<reference path="dedent.d.ts" />
8
9
  ///<reference path="deepEqual.d.ts" />
9
10
  ///<reference path="enhancedMap.d.ts" />
@@ -1,10 +1,10 @@
1
- import {
2
- sleep
3
- } from "./chunk-HLFWWIDK.js";
4
1
  import {
5
2
  Result,
6
3
  normalizeError
7
4
  } from "./chunk-HYFUWQ2E.js";
5
+ import {
6
+ sleep
7
+ } from "./chunk-HLFWWIDK.js";
8
8
  import "./chunk-4DRNU53E.js";
9
9
  import {
10
10
  invariant,
package/dist/testUtils.js CHANGED
@@ -1,3 +1,6 @@
1
+ import {
2
+ deepEqual
3
+ } from "./chunk-JZUOIOMU.js";
1
4
  import {
2
5
  clampMin
3
6
  } from "./chunk-J66LUDLH.js";
@@ -12,9 +15,6 @@ import {
12
15
  import {
13
16
  isObject
14
17
  } from "./chunk-JT3EBBCA.js";
15
- import {
16
- deepEqual
17
- } from "./chunk-JZUOIOMU.js";
18
18
 
19
19
  // src/testUtils.ts
20
20
  function createLoggerStore({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "1.1.0",
4
+ "version": "1.2.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -40,6 +40,10 @@
40
40
  "import": "./dist/createThrottleController.js",
41
41
  "types": "./dist/createThrottleController.d.ts"
42
42
  },
43
+ "./debounce": {
44
+ "import": "./dist/debounce.js",
45
+ "types": "./dist/debounce.d.ts"
46
+ },
43
47
  "./dedent": {
44
48
  "import": "./dist/dedent.js",
45
49
  "types": "./dist/dedent.d.ts"
@@ -159,6 +163,9 @@
159
163
  "createThrottleController": [
160
164
  "./dist/createThrottleController.d.ts"
161
165
  ],
166
+ "debounce": [
167
+ "./dist/debounce.d.ts"
168
+ ],
162
169
  "dedent": [
163
170
  "./dist/dedent.d.ts"
164
171
  ],