@zag-js/utils 1.14.0 → 1.15.1

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 CHANGED
@@ -9,6 +9,7 @@ declare const remove: <T>(v: T[], ...items: T[]) => T[];
9
9
  declare const removeAt: <T>(v: T[], i: number) => T[];
10
10
  declare const insertAt: <T>(v: T[], i: number, ...items: T[]) => T[];
11
11
  declare const uniq: <T>(v: T[]) => T[];
12
+ declare const diff: <T>(a: T[], b: T[]) => T[];
12
13
  declare const addOrRemove: <T>(v: T[], item: T) => T[];
13
14
  declare function clear<T>(v: T[]): T[];
14
15
  type IndexOptions = {
@@ -113,4 +114,4 @@ declare function ensure<T>(c: T | null | undefined, m: () => string): asserts c
113
114
  type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
114
115
  declare function ensureProps<T, K extends keyof T>(props: T, keys: K[], scope?: string): asserts props is T & RequiredBy<T, K>;
115
116
 
116
- export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
117
+ export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, diff, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ declare const remove: <T>(v: T[], ...items: T[]) => T[];
9
9
  declare const removeAt: <T>(v: T[], i: number) => T[];
10
10
  declare const insertAt: <T>(v: T[], i: number, ...items: T[]) => T[];
11
11
  declare const uniq: <T>(v: T[]) => T[];
12
+ declare const diff: <T>(a: T[], b: T[]) => T[];
12
13
  declare const addOrRemove: <T>(v: T[], item: T) => T[];
13
14
  declare function clear<T>(v: T[]): T[];
14
15
  type IndexOptions = {
@@ -113,4 +114,4 @@ declare function ensure<T>(c: T | null | undefined, m: () => string): asserts c
113
114
  type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
114
115
  declare function ensureProps<T, K extends keyof T>(props: T, keys: K[], scope?: string): asserts props is T & RequiredBy<T, K>;
115
116
 
116
- export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
117
+ export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, diff, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/array.ts
4
4
  function toArray(v) {
5
- if (!v) return [];
5
+ if (v == null) return [];
6
6
  return Array.isArray(v) ? v : [v];
7
7
  }
8
8
  var fromLength = (length) => Array.from(Array(length).keys());
@@ -15,6 +15,10 @@ var remove = (v, ...items) => v.filter((t) => !items.includes(t));
15
15
  var removeAt = (v, i) => v.filter((_, idx) => idx !== i);
16
16
  var insertAt = (v, i, ...items) => [...v.slice(0, i), ...items, ...v.slice(i)];
17
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
+ };
18
22
  var addOrRemove = (v, item) => {
19
23
  if (has(v, item)) return remove(v, item);
20
24
  return add(v, item);
@@ -221,23 +225,19 @@ var roundToStepPrecision = (v, step) => {
221
225
  };
222
226
  var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
223
227
  var snapValueToStep = (v, vmin, vmax, step) => {
224
- vmin = Number(vmin);
225
- vmax = Number(vmax);
226
- let remainder = (v - (isNaN(vmin) ? 0 : vmin)) % step;
227
- let sv = roundToStepPrecision(
228
- abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder,
229
- step
230
- );
231
- if (!isNaN(vmin)) {
232
- if (sv < vmin) {
233
- sv = vmin;
234
- } else if (!isNaN(vmax) && sv > vmax) {
235
- sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
236
- }
237
- } else if (!isNaN(vmax) && sv > vmax) {
238
- sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
228
+ const min2 = vmin != null ? Number(vmin) : 0;
229
+ const max2 = Number(vmax);
230
+ const remainder = (v - min2) % step;
231
+ let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
232
+ snapped = roundToStepPrecision(snapped, step);
233
+ if (!isNaN(min2) && snapped < min2) {
234
+ snapped = min2;
235
+ } else if (!isNaN(max2) && snapped > max2) {
236
+ const stepsInRange = floor((max2 - min2) / step);
237
+ const largestValidStep = min2 + stepsInRange * step;
238
+ snapped = stepsInRange <= 0 || largestValidStep < min2 ? max2 : largestValidStep;
239
239
  }
240
- return roundToStepPrecision(sv, step);
240
+ return roundToStepPrecision(snapped, step);
241
241
  };
242
242
  var setValueAtIndex = (vs, i, v) => {
243
243
  if (vs[i] === v) return vs;
@@ -427,6 +427,7 @@ exports.compact = compact;
427
427
  exports.createSplitProps = createSplitProps;
428
428
  exports.debounce = debounce;
429
429
  exports.decrementValue = decrementValue;
430
+ exports.diff = diff;
430
431
  exports.ensure = ensure;
431
432
  exports.ensureProps = ensureProps;
432
433
  exports.first = first;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/array.ts
2
2
  function toArray(v) {
3
- if (!v) return [];
3
+ if (v == null) return [];
4
4
  return Array.isArray(v) ? v : [v];
5
5
  }
6
6
  var fromLength = (length) => Array.from(Array(length).keys());
@@ -13,6 +13,10 @@ var remove = (v, ...items) => v.filter((t) => !items.includes(t));
13
13
  var removeAt = (v, i) => v.filter((_, idx) => idx !== i);
14
14
  var insertAt = (v, i, ...items) => [...v.slice(0, i), ...items, ...v.slice(i)];
15
15
  var uniq = (v) => Array.from(new Set(v));
16
+ var diff = (a, b) => {
17
+ const set = new Set(b);
18
+ return a.filter((t) => !set.has(t));
19
+ };
16
20
  var addOrRemove = (v, item) => {
17
21
  if (has(v, item)) return remove(v, item);
18
22
  return add(v, item);
@@ -219,23 +223,19 @@ var roundToStepPrecision = (v, step) => {
219
223
  };
220
224
  var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
221
225
  var snapValueToStep = (v, vmin, vmax, step) => {
222
- vmin = Number(vmin);
223
- vmax = Number(vmax);
224
- let remainder = (v - (isNaN(vmin) ? 0 : vmin)) % step;
225
- let sv = roundToStepPrecision(
226
- abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder,
227
- step
228
- );
229
- if (!isNaN(vmin)) {
230
- if (sv < vmin) {
231
- sv = vmin;
232
- } else if (!isNaN(vmax) && sv > vmax) {
233
- sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
234
- }
235
- } else if (!isNaN(vmax) && sv > vmax) {
236
- sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
226
+ const min2 = vmin != null ? Number(vmin) : 0;
227
+ const max2 = Number(vmax);
228
+ const remainder = (v - min2) % step;
229
+ let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
230
+ snapped = roundToStepPrecision(snapped, step);
231
+ if (!isNaN(min2) && snapped < min2) {
232
+ snapped = min2;
233
+ } else if (!isNaN(max2) && snapped > max2) {
234
+ const stepsInRange = floor((max2 - min2) / step);
235
+ const largestValidStep = min2 + stepsInRange * step;
236
+ snapped = stepsInRange <= 0 || largestValidStep < min2 ? max2 : largestValidStep;
237
237
  }
238
- return roundToStepPrecision(sv, step);
238
+ return roundToStepPrecision(snapped, step);
239
239
  };
240
240
  var setValueAtIndex = (vs, i, v) => {
241
241
  if (vs[i] === v) return vs;
@@ -413,4 +413,4 @@ function ensureProps(props, keys, scope) {
413
413
  throw new Error(`[zag-js${scope ? ` > ${scope}` : ""}] missing required props: ${missingKeys.join(", ")}`);
414
414
  }
415
415
 
416
- export { add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
416
+ export { add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, debounce, decrementValue, diff, ensure, ensureProps, 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, toPx, tryCatch, uniq, uuid, warn, wrap };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/utils",
3
- "version": "1.14.0",
3
+ "version": "1.15.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",