@neutro/form 0.0.5 → 0.1.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.
@@ -0,0 +1,217 @@
1
+ // ../core/src/index.ts
2
+ function isDeepEqual(a, b, hash = /* @__PURE__ */ new WeakMap()) {
3
+ if (a === b) return true;
4
+ if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
5
+ if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
6
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
7
+ if (hash.has(a) && hash.get(a) === b) return true;
8
+ hash.set(a, b);
9
+ if (a instanceof Set && b instanceof Set) {
10
+ if (a.size !== b.size) return false;
11
+ for (const item of a) if (!b.has(item)) return false;
12
+ return true;
13
+ }
14
+ if (a instanceof Map && b instanceof Map) {
15
+ if (a.size !== b.size) return false;
16
+ for (const [key, val] of a) {
17
+ if (!b.has(key) || !isDeepEqual(val, b.get(key), hash)) return false;
18
+ }
19
+ return true;
20
+ }
21
+ const keysA = Reflect.ownKeys(a);
22
+ const keysB = new Set(Reflect.ownKeys(b));
23
+ if (keysA.length !== keysB.size) return false;
24
+ for (const key of keysA) {
25
+ if (!keysB.has(key) || !isDeepEqual(a[key], b[key], hash)) return false;
26
+ }
27
+ return true;
28
+ }
29
+
30
+ // ../core/src/devtools.ts
31
+ var BADGE_STYLE = "background:#6366f1;color:#fff;padding:2px 6px;border-radius:3px;font-weight:bold;font-size:11px;";
32
+ var DIM_STYLE = "color:#888;font-weight:normal;";
33
+ var ACTION_STYLE = "color:#f59e0b;font-weight:bold;";
34
+ var RESET_STYLE = "color:inherit;font-weight:normal;";
35
+ function formatElapsed(ms) {
36
+ return ms < 1e3 ? `+${ms}ms` : `+${(ms / 1e3).toFixed(1)}s`;
37
+ }
38
+ function describeAction(action) {
39
+ switch (action.type) {
40
+ case "SET":
41
+ return `SET ${action.path}`;
42
+ case "VALIDATE":
43
+ return action.paths ? `VALIDATE [${action.paths.join(", ")}]` : "VALIDATE";
44
+ case "SUBMIT":
45
+ return "SUBMIT";
46
+ case "RESET":
47
+ return "RESET";
48
+ case "SET_ERRORS":
49
+ return `SET_ERRORS [${Object.keys(action.errors).join(", ")}]`;
50
+ case "CLEAR_ERRORS":
51
+ return "CLEAR_ERRORS";
52
+ case "CONNECT":
53
+ return `CONNECT ${action.path}`;
54
+ case "DISCONNECT":
55
+ return `DISCONNECT ${action.path}`;
56
+ case "BLUR":
57
+ return `BLUR ${action.path}`;
58
+ case "BATCH_START":
59
+ return "BATCH_START";
60
+ case "BATCH_END":
61
+ return "BATCH_END";
62
+ case "ARRAY_APPEND":
63
+ return `ARRAY_APPEND ${action.path}`;
64
+ case "ARRAY_INSERT":
65
+ return `ARRAY_INSERT ${action.path}[${action.index}]`;
66
+ case "ARRAY_REMOVE":
67
+ return `ARRAY_REMOVE ${action.path}[${action.index}]`;
68
+ case "ARRAY_MOVE":
69
+ return `ARRAY_MOVE ${action.path} ${action.from}\u2192${action.to}`;
70
+ case "ARRAY_SWAP":
71
+ return `ARRAY_SWAP ${action.path} [${action.i}\u2194${action.j}]`;
72
+ default:
73
+ return action.type;
74
+ }
75
+ }
76
+ function computeDiff(prev, next) {
77
+ const rows = [];
78
+ const slices = ["values", "errors", "touched", "dirty"];
79
+ for (const slice of slices) {
80
+ const prevSlice = prev[slice];
81
+ const nextSlice = next[slice];
82
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(prevSlice), ...Object.keys(nextSlice)]);
83
+ for (const key of allKeys) {
84
+ const p = prevSlice[key];
85
+ const n = nextSlice[key];
86
+ const equal = slice === "values" ? isDeepEqual(p, n) : p === n;
87
+ if (!equal) rows.push({ slice, key, prev: p, next: n });
88
+ }
89
+ }
90
+ if (prev.isSubmitting !== next.isSubmitting) {
91
+ rows.push({
92
+ slice: "meta",
93
+ key: "isSubmitting",
94
+ prev: prev.isSubmitting,
95
+ next: next.isSubmitting
96
+ });
97
+ }
98
+ if (prev.isValidating !== next.isValidating) {
99
+ rows.push({
100
+ slice: "meta",
101
+ key: "isValidating",
102
+ prev: prev.isValidating,
103
+ next: next.isValidating
104
+ });
105
+ }
106
+ return rows;
107
+ }
108
+ function logAction(action, state, prev, name, groupFn, lastTimeRef) {
109
+ const now = Date.now();
110
+ const elapsed = now - lastTimeRef.value;
111
+ lastTimeRef.value = now;
112
+ const label = describeAction(action);
113
+ const timestamp = new Date(now).toLocaleTimeString("en", {
114
+ hour12: false,
115
+ hour: "2-digit",
116
+ minute: "2-digit",
117
+ second: "2-digit"
118
+ });
119
+ groupFn(
120
+ "%c NeutroForm: %s %c %s %c %s %s",
121
+ BADGE_STYLE,
122
+ name,
123
+ RESET_STYLE,
124
+ label,
125
+ DIM_STYLE,
126
+ timestamp,
127
+ formatElapsed(elapsed)
128
+ );
129
+ console.log("%c action", ACTION_STYLE, action);
130
+ const diff = computeDiff(prev, state);
131
+ if (diff.length > 0) {
132
+ console.table(diff);
133
+ } else {
134
+ console.log("%c no state change", DIM_STYLE);
135
+ }
136
+ console.groupCollapsed("%c full state", DIM_STYLE);
137
+ console.log("%o", state);
138
+ console.groupEnd();
139
+ console.groupEnd();
140
+ }
141
+ var registeredForms = /* @__PURE__ */ new WeakSet();
142
+ function devtools(form, options = {}) {
143
+ if (registeredForms.has(form)) {
144
+ console.warn(
145
+ "[NeutroForm devtools] devtools() was called twice on the same form instance. Ignoring duplicate registration."
146
+ );
147
+ return () => {
148
+ };
149
+ }
150
+ registeredForms.add(form);
151
+ const name = options.name ?? "Form";
152
+ const collapsed = options.collapsed ?? true;
153
+ const groupFn = collapsed ? console.groupCollapsed.bind(console) : console.group.bind(console);
154
+ let prevState = form.getState();
155
+ const lastTimeRef = { value: Date.now() };
156
+ let inBatch = false;
157
+ let batchActions = [];
158
+ groupFn("%c NeutroForm: %s %c init", BADGE_STYLE, name, RESET_STYLE);
159
+ console.log("%c initial state", DIM_STYLE, form.getState());
160
+ console.groupEnd();
161
+ const unsubscribe = form._subscribeToActions((action, state) => {
162
+ if (action.type === "BATCH_START") {
163
+ inBatch = true;
164
+ batchActions = [];
165
+ return;
166
+ }
167
+ if (action.type === "BATCH_END") {
168
+ inBatch = false;
169
+ const count = batchActions.length;
170
+ if (count === 0) {
171
+ batchActions = [];
172
+ return;
173
+ }
174
+ const now = Date.now();
175
+ const elapsed = now - lastTimeRef.value;
176
+ lastTimeRef.value = now;
177
+ const timestamp = new Date(now).toLocaleTimeString("en", {
178
+ hour12: false,
179
+ hour: "2-digit",
180
+ minute: "2-digit",
181
+ second: "2-digit"
182
+ });
183
+ groupFn(
184
+ "%c NeutroForm: %s %c BATCH (%d mutations) %c %s %s",
185
+ BADGE_STYLE,
186
+ name,
187
+ RESET_STYLE,
188
+ count,
189
+ DIM_STYLE,
190
+ timestamp,
191
+ formatElapsed(elapsed)
192
+ );
193
+ const frozenTimeRef = { value: lastTimeRef.value };
194
+ for (const { action: a, state: s, prev: prev2 } of batchActions) {
195
+ logAction(a, s, prev2, name, groupFn, frozenTimeRef);
196
+ }
197
+ console.groupEnd();
198
+ batchActions = [];
199
+ return;
200
+ }
201
+ if (inBatch) {
202
+ batchActions.push({ action, state, prev: prevState });
203
+ prevState = state;
204
+ return;
205
+ }
206
+ const prev = prevState;
207
+ prevState = state;
208
+ logAction(action, state, prev, name, groupFn, lastTimeRef);
209
+ });
210
+ return () => {
211
+ registeredForms.delete(form);
212
+ unsubscribe();
213
+ };
214
+ }
215
+ export {
216
+ devtools
217
+ };