@zag-js/utils 1.34.1 → 1.35.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.
package/dist/number.js ADDED
@@ -0,0 +1,202 @@
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
52
+ });
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;
68
+ };
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;
84
+ };
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);
100
+ };
101
+ var setValueAtIndex = (vs, i, v) => {
102
+ if (vs[i] === v) return vs;
103
+ return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
104
+ };
105
+ 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
+ };
115
+ }
116
+ function getNextStepValue(index, ctx) {
117
+ const nextValue = ctx.values[index] + ctx.step;
118
+ return getValueSetterAtIndex(index, ctx)(nextValue);
119
+ }
120
+ function getPreviousStepValue(index, ctx) {
121
+ const nextValue = ctx.values[index] - ctx.step;
122
+ return getValueSetterAtIndex(index, ctx)(nextValue);
123
+ }
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;
132
+ };
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
138
+ }));
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);
143
+ };
144
+ var toFixedNumber = (v, d = 0, b = 10) => {
145
+ const pow2 = Math.pow(b, d);
146
+ return round(v * pow2) / pow2;
147
+ };
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;
156
+ };
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;
167
+ };
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
+ });
@@ -0,0 +1,151 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // 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;
18
+ };
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;
34
+ };
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);
50
+ };
51
+ var setValueAtIndex = (vs, i, v) => {
52
+ if (vs[i] === v) return vs;
53
+ return [...vs.slice(0, i), v, ...vs.slice(i + 1)];
54
+ };
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
+ };
65
+ }
66
+ function getNextStepValue(index, ctx) {
67
+ const nextValue = ctx.values[index] + ctx.step;
68
+ return getValueSetterAtIndex(index, ctx)(nextValue);
69
+ }
70
+ function getPreviousStepValue(index, ctx) {
71
+ const nextValue = ctx.values[index] - ctx.step;
72
+ return getValueSetterAtIndex(index, ctx)(nextValue);
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;
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
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);
93
+ };
94
+ var toFixedNumber = (v, d = 0, b = 10) => {
95
+ const pow2 = Math.pow(b, d);
96
+ return round(v * pow2) / pow2;
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;
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
151
+ };
@@ -0,0 +1,9 @@
1
+ declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
2
+ declare const json: (v: any) => any;
3
+ declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
4
+ type Dict = Record<string | symbol, any>;
5
+ declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
6
+ declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
7
+ declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
8
+
9
+ export { compact, createSplitProps, json, omit, pick, splitProps };
@@ -0,0 +1,9 @@
1
+ declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
2
+ declare const json: (v: any) => any;
3
+ declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
4
+ type Dict = Record<string | symbol, any>;
5
+ declare function splitProps<T extends Dict>(props: T, keys: (keyof T)[]): Dict[];
6
+ declare const createSplitProps: <T extends Dict>(keys: (keyof T)[]) => <Props extends T>(props: Props) => [T, Omit<Props, keyof T>];
7
+ declare function omit<T extends Record<string, any>>(obj: T, keys: string[]): Omit<T, string | number>;
8
+
9
+ export { compact, createSplitProps, json, omit, pick, splitProps };
package/dist/object.js ADDED
@@ -0,0 +1,85 @@
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 = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/object.ts
21
+ var object_exports = {};
22
+ __export(object_exports, {
23
+ compact: () => compact,
24
+ createSplitProps: () => createSplitProps,
25
+ json: () => json,
26
+ omit: () => omit,
27
+ pick: () => pick,
28
+ splitProps: () => splitProps
29
+ });
30
+ module.exports = __toCommonJS(object_exports);
31
+ var import_guard = require("./guard.cjs");
32
+ function compact(obj) {
33
+ if (!(0, import_guard.isPlainObject)(obj) || obj === void 0) return obj;
34
+ const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string");
35
+ const filtered = {};
36
+ for (const key of keys) {
37
+ const value = obj[key];
38
+ if (value !== void 0) {
39
+ filtered[key] = compact(value);
40
+ }
41
+ }
42
+ return filtered;
43
+ }
44
+ var json = (v) => JSON.parse(JSON.stringify(v));
45
+ function pick(obj, keys) {
46
+ const filtered = {};
47
+ for (const key of keys) {
48
+ const value = obj[key];
49
+ if (value !== void 0) {
50
+ filtered[key] = value;
51
+ }
52
+ }
53
+ return filtered;
54
+ }
55
+ function splitProps(props, keys) {
56
+ const rest = {};
57
+ const result = {};
58
+ const keySet = new Set(keys);
59
+ const ownKeys = Reflect.ownKeys(props);
60
+ for (const key of ownKeys) {
61
+ if (keySet.has(key)) {
62
+ result[key] = props[key];
63
+ } else {
64
+ rest[key] = props[key];
65
+ }
66
+ }
67
+ return [result, rest];
68
+ }
69
+ var createSplitProps = (keys) => {
70
+ return function split(props) {
71
+ return splitProps(props, keys);
72
+ };
73
+ };
74
+ function omit(obj, keys) {
75
+ return createSplitProps(keys)(obj)[1];
76
+ }
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ compact,
80
+ createSplitProps,
81
+ json,
82
+ omit,
83
+ pick,
84
+ splitProps
85
+ });
@@ -0,0 +1,57 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // src/object.ts
4
+ import { isPlainObject } from "./guard.mjs";
5
+ function compact(obj) {
6
+ if (!isPlainObject(obj) || obj === void 0) return obj;
7
+ const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string");
8
+ const filtered = {};
9
+ for (const key of keys) {
10
+ const value = obj[key];
11
+ if (value !== void 0) {
12
+ filtered[key] = compact(value);
13
+ }
14
+ }
15
+ return filtered;
16
+ }
17
+ var json = (v) => JSON.parse(JSON.stringify(v));
18
+ function pick(obj, keys) {
19
+ const filtered = {};
20
+ for (const key of keys) {
21
+ const value = obj[key];
22
+ if (value !== void 0) {
23
+ filtered[key] = value;
24
+ }
25
+ }
26
+ return filtered;
27
+ }
28
+ function splitProps(props, keys) {
29
+ const rest = {};
30
+ const result = {};
31
+ const keySet = new Set(keys);
32
+ const ownKeys = Reflect.ownKeys(props);
33
+ for (const key of ownKeys) {
34
+ if (keySet.has(key)) {
35
+ result[key] = props[key];
36
+ } else {
37
+ rest[key] = props[key];
38
+ }
39
+ }
40
+ return [result, rest];
41
+ }
42
+ var createSplitProps = (keys) => {
43
+ return function split(props) {
44
+ return splitProps(props, keys);
45
+ };
46
+ };
47
+ function omit(obj, keys) {
48
+ return createSplitProps(keys)(obj)[1];
49
+ }
50
+ export {
51
+ compact,
52
+ createSplitProps,
53
+ json,
54
+ omit,
55
+ pick,
56
+ splitProps
57
+ };
@@ -0,0 +1,12 @@
1
+ type StoreListener = VoidFunction;
2
+ type StoreCompareFn<T> = (a: T, b: T) => boolean;
3
+ declare function createStore<T extends Record<string, any>>(initialState: T, compare?: StoreCompareFn<T>): Store<T>;
4
+ interface Store<T extends Record<string, any>> {
5
+ subscribe: (listener: StoreListener) => () => void;
6
+ get: <K extends keyof T>(key: K) => T[K];
7
+ set: <K extends keyof T>(key: K, value: T[K]) => void;
8
+ update: (updates: Partial<T>) => void;
9
+ snapshot: () => T;
10
+ }
11
+
12
+ export { type Store, type StoreCompareFn, type StoreListener, createStore };
@@ -0,0 +1,12 @@
1
+ type StoreListener = VoidFunction;
2
+ type StoreCompareFn<T> = (a: T, b: T) => boolean;
3
+ declare function createStore<T extends Record<string, any>>(initialState: T, compare?: StoreCompareFn<T>): Store<T>;
4
+ interface Store<T extends Record<string, any>> {
5
+ subscribe: (listener: StoreListener) => () => void;
6
+ get: <K extends keyof T>(key: K) => T[K];
7
+ set: <K extends keyof T>(key: K, value: T[K]) => void;
8
+ update: (updates: Partial<T>) => void;
9
+ snapshot: () => T;
10
+ }
11
+
12
+ export { type Store, type StoreCompareFn, type StoreListener, createStore };
package/dist/store.js ADDED
@@ -0,0 +1,70 @@
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 = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/store.ts
21
+ var store_exports = {};
22
+ __export(store_exports, {
23
+ createStore: () => createStore
24
+ });
25
+ module.exports = __toCommonJS(store_exports);
26
+ function createStore(initialState, compare = Object.is) {
27
+ let state = { ...initialState };
28
+ const listeners = /* @__PURE__ */ new Set();
29
+ const subscribe = (listener) => {
30
+ listeners.add(listener);
31
+ return () => listeners.delete(listener);
32
+ };
33
+ const publish = () => {
34
+ listeners.forEach((listener) => listener());
35
+ };
36
+ const get = (key) => {
37
+ return state[key];
38
+ };
39
+ const set = (key, value) => {
40
+ if (!compare(state[key], value)) {
41
+ state[key] = value;
42
+ publish();
43
+ }
44
+ };
45
+ const update = (updates) => {
46
+ let hasChanges = false;
47
+ for (const key in updates) {
48
+ const value = updates[key];
49
+ if (value !== void 0 && !compare(state[key], value)) {
50
+ state[key] = value;
51
+ hasChanges = true;
52
+ }
53
+ }
54
+ if (hasChanges) {
55
+ publish();
56
+ }
57
+ };
58
+ const snapshot = () => ({ ...state });
59
+ return {
60
+ subscribe,
61
+ get,
62
+ set,
63
+ update,
64
+ snapshot
65
+ };
66
+ }
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ createStore
70
+ });
package/dist/store.mjs ADDED
@@ -0,0 +1,47 @@
1
+ import "./chunk-MXGZDBDQ.mjs";
2
+
3
+ // src/store.ts
4
+ function createStore(initialState, compare = Object.is) {
5
+ let state = { ...initialState };
6
+ const listeners = /* @__PURE__ */ new Set();
7
+ const subscribe = (listener) => {
8
+ listeners.add(listener);
9
+ return () => listeners.delete(listener);
10
+ };
11
+ const publish = () => {
12
+ listeners.forEach((listener) => listener());
13
+ };
14
+ const get = (key) => {
15
+ return state[key];
16
+ };
17
+ const set = (key, value) => {
18
+ if (!compare(state[key], value)) {
19
+ state[key] = value;
20
+ publish();
21
+ }
22
+ };
23
+ const update = (updates) => {
24
+ let hasChanges = false;
25
+ for (const key in updates) {
26
+ const value = updates[key];
27
+ if (value !== void 0 && !compare(state[key], value)) {
28
+ state[key] = value;
29
+ hasChanges = true;
30
+ }
31
+ }
32
+ if (hasChanges) {
33
+ publish();
34
+ }
35
+ };
36
+ const snapshot = () => ({ ...state });
37
+ return {
38
+ subscribe,
39
+ get,
40
+ set,
41
+ update,
42
+ snapshot
43
+ };
44
+ }
45
+ export {
46
+ createStore
47
+ };
@@ -0,0 +1,26 @@
1
+ interface TimerBaseContext {
2
+ startMs: number;
3
+ deltaMs: number;
4
+ }
5
+ interface TimerContext extends TimerBaseContext {
6
+ now: number;
7
+ }
8
+ type TimerContextFn = (ctx: TimerContext) => boolean | void;
9
+ declare class Timer {
10
+ #private;
11
+ private readonly onTick;
12
+ private frameId;
13
+ private pausedAtMs;
14
+ private context;
15
+ constructor(onTick: TimerContextFn);
16
+ private cancelFrame;
17
+ setStartMs: (startMs: number) => void;
18
+ get elapsedMs(): number;
19
+ start: () => void;
20
+ pause: () => void;
21
+ stop: () => void;
22
+ }
23
+ declare function setRafInterval(fn: (ctx: TimerBaseContext) => void, intervalMs: number): () => void;
24
+ declare function setRafTimeout(fn: () => void, delayMs: number): () => void;
25
+
26
+ export { Timer, type TimerBaseContext, type TimerContextFn, setRafInterval, setRafTimeout };