@zag-js/utils 1.34.0 → 1.35.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,27 @@
1
+ declare function toArray<T>(v: T | T[] | undefined | null): T[];
2
+ declare const fromLength: (length: number) => number[];
3
+ declare const first: <T>(v: T[]) => T | undefined;
4
+ declare const last: <T>(v: T[]) => T | undefined;
5
+ declare const isEmpty: <T>(v: T[]) => boolean;
6
+ declare const has: <T>(v: T[], t: T) => boolean;
7
+ declare const add: <T>(v: T[], ...items: T[]) => T[];
8
+ declare const remove: <T>(v: T[], ...items: T[]) => T[];
9
+ declare const removeAt: <T>(v: T[], i: number) => T[];
10
+ declare const insertAt: <T>(v: T[], i: number, ...items: T[]) => T[];
11
+ declare const uniq: <T>(v: T[]) => T[];
12
+ declare const diff: <T>(a: T[], b: T[]) => T[];
13
+ declare const addOrRemove: <T>(v: T[], item: T) => T[];
14
+ declare function clear<T>(v: T[]): T[];
15
+ type IndexOptions = {
16
+ step?: number | undefined;
17
+ loop?: boolean | undefined;
18
+ };
19
+ declare function nextIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
20
+ declare function next<T>(v: T[], idx: number, opts?: IndexOptions): T | undefined;
21
+ declare function prevIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
22
+ declare function prev<T>(v: T[], index: number, opts?: IndexOptions): T | undefined;
23
+ declare function chunk<T>(v: T[], size: number): T[][];
24
+ declare function flatArray<T>(arr: T[]): T[];
25
+ declare function partition<T>(arr: T[], fn: (value: T) => boolean): [T[], T[]];
26
+
27
+ export { type IndexOptions, add, addOrRemove, chunk, clear, diff, first, flatArray, fromLength, has, insertAt, isEmpty, last, next, nextIndex, partition, prev, prevIndex, remove, removeAt, toArray, uniq };
@@ -0,0 +1,27 @@
1
+ declare function toArray<T>(v: T | T[] | undefined | null): T[];
2
+ declare const fromLength: (length: number) => number[];
3
+ declare const first: <T>(v: T[]) => T | undefined;
4
+ declare const last: <T>(v: T[]) => T | undefined;
5
+ declare const isEmpty: <T>(v: T[]) => boolean;
6
+ declare const has: <T>(v: T[], t: T) => boolean;
7
+ declare const add: <T>(v: T[], ...items: T[]) => T[];
8
+ declare const remove: <T>(v: T[], ...items: T[]) => T[];
9
+ declare const removeAt: <T>(v: T[], i: number) => T[];
10
+ declare const insertAt: <T>(v: T[], i: number, ...items: T[]) => T[];
11
+ declare const uniq: <T>(v: T[]) => T[];
12
+ declare const diff: <T>(a: T[], b: T[]) => T[];
13
+ declare const addOrRemove: <T>(v: T[], item: T) => T[];
14
+ declare function clear<T>(v: T[]): T[];
15
+ type IndexOptions = {
16
+ step?: number | undefined;
17
+ loop?: boolean | undefined;
18
+ };
19
+ declare function nextIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
20
+ declare function next<T>(v: T[], idx: number, opts?: IndexOptions): T | undefined;
21
+ declare function prevIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
22
+ declare function prev<T>(v: T[], index: number, opts?: IndexOptions): T | undefined;
23
+ declare function chunk<T>(v: T[], size: number): T[][];
24
+ declare function flatArray<T>(arr: T[]): T[];
25
+ declare function partition<T>(arr: T[], fn: (value: T) => boolean): [T[], T[]];
26
+
27
+ export { type IndexOptions, add, addOrRemove, chunk, clear, diff, first, flatArray, fromLength, has, insertAt, isEmpty, last, next, nextIndex, partition, prev, prevIndex, remove, removeAt, toArray, uniq };
package/dist/array.js ADDED
@@ -0,0 +1,137 @@
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/array.ts
21
+ var array_exports = {};
22
+ __export(array_exports, {
23
+ add: () => add,
24
+ addOrRemove: () => addOrRemove,
25
+ chunk: () => chunk,
26
+ clear: () => clear,
27
+ diff: () => diff,
28
+ first: () => first,
29
+ flatArray: () => flatArray,
30
+ fromLength: () => fromLength,
31
+ has: () => has,
32
+ insertAt: () => insertAt,
33
+ isEmpty: () => isEmpty,
34
+ last: () => last,
35
+ next: () => next,
36
+ nextIndex: () => nextIndex,
37
+ partition: () => partition,
38
+ prev: () => prev,
39
+ prevIndex: () => prevIndex,
40
+ remove: () => remove,
41
+ removeAt: () => removeAt,
42
+ toArray: () => toArray,
43
+ uniq: () => uniq
44
+ });
45
+ module.exports = __toCommonJS(array_exports);
46
+ function toArray(v) {
47
+ if (v == null) return [];
48
+ return Array.isArray(v) ? v : [v];
49
+ }
50
+ var fromLength = (length) => Array.from(Array(length).keys());
51
+ var first = (v) => v[0];
52
+ var last = (v) => v[v.length - 1];
53
+ var isEmpty = (v) => v.length === 0;
54
+ var has = (v, t) => v.indexOf(t) !== -1;
55
+ var add = (v, ...items) => v.concat(items);
56
+ var remove = (v, ...items) => v.filter((t) => !items.includes(t));
57
+ var removeAt = (v, i) => v.filter((_, idx) => idx !== i);
58
+ var insertAt = (v, i, ...items) => [...v.slice(0, i), ...items, ...v.slice(i)];
59
+ var uniq = (v) => Array.from(new Set(v));
60
+ var diff = (a, b) => {
61
+ const set = new Set(b);
62
+ return a.filter((t) => !set.has(t));
63
+ };
64
+ var addOrRemove = (v, item) => has(v, item) ? remove(v, item) : add(v, item);
65
+ function clear(v) {
66
+ while (v.length > 0) v.pop();
67
+ return v;
68
+ }
69
+ function nextIndex(v, idx, opts = {}) {
70
+ const { step = 1, loop = true } = opts;
71
+ const next2 = idx + step;
72
+ const len = v.length;
73
+ const last2 = len - 1;
74
+ if (idx === -1) return step > 0 ? 0 : last2;
75
+ if (next2 < 0) return loop ? last2 : 0;
76
+ if (next2 >= len) return loop ? 0 : idx > len ? len : idx;
77
+ return next2;
78
+ }
79
+ function next(v, idx, opts = {}) {
80
+ return v[nextIndex(v, idx, opts)];
81
+ }
82
+ function prevIndex(v, idx, opts = {}) {
83
+ const { step = 1, loop = true } = opts;
84
+ return nextIndex(v, idx, { step: -step, loop });
85
+ }
86
+ function prev(v, index, opts = {}) {
87
+ return v[prevIndex(v, index, opts)];
88
+ }
89
+ function chunk(v, size) {
90
+ return v.reduce((rows, value, index) => {
91
+ if (index % size === 0) rows.push([value]);
92
+ else last(rows)?.push(value);
93
+ return rows;
94
+ }, []);
95
+ }
96
+ function flatArray(arr) {
97
+ return arr.reduce((flat, item) => {
98
+ if (Array.isArray(item)) {
99
+ return flat.concat(flatArray(item));
100
+ }
101
+ return flat.concat(item);
102
+ }, []);
103
+ }
104
+ function partition(arr, fn) {
105
+ return arr.reduce(
106
+ ([pass, fail], value) => {
107
+ if (fn(value)) pass.push(value);
108
+ else fail.push(value);
109
+ return [pass, fail];
110
+ },
111
+ [[], []]
112
+ );
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ add,
117
+ addOrRemove,
118
+ chunk,
119
+ clear,
120
+ diff,
121
+ first,
122
+ flatArray,
123
+ fromLength,
124
+ has,
125
+ insertAt,
126
+ isEmpty,
127
+ last,
128
+ next,
129
+ nextIndex,
130
+ partition,
131
+ prev,
132
+ prevIndex,
133
+ remove,
134
+ removeAt,
135
+ toArray,
136
+ uniq
137
+ });
package/dist/array.mjs ADDED
@@ -0,0 +1,94 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // src/array.ts
4
+ function toArray(v) {
5
+ if (v == null) return [];
6
+ return Array.isArray(v) ? v : [v];
7
+ }
8
+ var fromLength = (length) => Array.from(Array(length).keys());
9
+ var first = (v) => v[0];
10
+ var last = (v) => v[v.length - 1];
11
+ var isEmpty = (v) => v.length === 0;
12
+ var has = (v, t) => v.indexOf(t) !== -1;
13
+ var add = (v, ...items) => v.concat(items);
14
+ var remove = (v, ...items) => v.filter((t) => !items.includes(t));
15
+ var removeAt = (v, i) => v.filter((_, idx) => idx !== i);
16
+ var insertAt = (v, i, ...items) => [...v.slice(0, i), ...items, ...v.slice(i)];
17
+ var uniq = (v) => Array.from(new Set(v));
18
+ var diff = (a, b) => {
19
+ const set = new Set(b);
20
+ return a.filter((t) => !set.has(t));
21
+ };
22
+ var addOrRemove = (v, item) => has(v, item) ? remove(v, item) : add(v, item);
23
+ function clear(v) {
24
+ while (v.length > 0) v.pop();
25
+ return v;
26
+ }
27
+ function nextIndex(v, idx, opts = {}) {
28
+ const { step = 1, loop = true } = opts;
29
+ const next2 = idx + step;
30
+ const len = v.length;
31
+ const last2 = len - 1;
32
+ if (idx === -1) return step > 0 ? 0 : last2;
33
+ if (next2 < 0) return loop ? last2 : 0;
34
+ if (next2 >= len) return loop ? 0 : idx > len ? len : idx;
35
+ return next2;
36
+ }
37
+ function next(v, idx, opts = {}) {
38
+ return v[nextIndex(v, idx, opts)];
39
+ }
40
+ function prevIndex(v, idx, opts = {}) {
41
+ const { step = 1, loop = true } = opts;
42
+ return nextIndex(v, idx, { step: -step, loop });
43
+ }
44
+ function prev(v, index, opts = {}) {
45
+ return v[prevIndex(v, index, opts)];
46
+ }
47
+ function chunk(v, size) {
48
+ return v.reduce((rows, value, index) => {
49
+ if (index % size === 0) rows.push([value]);
50
+ else last(rows)?.push(value);
51
+ return rows;
52
+ }, []);
53
+ }
54
+ function flatArray(arr) {
55
+ return arr.reduce((flat, item) => {
56
+ if (Array.isArray(item)) {
57
+ return flat.concat(flatArray(item));
58
+ }
59
+ return flat.concat(item);
60
+ }, []);
61
+ }
62
+ function partition(arr, fn) {
63
+ return arr.reduce(
64
+ ([pass, fail], value) => {
65
+ if (fn(value)) pass.push(value);
66
+ else fail.push(value);
67
+ return [pass, fail];
68
+ },
69
+ [[], []]
70
+ );
71
+ }
72
+ export {
73
+ add,
74
+ addOrRemove,
75
+ chunk,
76
+ clear,
77
+ diff,
78
+ first,
79
+ flatArray,
80
+ fromLength,
81
+ has,
82
+ insertAt,
83
+ isEmpty,
84
+ last,
85
+ next,
86
+ nextIndex,
87
+ partition,
88
+ prev,
89
+ prevIndex,
90
+ remove,
91
+ removeAt,
92
+ toArray,
93
+ uniq
94
+ };
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
+
11
+ export {
12
+ __publicField,
13
+ __privateGet,
14
+ __privateAdd
15
+ };
@@ -0,0 +1,3 @@
1
+ declare const isEqual: (a: any, b: any) => boolean;
2
+
3
+ export { isEqual };
@@ -0,0 +1,3 @@
1
+ declare const isEqual: (a: any, b: any) => boolean;
2
+
3
+ export { isEqual };
package/dist/equal.js ADDED
@@ -0,0 +1,62 @@
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
24
+ });
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;
33
+ };
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
+ for (let i = 0; i < length; i++) {
50
+ const hasKey = Reflect.has(a, keys[i]);
51
+ if (!hasKey) return false;
52
+ }
53
+ for (let i = 0; i < length; i++) {
54
+ const key = keys[i];
55
+ if (!isEqual(a[key], b[key])) return false;
56
+ }
57
+ return true;
58
+ };
59
+ // Annotate the CommonJS export names for ESM import in node:
60
+ 0 && (module.exports = {
61
+ isEqual
62
+ });
package/dist/equal.mjs ADDED
@@ -0,0 +1,39 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // 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;
11
+ };
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
+ for (let i = 0; i < length; i++) {
28
+ const hasKey = Reflect.has(a, keys[i]);
29
+ if (!hasKey) return false;
30
+ }
31
+ for (let i = 0; i < length; i++) {
32
+ const key = keys[i];
33
+ if (!isEqual(a[key], b[key])) return false;
34
+ }
35
+ return true;
36
+ };
37
+ export {
38
+ isEqual
39
+ };
@@ -0,0 +1,15 @@
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 };
@@ -0,0 +1,15 @@
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 };
@@ -0,0 +1,136 @@
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
34
+ });
35
+ module.exports = __toCommonJS(functions_exports);
36
+ var import_guard = require("./guard.cjs");
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 = () => {
44
+ };
45
+ var callAll = (...fns) => (...a) => {
46
+ fns.forEach(function(fn) {
47
+ fn?.(...a);
48
+ });
49
+ };
50
+ var uuid = /* @__PURE__ */ (() => {
51
+ let id = 0;
52
+ return () => {
53
+ id++;
54
+ return id.toString(36);
55
+ };
56
+ })();
57
+ 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;
65
+ }
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
+ }
75
+ };
76
+ 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
+ });
97
+ }
98
+ 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
+ });
109
+ }
110
+ var toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
111
+ 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;
116
+ }
117
+ function toPhash(h, x) {
118
+ let i = x.length;
119
+ while (i) h = h * 33 ^ x.charCodeAt(--i);
120
+ return h;
121
+ }
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
+ });
@@ -0,0 +1,103 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // src/functions.ts
4
+ 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 = () => {
12
+ };
13
+ var callAll = (...fns) => (...a) => {
14
+ fns.forEach(function(fn) {
15
+ fn?.(...a);
16
+ });
17
+ };
18
+ var uuid = /* @__PURE__ */ (() => {
19
+ let id = 0;
20
+ return () => {
21
+ id++;
22
+ return id.toString(36);
23
+ };
24
+ })();
25
+ 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;
33
+ }
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
+ }
43
+ };
44
+ 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
+ });
65
+ }
66
+ 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
+ });
77
+ }
78
+ var toChar = (code) => String.fromCharCode(code + (code > 25 ? 39 : 97));
79
+ 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;
84
+ }
85
+ function toPhash(h, x) {
86
+ let i = x.length;
87
+ while (i) h = h * 33 ^ x.charCodeAt(--i);
88
+ return h;
89
+ }
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
+ };