@zag-js/utils 1.47.0 → 1.49.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.
Files changed (58) hide show
  1. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/assertClassBrand.js +6 -0
  2. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/assertClassBrand.mjs +6 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/checkPrivateRedeclaration.js +5 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/checkPrivateRedeclaration.mjs +5 -0
  5. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldGet2.js +6 -0
  6. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldGet2.mjs +6 -0
  7. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldInitSpec.js +6 -0
  8. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/classPrivateFieldInitSpec.mjs +6 -0
  9. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js +11 -0
  10. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs +11 -0
  11. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.js +13 -0
  12. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.mjs +13 -0
  13. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.js +8 -0
  14. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.mjs +8 -0
  15. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/typeof.js +15 -0
  16. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/typeof.mjs +10 -0
  17. package/dist/array.d.mts +24 -26
  18. package/dist/array.d.ts +24 -26
  19. package/dist/array.js +76 -117
  20. package/dist/array.mjs +52 -74
  21. package/dist/equal.d.mts +1 -3
  22. package/dist/equal.d.ts +1 -3
  23. package/dist/equal.js +26 -60
  24. package/dist/equal.mjs +22 -37
  25. package/dist/functions.d.mts +13 -15
  26. package/dist/functions.d.ts +13 -15
  27. package/dist/functions.js +82 -121
  28. package/dist/functions.mjs +68 -88
  29. package/dist/guard.d.mts +12 -13
  30. package/dist/guard.d.ts +12 -13
  31. package/dist/guard.js +37 -70
  32. package/dist/guard.mjs +23 -37
  33. package/dist/index.d.mts +10 -9
  34. package/dist/index.d.ts +10 -9
  35. package/dist/index.js +101 -39
  36. package/dist/index.mjs +10 -10
  37. package/dist/number.d.mts +37 -38
  38. package/dist/number.d.ts +37 -38
  39. package/dist/number.js +136 -185
  40. package/dist/number.mjs +104 -134
  41. package/dist/object.d.mts +11 -16
  42. package/dist/object.d.ts +11 -16
  43. package/dist/object.js +52 -91
  44. package/dist/object.mjs +41 -61
  45. package/dist/store.d.mts +10 -12
  46. package/dist/store.d.ts +10 -12
  47. package/dist/store.js +42 -67
  48. package/dist/store.mjs +38 -44
  49. package/dist/timers.d.mts +21 -22
  50. package/dist/timers.d.ts +21 -22
  51. package/dist/timers.js +83 -112
  52. package/dist/timers.mjs +77 -83
  53. package/dist/warning.d.mts +7 -8
  54. package/dist/warning.d.ts +7 -8
  55. package/dist/warning.js +16 -51
  56. package/dist/warning.mjs +9 -25
  57. package/package.json +3 -3
  58. package/dist/chunk-MXGZDBDQ.mjs +0 -15
package/dist/number.js CHANGED
@@ -1,202 +1,153 @@
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 = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
19
-
20
- // src/number.ts
21
- var number_exports = {};
22
- __export(number_exports, {
23
- clampPercent: () => clampPercent,
24
- clampValue: () => clampValue,
25
- decrementValue: () => decrementValue,
26
- getClosestValue: () => getClosestValue,
27
- getClosestValueIndex: () => getClosestValueIndex,
28
- getMaxValueAtIndex: () => getMaxValueAtIndex,
29
- getMinValueAtIndex: () => getMinValueAtIndex,
30
- getNextStepValue: () => getNextStepValue,
31
- getPercentValue: () => getPercentValue,
32
- getPreviousStepValue: () => getPreviousStepValue,
33
- getValuePercent: () => getValuePercent,
34
- getValueRanges: () => getValueRanges,
35
- getValueSetterAtIndex: () => getValueSetterAtIndex,
36
- getValueTransformer: () => getValueTransformer,
37
- incrementValue: () => incrementValue,
38
- isNaN: () => isNaN,
39
- isValueAtMax: () => isValueAtMax,
40
- isValueAtMin: () => isValueAtMin,
41
- isValueWithinRange: () => isValueWithinRange,
42
- mod: () => mod,
43
- nan: () => nan,
44
- roundToDpr: () => roundToDpr,
45
- roundToStepPrecision: () => roundToStepPrecision,
46
- roundValue: () => roundValue,
47
- setValueAtIndex: () => setValueAtIndex,
48
- snapValueToStep: () => snapValueToStep,
49
- toFixedNumber: () => toFixedNumber,
50
- toPx: () => toPx,
51
- wrap: () => wrap
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
52
4
  });
53
- module.exports = __toCommonJS(number_exports);
54
- var { floor, abs, round, min, max, pow, sign } = Math;
55
- var isNaN = (v) => Number.isNaN(v);
56
- var nan = (v) => isNaN(v) ? 0 : v;
57
- var mod = (v, m) => (v % m + m) % m;
58
- var wrap = (v, vmax) => (v % vmax + vmax) % vmax;
59
- var getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
60
- var getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
61
- var isValueAtMax = (v, vmax) => nan(v) >= vmax;
62
- var isValueAtMin = (v, vmin) => nan(v) <= vmin;
63
- var isValueWithinRange = (v, vmin, vmax) => {
64
- const value = nan(v);
65
- const minCheck = vmin == null || value >= vmin;
66
- const maxCheck = vmax == null || value <= vmax;
67
- return minCheck && maxCheck;
5
+ // src/number.ts
6
+ const { floor, abs, round, min, max, pow, sign } = Math;
7
+ const isNaN = (v) => Number.isNaN(v);
8
+ const nan = (v) => isNaN(v) ? 0 : v;
9
+ const mod = (v, m) => (v % m + m) % m;
10
+ const wrap = (v, vmax) => (v % vmax + vmax) % vmax;
11
+ const getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
12
+ const getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
13
+ const isValueAtMax = (v, vmax) => nan(v) >= vmax;
14
+ const isValueAtMin = (v, vmin) => nan(v) <= vmin;
15
+ const isValueWithinRange = (v, vmin, vmax) => {
16
+ const value = nan(v);
17
+ return (vmin == null || value >= vmin) && (vmax == null || value <= vmax);
68
18
  };
69
- var roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
70
- var clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
71
- var clampPercent = (v) => clampValue(v, 0, 1);
72
- var getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
73
- var getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
74
- var roundToStepPrecision = (v, step) => {
75
- let rv = v;
76
- let ss = step.toString();
77
- let pi = ss.indexOf(".");
78
- let p = pi >= 0 ? ss.length - pi : 0;
79
- if (p > 0) {
80
- let pw = pow(10, p);
81
- rv = round(rv * pw) / pw;
82
- }
83
- return rv;
19
+ const roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
20
+ const clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
21
+ const clampPercent = (v) => clampValue(v, 0, 1);
22
+ const getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
23
+ const getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
24
+ const roundToStepPrecision = (v, step) => {
25
+ let rv = v;
26
+ let ss = step.toString();
27
+ let pi = ss.indexOf(".");
28
+ let p = pi >= 0 ? ss.length - pi : 0;
29
+ if (p > 0) {
30
+ let pw = pow(10, p);
31
+ rv = round(rv * pw) / pw;
32
+ }
33
+ return rv;
84
34
  };
85
- var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
86
- var snapValueToStep = (v, vmin, vmax, step) => {
87
- const min2 = vmin != null ? Number(vmin) : 0;
88
- const max2 = Number(vmax);
89
- const remainder = (v - min2) % step;
90
- let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
91
- snapped = roundToStepPrecision(snapped, step);
92
- if (!isNaN(min2) && snapped < min2) {
93
- snapped = min2;
94
- } else if (!isNaN(max2) && snapped > max2) {
95
- const stepsInRange = floor((max2 - min2) / step);
96
- const largestValidStep = min2 + stepsInRange * step;
97
- snapped = stepsInRange <= 0 || largestValidStep < min2 ? max2 : largestValidStep;
98
- }
99
- return roundToStepPrecision(snapped, step);
35
+ const roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + .5) / dpr : round(v);
36
+ const snapValueToStep = (v, vmin, vmax, step) => {
37
+ const min = vmin != null ? Number(vmin) : 0;
38
+ const max = Number(vmax);
39
+ const remainder = (v - min) % step;
40
+ let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
41
+ snapped = roundToStepPrecision(snapped, step);
42
+ if (!isNaN(min) && snapped < min) snapped = min;
43
+ else if (!isNaN(max) && snapped > max) {
44
+ const scale = pow(10, Math.max(countDecimals(min), countDecimals(max), countDecimals(step)));
45
+ const stepsInRange = floor((round(max * scale) - round(min * scale)) / round(step * scale));
46
+ const largestValidStep = min + stepsInRange * step;
47
+ snapped = stepsInRange <= 0 || largestValidStep < min ? max : largestValidStep;
48
+ }
49
+ return roundToStepPrecision(snapped, step);
100
50
  };
101
- var setValueAtIndex = (vs, i, v) => {
102
- if (vs[i] === v) return vs;
103
- return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
51
+ const setValueAtIndex = (vs, i, v) => {
52
+ if (vs[i] === v) return vs;
53
+ return [
54
+ ...vs.slice(0, i),
55
+ v,
56
+ ...vs.slice(i + 1)
57
+ ];
104
58
  };
105
59
  function getValueSetterAtIndex(index, ctx) {
106
- const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
107
- const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
108
- let nextValues = ctx.values.slice();
109
- return function setValue(value) {
110
- let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
111
- nextValues = setValueAtIndex(nextValues, index, value);
112
- nextValues[index] = nextValue;
113
- return nextValues;
114
- };
60
+ const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
61
+ const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
62
+ let nextValues = ctx.values.slice();
63
+ return function setValue(value) {
64
+ let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
65
+ nextValues = setValueAtIndex(nextValues, index, value);
66
+ nextValues[index] = nextValue;
67
+ return nextValues;
68
+ };
115
69
  }
116
70
  function getNextStepValue(index, ctx) {
117
- const nextValue = ctx.values[index] + ctx.step;
118
- return getValueSetterAtIndex(index, ctx)(nextValue);
71
+ const nextValue = ctx.values[index] + ctx.step;
72
+ return getValueSetterAtIndex(index, ctx)(nextValue);
119
73
  }
120
74
  function getPreviousStepValue(index, ctx) {
121
- const nextValue = ctx.values[index] - ctx.step;
122
- return getValueSetterAtIndex(index, ctx)(nextValue);
75
+ const nextValue = ctx.values[index] - ctx.step;
76
+ return getValueSetterAtIndex(index, ctx)(nextValue);
123
77
  }
124
- var getClosestValueIndex = (vs, t) => {
125
- let i = vs.findIndex((v) => t - v < 0);
126
- if (i === 0) return i;
127
- if (i === -1) return vs.length - 1;
128
- let vLeft = vs[i - 1];
129
- let vRight = vs[i];
130
- if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
131
- return i;
78
+ const getClosestValueIndex = (vs, t) => {
79
+ let i = vs.findIndex((v) => t - v < 0);
80
+ if (i === 0) return i;
81
+ if (i === -1) return vs.length - 1;
82
+ let vLeft = vs[i - 1];
83
+ let vRight = vs[i];
84
+ if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
85
+ return i;
132
86
  };
133
- var getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
134
- var getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
135
- min: i === 0 ? vmin : vs[i - 1] + gap,
136
- max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
137
- value: v
87
+ const getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
88
+ const getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
89
+ min: i === 0 ? vmin : vs[i - 1] + gap,
90
+ max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
91
+ value: v
138
92
  }));
139
- var getValueTransformer = (va, vb) => {
140
- const [a, b] = va;
141
- const [c, d] = vb;
142
- return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
93
+ const getValueTransformer = (va, vb) => {
94
+ const [a, b] = va;
95
+ const [c, d] = vb;
96
+ return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
143
97
  };
144
- var toFixedNumber = (v, d = 0, b = 10) => {
145
- const pow2 = Math.pow(b, d);
146
- return round(v * pow2) / pow2;
98
+ const toFixedNumber = (v, d = 0, b = 10) => {
99
+ const pow = Math.pow(b, d);
100
+ return round(v * pow) / pow;
147
101
  };
148
- var countDecimals = (value) => {
149
- if (!Number.isFinite(value)) return 0;
150
- let e = 1, p = 0;
151
- while (Math.round(value * e) / e !== value) {
152
- e *= 10;
153
- p += 1;
154
- }
155
- return p;
102
+ const countDecimals = (value) => {
103
+ if (!Number.isFinite(value)) return 0;
104
+ let e = 1, p = 0;
105
+ while (Math.round(value * e) / e !== value) {
106
+ e *= 10;
107
+ p += 1;
108
+ }
109
+ return p;
156
110
  };
157
- var decimalOp = (a, op, b) => {
158
- let result = op === "+" ? a + b : a - b;
159
- if (a % 1 !== 0 || b % 1 !== 0) {
160
- const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
161
- a = Math.round(a * multiplier);
162
- b = Math.round(b * multiplier);
163
- result = op === "+" ? a + b : a - b;
164
- result /= multiplier;
165
- }
166
- return result;
111
+ const decimalOp = (a, op, b) => {
112
+ let result = op === "+" ? a + b : a - b;
113
+ if (a % 1 !== 0 || b % 1 !== 0) {
114
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
115
+ a = Math.round(a * multiplier);
116
+ b = Math.round(b * multiplier);
117
+ result = op === "+" ? a + b : a - b;
118
+ result /= multiplier;
119
+ }
120
+ return result;
167
121
  };
168
- var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
169
- var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
170
- var toPx = (v) => typeof v === "number" ? `${v}px` : v;
171
- // Annotate the CommonJS export names for ESM import in node:
172
- 0 && (module.exports = {
173
- clampPercent,
174
- clampValue,
175
- decrementValue,
176
- getClosestValue,
177
- getClosestValueIndex,
178
- getMaxValueAtIndex,
179
- getMinValueAtIndex,
180
- getNextStepValue,
181
- getPercentValue,
182
- getPreviousStepValue,
183
- getValuePercent,
184
- getValueRanges,
185
- getValueSetterAtIndex,
186
- getValueTransformer,
187
- incrementValue,
188
- isNaN,
189
- isValueAtMax,
190
- isValueAtMin,
191
- isValueWithinRange,
192
- mod,
193
- nan,
194
- roundToDpr,
195
- roundToStepPrecision,
196
- roundValue,
197
- setValueAtIndex,
198
- snapValueToStep,
199
- toFixedNumber,
200
- toPx,
201
- wrap
202
- });
122
+ const incrementValue = (v, s) => decimalOp(nan(v), "+", s);
123
+ const decrementValue = (v, s) => decimalOp(nan(v), "-", s);
124
+ const toPx = (v) => typeof v === "number" ? `${v}px` : v;
125
+ exports.clampPercent = clampPercent;
126
+ exports.clampValue = clampValue;
127
+ exports.decrementValue = decrementValue;
128
+ exports.getClosestValue = getClosestValue;
129
+ exports.getClosestValueIndex = getClosestValueIndex;
130
+ exports.getMaxValueAtIndex = getMaxValueAtIndex;
131
+ exports.getMinValueAtIndex = getMinValueAtIndex;
132
+ exports.getNextStepValue = getNextStepValue;
133
+ exports.getPercentValue = getPercentValue;
134
+ exports.getPreviousStepValue = getPreviousStepValue;
135
+ exports.getValuePercent = getValuePercent;
136
+ exports.getValueRanges = getValueRanges;
137
+ exports.getValueSetterAtIndex = getValueSetterAtIndex;
138
+ exports.getValueTransformer = getValueTransformer;
139
+ exports.incrementValue = incrementValue;
140
+ exports.isNaN = isNaN;
141
+ exports.isValueAtMax = isValueAtMax;
142
+ exports.isValueAtMin = isValueAtMin;
143
+ exports.isValueWithinRange = isValueWithinRange;
144
+ exports.mod = mod;
145
+ exports.nan = nan;
146
+ exports.roundToDpr = roundToDpr;
147
+ exports.roundToStepPrecision = roundToStepPrecision;
148
+ exports.roundValue = roundValue;
149
+ exports.setValueAtIndex = setValueAtIndex;
150
+ exports.snapValueToStep = snapValueToStep;
151
+ exports.toFixedNumber = toFixedNumber;
152
+ exports.toPx = toPx;
153
+ exports.wrap = wrap;
package/dist/number.mjs CHANGED
@@ -1,151 +1,121 @@
1
- import "./chunk-MXGZDBDQ.mjs";
2
-
3
1
  // src/number.ts
4
- var { floor, abs, round, min, max, pow, sign } = Math;
5
- var isNaN = (v) => Number.isNaN(v);
6
- var nan = (v) => isNaN(v) ? 0 : v;
7
- var mod = (v, m) => (v % m + m) % m;
8
- var wrap = (v, vmax) => (v % vmax + vmax) % vmax;
9
- var getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
10
- var getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
11
- var isValueAtMax = (v, vmax) => nan(v) >= vmax;
12
- var isValueAtMin = (v, vmin) => nan(v) <= vmin;
13
- var isValueWithinRange = (v, vmin, vmax) => {
14
- const value = nan(v);
15
- const minCheck = vmin == null || value >= vmin;
16
- const maxCheck = vmax == null || value <= vmax;
17
- return minCheck && maxCheck;
2
+ const { floor, abs, round, min, max, pow, sign } = Math;
3
+ const isNaN = (v) => Number.isNaN(v);
4
+ const nan = (v) => isNaN(v) ? 0 : v;
5
+ const mod = (v, m) => (v % m + m) % m;
6
+ const wrap = (v, vmax) => (v % vmax + vmax) % vmax;
7
+ const getMinValueAtIndex = (i, v, vmin) => i === 0 ? vmin : v[i - 1];
8
+ const getMaxValueAtIndex = (i, v, vmax) => i === v.length - 1 ? vmax : v[i + 1];
9
+ const isValueAtMax = (v, vmax) => nan(v) >= vmax;
10
+ const isValueAtMin = (v, vmin) => nan(v) <= vmin;
11
+ const isValueWithinRange = (v, vmin, vmax) => {
12
+ const value = nan(v);
13
+ return (vmin == null || value >= vmin) && (vmax == null || value <= vmax);
18
14
  };
19
- var roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
20
- var clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
21
- var clampPercent = (v) => clampValue(v, 0, 1);
22
- var getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
23
- var getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
24
- var roundToStepPrecision = (v, step) => {
25
- let rv = v;
26
- let ss = step.toString();
27
- let pi = ss.indexOf(".");
28
- let p = pi >= 0 ? ss.length - pi : 0;
29
- if (p > 0) {
30
- let pw = pow(10, p);
31
- rv = round(rv * pw) / pw;
32
- }
33
- return rv;
15
+ const roundValue = (v, vmin, step) => round((nan(v) - vmin) / step) * step + vmin;
16
+ const clampValue = (v, vmin, vmax) => min(max(nan(v), vmin), vmax);
17
+ const clampPercent = (v) => clampValue(v, 0, 1);
18
+ const getValuePercent = (v, vmin, vmax) => (nan(v) - vmin) / (vmax - vmin);
19
+ const getPercentValue = (p, vmin, vmax, step) => clampValue(roundValue(p * (vmax - vmin) + vmin, vmin, step), vmin, vmax);
20
+ const roundToStepPrecision = (v, step) => {
21
+ let rv = v;
22
+ let ss = step.toString();
23
+ let pi = ss.indexOf(".");
24
+ let p = pi >= 0 ? ss.length - pi : 0;
25
+ if (p > 0) {
26
+ let pw = pow(10, p);
27
+ rv = round(rv * pw) / pw;
28
+ }
29
+ return rv;
34
30
  };
35
- var roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + 0.5) / dpr : round(v);
36
- var snapValueToStep = (v, vmin, vmax, step) => {
37
- const min2 = vmin != null ? Number(vmin) : 0;
38
- const max2 = Number(vmax);
39
- const remainder = (v - min2) % step;
40
- let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
41
- snapped = roundToStepPrecision(snapped, step);
42
- if (!isNaN(min2) && snapped < min2) {
43
- snapped = min2;
44
- } else if (!isNaN(max2) && snapped > max2) {
45
- const stepsInRange = floor((max2 - min2) / step);
46
- const largestValidStep = min2 + stepsInRange * step;
47
- snapped = stepsInRange <= 0 || largestValidStep < min2 ? max2 : largestValidStep;
48
- }
49
- return roundToStepPrecision(snapped, step);
31
+ const roundToDpr = (v, dpr) => typeof dpr === "number" ? floor(v * dpr + .5) / dpr : round(v);
32
+ const snapValueToStep = (v, vmin, vmax, step) => {
33
+ const min = vmin != null ? Number(vmin) : 0;
34
+ const max = Number(vmax);
35
+ const remainder = (v - min) % step;
36
+ let snapped = abs(remainder) * 2 >= step ? v + sign(remainder) * (step - abs(remainder)) : v - remainder;
37
+ snapped = roundToStepPrecision(snapped, step);
38
+ if (!isNaN(min) && snapped < min) snapped = min;
39
+ else if (!isNaN(max) && snapped > max) {
40
+ const scale = pow(10, Math.max(countDecimals(min), countDecimals(max), countDecimals(step)));
41
+ const stepsInRange = floor((round(max * scale) - round(min * scale)) / round(step * scale));
42
+ const largestValidStep = min + stepsInRange * step;
43
+ snapped = stepsInRange <= 0 || largestValidStep < min ? max : largestValidStep;
44
+ }
45
+ return roundToStepPrecision(snapped, step);
50
46
  };
51
- var setValueAtIndex = (vs, i, v) => {
52
- if (vs[i] === v) return vs;
53
- return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
47
+ const setValueAtIndex = (vs, i, v) => {
48
+ if (vs[i] === v) return vs;
49
+ return [
50
+ ...vs.slice(0, i),
51
+ v,
52
+ ...vs.slice(i + 1)
53
+ ];
54
54
  };
55
55
  function getValueSetterAtIndex(index, ctx) {
56
- const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
57
- const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
58
- let nextValues = ctx.values.slice();
59
- return function setValue(value) {
60
- let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
61
- nextValues = setValueAtIndex(nextValues, index, value);
62
- nextValues[index] = nextValue;
63
- return nextValues;
64
- };
56
+ const minValueAtIndex = getMinValueAtIndex(index, ctx.values, ctx.min);
57
+ const maxValueAtIndex = getMaxValueAtIndex(index, ctx.values, ctx.max);
58
+ let nextValues = ctx.values.slice();
59
+ return function setValue(value) {
60
+ let nextValue = snapValueToStep(value, minValueAtIndex, maxValueAtIndex, ctx.step);
61
+ nextValues = setValueAtIndex(nextValues, index, value);
62
+ nextValues[index] = nextValue;
63
+ return nextValues;
64
+ };
65
65
  }
66
66
  function getNextStepValue(index, ctx) {
67
- const nextValue = ctx.values[index] + ctx.step;
68
- return getValueSetterAtIndex(index, ctx)(nextValue);
67
+ const nextValue = ctx.values[index] + ctx.step;
68
+ return getValueSetterAtIndex(index, ctx)(nextValue);
69
69
  }
70
70
  function getPreviousStepValue(index, ctx) {
71
- const nextValue = ctx.values[index] - ctx.step;
72
- return getValueSetterAtIndex(index, ctx)(nextValue);
71
+ const nextValue = ctx.values[index] - ctx.step;
72
+ return getValueSetterAtIndex(index, ctx)(nextValue);
73
73
  }
74
- var getClosestValueIndex = (vs, t) => {
75
- let i = vs.findIndex((v) => t - v < 0);
76
- if (i === 0) return i;
77
- if (i === -1) return vs.length - 1;
78
- let vLeft = vs[i - 1];
79
- let vRight = vs[i];
80
- if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
81
- return i;
74
+ const getClosestValueIndex = (vs, t) => {
75
+ let i = vs.findIndex((v) => t - v < 0);
76
+ if (i === 0) return i;
77
+ if (i === -1) return vs.length - 1;
78
+ let vLeft = vs[i - 1];
79
+ let vRight = vs[i];
80
+ if (abs(vLeft - t) < abs(vRight - t)) return i - 1;
81
+ return i;
82
82
  };
83
- var getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
84
- var getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
85
- min: i === 0 ? vmin : vs[i - 1] + gap,
86
- max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
87
- value: v
83
+ const getClosestValue = (vs, t) => vs[getClosestValueIndex(vs, t)];
84
+ const getValueRanges = (vs, vmin, vmax, gap) => vs.map((v, i) => ({
85
+ min: i === 0 ? vmin : vs[i - 1] + gap,
86
+ max: i === vs.length - 1 ? vmax : vs[i + 1] - gap,
87
+ value: v
88
88
  }));
89
- var getValueTransformer = (va, vb) => {
90
- const [a, b] = va;
91
- const [c, d] = vb;
92
- return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
89
+ const getValueTransformer = (va, vb) => {
90
+ const [a, b] = va;
91
+ const [c, d] = vb;
92
+ return (v) => a === b || c === d ? c : c + (d - c) / (b - a) * (v - a);
93
93
  };
94
- var toFixedNumber = (v, d = 0, b = 10) => {
95
- const pow2 = Math.pow(b, d);
96
- return round(v * pow2) / pow2;
94
+ const toFixedNumber = (v, d = 0, b = 10) => {
95
+ const pow = Math.pow(b, d);
96
+ return round(v * pow) / pow;
97
97
  };
98
- var countDecimals = (value) => {
99
- if (!Number.isFinite(value)) return 0;
100
- let e = 1, p = 0;
101
- while (Math.round(value * e) / e !== value) {
102
- e *= 10;
103
- p += 1;
104
- }
105
- return p;
98
+ const countDecimals = (value) => {
99
+ if (!Number.isFinite(value)) return 0;
100
+ let e = 1, p = 0;
101
+ while (Math.round(value * e) / e !== value) {
102
+ e *= 10;
103
+ p += 1;
104
+ }
105
+ return p;
106
106
  };
107
- var decimalOp = (a, op, b) => {
108
- let result = op === "+" ? a + b : a - b;
109
- if (a % 1 !== 0 || b % 1 !== 0) {
110
- const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
111
- a = Math.round(a * multiplier);
112
- b = Math.round(b * multiplier);
113
- result = op === "+" ? a + b : a - b;
114
- result /= multiplier;
115
- }
116
- return result;
117
- };
118
- var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
119
- var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
120
- var toPx = (v) => typeof v === "number" ? `${v}px` : v;
121
- export {
122
- clampPercent,
123
- clampValue,
124
- decrementValue,
125
- getClosestValue,
126
- getClosestValueIndex,
127
- getMaxValueAtIndex,
128
- getMinValueAtIndex,
129
- getNextStepValue,
130
- getPercentValue,
131
- getPreviousStepValue,
132
- getValuePercent,
133
- getValueRanges,
134
- getValueSetterAtIndex,
135
- getValueTransformer,
136
- incrementValue,
137
- isNaN,
138
- isValueAtMax,
139
- isValueAtMin,
140
- isValueWithinRange,
141
- mod,
142
- nan,
143
- roundToDpr,
144
- roundToStepPrecision,
145
- roundValue,
146
- setValueAtIndex,
147
- snapValueToStep,
148
- toFixedNumber,
149
- toPx,
150
- wrap
107
+ const decimalOp = (a, op, b) => {
108
+ let result = op === "+" ? a + b : a - b;
109
+ if (a % 1 !== 0 || b % 1 !== 0) {
110
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
111
+ a = Math.round(a * multiplier);
112
+ b = Math.round(b * multiplier);
113
+ result = op === "+" ? a + b : a - b;
114
+ result /= multiplier;
115
+ }
116
+ return result;
151
117
  };
118
+ const incrementValue = (v, s) => decimalOp(nan(v), "+", s);
119
+ const decrementValue = (v, s) => decimalOp(nan(v), "-", s);
120
+ const toPx = (v) => typeof v === "number" ? `${v}px` : v;
121
+ export { clampPercent, clampValue, decrementValue, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, incrementValue, isNaN, isValueAtMax, isValueAtMin, isValueWithinRange, mod, nan, roundToDpr, roundToStepPrecision, roundValue, setValueAtIndex, snapValueToStep, toFixedNumber, toPx, wrap };
package/dist/object.d.mts CHANGED
@@ -1,17 +1,12 @@
1
- declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
2
- declare const json: (v: any) => any;
3
- declare const keys: <T extends Record<string, any>>(obj: T) => (keyof T)[];
4
- declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
1
+ export declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
2
+ export declare const json: (v: any) => any;
3
+ export declare const keys: <T extends Record<string, any>>(obj: T) => (keyof T)[];
4
+ export declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
5
5
  type Dict = Record<string | symbol, any>;
6
- declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
7
- declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
8
- declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
9
- type Defined<T> = {
10
- [K in keyof T]-?: Exclude<T[K], undefined>;
11
- };
12
- type MergeWithDefault<D, O = unknown> = Defined<D> & ([NonNullable<O>] extends [never] ? unknown : Omit<NonNullable<O>, keyof D>);
13
- declare function mergeWithDefault<D extends object, O extends {
14
- [K in keyof D]?: D[K] | undefined;
15
- } | undefined>(defaults: D, overrides?: O): MergeWithDefault<D, O>;
16
-
17
- export { type MergeWithDefault, compact, createSplitProps, json, keys, mergeWithDefault, omit, pick, splitProps };
6
+ export declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
7
+ export declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
8
+ export declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
9
+ type Defined<T> = { [K in keyof T]-?: Exclude<T[K], undefined>; };
10
+ export type MergeWithDefault<D, O = unknown> = Defined<D> & ([NonNullable<O>] extends [never] ? unknown : Omit<NonNullable<O>, keyof D>);
11
+ export declare function mergeWithDefault<D extends object, O extends { [K in keyof D]?: D[K] | undefined; } | undefined>(defaults: D, overrides?: O): MergeWithDefault<D, O>;
12
+ export {};