@mustrd/core 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wilson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,293 @@
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/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ INTERNAL_KEYS: () => INTERNAL_KEYS,
24
+ ITERATORS: () => ITERATORS,
25
+ MUTATORS: () => MUTATORS,
26
+ buildNested: () => buildNested,
27
+ createMustard: () => createMustard,
28
+ getAtPath: () => getAtPath,
29
+ record: () => record,
30
+ setPathValue: () => setPathValue,
31
+ squeeze: () => squeeze,
32
+ unwrap: () => unwrap
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+ var MUTATORS = /* @__PURE__ */ new Set(["push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin"]);
36
+ var ITERATORS = /* @__PURE__ */ new Set(["forEach", "map", "filter", "find", "findIndex", "some", "every", "reduce", "reduceRight", "flatMap"]);
37
+ var INTERNAL_KEYS = /* @__PURE__ */ new Set(["_MST_STORE_", "_MST_SOURCE_", "_MST_RECORD_", "$", "reset"]);
38
+ var unwrap = (obj) => {
39
+ if (obj != null && typeof obj === "object") {
40
+ const source = obj._MST_SOURCE_;
41
+ if (source !== void 0) return source;
42
+ }
43
+ return obj;
44
+ };
45
+ var record = (obj) => obj._MST_RECORD_;
46
+ var squeeze = (obj) => {
47
+ if (typeof obj !== "object" || obj === null) return obj;
48
+ if (Array.isArray(obj)) {
49
+ const arr = [...obj];
50
+ for (let i = 0, len = arr.length; i < len; i++) {
51
+ if (typeof arr[i] === "object" && arr[i] !== null) arr[i] = squeeze(arr[i]);
52
+ }
53
+ return arr;
54
+ }
55
+ const copy = { ...obj };
56
+ const keys = Object.keys(copy);
57
+ for (let i = 0, len = keys.length; i < len; i++) {
58
+ if (typeof copy[keys[i]] === "object" && copy[keys[i]] !== null) copy[keys[i]] = squeeze(copy[keys[i]]);
59
+ }
60
+ return copy;
61
+ };
62
+ var setPathValue = (curr, path, callback, depth = 0) => {
63
+ if (depth === path.length) {
64
+ const next2 = Array.isArray(curr) ? [...curr] : { ...curr };
65
+ const result = callback(next2);
66
+ return result !== void 0 ? result : next2;
67
+ }
68
+ const key = path[depth];
69
+ const next = Array.isArray(curr) ? [...curr] : { ...curr };
70
+ next[key] = setPathValue(curr[key], path, callback, depth + 1);
71
+ return next;
72
+ };
73
+ var getAtPath = (state, path) => {
74
+ let current = state;
75
+ for (let i = 0; i < path.length; i++) {
76
+ if (current == null) return void 0;
77
+ current = current[path[i]];
78
+ }
79
+ return current;
80
+ };
81
+ var buildNested = (entries) => {
82
+ const result = {};
83
+ for (const entry of entries) {
84
+ const segs = entry.path.split(".");
85
+ let current = result;
86
+ for (let i = 0; i < segs.length - 1; i++) {
87
+ if (current[segs[i]] == null) current[segs[i]] = {};
88
+ current = current[segs[i]];
89
+ }
90
+ current[segs[segs.length - 1]] = entry.after;
91
+ }
92
+ return result;
93
+ };
94
+ var createMustard = (initialState) => {
95
+ let state = squeeze(initialState);
96
+ let version = 0;
97
+ const listeners = /* @__PURE__ */ new Set();
98
+ let history = [];
99
+ let pendingNotify = false;
100
+ const notify = () => {
101
+ version++;
102
+ if (!pendingNotify) {
103
+ pendingNotify = true;
104
+ Promise.resolve().then(() => {
105
+ pendingNotify = false;
106
+ listeners.forEach((l) => l());
107
+ });
108
+ }
109
+ };
110
+ const pushRecord = (path, key, before, after) => {
111
+ const fullPath = [...path, key].join(".");
112
+ history.push({
113
+ path: fullPath,
114
+ before: typeof before === "object" && before !== null ? squeeze(before) : before,
115
+ after: typeof after === "object" && after !== null ? squeeze(after) : after
116
+ });
117
+ };
118
+ const applyUndo = (entry) => {
119
+ const segs = entry.path.split(".");
120
+ const parentPath = segs.slice(0, -1);
121
+ const key = segs[segs.length - 1];
122
+ if (parentPath.length === 0) {
123
+ state = { ...state, [key]: entry.before };
124
+ } else {
125
+ state = setPathValue(state, parentPath, (parent) => {
126
+ parent[key] = entry.before;
127
+ });
128
+ }
129
+ };
130
+ const recordApi = {
131
+ data: () => {
132
+ const latest = /* @__PURE__ */ new Map();
133
+ for (const entry of history) {
134
+ const existing = latest.get(entry.path);
135
+ if (existing) {
136
+ existing.after = entry.after;
137
+ } else {
138
+ latest.set(entry.path, { ...entry });
139
+ }
140
+ }
141
+ return buildNested(Array.from(latest.values()));
142
+ },
143
+ paths: () => [...new Set(history.map((e) => e.path))],
144
+ undo: () => {
145
+ const entry = history.pop();
146
+ if (!entry) return;
147
+ applyUndo(entry);
148
+ notify();
149
+ },
150
+ undoAll: () => {
151
+ if (history.length === 0) return;
152
+ while (history.length > 0) applyUndo(history.pop());
153
+ notify();
154
+ },
155
+ undoTo: (index) => {
156
+ if (index < 0 || index >= history.length) return;
157
+ while (history.length > index) applyUndo(history.pop());
158
+ notify();
159
+ },
160
+ clear: () => {
161
+ history = [];
162
+ },
163
+ size: () => history.length
164
+ };
165
+ const proxyCache = /* @__PURE__ */ new Map();
166
+ const createProxy = (path = []) => {
167
+ const getCurrent = path.length === 0 ? () => state : () => getAtPath(state, path);
168
+ const curr = getCurrent();
169
+ const target = Array.isArray(curr) ? [] : {};
170
+ const proxy2 = new Proxy(target, {
171
+ get: (_, key) => {
172
+ if (typeof key === "symbol") {
173
+ const curr3 = getCurrent();
174
+ if (curr3 == null) return void 0;
175
+ const val = curr3[key];
176
+ return typeof val === "function" ? val.bind(curr3) : val;
177
+ }
178
+ if (key === "_MST_STORE_") return store;
179
+ if (key === "_MST_SOURCE_") return getCurrent();
180
+ if (key === "_MST_RECORD_") return recordApi;
181
+ if (key === "$") return getCurrent();
182
+ if (key === "reset") return (data) => {
183
+ state = typeof data === "function" ? squeeze(data()) : squeeze(data);
184
+ notify();
185
+ };
186
+ const curr2 = getCurrent();
187
+ if (curr2 == null) return void 0;
188
+ const value = curr2[key];
189
+ if (Array.isArray(curr2) && typeof value === "function" && MUTATORS.has(key)) {
190
+ return (...args) => {
191
+ const cleanArgs = args.map((a) => typeof a === "object" && a !== null ? squeeze(a) : a);
192
+ const beforeArr = squeeze(curr2);
193
+ let result;
194
+ state = setPathValue(state, path, (arr) => {
195
+ result = arr[key](...cleanArgs);
196
+ return arr;
197
+ });
198
+ const afterArr = squeeze(getAtPath(state, path));
199
+ const fullPath = path.join(".");
200
+ history.push({
201
+ path: fullPath,
202
+ before: beforeArr,
203
+ after: afterArr
204
+ });
205
+ notify();
206
+ return result;
207
+ };
208
+ }
209
+ if (Array.isArray(curr2) && typeof value === "function" && ITERATORS.has(key)) {
210
+ return value.bind(proxy2);
211
+ }
212
+ if (typeof value === "function") return value.bind(curr2);
213
+ if (typeof value === "object" && value !== null) {
214
+ const cacheKey = path.length === 0 ? key : path.join("\0") + "\0" + key;
215
+ let child = proxyCache.get(cacheKey);
216
+ if (!child) {
217
+ child = createProxy([...path, key]);
218
+ proxyCache.set(cacheKey, child);
219
+ }
220
+ return child;
221
+ }
222
+ return value;
223
+ },
224
+ set: (_, key, val) => {
225
+ const curr2 = getCurrent();
226
+ if (curr2 == null) return true;
227
+ if (curr2[key] === val) return true;
228
+ const before = curr2[key];
229
+ if (typeof val === "object" && val !== null) val = squeeze(val);
230
+ state = setPathValue(state, path, (parent) => {
231
+ parent[key] = val;
232
+ });
233
+ pushRecord(path, key, before, val);
234
+ notify();
235
+ return true;
236
+ },
237
+ ownKeys: () => {
238
+ const curr2 = getCurrent();
239
+ return curr2 ? Reflect.ownKeys(curr2) : [];
240
+ },
241
+ getOwnPropertyDescriptor: (_, key) => {
242
+ const curr2 = getCurrent();
243
+ if (curr2 != null && key in curr2) {
244
+ if (key === "length" && Array.isArray(curr2)) {
245
+ return { configurable: false, enumerable: false, writable: true, value: curr2.length };
246
+ }
247
+ return { configurable: true, enumerable: true, writable: true, value: curr2[key] };
248
+ }
249
+ return void 0;
250
+ },
251
+ deleteProperty: (_, key) => {
252
+ const curr2 = getCurrent();
253
+ if (curr2 == null || !(key in curr2)) return true;
254
+ const before = curr2[key];
255
+ state = setPathValue(state, path, (parent) => {
256
+ delete parent[key];
257
+ });
258
+ pushRecord(path, key, before, void 0);
259
+ notify();
260
+ return true;
261
+ },
262
+ has: (_, key) => {
263
+ const curr2 = getCurrent();
264
+ return curr2 != null && key in curr2;
265
+ }
266
+ });
267
+ return proxy2;
268
+ };
269
+ const proxy = createProxy();
270
+ const store = {
271
+ getState: () => state,
272
+ getVersion: () => version,
273
+ subscribe: (listener) => {
274
+ listeners.add(listener);
275
+ return () => listeners.delete(listener);
276
+ },
277
+ proxy
278
+ };
279
+ return store;
280
+ };
281
+ // Annotate the CommonJS export names for ESM import in node:
282
+ 0 && (module.exports = {
283
+ INTERNAL_KEYS,
284
+ ITERATORS,
285
+ MUTATORS,
286
+ buildNested,
287
+ createMustard,
288
+ getAtPath,
289
+ record,
290
+ setPathValue,
291
+ squeeze,
292
+ unwrap
293
+ });
@@ -0,0 +1,45 @@
1
+ interface RecordEntry {
2
+ path: string;
3
+ before: any;
4
+ after: any;
5
+ }
6
+ interface RecordApi {
7
+ /** Changed fields as nested object (for partial updates) */
8
+ data: () => any;
9
+ /** List of changed paths (dot notation) */
10
+ paths: () => string[];
11
+ /** Revert last change */
12
+ undo: () => void;
13
+ /** Revert all changes */
14
+ undoAll: () => void;
15
+ /** Revert to specific history index (exclusive, keeps 0..index-1) */
16
+ undoTo: (index: number) => void;
17
+ /** Clear all records (new baseline) */
18
+ clear: () => void;
19
+ /** Number of recorded changes */
20
+ size: () => number;
21
+ }
22
+ interface MustardStore<T = any> {
23
+ getState: () => T;
24
+ getVersion: () => number;
25
+ subscribe: (listener: () => void) => () => void;
26
+ proxy: any;
27
+ }
28
+ declare const MUTATORS: Set<string>;
29
+ declare const ITERATORS: Set<string>;
30
+ declare const INTERNAL_KEYS: Set<string>;
31
+ /** Unwrap proxy to get raw value */
32
+ declare const unwrap: (obj: any) => any;
33
+ /** Get record API from a mustard proxy */
34
+ declare const record: (obj: any) => RecordApi;
35
+ /** Deep clone (strips proxy references) */
36
+ declare const squeeze: (obj: any) => any;
37
+ /** Structural sharing: create new references along path */
38
+ declare const setPathValue: (curr: any, path: any[], callback: Function, depth?: number) => any;
39
+ /** Traverse state by path segments */
40
+ declare const getAtPath: (state: any, path: string[]) => any;
41
+ /** Build nested object from flat record entries (aggregated) */
42
+ declare const buildNested: (entries: RecordEntry[]) => any;
43
+ declare const createMustard: <T extends object>(initialState: T) => MustardStore<T>;
44
+
45
+ export { INTERNAL_KEYS, ITERATORS, MUTATORS, type MustardStore, type RecordApi, buildNested, createMustard, getAtPath, record, setPathValue, squeeze, unwrap };
@@ -0,0 +1,45 @@
1
+ interface RecordEntry {
2
+ path: string;
3
+ before: any;
4
+ after: any;
5
+ }
6
+ interface RecordApi {
7
+ /** Changed fields as nested object (for partial updates) */
8
+ data: () => any;
9
+ /** List of changed paths (dot notation) */
10
+ paths: () => string[];
11
+ /** Revert last change */
12
+ undo: () => void;
13
+ /** Revert all changes */
14
+ undoAll: () => void;
15
+ /** Revert to specific history index (exclusive, keeps 0..index-1) */
16
+ undoTo: (index: number) => void;
17
+ /** Clear all records (new baseline) */
18
+ clear: () => void;
19
+ /** Number of recorded changes */
20
+ size: () => number;
21
+ }
22
+ interface MustardStore<T = any> {
23
+ getState: () => T;
24
+ getVersion: () => number;
25
+ subscribe: (listener: () => void) => () => void;
26
+ proxy: any;
27
+ }
28
+ declare const MUTATORS: Set<string>;
29
+ declare const ITERATORS: Set<string>;
30
+ declare const INTERNAL_KEYS: Set<string>;
31
+ /** Unwrap proxy to get raw value */
32
+ declare const unwrap: (obj: any) => any;
33
+ /** Get record API from a mustard proxy */
34
+ declare const record: (obj: any) => RecordApi;
35
+ /** Deep clone (strips proxy references) */
36
+ declare const squeeze: (obj: any) => any;
37
+ /** Structural sharing: create new references along path */
38
+ declare const setPathValue: (curr: any, path: any[], callback: Function, depth?: number) => any;
39
+ /** Traverse state by path segments */
40
+ declare const getAtPath: (state: any, path: string[]) => any;
41
+ /** Build nested object from flat record entries (aggregated) */
42
+ declare const buildNested: (entries: RecordEntry[]) => any;
43
+ declare const createMustard: <T extends object>(initialState: T) => MustardStore<T>;
44
+
45
+ export { INTERNAL_KEYS, ITERATORS, MUTATORS, type MustardStore, type RecordApi, buildNested, createMustard, getAtPath, record, setPathValue, squeeze, unwrap };
package/dist/index.js ADDED
@@ -0,0 +1,259 @@
1
+ // src/index.ts
2
+ var MUTATORS = /* @__PURE__ */ new Set(["push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin"]);
3
+ var ITERATORS = /* @__PURE__ */ new Set(["forEach", "map", "filter", "find", "findIndex", "some", "every", "reduce", "reduceRight", "flatMap"]);
4
+ var INTERNAL_KEYS = /* @__PURE__ */ new Set(["_MST_STORE_", "_MST_SOURCE_", "_MST_RECORD_", "$", "reset"]);
5
+ var unwrap = (obj) => {
6
+ if (obj != null && typeof obj === "object") {
7
+ const source = obj._MST_SOURCE_;
8
+ if (source !== void 0) return source;
9
+ }
10
+ return obj;
11
+ };
12
+ var record = (obj) => obj._MST_RECORD_;
13
+ var squeeze = (obj) => {
14
+ if (typeof obj !== "object" || obj === null) return obj;
15
+ if (Array.isArray(obj)) {
16
+ const arr = [...obj];
17
+ for (let i = 0, len = arr.length; i < len; i++) {
18
+ if (typeof arr[i] === "object" && arr[i] !== null) arr[i] = squeeze(arr[i]);
19
+ }
20
+ return arr;
21
+ }
22
+ const copy = { ...obj };
23
+ const keys = Object.keys(copy);
24
+ for (let i = 0, len = keys.length; i < len; i++) {
25
+ if (typeof copy[keys[i]] === "object" && copy[keys[i]] !== null) copy[keys[i]] = squeeze(copy[keys[i]]);
26
+ }
27
+ return copy;
28
+ };
29
+ var setPathValue = (curr, path, callback, depth = 0) => {
30
+ if (depth === path.length) {
31
+ const next2 = Array.isArray(curr) ? [...curr] : { ...curr };
32
+ const result = callback(next2);
33
+ return result !== void 0 ? result : next2;
34
+ }
35
+ const key = path[depth];
36
+ const next = Array.isArray(curr) ? [...curr] : { ...curr };
37
+ next[key] = setPathValue(curr[key], path, callback, depth + 1);
38
+ return next;
39
+ };
40
+ var getAtPath = (state, path) => {
41
+ let current = state;
42
+ for (let i = 0; i < path.length; i++) {
43
+ if (current == null) return void 0;
44
+ current = current[path[i]];
45
+ }
46
+ return current;
47
+ };
48
+ var buildNested = (entries) => {
49
+ const result = {};
50
+ for (const entry of entries) {
51
+ const segs = entry.path.split(".");
52
+ let current = result;
53
+ for (let i = 0; i < segs.length - 1; i++) {
54
+ if (current[segs[i]] == null) current[segs[i]] = {};
55
+ current = current[segs[i]];
56
+ }
57
+ current[segs[segs.length - 1]] = entry.after;
58
+ }
59
+ return result;
60
+ };
61
+ var createMustard = (initialState) => {
62
+ let state = squeeze(initialState);
63
+ let version = 0;
64
+ const listeners = /* @__PURE__ */ new Set();
65
+ let history = [];
66
+ let pendingNotify = false;
67
+ const notify = () => {
68
+ version++;
69
+ if (!pendingNotify) {
70
+ pendingNotify = true;
71
+ Promise.resolve().then(() => {
72
+ pendingNotify = false;
73
+ listeners.forEach((l) => l());
74
+ });
75
+ }
76
+ };
77
+ const pushRecord = (path, key, before, after) => {
78
+ const fullPath = [...path, key].join(".");
79
+ history.push({
80
+ path: fullPath,
81
+ before: typeof before === "object" && before !== null ? squeeze(before) : before,
82
+ after: typeof after === "object" && after !== null ? squeeze(after) : after
83
+ });
84
+ };
85
+ const applyUndo = (entry) => {
86
+ const segs = entry.path.split(".");
87
+ const parentPath = segs.slice(0, -1);
88
+ const key = segs[segs.length - 1];
89
+ if (parentPath.length === 0) {
90
+ state = { ...state, [key]: entry.before };
91
+ } else {
92
+ state = setPathValue(state, parentPath, (parent) => {
93
+ parent[key] = entry.before;
94
+ });
95
+ }
96
+ };
97
+ const recordApi = {
98
+ data: () => {
99
+ const latest = /* @__PURE__ */ new Map();
100
+ for (const entry of history) {
101
+ const existing = latest.get(entry.path);
102
+ if (existing) {
103
+ existing.after = entry.after;
104
+ } else {
105
+ latest.set(entry.path, { ...entry });
106
+ }
107
+ }
108
+ return buildNested(Array.from(latest.values()));
109
+ },
110
+ paths: () => [...new Set(history.map((e) => e.path))],
111
+ undo: () => {
112
+ const entry = history.pop();
113
+ if (!entry) return;
114
+ applyUndo(entry);
115
+ notify();
116
+ },
117
+ undoAll: () => {
118
+ if (history.length === 0) return;
119
+ while (history.length > 0) applyUndo(history.pop());
120
+ notify();
121
+ },
122
+ undoTo: (index) => {
123
+ if (index < 0 || index >= history.length) return;
124
+ while (history.length > index) applyUndo(history.pop());
125
+ notify();
126
+ },
127
+ clear: () => {
128
+ history = [];
129
+ },
130
+ size: () => history.length
131
+ };
132
+ const proxyCache = /* @__PURE__ */ new Map();
133
+ const createProxy = (path = []) => {
134
+ const getCurrent = path.length === 0 ? () => state : () => getAtPath(state, path);
135
+ const curr = getCurrent();
136
+ const target = Array.isArray(curr) ? [] : {};
137
+ const proxy2 = new Proxy(target, {
138
+ get: (_, key) => {
139
+ if (typeof key === "symbol") {
140
+ const curr3 = getCurrent();
141
+ if (curr3 == null) return void 0;
142
+ const val = curr3[key];
143
+ return typeof val === "function" ? val.bind(curr3) : val;
144
+ }
145
+ if (key === "_MST_STORE_") return store;
146
+ if (key === "_MST_SOURCE_") return getCurrent();
147
+ if (key === "_MST_RECORD_") return recordApi;
148
+ if (key === "$") return getCurrent();
149
+ if (key === "reset") return (data) => {
150
+ state = typeof data === "function" ? squeeze(data()) : squeeze(data);
151
+ notify();
152
+ };
153
+ const curr2 = getCurrent();
154
+ if (curr2 == null) return void 0;
155
+ const value = curr2[key];
156
+ if (Array.isArray(curr2) && typeof value === "function" && MUTATORS.has(key)) {
157
+ return (...args) => {
158
+ const cleanArgs = args.map((a) => typeof a === "object" && a !== null ? squeeze(a) : a);
159
+ const beforeArr = squeeze(curr2);
160
+ let result;
161
+ state = setPathValue(state, path, (arr) => {
162
+ result = arr[key](...cleanArgs);
163
+ return arr;
164
+ });
165
+ const afterArr = squeeze(getAtPath(state, path));
166
+ const fullPath = path.join(".");
167
+ history.push({
168
+ path: fullPath,
169
+ before: beforeArr,
170
+ after: afterArr
171
+ });
172
+ notify();
173
+ return result;
174
+ };
175
+ }
176
+ if (Array.isArray(curr2) && typeof value === "function" && ITERATORS.has(key)) {
177
+ return value.bind(proxy2);
178
+ }
179
+ if (typeof value === "function") return value.bind(curr2);
180
+ if (typeof value === "object" && value !== null) {
181
+ const cacheKey = path.length === 0 ? key : path.join("\0") + "\0" + key;
182
+ let child = proxyCache.get(cacheKey);
183
+ if (!child) {
184
+ child = createProxy([...path, key]);
185
+ proxyCache.set(cacheKey, child);
186
+ }
187
+ return child;
188
+ }
189
+ return value;
190
+ },
191
+ set: (_, key, val) => {
192
+ const curr2 = getCurrent();
193
+ if (curr2 == null) return true;
194
+ if (curr2[key] === val) return true;
195
+ const before = curr2[key];
196
+ if (typeof val === "object" && val !== null) val = squeeze(val);
197
+ state = setPathValue(state, path, (parent) => {
198
+ parent[key] = val;
199
+ });
200
+ pushRecord(path, key, before, val);
201
+ notify();
202
+ return true;
203
+ },
204
+ ownKeys: () => {
205
+ const curr2 = getCurrent();
206
+ return curr2 ? Reflect.ownKeys(curr2) : [];
207
+ },
208
+ getOwnPropertyDescriptor: (_, key) => {
209
+ const curr2 = getCurrent();
210
+ if (curr2 != null && key in curr2) {
211
+ if (key === "length" && Array.isArray(curr2)) {
212
+ return { configurable: false, enumerable: false, writable: true, value: curr2.length };
213
+ }
214
+ return { configurable: true, enumerable: true, writable: true, value: curr2[key] };
215
+ }
216
+ return void 0;
217
+ },
218
+ deleteProperty: (_, key) => {
219
+ const curr2 = getCurrent();
220
+ if (curr2 == null || !(key in curr2)) return true;
221
+ const before = curr2[key];
222
+ state = setPathValue(state, path, (parent) => {
223
+ delete parent[key];
224
+ });
225
+ pushRecord(path, key, before, void 0);
226
+ notify();
227
+ return true;
228
+ },
229
+ has: (_, key) => {
230
+ const curr2 = getCurrent();
231
+ return curr2 != null && key in curr2;
232
+ }
233
+ });
234
+ return proxy2;
235
+ };
236
+ const proxy = createProxy();
237
+ const store = {
238
+ getState: () => state,
239
+ getVersion: () => version,
240
+ subscribe: (listener) => {
241
+ listeners.add(listener);
242
+ return () => listeners.delete(listener);
243
+ },
244
+ proxy
245
+ };
246
+ return store;
247
+ };
248
+ export {
249
+ INTERNAL_KEYS,
250
+ ITERATORS,
251
+ MUTATORS,
252
+ buildNested,
253
+ createMustard,
254
+ getAtPath,
255
+ record,
256
+ setPathValue,
257
+ squeeze,
258
+ unwrap
259
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@mustrd/core",
3
+ "version": "0.2.0",
4
+ "description": "Proxy-based state engine — direct assignment, diff tracking, undo. Zero dependencies.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "sideEffects": false,
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "devDependencies": {
26
+ "tsup": "^8.0.0",
27
+ "typescript": "^5.0.0",
28
+ "vitest": "^4.0.18"
29
+ },
30
+ "license": "MIT",
31
+ "scripts": {
32
+ "build": "tsup",
33
+ "dev": "tsup --watch",
34
+ "test": "vitest run"
35
+ }
36
+ }