@koine/utils 2.0.0-beta.6 → 2.0.0-beta.8

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/clsx.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- import type { ClassValue } from "clsx";
1
+ type ClassValue = ClassArray | ClassDictionary | string | number | null | boolean | undefined;
2
+ type ClassDictionary = Record<string, any>;
3
+ type ClassArray = ClassValue[];
4
+ export type ClsxClassValue = ClassValue;
2
5
  /**
3
6
  * Class names utility
4
7
  *
package/clsx.js CHANGED
@@ -1,3 +1,4 @@
1
+ // import type { ClassValue } from "clsx";
1
2
  "use strict";
2
3
  Object.defineProperty(exports, "__esModule", {
3
4
  value: true
package/clsx.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ // import type { ClassValue } from "clsx";
1
2
  // import _clsx from "clsx";
2
3
  // export const clsx = _clsx;
3
4
  function toVal(mix) {
@@ -1,11 +1,20 @@
1
- import { debounce as tsDebounce } from "ts-debounce";
2
- export { type Options as DebounceOptions } from "ts-debounce";
3
- export { type DebouncedFunction } from "ts-debounce";
1
+ /**
2
+ * @file Copy pasted from https://github.com/chodorowicz/ts-debounce/blob/master/src/index.ts
3
+ */
4
+ export type DebounceOptions<Result> = {
5
+ isImmediate?: boolean;
6
+ maxWait?: number;
7
+ callback?: (data: Result) => void;
8
+ };
9
+ export interface DebouncedFunction<Args extends any[], F extends (...args: Args) => any> {
10
+ (this: ThisParameterType<F>, ...args: Args & Parameters<F>): Promise<ReturnType<F>>;
11
+ cancel: (reason?: any) => void;
12
+ }
4
13
  /**
5
14
  * Debounce function (with `Promise`)
6
15
  *
7
16
  * @category function
8
17
  * @borrows [chodorowicz/ts-debounce](https://github.com/chodorowicz/ts-debounce)
18
+ * @license [MIT: Jakub Chodorowicz](Jakub Chodorowicz)
9
19
  */
10
- export declare const debouncePromise: typeof tsDebounce;
11
- export default debouncePromise;
20
+ export declare function debouncePromise<Args extends any[], F extends (...args: Args) => any>(func: F, waitMilliseconds?: number, options?: DebounceOptions<ReturnType<F>>): DebouncedFunction<Args, F>;
@@ -1,21 +1,78 @@
1
- "use strict";
1
+ // import { debounce as tsDebounce } from "ts-debounce";
2
+ // export { type Options as DebounceOptions } from "ts-debounce";
3
+ // export { type DebouncedFunction } from "ts-debounce";
4
+ // /**
5
+ // * Debounce function (with `Promise`)
6
+ // *
7
+ // * @category function
8
+ // * @borrows [chodorowicz/ts-debounce](https://github.com/chodorowicz/ts-debounce)
9
+ // */
10
+ // export const debouncePromise = tsDebounce;
11
+ // export default debouncePromise;
12
+ /**
13
+ * @file Copy pasted from https://github.com/chodorowicz/ts-debounce/blob/master/src/index.ts
14
+ */ "use strict";
2
15
  Object.defineProperty(exports, "__esModule", {
3
16
  value: true
4
17
  });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- debouncePromise: function() {
18
+ Object.defineProperty(exports, "debouncePromise", {
19
+ enumerable: true,
20
+ get: function() {
13
21
  return debouncePromise;
14
- },
15
- default: function() {
16
- return _default;
17
22
  }
18
23
  });
19
- const _tsdebounce = require("ts-debounce");
20
- const debouncePromise = _tsdebounce.debounce;
21
- const _default = debouncePromise;
24
+ function debouncePromise(func, waitMilliseconds = 50, options = {}) {
25
+ let timeoutId;
26
+ const isImmediate = options.isImmediate ?? false;
27
+ const callback = options.callback ?? false;
28
+ const maxWait = options.maxWait;
29
+ let lastInvokeTime = Date.now();
30
+ let promises = [];
31
+ function nextInvokeTimeout() {
32
+ if (maxWait !== undefined) {
33
+ const timeSinceLastInvocation = Date.now() - lastInvokeTime;
34
+ if (timeSinceLastInvocation + waitMilliseconds >= maxWait) {
35
+ return maxWait - timeSinceLastInvocation;
36
+ }
37
+ }
38
+ return waitMilliseconds;
39
+ }
40
+ const debouncedFunction = function(...args) {
41
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
42
+ const context = this;
43
+ return new Promise((resolve, reject)=>{
44
+ const invokeFunction = function() {
45
+ timeoutId = undefined;
46
+ lastInvokeTime = Date.now();
47
+ if (!isImmediate) {
48
+ const result = func.apply(context, args);
49
+ callback && callback(result);
50
+ promises.forEach(({ resolve })=>resolve(result));
51
+ promises = [];
52
+ }
53
+ };
54
+ const shouldCallNow = isImmediate && timeoutId === undefined;
55
+ if (timeoutId !== undefined) {
56
+ clearTimeout(timeoutId);
57
+ }
58
+ timeoutId = setTimeout(invokeFunction, nextInvokeTimeout());
59
+ if (shouldCallNow) {
60
+ const result = func.apply(context, args);
61
+ callback && callback(result);
62
+ return resolve(result);
63
+ }
64
+ promises.push({
65
+ resolve,
66
+ reject
67
+ });
68
+ });
69
+ };
70
+ debouncedFunction.cancel = function(reason) {
71
+ if (timeoutId !== undefined) {
72
+ clearTimeout(timeoutId);
73
+ }
74
+ promises.forEach(({ reject })=>reject(reason));
75
+ promises = [];
76
+ };
77
+ return debouncedFunction;
78
+ }
@@ -1,8 +1,74 @@
1
- import { debounce as tsDebounce } from "ts-debounce";
1
+ // import { debounce as tsDebounce } from "ts-debounce";
2
+ // export { type Options as DebounceOptions } from "ts-debounce";
3
+ // export { type DebouncedFunction } from "ts-debounce";
4
+ // /**
5
+ // * Debounce function (with `Promise`)
6
+ // *
7
+ // * @category function
8
+ // * @borrows [chodorowicz/ts-debounce](https://github.com/chodorowicz/ts-debounce)
9
+ // */
10
+ // export const debouncePromise = tsDebounce;
11
+ // export default debouncePromise;
2
12
  /**
13
+ * @file Copy pasted from https://github.com/chodorowicz/ts-debounce/blob/master/src/index.ts
14
+ */ /**
3
15
  * Debounce function (with `Promise`)
4
16
  *
5
17
  * @category function
6
18
  * @borrows [chodorowicz/ts-debounce](https://github.com/chodorowicz/ts-debounce)
7
- */ export const debouncePromise = tsDebounce;
8
- export default debouncePromise;
19
+ * @license [MIT: Jakub Chodorowicz](Jakub Chodorowicz)
20
+ */ export function debouncePromise(func, waitMilliseconds = 50, options = {}) {
21
+ let timeoutId;
22
+ const isImmediate = options.isImmediate ?? false;
23
+ const callback = options.callback ?? false;
24
+ const maxWait = options.maxWait;
25
+ let lastInvokeTime = Date.now();
26
+ let promises = [];
27
+ function nextInvokeTimeout() {
28
+ if (maxWait !== undefined) {
29
+ const timeSinceLastInvocation = Date.now() - lastInvokeTime;
30
+ if (timeSinceLastInvocation + waitMilliseconds >= maxWait) {
31
+ return maxWait - timeSinceLastInvocation;
32
+ }
33
+ }
34
+ return waitMilliseconds;
35
+ }
36
+ const debouncedFunction = function(...args) {
37
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
38
+ const context = this;
39
+ return new Promise((resolve, reject)=>{
40
+ const invokeFunction = function() {
41
+ timeoutId = undefined;
42
+ lastInvokeTime = Date.now();
43
+ if (!isImmediate) {
44
+ const result = func.apply(context, args);
45
+ callback && callback(result);
46
+ promises.forEach(({ resolve })=>resolve(result));
47
+ promises = [];
48
+ }
49
+ };
50
+ const shouldCallNow = isImmediate && timeoutId === undefined;
51
+ if (timeoutId !== undefined) {
52
+ clearTimeout(timeoutId);
53
+ }
54
+ timeoutId = setTimeout(invokeFunction, nextInvokeTimeout());
55
+ if (shouldCallNow) {
56
+ const result = func.apply(context, args);
57
+ callback && callback(result);
58
+ return resolve(result);
59
+ }
60
+ promises.push({
61
+ resolve,
62
+ reject
63
+ });
64
+ });
65
+ };
66
+ debouncedFunction.cancel = function(reason) {
67
+ if (timeoutId !== undefined) {
68
+ clearTimeout(timeoutId);
69
+ }
70
+ promises.forEach(({ reject })=>reject(reason));
71
+ promises = [];
72
+ };
73
+ return debouncedFunction;
74
+ }
package/index.d.ts CHANGED
@@ -10,7 +10,7 @@ export { changeUrlPath } from "./changeUrlPath";
10
10
  export { chunkByChunks } from "./chunkByChunks";
11
11
  export { chunkBySize } from "./chunkBySize";
12
12
  export { clamp } from "./clamp";
13
- export { clsx } from "./clsx";
13
+ export { clsx, type ClsxClassValue } from "./clsx";
14
14
  export { convertRange } from "./convertRange";
15
15
  export { type CookieAttributesClient, type CookieAttributesServer, } from "./cookie";
16
16
  export { createPalette } from "./createPalette";
package/package.json CHANGED
@@ -5,6 +5,8 @@
5
5
  "module": "./index.mjs",
6
6
  "main": "./index.js",
7
7
  "types": "./index.d.ts",
8
- "peerDependencies": {},
9
- "version": "2.0.0-beta.6"
8
+ "peerDependencies": {
9
+ "type-fest": "^3.12.0"
10
+ },
11
+ "version": "2.0.0-beta.8"
10
12
  }