@zag-js/utils 0.80.0 → 0.81.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
@@ -33,6 +33,7 @@ declare const uuid: () => string;
33
33
  declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
34
34
  declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
35
35
 
36
+ type AnyFunction = (...args: any[]) => any;
36
37
  declare const isDev: () => boolean;
37
38
  declare const isArray: (v: any) => v is any[];
38
39
  declare const isBoolean: (v: any) => v is boolean;
@@ -40,23 +41,61 @@ declare const isObjectLike: (v: any) => v is Record<string, any>;
40
41
  declare const isObject: (v: any) => v is Record<string, any>;
41
42
  declare const isNumber: (v: any) => v is number;
42
43
  declare const isString: (v: any) => v is string;
43
- declare const isFunction: (v: any) => v is Function;
44
+ declare const isFunction: (v: any) => v is AnyFunction;
44
45
  declare const isNull: (v: any) => v is null | undefined;
45
46
  declare const hasProp: <T extends string>(obj: any, prop: T) => obj is Record<T, any>;
46
47
  declare const isPlainObject: (v: any) => boolean;
47
48
 
49
+ declare const isNaN: (v: number) => boolean;
50
+ declare const nan: (v: number) => number;
51
+ declare const mod: (v: number, m: number) => number;
52
+ declare const wrap: (v: number, vmax: number) => number;
53
+ declare const getMinValueAtIndex: (i: number, v: number[], vmin: number) => number;
54
+ declare const getMaxValueAtIndex: (i: number, v: number[], vmax: number) => number;
55
+ declare const isValueAtMax: (v: number, vmax: number) => boolean;
56
+ declare const isValueAtMin: (v: number, vmin: number) => boolean;
57
+ declare const isValueWithinRange: (v: number, vmin: number, vmax: number) => boolean;
58
+ declare const roundValue: (v: number, vmin: number, step: number) => number;
59
+ declare const clampValue: (v: number, vmin: number, vmax: number) => number;
60
+ declare const clampPercent: (v: number) => number;
61
+ declare const getValuePercent: (v: number, vmin: number, vmax: number) => number;
62
+ declare const getPercentValue: (p: number, vmin: number, vmax: number, step: number) => number;
63
+ declare const roundToStepPrecision: (v: number, step: number) => number;
64
+ declare const roundToDpr: (v: number, dpr: unknown) => number;
65
+ declare const snapValueToStep: (v: number, vmin: number | undefined, vmax: number | undefined, step: number) => number;
66
+ declare const setValueAtIndex: <T>(vs: T[], i: number, v: T) => T[];
67
+ interface RangeContext {
68
+ min: number;
69
+ max: number;
70
+ step: number;
71
+ values: number[];
72
+ }
73
+ declare function getValueSetterAtIndex(index: number, ctx: RangeContext): (value: number) => number[];
74
+ declare function getNextStepValue(index: number, ctx: RangeContext): number[];
75
+ declare function getPreviousStepValue(index: number, ctx: RangeContext): number[];
76
+ declare const getClosestValueIndex: (vs: number[], t: number) => number;
77
+ declare const getClosestValue: (vs: number[], t: number) => number;
78
+ declare const getValueRanges: (vs: number[], vmin: number, vmax: number, gap: number) => {
79
+ min: number;
80
+ max: number;
81
+ value: number;
82
+ }[];
83
+ declare const getValueTransformer: (va: number[], vb: number[]) => (v: number) => number;
84
+ declare const toFixedNumber: (v: number, d?: number, b?: number) => number;
85
+ declare const incrementValue: (v: number, s: number) => number;
86
+ declare const decrementValue: (v: number, s: number) => number;
87
+
48
88
  declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
49
- declare function json(value: any): any;
89
+ declare const json: (v: any) => any;
50
90
  declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
51
- declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
52
-
53
91
  type Dict = Record<string, any>;
54
92
  declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
55
93
  declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
94
+ declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
56
95
 
57
96
  declare function warn(m: string): void;
58
97
  declare function warn(c: boolean, m: string): void;
59
98
  declare function invariant(m: string): void;
60
99
  declare function invariant(c: boolean, m: string): void;
61
100
 
62
- export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clear, compact, createSplitProps, first, fromLength, has, hasProp, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, json, last, match, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, runIfFn, splitProps, toArray, tryCatch, uniq, uuid, warn };
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 };
package/dist/index.d.ts CHANGED
@@ -33,6 +33,7 @@ declare const uuid: () => string;
33
33
  declare function match<V extends string | number = string, R = unknown>(key: V, record: Record<V, R | ((...args: any[]) => R)>, ...args: any[]): R;
34
34
  declare const tryCatch: <R>(fn: () => R, fallback: () => R) => R;
35
35
 
36
+ type AnyFunction = (...args: any[]) => any;
36
37
  declare const isDev: () => boolean;
37
38
  declare const isArray: (v: any) => v is any[];
38
39
  declare const isBoolean: (v: any) => v is boolean;
@@ -40,23 +41,61 @@ declare const isObjectLike: (v: any) => v is Record<string, any>;
40
41
  declare const isObject: (v: any) => v is Record<string, any>;
41
42
  declare const isNumber: (v: any) => v is number;
42
43
  declare const isString: (v: any) => v is string;
43
- declare const isFunction: (v: any) => v is Function;
44
+ declare const isFunction: (v: any) => v is AnyFunction;
44
45
  declare const isNull: (v: any) => v is null | undefined;
45
46
  declare const hasProp: <T extends string>(obj: any, prop: T) => obj is Record<T, any>;
46
47
  declare const isPlainObject: (v: any) => boolean;
47
48
 
49
+ declare const isNaN: (v: number) => boolean;
50
+ declare const nan: (v: number) => number;
51
+ declare const mod: (v: number, m: number) => number;
52
+ declare const wrap: (v: number, vmax: number) => number;
53
+ declare const getMinValueAtIndex: (i: number, v: number[], vmin: number) => number;
54
+ declare const getMaxValueAtIndex: (i: number, v: number[], vmax: number) => number;
55
+ declare const isValueAtMax: (v: number, vmax: number) => boolean;
56
+ declare const isValueAtMin: (v: number, vmin: number) => boolean;
57
+ declare const isValueWithinRange: (v: number, vmin: number, vmax: number) => boolean;
58
+ declare const roundValue: (v: number, vmin: number, step: number) => number;
59
+ declare const clampValue: (v: number, vmin: number, vmax: number) => number;
60
+ declare const clampPercent: (v: number) => number;
61
+ declare const getValuePercent: (v: number, vmin: number, vmax: number) => number;
62
+ declare const getPercentValue: (p: number, vmin: number, vmax: number, step: number) => number;
63
+ declare const roundToStepPrecision: (v: number, step: number) => number;
64
+ declare const roundToDpr: (v: number, dpr: unknown) => number;
65
+ declare const snapValueToStep: (v: number, vmin: number | undefined, vmax: number | undefined, step: number) => number;
66
+ declare const setValueAtIndex: <T>(vs: T[], i: number, v: T) => T[];
67
+ interface RangeContext {
68
+ min: number;
69
+ max: number;
70
+ step: number;
71
+ values: number[];
72
+ }
73
+ declare function getValueSetterAtIndex(index: number, ctx: RangeContext): (value: number) => number[];
74
+ declare function getNextStepValue(index: number, ctx: RangeContext): number[];
75
+ declare function getPreviousStepValue(index: number, ctx: RangeContext): number[];
76
+ declare const getClosestValueIndex: (vs: number[], t: number) => number;
77
+ declare const getClosestValue: (vs: number[], t: number) => number;
78
+ declare const getValueRanges: (vs: number[], vmin: number, vmax: number, gap: number) => {
79
+ min: number;
80
+ max: number;
81
+ value: number;
82
+ }[];
83
+ declare const getValueTransformer: (va: number[], vb: number[]) => (v: number) => number;
84
+ declare const toFixedNumber: (v: number, d?: number, b?: number) => number;
85
+ declare const incrementValue: (v: number, s: number) => number;
86
+ declare const decrementValue: (v: number, s: number) => number;
87
+
48
88
  declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
49
- declare function json(value: any): any;
89
+ declare const json: (v: any) => any;
50
90
  declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
51
- declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
52
-
53
91
  type Dict = Record<string, any>;
54
92
  declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
55
93
  declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
94
+ declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
56
95
 
57
96
  declare function warn(m: string): void;
58
97
  declare function warn(c: boolean, m: string): void;
59
98
  declare function invariant(m: string): void;
60
99
  declare function invariant(c: boolean, m: string): void;
61
100
 
62
- export { type IndexOptions, type MaybeFunction, type Nullable, add, addOrRemove, callAll, cast, chunk, clear, compact, createSplitProps, first, fromLength, has, hasProp, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, json, last, match, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, runIfFn, splitProps, toArray, tryCatch, uniq, uuid, warn };
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 };
package/dist/index.js CHANGED
@@ -149,31 +149,126 @@ var isPlainObject = (v) => {
149
149
  return typeof Ctor == "function" && Ctor instanceof Ctor && fnToString.call(Ctor) == objectCtorString;
150
150
  };
151
151
 
152
- // src/split-props.ts
153
- function splitProps(props, keys) {
154
- const rest = {};
155
- const result = {};
156
- const keySet = new Set(keys);
157
- for (const key in props) {
158
- if (keySet.has(key)) {
159
- result[key] = props[key];
160
- } else {
161
- rest[key] = props[key];
152
+ // src/number.ts
153
+ var { floor, abs, round, min, max, pow, sign } = Math;
154
+ var isNaN = (v) => Number.isNaN(v);
155
+ var nan = (v) => isNaN(v) ? 0 : v;
156
+ var mod = (v, m) => (v % m + m) % m;
157
+ var wrap = (v, vmax) => (v % vmax + vmax) % vmax;
158
+ var getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
159
+ var getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
160
+ var isValueAtMax = (v, vmax) => nan(v) >= vmax;
161
+ var isValueAtMin = (v, vmin) => nan(v) <= vmin;
162
+ var isValueWithinRange = (v, vmin, vmax) => nan(v) >= vmin && nan(v) <= vmax;
163
+ var roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
164
+ var clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
165
+ var clampPercent = (v) => clampValue(v, 0, 1);
166
+ var getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
167
+ var getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
168
+ var roundToStepPrecision = (v, step) => {
169
+ let rv = v;
170
+ let ss = step.toString();
171
+ let pi = ss.indexOf(".");
172
+ let p = pi >= 0 ? ss.length - pi : 0;
173
+ if (p > 0) {
174
+ let pw = pow(10, p);
175
+ rv = round(rv * pw) / pw;
176
+ }
177
+ return rv;
178
+ };
179
+ var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
180
+ var snapValueToStep = (v, vmin, vmax, step) => {
181
+ vmin = Number(vmin);
182
+ vmax = Number(vmax);
183
+ let remainder = (v - (isNaN(vmin) ? 0 : vmin)) % step;
184
+ let sv = roundToStepPrecision(
185
+ abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder,
186
+ step
187
+ );
188
+ if (!isNaN(vmin)) {
189
+ if (sv < vmin) {
190
+ sv = vmin;
191
+ } else if (!isNaN(vmax) && sv > vmax) {
192
+ sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
162
193
  }
194
+ } else if (!isNaN(vmax) && sv > vmax) {
195
+ sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
163
196
  }
164
- return [result, rest];
165
- }
166
- var createSplitProps = (keys) => {
167
- return function split(props) {
168
- return splitProps(props, keys);
197
+ return roundToStepPrecision(sv, step);
198
+ };
199
+ var setValueAtIndex = (vs, i, v) => {
200
+ if (vs[i] === v) return vs;
201
+ return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
202
+ };
203
+ function getValueSetterAtIndex(index, ctx) {
204
+ const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
205
+ const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
206
+ let nextValues = ctx.values.slice();
207
+ return function setValue(value) {
208
+ let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
209
+ nextValues = setValueAtIndex(nextValues, index, value);
210
+ nextValues[index] = nextValue;
211
+ return nextValues;
169
212
  };
213
+ }
214
+ function getNextStepValue(index, ctx) {
215
+ const nextValue = ctx.values[index] + ctx.step;
216
+ return getValueSetterAtIndex(index, ctx)(nextValue);
217
+ }
218
+ function getPreviousStepValue(index, ctx) {
219
+ const nextValue = ctx.values[index] - ctx.step;
220
+ return getValueSetterAtIndex(index, ctx)(nextValue);
221
+ }
222
+ var getClosestValueIndex = (vs, t) => {
223
+ let i = vs.findIndex((v) => t - v < 0);
224
+ if (i === 0) return i;
225
+ if (i === -1) return vs.length - 1;
226
+ let vLeft = vs[i - 1];
227
+ let vRight = vs[i];
228
+ if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
229
+ return i;
170
230
  };
231
+ var getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
232
+ var getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
233
+ min: i === 0 ? vmin : vs[i - 1] + gap,
234
+ max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
235
+ value: v
236
+ }));
237
+ var getValueTransformer = (va, vb) => {
238
+ const [a, b] = va;
239
+ const [c, d] = vb;
240
+ return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
241
+ };
242
+ var toFixedNumber = (v, d = 0, b = 10) => {
243
+ const pow2 = Math.pow(b, d);
244
+ return round(v * pow2) / pow2;
245
+ };
246
+ var countDecimals = (value) => {
247
+ if (!Number.isFinite(value)) return 0;
248
+ let e = 1, p = 0;
249
+ while (Math.round(value * e) / e !== value) {
250
+ e *= 10;
251
+ p += 1;
252
+ }
253
+ return p;
254
+ };
255
+ var decimalOp = (a, op, b) => {
256
+ let result = op === "+" ? a + b : a - b;
257
+ if (a % 1 !== 0 || b % 1 !== 0) {
258
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
259
+ a = Math.round(a * multiplier);
260
+ b = Math.round(b * multiplier);
261
+ result = op === "+" ? a + b : a - b;
262
+ result /= multiplier;
263
+ }
264
+ return result;
265
+ };
266
+ var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
267
+ var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
171
268
 
172
269
  // src/object.ts
173
270
  function compact(obj) {
174
- if (!isPlainObject2(obj) || obj === void 0) {
175
- return obj;
176
- }
271
+ if (!isPlainObject2(obj) || obj === void 0) return obj;
177
272
  const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string");
178
273
  const filtered = {};
179
274
  for (const key of keys) {
@@ -184,11 +279,9 @@ function compact(obj) {
184
279
  }
185
280
  return filtered;
186
281
  }
187
- function json(value) {
188
- return JSON.parse(JSON.stringify(value));
189
- }
190
- var isPlainObject2 = (value) => {
191
- return value && typeof value === "object" && value.constructor === Object;
282
+ var json = (v) => JSON.parse(JSON.stringify(v));
283
+ var isPlainObject2 = (v) => {
284
+ return v && typeof v === "object" && v.constructor === Object;
192
285
  };
193
286
  function pick(obj, keys) {
194
287
  const filtered = {};
@@ -200,6 +293,24 @@ function pick(obj, keys) {
200
293
  }
201
294
  return filtered;
202
295
  }
296
+ function splitProps(props, keys) {
297
+ const rest = {};
298
+ const result = {};
299
+ const keySet = new Set(keys);
300
+ for (const key in props) {
301
+ if (keySet.has(key)) {
302
+ result[key] = props[key];
303
+ } else {
304
+ rest[key] = props[key];
305
+ }
306
+ }
307
+ return [result, rest];
308
+ }
309
+ var createSplitProps = (keys) => {
310
+ return function split(props) {
311
+ return splitProps(props, keys);
312
+ };
313
+ };
203
314
  function omit(obj, keys) {
204
315
  return createSplitProps(keys)(obj)[1];
205
316
  }
@@ -225,13 +336,28 @@ exports.addOrRemove = addOrRemove;
225
336
  exports.callAll = callAll;
226
337
  exports.cast = cast;
227
338
  exports.chunk = chunk;
339
+ exports.clampPercent = clampPercent;
340
+ exports.clampValue = clampValue;
228
341
  exports.clear = clear;
229
342
  exports.compact = compact;
230
343
  exports.createSplitProps = createSplitProps;
344
+ exports.decrementValue = decrementValue;
231
345
  exports.first = first;
232
346
  exports.fromLength = fromLength;
347
+ exports.getClosestValue = getClosestValue;
348
+ exports.getClosestValueIndex = getClosestValueIndex;
349
+ exports.getMaxValueAtIndex = getMaxValueAtIndex;
350
+ exports.getMinValueAtIndex = getMinValueAtIndex;
351
+ exports.getNextStepValue = getNextStepValue;
352
+ exports.getPercentValue = getPercentValue;
353
+ exports.getPreviousStepValue = getPreviousStepValue;
354
+ exports.getValuePercent = getValuePercent;
355
+ exports.getValueRanges = getValueRanges;
356
+ exports.getValueSetterAtIndex = getValueSetterAtIndex;
357
+ exports.getValueTransformer = getValueTransformer;
233
358
  exports.has = has;
234
359
  exports.hasProp = hasProp;
360
+ exports.incrementValue = incrementValue;
235
361
  exports.insertAt = insertAt;
236
362
  exports.invariant = invariant;
237
363
  exports.isArray = isArray;
@@ -240,15 +366,21 @@ exports.isDev = isDev;
240
366
  exports.isEmpty = isEmpty;
241
367
  exports.isEqual = isEqual;
242
368
  exports.isFunction = isFunction;
369
+ exports.isNaN = isNaN;
243
370
  exports.isNull = isNull;
244
371
  exports.isNumber = isNumber;
245
372
  exports.isObject = isObject;
246
373
  exports.isObjectLike = isObjectLike;
247
374
  exports.isPlainObject = isPlainObject;
248
375
  exports.isString = isString;
376
+ exports.isValueAtMax = isValueAtMax;
377
+ exports.isValueAtMin = isValueAtMin;
378
+ exports.isValueWithinRange = isValueWithinRange;
249
379
  exports.json = json;
250
380
  exports.last = last;
251
381
  exports.match = match;
382
+ exports.mod = mod;
383
+ exports.nan = nan;
252
384
  exports.next = next;
253
385
  exports.nextIndex = nextIndex;
254
386
  exports.noop = noop;
@@ -258,10 +390,17 @@ exports.prev = prev;
258
390
  exports.prevIndex = prevIndex;
259
391
  exports.remove = remove;
260
392
  exports.removeAt = removeAt;
393
+ exports.roundToDpr = roundToDpr;
394
+ exports.roundToStepPrecision = roundToStepPrecision;
395
+ exports.roundValue = roundValue;
261
396
  exports.runIfFn = runIfFn;
397
+ exports.setValueAtIndex = setValueAtIndex;
398
+ exports.snapValueToStep = snapValueToStep;
262
399
  exports.splitProps = splitProps;
263
400
  exports.toArray = toArray;
401
+ exports.toFixedNumber = toFixedNumber;
264
402
  exports.tryCatch = tryCatch;
265
403
  exports.uniq = uniq;
266
404
  exports.uuid = uuid;
267
405
  exports.warn = warn;
406
+ exports.wrap = wrap;
package/dist/index.mjs CHANGED
@@ -147,31 +147,126 @@ var isPlainObject = (v) => {
147
147
  return typeof Ctor == "function" && Ctor instanceof Ctor && fnToString.call(Ctor) == objectCtorString;
148
148
  };
149
149
 
150
- // src/split-props.ts
151
- function splitProps(props, keys) {
152
- const rest = {};
153
- const result = {};
154
- const keySet = new Set(keys);
155
- for (const key in props) {
156
- if (keySet.has(key)) {
157
- result[key] = props[key];
158
- } else {
159
- rest[key] = props[key];
150
+ // src/number.ts
151
+ var { floor, abs, round, min, max, pow, sign } = Math;
152
+ var isNaN = (v) => Number.isNaN(v);
153
+ var nan = (v) => isNaN(v) ? 0 : v;
154
+ var mod = (v, m) => (v % m + m) % m;
155
+ var wrap = (v, vmax) => (v % vmax + vmax) % vmax;
156
+ var getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
157
+ var getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
158
+ var isValueAtMax = (v, vmax) => nan(v) >= vmax;
159
+ var isValueAtMin = (v, vmin) => nan(v) <= vmin;
160
+ var isValueWithinRange = (v, vmin, vmax) => nan(v) >= vmin && nan(v) <= vmax;
161
+ var roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
162
+ var clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
163
+ var clampPercent = (v) => clampValue(v, 0, 1);
164
+ var getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
165
+ var getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
166
+ var roundToStepPrecision = (v, step) => {
167
+ let rv = v;
168
+ let ss = step.toString();
169
+ let pi = ss.indexOf(".");
170
+ let p = pi >= 0 ? ss.length - pi : 0;
171
+ if (p > 0) {
172
+ let pw = pow(10, p);
173
+ rv = round(rv * pw) / pw;
174
+ }
175
+ return rv;
176
+ };
177
+ var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
178
+ var snapValueToStep = (v, vmin, vmax, step) => {
179
+ vmin = Number(vmin);
180
+ vmax = Number(vmax);
181
+ let remainder = (v - (isNaN(vmin) ? 0 : vmin)) % step;
182
+ let sv = roundToStepPrecision(
183
+ abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder,
184
+ step
185
+ );
186
+ if (!isNaN(vmin)) {
187
+ if (sv < vmin) {
188
+ sv = vmin;
189
+ } else if (!isNaN(vmax) && sv > vmax) {
190
+ sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
160
191
  }
192
+ } else if (!isNaN(vmax) && sv > vmax) {
193
+ sv = vmin + floor(roundToStepPrecision((vmax - vmin) / step, step)) * step;
161
194
  }
162
- return [result, rest];
163
- }
164
- var createSplitProps = (keys) => {
165
- return function split(props) {
166
- return splitProps(props, keys);
195
+ return roundToStepPrecision(sv, step);
196
+ };
197
+ var setValueAtIndex = (vs, i, v) => {
198
+ if (vs[i] === v) return vs;
199
+ return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
200
+ };
201
+ function getValueSetterAtIndex(index, ctx) {
202
+ const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
203
+ const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
204
+ let nextValues = ctx.values.slice();
205
+ return function setValue(value) {
206
+ let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
207
+ nextValues = setValueAtIndex(nextValues, index, value);
208
+ nextValues[index] = nextValue;
209
+ return nextValues;
167
210
  };
211
+ }
212
+ function getNextStepValue(index, ctx) {
213
+ const nextValue = ctx.values[index] + ctx.step;
214
+ return getValueSetterAtIndex(index, ctx)(nextValue);
215
+ }
216
+ function getPreviousStepValue(index, ctx) {
217
+ const nextValue = ctx.values[index] - ctx.step;
218
+ return getValueSetterAtIndex(index, ctx)(nextValue);
219
+ }
220
+ var getClosestValueIndex = (vs, t) => {
221
+ let i = vs.findIndex((v) => t - v < 0);
222
+ if (i === 0) return i;
223
+ if (i === -1) return vs.length - 1;
224
+ let vLeft = vs[i - 1];
225
+ let vRight = vs[i];
226
+ if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
227
+ return i;
168
228
  };
229
+ var getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
230
+ var getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
231
+ min: i === 0 ? vmin : vs[i - 1] + gap,
232
+ max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
233
+ value: v
234
+ }));
235
+ var getValueTransformer = (va, vb) => {
236
+ const [a, b] = va;
237
+ const [c, d] = vb;
238
+ return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
239
+ };
240
+ var toFixedNumber = (v, d = 0, b = 10) => {
241
+ const pow2 = Math.pow(b, d);
242
+ return round(v * pow2) / pow2;
243
+ };
244
+ var countDecimals = (value) => {
245
+ if (!Number.isFinite(value)) return 0;
246
+ let e = 1, p = 0;
247
+ while (Math.round(value * e) / e !== value) {
248
+ e *= 10;
249
+ p += 1;
250
+ }
251
+ return p;
252
+ };
253
+ var decimalOp = (a, op, b) => {
254
+ let result = op === "+" ? a + b : a - b;
255
+ if (a % 1 !== 0 || b % 1 !== 0) {
256
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
257
+ a = Math.round(a * multiplier);
258
+ b = Math.round(b * multiplier);
259
+ result = op === "+" ? a + b : a - b;
260
+ result /= multiplier;
261
+ }
262
+ return result;
263
+ };
264
+ var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
265
+ var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
169
266
 
170
267
  // src/object.ts
171
268
  function compact(obj) {
172
- if (!isPlainObject2(obj) || obj === void 0) {
173
- return obj;
174
- }
269
+ if (!isPlainObject2(obj) || obj === void 0) return obj;
175
270
  const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string");
176
271
  const filtered = {};
177
272
  for (const key of keys) {
@@ -182,11 +277,9 @@ function compact(obj) {
182
277
  }
183
278
  return filtered;
184
279
  }
185
- function json(value) {
186
- return JSON.parse(JSON.stringify(value));
187
- }
188
- var isPlainObject2 = (value) => {
189
- return value && typeof value === "object" && value.constructor === Object;
280
+ var json = (v) => JSON.parse(JSON.stringify(v));
281
+ var isPlainObject2 = (v) => {
282
+ return v && typeof v === "object" && v.constructor === Object;
190
283
  };
191
284
  function pick(obj, keys) {
192
285
  const filtered = {};
@@ -198,6 +291,24 @@ function pick(obj, keys) {
198
291
  }
199
292
  return filtered;
200
293
  }
294
+ function splitProps(props, keys) {
295
+ const rest = {};
296
+ const result = {};
297
+ const keySet = new Set(keys);
298
+ for (const key in props) {
299
+ if (keySet.has(key)) {
300
+ result[key] = props[key];
301
+ } else {
302
+ rest[key] = props[key];
303
+ }
304
+ }
305
+ return [result, rest];
306
+ }
307
+ var createSplitProps = (keys) => {
308
+ return function split(props) {
309
+ return splitProps(props, keys);
310
+ };
311
+ };
201
312
  function omit(obj, keys) {
202
313
  return createSplitProps(keys)(obj)[1];
203
314
  }
@@ -218,4 +329,4 @@ function invariant(...a) {
218
329
  }
219
330
  }
220
331
 
221
- export { add, addOrRemove, callAll, cast, chunk, clear, compact, createSplitProps, first, fromLength, has, hasProp, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, json, last, match, next, nextIndex, noop, omit, pick, prev, prevIndex, remove, removeAt, runIfFn, splitProps, toArray, tryCatch, uniq, uuid, warn };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/utils",
3
- "version": "0.80.0",
3
+ "version": "0.81.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",