@zag-js/utils 2.0.0-next.2 → 2.0.0-next.4
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/array.d.mts +24 -26
- package/dist/array.d.ts +24 -26
- package/dist/array.js +76 -117
- package/dist/array.mjs +52 -74
- package/dist/equal.d.mts +1 -2
- package/dist/equal.d.ts +1 -2
- package/dist/equal.js +23 -46
- package/dist/equal.mjs +19 -23
- package/dist/functions.d.mts +13 -15
- package/dist/functions.d.ts +13 -15
- package/dist/functions.js +82 -121
- package/dist/functions.mjs +68 -88
- package/dist/guard.d.mts +12 -13
- package/dist/guard.d.ts +12 -13
- package/dist/guard.js +37 -70
- package/dist/guard.mjs +23 -37
- package/dist/index.d.mts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +97 -41
- package/dist/index.mjs +9 -11
- package/dist/lifecycle.d.mts +2 -4
- package/dist/lifecycle.d.ts +2 -4
- package/dist/lifecycle.js +4 -18
- package/dist/lifecycle.mjs +1 -0
- package/dist/number.d.mts +37 -38
- package/dist/number.d.ts +37 -38
- package/dist/number.js +136 -185
- package/dist/number.mjs +104 -134
- package/dist/object.d.mts +11 -16
- package/dist/object.d.ts +11 -16
- package/dist/object.js +52 -91
- package/dist/object.mjs +41 -61
- package/dist/store.d.mts +10 -12
- package/dist/store.d.ts +10 -12
- package/dist/store.js +42 -67
- package/dist/store.mjs +38 -44
- package/dist/warning.d.mts +7 -8
- package/dist/warning.d.ts +7 -8
- package/dist/warning.js +16 -51
- package/dist/warning.mjs +9 -25
- package/package.json +2 -2
- package/dist/chunk-MXGZDBDQ.mjs +0 -15
- package/dist/timers.d.mts +0 -26
- package/dist/timers.d.ts +0 -26
- package/dist/timers.js +0 -119
- package/dist/timers.mjs +0 -90
package/dist/number.js
CHANGED
|
@@ -1,202 +1,153 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
118
|
-
|
|
71
|
+
const nextValue = ctx.values[index] + ctx.step;
|
|
72
|
+
return getValueSetterAtIndex(index, ctx)(nextValue);
|
|
119
73
|
}
|
|
120
74
|
function getPreviousStepValue(index, ctx) {
|
|
121
|
-
|
|
122
|
-
|
|
75
|
+
const nextValue = ctx.values[index] - ctx.step;
|
|
76
|
+
return getValueSetterAtIndex(index, ctx)(nextValue);
|
|
123
77
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
98
|
+
const toFixedNumber = (v, d = 0, b = 10) => {
|
|
99
|
+
const pow = Math.pow(b, d);
|
|
100
|
+
return round(v * pow) / pow;
|
|
147
101
|
};
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
68
|
-
|
|
67
|
+
const nextValue = ctx.values[index] + ctx.step;
|
|
68
|
+
return getValueSetterAtIndex(index, ctx)(nextValue);
|
|
69
69
|
}
|
|
70
70
|
function getPreviousStepValue(index, ctx) {
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
const nextValue = ctx.values[index] - ctx.step;
|
|
72
|
+
return getValueSetterAtIndex(index, ctx)(nextValue);
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
const toFixedNumber = (v, d = 0, b = 10) => {
|
|
95
|
+
const pow = Math.pow(b, d);
|
|
96
|
+
return round(v * pow) / pow;
|
|
97
97
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
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 {};
|