@zag-js/utils 1.47.0 → 1.49.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.
Files changed (58) hide show
  1. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/assertClassBrand.js +6 -0
  2. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/assertClassBrand.mjs +6 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/checkPrivateRedeclaration.js +5 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/checkPrivateRedeclaration.mjs +5 -0
  5. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldGet2.js +6 -0
  6. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldGet2.mjs +6 -0
  7. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldInitSpec.js +6 -0
  8. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldInitSpec.mjs +6 -0
  9. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js +11 -0
  10. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs +11 -0
  11. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.js +13 -0
  12. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.mjs +13 -0
  13. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.js +8 -0
  14. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.mjs +8 -0
  15. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/typeof.js +15 -0
  16. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/typeof.mjs +10 -0
  17. package/dist/array.d.mts +24 -26
  18. package/dist/array.d.ts +24 -26
  19. package/dist/array.js +76 -117
  20. package/dist/array.mjs +52 -74
  21. package/dist/equal.d.mts +1 -3
  22. package/dist/equal.d.ts +1 -3
  23. package/dist/equal.js +26 -60
  24. package/dist/equal.mjs +22 -37
  25. package/dist/functions.d.mts +13 -15
  26. package/dist/functions.d.ts +13 -15
  27. package/dist/functions.js +82 -121
  28. package/dist/functions.mjs +68 -88
  29. package/dist/guard.d.mts +12 -13
  30. package/dist/guard.d.ts +12 -13
  31. package/dist/guard.js +37 -70
  32. package/dist/guard.mjs +23 -37
  33. package/dist/index.d.mts +10 -9
  34. package/dist/index.d.ts +10 -9
  35. package/dist/index.js +101 -39
  36. package/dist/index.mjs +10 -10
  37. package/dist/number.d.mts +37 -38
  38. package/dist/number.d.ts +37 -38
  39. package/dist/number.js +136 -185
  40. package/dist/number.mjs +104 -134
  41. package/dist/object.d.mts +11 -16
  42. package/dist/object.d.ts +11 -16
  43. package/dist/object.js +52 -91
  44. package/dist/object.mjs +41 -61
  45. package/dist/store.d.mts +10 -12
  46. package/dist/store.d.ts +10 -12
  47. package/dist/store.js +42 -67
  48. package/dist/store.mjs +38 -44
  49. package/dist/timers.d.mts +21 -22
  50. package/dist/timers.d.ts +21 -22
  51. package/dist/timers.js +83 -112
  52. package/dist/timers.mjs +77 -83
  53. package/dist/warning.d.mts +7 -8
  54. package/dist/warning.d.ts +7 -8
  55. package/dist/warning.js +16 -51
  56. package/dist/warning.mjs +9 -25
  57. package/package.json +3 -3
  58. package/dist/chunk-MXGZDBDQ.mjs +0 -15
package/dist/equal.js CHANGED
@@ -1,63 +1,29 @@
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/equal.ts
21
- var equal_exports = {};
22
- __export(equal_exports, {
23
- isEqual: () => isEqual
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
24
4
  });
25
- module.exports = __toCommonJS(equal_exports);
26
- var isArrayLike = (value) => value?.constructor.name === "Array";
27
- var isArrayEqual = (a, b) => {
28
- if (a.length !== b.length) return false;
29
- for (let i = 0; i < a.length; i++) {
30
- if (!isEqual(a[i], b[i])) return false;
31
- }
32
- return true;
5
+ // src/equal.ts
6
+ const isArrayLike = (value) => value?.constructor.name === "Array";
7
+ const isArrayEqual = (a, b) => {
8
+ if (a.length !== b.length) return false;
9
+ for (let i = 0; i < a.length; i++) if (!isEqual(a[i], b[i])) return false;
10
+ return true;
33
11
  };
34
- var isEqual = (a, b) => {
35
- if (Object.is(a, b)) return true;
36
- if (a == null && b != null || a != null && b == null) return false;
37
- if (typeof a?.isEqual === "function" && typeof b?.isEqual === "function") {
38
- return a.isEqual(b);
39
- }
40
- if (typeof a === "function" && typeof b === "function") {
41
- return a.toString() === b.toString();
42
- }
43
- if (isArrayLike(a) && isArrayLike(b)) {
44
- return isArrayEqual(Array.from(a), Array.from(b));
45
- }
46
- if (!(typeof a === "object") || !(typeof b === "object")) return false;
47
- const keys = Object.keys(b ?? /* @__PURE__ */ Object.create(null));
48
- const length = keys.length;
49
- if (Object.keys(a).length !== length) return false;
50
- for (let i = 0; i < length; i++) {
51
- const hasKey = Reflect.has(a, keys[i]);
52
- if (!hasKey) return false;
53
- }
54
- for (let i = 0; i < length; i++) {
55
- const key = keys[i];
56
- if (!isEqual(a[key], b[key])) return false;
57
- }
58
- return true;
12
+ const isEqual = (a, b) => {
13
+ if (Object.is(a, b)) return true;
14
+ if (a == null && b != null || a != null && b == null) return false;
15
+ if (typeof a?.isEqual === "function" && typeof b?.isEqual === "function") return a.isEqual(b);
16
+ if (typeof a === "function" && typeof b === "function") return a.toString() === b.toString();
17
+ if (isArrayLike(a) && isArrayLike(b)) return isArrayEqual(Array.from(a), Array.from(b));
18
+ if (!(typeof a === "object") || !(typeof b === "object")) return false;
19
+ const keys = Object.keys(b ?? Object.create(null));
20
+ const length = keys.length;
21
+ if (Object.keys(a).length !== length) return false;
22
+ for (let i = 0; i < length; i++) if (!Reflect.has(a, keys[i])) return false;
23
+ for (let i = 0; i < length; i++) {
24
+ const key = keys[i];
25
+ if (!isEqual(a[key], b[key])) return false;
26
+ }
27
+ return true;
59
28
  };
60
- // Annotate the CommonJS export names for ESM import in node:
61
- 0 && (module.exports = {
62
- isEqual
63
- });
29
+ exports.isEqual = isEqual;
package/dist/equal.mjs CHANGED
@@ -1,40 +1,25 @@
1
- import "./chunk-MXGZDBDQ.mjs";
2
-
3
1
  // src/equal.ts
4
- var isArrayLike = (value) => value?.constructor.name === "Array";
5
- var isArrayEqual = (a, b) => {
6
- if (a.length !== b.length) return false;
7
- for (let i = 0; i < a.length; i++) {
8
- if (!isEqual(a[i], b[i])) return false;
9
- }
10
- return true;
2
+ const isArrayLike = (value) => value?.constructor.name === "Array";
3
+ const isArrayEqual = (a, b) => {
4
+ if (a.length !== b.length) return false;
5
+ for (let i = 0; i < a.length; i++) if (!isEqual(a[i], b[i])) return false;
6
+ return true;
11
7
  };
12
- var isEqual = (a, b) => {
13
- if (Object.is(a, b)) return true;
14
- if (a == null && b != null || a != null && b == null) return false;
15
- if (typeof a?.isEqual === "function" && typeof b?.isEqual === "function") {
16
- return a.isEqual(b);
17
- }
18
- if (typeof a === "function" && typeof b === "function") {
19
- return a.toString() === b.toString();
20
- }
21
- if (isArrayLike(a) && isArrayLike(b)) {
22
- return isArrayEqual(Array.from(a), Array.from(b));
23
- }
24
- if (!(typeof a === "object") || !(typeof b === "object")) return false;
25
- const keys = Object.keys(b ?? /* @__PURE__ */ Object.create(null));
26
- const length = keys.length;
27
- if (Object.keys(a).length !== length) return false;
28
- for (let i = 0; i < length; i++) {
29
- const hasKey = Reflect.has(a, keys[i]);
30
- if (!hasKey) return false;
31
- }
32
- for (let i = 0; i < length; i++) {
33
- const key = keys[i];
34
- if (!isEqual(a[key], b[key])) return false;
35
- }
36
- return true;
37
- };
38
- export {
39
- isEqual
8
+ const isEqual = (a, b) => {
9
+ if (Object.is(a, b)) return true;
10
+ if (a == null && b != null || a != null && b == null) return false;
11
+ if (typeof a?.isEqual === "function" && typeof b?.isEqual === "function") return a.isEqual(b);
12
+ if (typeof a === "function" && typeof b === "function") return a.toString() === b.toString();
13
+ if (isArrayLike(a) && isArrayLike(b)) return isArrayEqual(Array.from(a), Array.from(b));
14
+ if (!(typeof a === "object") || !(typeof b === "object")) return false;
15
+ const keys = Object.keys(b ?? Object.create(null));
16
+ const length = keys.length;
17
+ if (Object.keys(a).length !== length) return false;
18
+ for (let i = 0; i < length; i++) if (!Reflect.has(a, keys[i])) return false;
19
+ for (let i = 0; i < length; i++) {
20
+ const key = keys[i];
21
+ if (!isEqual(a[key], b[key])) return false;
22
+ }
23
+ return true;
40
24
  };
25
+ export { isEqual };
@@ -1,15 +1,13 @@
1
- type MaybeFunction<T> = T | (() => T);
2
- type Nullable<T> = T | null | undefined;
3
- declare const runIfFn: <T>(v: T | undefined, ...a: T extends (...a: any[]) => void ? Parameters<T> : never) => T extends (...a: any[]) => void ? NonNullable<ReturnType<T>> : NonNullable<T>;
4
- declare const cast: <T>(v: unknown) => T;
5
- declare const identity: (v: VoidFunction) => void;
6
- declare const noop: () => void;
7
- declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | null | undefined)[]) => (...a: Parameters<T>) => void;
8
- declare const uuid: () => string;
9
- declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
10
- declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
11
- declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
12
- declare function debounce<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
13
- declare const hash: (value: string) => string;
14
-
15
- export { type MaybeFunction, type Nullable, callAll, cast, debounce, hash, identity, match, noop, runIfFn, throttle, tryCatch, uuid };
1
+ export type MaybeFunction<T> = T | (() => T);
2
+ export type Nullable<T> = T | null | undefined;
3
+ export declare const runIfFn: <T>(v: T | undefined, ...a: T extends ((...a: any[]) => void) ? Parameters<T> : never) => T extends ((...a: any[]) => void) ? NonNullable<ReturnType<T>> : NonNullable<T>;
4
+ export declare const cast: <T>(v: unknown) => T;
5
+ export declare const identity: (v: VoidFunction) => void;
6
+ export declare const noop: () => void;
7
+ export declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | null | undefined)[]) => (...a: Parameters<T>) => void;
8
+ export declare const uuid: () => string;
9
+ export declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
10
+ export declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
11
+ export declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
12
+ export declare function debounce<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
13
+ export declare const hash: (value: string) => string;
@@ -1,15 +1,13 @@
1
- type MaybeFunction<T> = T | (() => T);
2
- type Nullable<T> = T | null | undefined;
3
- declare const runIfFn: <T>(v: T | undefined, ...a: T extends (...a: any[]) => void ? Parameters<T> : never) => T extends (...a: any[]) => void ? NonNullable<ReturnType<T>> : NonNullable<T>;
4
- declare const cast: <T>(v: unknown) => T;
5
- declare const identity: (v: VoidFunction) => void;
6
- declare const noop: () => void;
7
- declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | null | undefined)[]) => (...a: Parameters<T>) => void;
8
- declare const uuid: () => string;
9
- declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
10
- declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
11
- declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
12
- declare function debounce<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
13
- declare const hash: (value: string) => string;
14
-
15
- export { type MaybeFunction, type Nullable, callAll, cast, debounce, hash, identity, match, noop, runIfFn, throttle, tryCatch, uuid };
1
+ export type MaybeFunction<T> = T | (() => T);
2
+ export type Nullable<T> = T | null | undefined;
3
+ export declare const runIfFn: <T>(v: T | undefined, ...a: T extends ((...a: any[]) => void) ? Parameters<T> : never) => T extends ((...a: any[]) => void) ? NonNullable<ReturnType<T>> : NonNullable<T>;
4
+ export declare const cast: <T>(v: unknown) => T;
5
+ export declare const identity: (v: VoidFunction) => void;
6
+ export declare const noop: () => void;
7
+ export declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | null | undefined)[]) => (...a: Parameters<T>) => void;
8
+ export declare const uuid: () => string;
9
+ export declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
10
+ export declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
11
+ export declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
12
+ export declare function debounce<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
13
+ export declare const hash: (value: string) => string;
package/dist/functions.js CHANGED
@@ -1,136 +1,97 @@
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/functions.ts
21
- var functions_exports = {};
22
- __export(functions_exports, {
23
- callAll: () => callAll,
24
- cast: () => cast,
25
- debounce: () => debounce,
26
- hash: () => hash,
27
- identity: () => identity,
28
- match: () => match,
29
- noop: () => noop,
30
- runIfFn: () => runIfFn,
31
- throttle: () => throttle,
32
- tryCatch: () => tryCatch,
33
- uuid: () => uuid
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
34
4
  });
35
- module.exports = __toCommonJS(functions_exports);
36
- var import_guard = require("./guard.js");
37
- var runIfFn = (v, ...a) => {
38
- const res = typeof v === "function" ? v(...a) : v;
39
- return res ?? void 0;
40
- };
41
- var cast = (v) => v;
42
- var identity = (v) => v();
43
- var noop = () => {
5
+ const require_guard = require("./guard.js");
6
+ // src/functions.ts
7
+ const runIfFn = (v, ...a) => {
8
+ return (typeof v === "function" ? v(...a) : v) ?? void 0;
44
9
  };
45
- var callAll = (...fns) => (...a) => {
46
- fns.forEach(function(fn) {
47
- fn?.(...a);
48
- });
10
+ const cast = (v) => v;
11
+ const identity = (v) => v();
12
+ const noop = () => {};
13
+ const callAll = (...fns) => (...a) => {
14
+ fns.forEach(function(fn) {
15
+ fn?.(...a);
16
+ });
49
17
  };
50
- var uuid = /* @__PURE__ */ (() => {
51
- let id = 0;
52
- return () => {
53
- id++;
54
- return id.toString(36);
55
- };
18
+ const uuid = /*#__PURE__*/ (() => {
19
+ let id = 0;
20
+ return () => {
21
+ id++;
22
+ return id.toString(36);
23
+ };
56
24
  })();
57
25
  function match(key, record, ...args) {
58
- if (key in record) {
59
- const fn = record[key];
60
- return (0, import_guard.isFunction)(fn) ? fn(...args) : fn;
61
- }
62
- const error = new Error(`No matching key: ${JSON.stringify(key)} in ${JSON.stringify(Object.keys(record))}`);
63
- Error.captureStackTrace?.(error, match);
64
- throw error;
26
+ if (key in record) {
27
+ const fn = record[key];
28
+ return require_guard.isFunction(fn) ? fn(...args) : fn;
29
+ }
30
+ const error = /* @__PURE__ */ new Error(`No matching key: ${JSON.stringify(key)} in ${JSON.stringify(Object.keys(record))}`);
31
+ Error.captureStackTrace?.(error, match);
32
+ throw error;
65
33
  }
66
- var tryCatch = (fn, fallback) => {
67
- try {
68
- return fn();
69
- } catch (error) {
70
- if (error instanceof Error) {
71
- Error.captureStackTrace?.(error, tryCatch);
72
- }
73
- return fallback?.();
74
- }
34
+ const tryCatch = (fn, fallback) => {
35
+ try {
36
+ return fn();
37
+ } catch (error) {
38
+ if (error instanceof Error) Error.captureStackTrace?.(error, tryCatch);
39
+ return fallback?.();
40
+ }
75
41
  };
76
42
  function throttle(fn, wait = 0) {
77
- let lastCall = 0;
78
- let timeout = null;
79
- return ((...args) => {
80
- const now = Date.now();
81
- const timeSinceLastCall = now - lastCall;
82
- if (timeSinceLastCall >= wait) {
83
- if (timeout) {
84
- clearTimeout(timeout);
85
- timeout = null;
86
- }
87
- fn(...args);
88
- lastCall = now;
89
- } else if (!timeout) {
90
- timeout = setTimeout(() => {
91
- fn(...args);
92
- lastCall = Date.now();
93
- timeout = null;
94
- }, wait - timeSinceLastCall);
95
- }
96
- });
43
+ let lastCall = 0;
44
+ let timeout = null;
45
+ return ((...args) => {
46
+ const now = Date.now();
47
+ const timeSinceLastCall = now - lastCall;
48
+ if (timeSinceLastCall >= wait) {
49
+ if (timeout) {
50
+ clearTimeout(timeout);
51
+ timeout = null;
52
+ }
53
+ fn(...args);
54
+ lastCall = now;
55
+ } else if (!timeout) timeout = setTimeout(() => {
56
+ fn(...args);
57
+ lastCall = Date.now();
58
+ timeout = null;
59
+ }, wait - timeSinceLastCall);
60
+ });
97
61
  }
98
62
  function debounce(fn, wait = 0) {
99
- let timeout = null;
100
- return ((...args) => {
101
- if (timeout) {
102
- clearTimeout(timeout);
103
- timeout = null;
104
- }
105
- timeout = setTimeout(() => {
106
- fn(...args);
107
- }, wait);
108
- });
63
+ let timeout = null;
64
+ return ((...args) => {
65
+ if (timeout) {
66
+ clearTimeout(timeout);
67
+ timeout = null;
68
+ }
69
+ timeout = setTimeout(() => {
70
+ fn(...args);
71
+ }, wait);
72
+ });
109
73
  }
110
- var toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
74
+ const toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
111
75
  function toName(code) {
112
- let name = "";
113
- let x;
114
- for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
115
- return toChar(x % 52) + name;
76
+ let name = "";
77
+ let x;
78
+ for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
79
+ return toChar(x % 52) + name;
116
80
  }
117
81
  function toPhash(h, x) {
118
- let i = x.length;
119
- while (i) h = h * 33 ^ x.charCodeAt(--i);
120
- return h;
82
+ let i = x.length;
83
+ while (i) h = h * 33 ^ x.charCodeAt(--i);
84
+ return h;
121
85
  }
122
- var hash = (value) => toName(toPhash(5381, value) >>> 0);
123
- // Annotate the CommonJS export names for ESM import in node:
124
- 0 && (module.exports = {
125
- callAll,
126
- cast,
127
- debounce,
128
- hash,
129
- identity,
130
- match,
131
- noop,
132
- runIfFn,
133
- throttle,
134
- tryCatch,
135
- uuid
136
- });
86
+ const hash = (value) => toName(toPhash(5381, value) >>> 0);
87
+ exports.callAll = callAll;
88
+ exports.cast = cast;
89
+ exports.debounce = debounce;
90
+ exports.hash = hash;
91
+ exports.identity = identity;
92
+ exports.match = match;
93
+ exports.noop = noop;
94
+ exports.runIfFn = runIfFn;
95
+ exports.throttle = throttle;
96
+ exports.tryCatch = tryCatch;
97
+ exports.uuid = uuid;
@@ -1,103 +1,83 @@
1
- import "./chunk-MXGZDBDQ.mjs";
2
-
3
- // src/functions.ts
4
1
  import { isFunction } from "./guard.mjs";
5
- var runIfFn = (v, ...a) => {
6
- const res = typeof v === "function" ? v(...a) : v;
7
- return res ?? void 0;
8
- };
9
- var cast = (v) => v;
10
- var identity = (v) => v();
11
- var noop = () => {
2
+ // src/functions.ts
3
+ const runIfFn = (v, ...a) => {
4
+ return (typeof v === "function" ? v(...a) : v) ?? void 0;
12
5
  };
13
- var callAll = (...fns) => (...a) => {
14
- fns.forEach(function(fn) {
15
- fn?.(...a);
16
- });
6
+ const cast = (v) => v;
7
+ const identity = (v) => v();
8
+ const noop = () => {};
9
+ const callAll = (...fns) => (...a) => {
10
+ fns.forEach(function(fn) {
11
+ fn?.(...a);
12
+ });
17
13
  };
18
- var uuid = /* @__PURE__ */ (() => {
19
- let id = 0;
20
- return () => {
21
- id++;
22
- return id.toString(36);
23
- };
14
+ const uuid = /*#__PURE__*/ (() => {
15
+ let id = 0;
16
+ return () => {
17
+ id++;
18
+ return id.toString(36);
19
+ };
24
20
  })();
25
21
  function match(key, record, ...args) {
26
- if (key in record) {
27
- const fn = record[key];
28
- return isFunction(fn) ? fn(...args) : fn;
29
- }
30
- const error = new Error(`No matching key: ${JSON.stringify(key)} in ${JSON.stringify(Object.keys(record))}`);
31
- Error.captureStackTrace?.(error, match);
32
- throw error;
22
+ if (key in record) {
23
+ const fn = record[key];
24
+ return isFunction(fn) ? fn(...args) : fn;
25
+ }
26
+ const error = /* @__PURE__ */ new Error(`No matching key: ${JSON.stringify(key)} in ${JSON.stringify(Object.keys(record))}`);
27
+ Error.captureStackTrace?.(error, match);
28
+ throw error;
33
29
  }
34
- var tryCatch = (fn, fallback) => {
35
- try {
36
- return fn();
37
- } catch (error) {
38
- if (error instanceof Error) {
39
- Error.captureStackTrace?.(error, tryCatch);
40
- }
41
- return fallback?.();
42
- }
30
+ const tryCatch = (fn, fallback) => {
31
+ try {
32
+ return fn();
33
+ } catch (error) {
34
+ if (error instanceof Error) Error.captureStackTrace?.(error, tryCatch);
35
+ return fallback?.();
36
+ }
43
37
  };
44
38
  function throttle(fn, wait = 0) {
45
- let lastCall = 0;
46
- let timeout = null;
47
- return ((...args) => {
48
- const now = Date.now();
49
- const timeSinceLastCall = now - lastCall;
50
- if (timeSinceLastCall >= wait) {
51
- if (timeout) {
52
- clearTimeout(timeout);
53
- timeout = null;
54
- }
55
- fn(...args);
56
- lastCall = now;
57
- } else if (!timeout) {
58
- timeout = setTimeout(() => {
59
- fn(...args);
60
- lastCall = Date.now();
61
- timeout = null;
62
- }, wait - timeSinceLastCall);
63
- }
64
- });
39
+ let lastCall = 0;
40
+ let timeout = null;
41
+ return ((...args) => {
42
+ const now = Date.now();
43
+ const timeSinceLastCall = now - lastCall;
44
+ if (timeSinceLastCall >= wait) {
45
+ if (timeout) {
46
+ clearTimeout(timeout);
47
+ timeout = null;
48
+ }
49
+ fn(...args);
50
+ lastCall = now;
51
+ } else if (!timeout) timeout = setTimeout(() => {
52
+ fn(...args);
53
+ lastCall = Date.now();
54
+ timeout = null;
55
+ }, wait - timeSinceLastCall);
56
+ });
65
57
  }
66
58
  function debounce(fn, wait = 0) {
67
- let timeout = null;
68
- return ((...args) => {
69
- if (timeout) {
70
- clearTimeout(timeout);
71
- timeout = null;
72
- }
73
- timeout = setTimeout(() => {
74
- fn(...args);
75
- }, wait);
76
- });
59
+ let timeout = null;
60
+ return ((...args) => {
61
+ if (timeout) {
62
+ clearTimeout(timeout);
63
+ timeout = null;
64
+ }
65
+ timeout = setTimeout(() => {
66
+ fn(...args);
67
+ }, wait);
68
+ });
77
69
  }
78
- var toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
70
+ const toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
79
71
  function toName(code) {
80
- let name = "";
81
- let x;
82
- for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
83
- return toChar(x % 52) + name;
72
+ let name = "";
73
+ let x;
74
+ for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
75
+ return toChar(x % 52) + name;
84
76
  }
85
77
  function toPhash(h, x) {
86
- let i = x.length;
87
- while (i) h = h * 33 ^ x.charCodeAt(--i);
88
- return h;
78
+ let i = x.length;
79
+ while (i) h = h * 33 ^ x.charCodeAt(--i);
80
+ return h;
89
81
  }
90
- var hash = (value) => toName(toPhash(5381, value) >>> 0);
91
- export {
92
- callAll,
93
- cast,
94
- debounce,
95
- hash,
96
- identity,
97
- match,
98
- noop,
99
- runIfFn,
100
- throttle,
101
- tryCatch,
102
- uuid
103
- };
82
+ const hash = (value) => toName(toPhash(5381, value) >>> 0);
83
+ export { callAll, cast, debounce, hash, identity, match, noop, runIfFn, throttle, tryCatch, uuid };
package/dist/guard.d.mts CHANGED
@@ -1,14 +1,13 @@
1
1
  type AnyFunction = (...args: any[]) => any;
2
- declare const isDev: () => boolean;
3
- declare const isArray: (v: any) => v is any[];
4
- declare const isBoolean: (v: any) => v is boolean;
5
- declare const isObjectLike: (v: any) => v is Record<string, any>;
6
- declare const isObject: (v: any) => v is Record<string, any>;
7
- declare const isNumber: (v: any) => v is number;
8
- declare const isString: (v: any) => v is string;
9
- declare const isFunction: (v: any) => v is AnyFunction;
10
- declare const isNull: (v: any) => v is null | undefined;
11
- declare const hasProp: <T extends string>(obj: any, prop: T) => obj is Record<T, any>;
12
- declare const isPlainObject: (v: any) => boolean;
13
-
14
- export { hasProp, isArray, isBoolean, isDev, isFunction, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString };
2
+ export declare const isDev: () => boolean;
3
+ export declare const isArray: (v: any) => v is any[];
4
+ export declare const isBoolean: (v: any) => v is boolean;
5
+ export declare const isObjectLike: (v: any) => v is Record<string, any>;
6
+ export declare const isObject: (v: any) => v is Record<string, any>;
7
+ export declare const isNumber: (v: any) => v is number;
8
+ export declare const isString: (v: any) => v is string;
9
+ export declare const isFunction: (v: any) => v is AnyFunction;
10
+ export declare const isNull: (v: any) => v is null | undefined;
11
+ export declare const hasProp: <T extends string>(obj: any, prop: T) => obj is Record<T, any>;
12
+ export declare const isPlainObject: (v: any) => boolean;
13
+ export {};