@zag-js/utils
Advanced tools
Comparing version 0.0.0-v1-beta-20250220125322 to 0.0.0-v1-beta-20250221135618
@@ -23,2 +23,3 @@ declare function toArray<T>(v: T | T[] | undefined | null): T[]; | ||
declare const chunk: <T>(v: T[], size: number) => T[][]; | ||
declare function flatArray<T>(arr: T[]): T[]; | ||
@@ -37,2 +38,3 @@ declare const isEqual: (a: any, b: any) => boolean; | ||
declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R; | ||
declare function throttle<T extends (...args: any[]) => void>(fn: T, wait?: number): T; | ||
@@ -107,2 +109,2 @@ type AnyFunction = (...args: any[]) => any; | ||
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, 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, toArray, toFixedNumber, tryCatch, uniq, uuid, warn, wrap }; | ||
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 }; |
@@ -54,2 +54,10 @@ 'use strict'; | ||
}; | ||
function flatArray(arr) { | ||
return arr.reduce((flat, item) => { | ||
if (Array.isArray(item)) { | ||
return flat.concat(flatArray(item)); | ||
} | ||
return flat.concat(item); | ||
}, []); | ||
} | ||
@@ -131,2 +139,24 @@ // src/equal.ts | ||
}; | ||
function throttle(fn, wait = 0) { | ||
let lastCall = 0; | ||
let timeout = null; | ||
return (...args) => { | ||
const now = Date.now(); | ||
const timeSinceLastCall = now - lastCall; | ||
if (timeSinceLastCall >= wait) { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
fn(...args); | ||
lastCall = now; | ||
} else if (!timeout) { | ||
timeout = setTimeout(() => { | ||
fn(...args); | ||
lastCall = Date.now(); | ||
timeout = null; | ||
}, wait - timeSinceLastCall); | ||
} | ||
}; | ||
} | ||
@@ -378,2 +408,3 @@ // src/guard.ts | ||
exports.first = first; | ||
exports.flatArray = flatArray; | ||
exports.fromLength = fromLength; | ||
@@ -436,2 +467,3 @@ exports.getClosestValue = getClosestValue; | ||
exports.splitProps = splitProps; | ||
exports.throttle = throttle; | ||
exports.toArray = toArray; | ||
@@ -438,0 +470,0 @@ exports.toFixedNumber = toFixedNumber; |
{ | ||
"name": "@zag-js/utils", | ||
"version": "0.0.0-v1-beta-20250220125322", | ||
"version": "0.0.0-v1-beta-20250221135618", | ||
"description": "", | ||
@@ -33,5 +33,10 @@ "keywords": [ | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
@@ -38,0 +43,0 @@ "./package.json": "./package.json" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43257
951