@zag-js/utils 0.82.1 → 1.0.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/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +65 -0
- package/dist/index.mjs +61 -1
- package/package.json +9 -4
package/dist/index.d.mts
CHANGED
|
@@ -20,6 +20,7 @@ declare function next<T>(v: T[], idx: number, opts?: IndexOptions): T | undefine
|
|
|
20
20
|
declare function prevIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
|
|
21
21
|
declare function prev<T>(v: T[], index: number, opts?: IndexOptions): T | undefined;
|
|
22
22
|
declare const chunk: <T>(v: T[], size: number) => T[][];
|
|
23
|
+
declare function flatArray<T>(arr: T[]): T[];
|
|
23
24
|
|
|
24
25
|
declare const isEqual: (a: any, b: any) => boolean;
|
|
25
26
|
|
|
@@ -27,11 +28,13 @@ type MaybeFunction<T> = T | (() => T);
|
|
|
27
28
|
type Nullable<T> = T | null | undefined;
|
|
28
29
|
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>;
|
|
29
30
|
declare const cast: <T>(v: unknown) => T;
|
|
31
|
+
declare const identity: (v: VoidFunction) => void;
|
|
30
32
|
declare const noop: () => void;
|
|
31
33
|
declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | undefined)[]) => (...a: Parameters<T>) => void;
|
|
32
34
|
declare const uuid: () => string;
|
|
33
35
|
declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
|
|
34
36
|
declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
|
|
37
|
+
declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
|
|
35
38
|
|
|
36
39
|
type AnyFunction = (...args: any[]) => any;
|
|
37
40
|
declare const isDev: () => boolean;
|
|
@@ -93,9 +96,12 @@ declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[]
|
|
|
93
96
|
declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
|
|
94
97
|
declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
|
|
95
98
|
|
|
99
|
+
declare function setRafInterval(callback: () => void, interval: number): () => void;
|
|
100
|
+
declare function setRafTimeout(callback: () => void, delay: number): () => void;
|
|
101
|
+
|
|
96
102
|
declare function warn(m: string): void;
|
|
97
103
|
declare function warn(c: boolean, m: string): void;
|
|
98
104
|
declare function invariant(m: string): void;
|
|
99
105
|
declare function invariant(c: boolean, m: string): void;
|
|
100
106
|
|
|
101
|
-
export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setValueAtIndex, snapValueToStep, splitProps, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
|
107
|
+
export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ declare function next<T>(v: T[], idx: number, opts?: IndexOptions): T | undefine
|
|
|
20
20
|
declare function prevIndex<T>(v: T[], idx: number, opts?: IndexOptions): number;
|
|
21
21
|
declare function prev<T>(v: T[], index: number, opts?: IndexOptions): T | undefined;
|
|
22
22
|
declare const chunk: <T>(v: T[], size: number) => T[][];
|
|
23
|
+
declare function flatArray<T>(arr: T[]): T[];
|
|
23
24
|
|
|
24
25
|
declare const isEqual: (a: any, b: any) => boolean;
|
|
25
26
|
|
|
@@ -27,11 +28,13 @@ type MaybeFunction<T> = T | (() => T);
|
|
|
27
28
|
type Nullable<T> = T | null | undefined;
|
|
28
29
|
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>;
|
|
29
30
|
declare const cast: <T>(v: unknown) => T;
|
|
31
|
+
declare const identity: (v: VoidFunction) => void;
|
|
30
32
|
declare const noop: () => void;
|
|
31
33
|
declare const callAll: <T extends (...a: any[]) => void>(...fns: (T | undefined)[]) => (...a: Parameters<T>) => void;
|
|
32
34
|
declare const uuid: () => string;
|
|
33
35
|
declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
|
|
34
36
|
declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
|
|
37
|
+
declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T;
|
|
35
38
|
|
|
36
39
|
type AnyFunction = (...args: any[]) => any;
|
|
37
40
|
declare const isDev: () => boolean;
|
|
@@ -93,9 +96,12 @@ declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[]
|
|
|
93
96
|
declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
|
|
94
97
|
declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
|
|
95
98
|
|
|
99
|
+
declare function setRafInterval(callback: () => void, interval: number): () => void;
|
|
100
|
+
declare function setRafTimeout(callback: () => void, delay: number): () => void;
|
|
101
|
+
|
|
96
102
|
declare function warn(m: string): void;
|
|
97
103
|
declare function warn(c: boolean, m: string): void;
|
|
98
104
|
declare function invariant(m: string): void;
|
|
99
105
|
declare function invariant(c: boolean, m: string): void;
|
|
100
106
|
|
|
101
|
-
export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setValueAtIndex, snapValueToStep, splitProps, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
|
107
|
+
export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
package/dist/index.js
CHANGED
|
@@ -51,6 +51,14 @@ var chunk = (v, size) => {
|
|
|
51
51
|
return rows;
|
|
52
52
|
}, res);
|
|
53
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
|
+
}
|
|
54
62
|
|
|
55
63
|
// src/equal.ts
|
|
56
64
|
var isArrayLike = (value) => value?.constructor.name === "Array";
|
|
@@ -93,6 +101,7 @@ var runIfFn = (v, ...a) => {
|
|
|
93
101
|
return res ?? void 0;
|
|
94
102
|
};
|
|
95
103
|
var cast = (v) => v;
|
|
104
|
+
var identity = (v) => v();
|
|
96
105
|
var noop = () => {
|
|
97
106
|
};
|
|
98
107
|
var callAll = (...fns) => (...a) => {
|
|
@@ -126,6 +135,28 @@ var tryCatch = (fn, fallback) => {
|
|
|
126
135
|
return fallback?.();
|
|
127
136
|
}
|
|
128
137
|
};
|
|
138
|
+
function throttle(fn, wait = 0) {
|
|
139
|
+
let lastCall = 0;
|
|
140
|
+
let timeout = null;
|
|
141
|
+
return (...args) => {
|
|
142
|
+
const now = Date.now();
|
|
143
|
+
const timeSinceLastCall = now - lastCall;
|
|
144
|
+
if (timeSinceLastCall >= wait) {
|
|
145
|
+
if (timeout) {
|
|
146
|
+
clearTimeout(timeout);
|
|
147
|
+
timeout = null;
|
|
148
|
+
}
|
|
149
|
+
fn(...args);
|
|
150
|
+
lastCall = now;
|
|
151
|
+
} else if (!timeout) {
|
|
152
|
+
timeout = setTimeout(() => {
|
|
153
|
+
fn(...args);
|
|
154
|
+
lastCall = Date.now();
|
|
155
|
+
timeout = null;
|
|
156
|
+
}, wait - timeSinceLastCall);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
129
160
|
|
|
130
161
|
// src/guard.ts
|
|
131
162
|
var isDev = () => process.env.NODE_ENV !== "production";
|
|
@@ -315,6 +346,35 @@ function omit(obj, keys) {
|
|
|
315
346
|
return createSplitProps(keys)(obj)[1];
|
|
316
347
|
}
|
|
317
348
|
|
|
349
|
+
// src/timers.ts
|
|
350
|
+
function setRafInterval(callback, interval) {
|
|
351
|
+
let start = performance.now();
|
|
352
|
+
let handle;
|
|
353
|
+
function loop(now) {
|
|
354
|
+
handle = requestAnimationFrame(loop);
|
|
355
|
+
const delta = now - start;
|
|
356
|
+
if (delta >= interval) {
|
|
357
|
+
start = now - delta % interval;
|
|
358
|
+
callback();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
handle = requestAnimationFrame(loop);
|
|
362
|
+
return () => cancelAnimationFrame(handle);
|
|
363
|
+
}
|
|
364
|
+
function setRafTimeout(callback, delay) {
|
|
365
|
+
const start = performance.now();
|
|
366
|
+
let handle;
|
|
367
|
+
function loop(now) {
|
|
368
|
+
handle = requestAnimationFrame(loop);
|
|
369
|
+
const delta = now - start;
|
|
370
|
+
if (delta >= delay) {
|
|
371
|
+
callback();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
handle = requestAnimationFrame(loop);
|
|
375
|
+
return () => cancelAnimationFrame(handle);
|
|
376
|
+
}
|
|
377
|
+
|
|
318
378
|
// src/warning.ts
|
|
319
379
|
function warn(...a) {
|
|
320
380
|
const m = a.length === 1 ? a[0] : a[1];
|
|
@@ -343,6 +403,7 @@ exports.compact = compact;
|
|
|
343
403
|
exports.createSplitProps = createSplitProps;
|
|
344
404
|
exports.decrementValue = decrementValue;
|
|
345
405
|
exports.first = first;
|
|
406
|
+
exports.flatArray = flatArray;
|
|
346
407
|
exports.fromLength = fromLength;
|
|
347
408
|
exports.getClosestValue = getClosestValue;
|
|
348
409
|
exports.getClosestValueIndex = getClosestValueIndex;
|
|
@@ -357,6 +418,7 @@ exports.getValueSetterAtIndex = getValueSetterAtIndex;
|
|
|
357
418
|
exports.getValueTransformer = getValueTransformer;
|
|
358
419
|
exports.has = has;
|
|
359
420
|
exports.hasProp = hasProp;
|
|
421
|
+
exports.identity = identity;
|
|
360
422
|
exports.incrementValue = incrementValue;
|
|
361
423
|
exports.insertAt = insertAt;
|
|
362
424
|
exports.invariant = invariant;
|
|
@@ -394,9 +456,12 @@ exports.roundToDpr = roundToDpr;
|
|
|
394
456
|
exports.roundToStepPrecision = roundToStepPrecision;
|
|
395
457
|
exports.roundValue = roundValue;
|
|
396
458
|
exports.runIfFn = runIfFn;
|
|
459
|
+
exports.setRafInterval = setRafInterval;
|
|
460
|
+
exports.setRafTimeout = setRafTimeout;
|
|
397
461
|
exports.setValueAtIndex = setValueAtIndex;
|
|
398
462
|
exports.snapValueToStep = snapValueToStep;
|
|
399
463
|
exports.splitProps = splitProps;
|
|
464
|
+
exports.throttle = throttle;
|
|
400
465
|
exports.toArray = toArray;
|
|
401
466
|
exports.toFixedNumber = toFixedNumber;
|
|
402
467
|
exports.tryCatch = tryCatch;
|
package/dist/index.mjs
CHANGED
|
@@ -49,6 +49,14 @@ var chunk = (v, size) => {
|
|
|
49
49
|
return rows;
|
|
50
50
|
}, res);
|
|
51
51
|
};
|
|
52
|
+
function flatArray(arr) {
|
|
53
|
+
return arr.reduce((flat, item) => {
|
|
54
|
+
if (Array.isArray(item)) {
|
|
55
|
+
return flat.concat(flatArray(item));
|
|
56
|
+
}
|
|
57
|
+
return flat.concat(item);
|
|
58
|
+
}, []);
|
|
59
|
+
}
|
|
52
60
|
|
|
53
61
|
// src/equal.ts
|
|
54
62
|
var isArrayLike = (value) => value?.constructor.name === "Array";
|
|
@@ -91,6 +99,7 @@ var runIfFn = (v, ...a) => {
|
|
|
91
99
|
return res ?? void 0;
|
|
92
100
|
};
|
|
93
101
|
var cast = (v) => v;
|
|
102
|
+
var identity = (v) => v();
|
|
94
103
|
var noop = () => {
|
|
95
104
|
};
|
|
96
105
|
var callAll = (...fns) => (...a) => {
|
|
@@ -124,6 +133,28 @@ var tryCatch = (fn, fallback) => {
|
|
|
124
133
|
return fallback?.();
|
|
125
134
|
}
|
|
126
135
|
};
|
|
136
|
+
function throttle(fn, wait = 0) {
|
|
137
|
+
let lastCall = 0;
|
|
138
|
+
let timeout = null;
|
|
139
|
+
return (...args) => {
|
|
140
|
+
const now = Date.now();
|
|
141
|
+
const timeSinceLastCall = now - lastCall;
|
|
142
|
+
if (timeSinceLastCall >= wait) {
|
|
143
|
+
if (timeout) {
|
|
144
|
+
clearTimeout(timeout);
|
|
145
|
+
timeout = null;
|
|
146
|
+
}
|
|
147
|
+
fn(...args);
|
|
148
|
+
lastCall = now;
|
|
149
|
+
} else if (!timeout) {
|
|
150
|
+
timeout = setTimeout(() => {
|
|
151
|
+
fn(...args);
|
|
152
|
+
lastCall = Date.now();
|
|
153
|
+
timeout = null;
|
|
154
|
+
}, wait - timeSinceLastCall);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
127
158
|
|
|
128
159
|
// src/guard.ts
|
|
129
160
|
var isDev = () => process.env.NODE_ENV !== "production";
|
|
@@ -313,6 +344,35 @@ function omit(obj, keys) {
|
|
|
313
344
|
return createSplitProps(keys)(obj)[1];
|
|
314
345
|
}
|
|
315
346
|
|
|
347
|
+
// src/timers.ts
|
|
348
|
+
function setRafInterval(callback, interval) {
|
|
349
|
+
let start = performance.now();
|
|
350
|
+
let handle;
|
|
351
|
+
function loop(now) {
|
|
352
|
+
handle = requestAnimationFrame(loop);
|
|
353
|
+
const delta = now - start;
|
|
354
|
+
if (delta >= interval) {
|
|
355
|
+
start = now - delta % interval;
|
|
356
|
+
callback();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
handle = requestAnimationFrame(loop);
|
|
360
|
+
return () => cancelAnimationFrame(handle);
|
|
361
|
+
}
|
|
362
|
+
function setRafTimeout(callback, delay) {
|
|
363
|
+
const start = performance.now();
|
|
364
|
+
let handle;
|
|
365
|
+
function loop(now) {
|
|
366
|
+
handle = requestAnimationFrame(loop);
|
|
367
|
+
const delta = now - start;
|
|
368
|
+
if (delta >= delay) {
|
|
369
|
+
callback();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
handle = requestAnimationFrame(loop);
|
|
373
|
+
return () => cancelAnimationFrame(handle);
|
|
374
|
+
}
|
|
375
|
+
|
|
316
376
|
// src/warning.ts
|
|
317
377
|
function warn(...a) {
|
|
318
378
|
const m = a.length === 1 ? a[0] : a[1];
|
|
@@ -329,4 +389,4 @@ function invariant(...a) {
|
|
|
329
389
|
}
|
|
330
390
|
}
|
|
331
391
|
|
|
332
|
-
export { add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setValueAtIndex, snapValueToStep, splitProps, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
|
392
|
+
export { add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, decrementValue, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -30,9 +30,14 @@
|
|
|
30
30
|
"types": "dist/index.d.ts",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": {
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"default": "./dist/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
}
|
|
36
41
|
},
|
|
37
42
|
"./package.json": "./package.json"
|
|
38
43
|
},
|