patch-recorder 0.0.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 (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +337 -0
  3. package/dist/arrays.d.ts +6 -0
  4. package/dist/arrays.d.ts.map +1 -0
  5. package/dist/arrays.js +143 -0
  6. package/dist/arrays.js.map +1 -0
  7. package/dist/index.d.ts +21 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +44 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/maps.d.ts +7 -0
  12. package/dist/maps.d.ts.map +1 -0
  13. package/dist/maps.js +96 -0
  14. package/dist/maps.js.map +1 -0
  15. package/dist/optimizer.d.ts +7 -0
  16. package/dist/optimizer.d.ts.map +1 -0
  17. package/dist/optimizer.js +123 -0
  18. package/dist/optimizer.js.map +1 -0
  19. package/dist/patches.d.ts +18 -0
  20. package/dist/patches.d.ts.map +1 -0
  21. package/dist/patches.js +46 -0
  22. package/dist/patches.js.map +1 -0
  23. package/dist/proxy.d.ts +3 -0
  24. package/dist/proxy.d.ts.map +1 -0
  25. package/dist/proxy.js +127 -0
  26. package/dist/proxy.js.map +1 -0
  27. package/dist/sets.d.ts +7 -0
  28. package/dist/sets.d.ts.map +1 -0
  29. package/dist/sets.js +78 -0
  30. package/dist/sets.js.map +1 -0
  31. package/dist/types.d.ts +55 -0
  32. package/dist/types.d.ts.map +1 -0
  33. package/dist/types.js +6 -0
  34. package/dist/types.js.map +1 -0
  35. package/dist/utils.d.ts +33 -0
  36. package/dist/utils.d.ts.map +1 -0
  37. package/dist/utils.js +132 -0
  38. package/dist/utils.js.map +1 -0
  39. package/package.json +60 -0
  40. package/src/arrays.ts +191 -0
  41. package/src/index.ts +71 -0
  42. package/src/maps.ts +120 -0
  43. package/src/optimizer.ts +136 -0
  44. package/src/patches.ts +67 -0
  45. package/src/proxy.ts +163 -0
  46. package/src/sets.ts +100 -0
  47. package/src/types.ts +67 -0
  48. package/src/utils.ts +150 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Ronan Sandford
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/README.md ADDED
@@ -0,0 +1,337 @@
1
+ # patch-recorder
2
+
3
+ > Record JSON patches (RFC 6902) from mutations applied to objects, arrays, Maps, and Sets via a proxy interface.
4
+
5
+ ## Features
6
+
7
+ - ✅ **Reference integrity** - Original object reference maintained (mutates in place)
8
+ - ✅ **Zero memory overhead** - No copying of large arrays/objects
9
+ - ✅ **Accurate patches** - JSON Patch (RFC 6902) compliant
10
+ - ✅ **Type safety** - Full TypeScript support
11
+ - ✅ **Immediate patch generation** - Patches generated as mutations occur
12
+ - ✅ **Optional optimization** - Can compress/merge redundant patches
13
+ - ✅ **Full collection support** - Works with objects, arrays, Maps, and Sets
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install patch-recorder
19
+ # or
20
+ pnpm add patch-recorder
21
+ # or
22
+ yarn add patch-recorder
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import {recordPatches} from 'patch-recorder';
29
+
30
+ const state = {
31
+ user: { name: 'John', age: 30 },
32
+ items: [1, 2, 3]
33
+ };
34
+
35
+ const patches = recordPatches(state, (draft) => {
36
+ draft.user.name = 'Jane';
37
+ draft.items.push(4);
38
+ });
39
+
40
+ console.log(state.user.name); // 'Jane' (mutated in place!)
41
+ console.log(patches);
42
+ // [
43
+ // { op: 'replace', path: ['user', 'name'], value: 'Jane' },
44
+ // { op: 'add', path: ['items', 3], value: 4 }
45
+ // ]
46
+ ```
47
+
48
+ ## Core Difference from Mutative/Immer
49
+
50
+ Unlike mutative or immer, **patch-recorder mutates the original object in place** while recording changes. This is its primary advantage:
51
+
52
+ **Reference Integrity:**
53
+ - **Zero memory overhead** from copying objects
54
+ - **Original object references are preserved** throughout the operation
55
+ - Perfect for scenarios where you need both mutation tracking AND direct object manipulation
56
+
57
+ **Performance:**
58
+ - Similar performance to mutative for most operations
59
+ - Slightly faster for object and Map operations
60
+ - Comparable performance for large array operations
61
+
62
+ ```typescript
63
+ // With patch-recorder
64
+ const state = { user: { name: 'John' } };
65
+ const patches = recordPatches(state, (draft) => {
66
+ draft.user.name = 'Jane';
67
+ });
68
+
69
+ // state === originalState (same reference)
70
+ // state.user.name === 'Jane'
71
+ ```
72
+
73
+ ## API
74
+
75
+ ### `recordPatches(state, mutate, options?)`
76
+
77
+ Records JSON patches from mutations applied to the state.
78
+
79
+ #### Parameters
80
+
81
+ - **`state`** (`T extends NonPrimitive`): The state object to mutate and record patches from
82
+ - **`mutate`** `(state: Draft<T>) => void`: Callback function that performs mutations on the draft
83
+ - **`options`** (`RecordPatchesOptions`, optional): Configuration options
84
+
85
+ #### Options
86
+
87
+ ```typescript
88
+ interface RecordPatchesOptions {
89
+ /**
90
+ * Return paths as arrays (default: true) or strings
91
+ */
92
+ pathAsArray?: boolean;
93
+
94
+ /**
95
+ * Include array length in patches (default: true)
96
+ */
97
+ arrayLengthAssignment?: boolean;
98
+
99
+ /**
100
+ * Optimize patches by merging redundant operations (default: false)
101
+ */
102
+ optimize?: boolean;
103
+ }
104
+ ```
105
+
106
+ #### Returns
107
+
108
+ `Patches<true>` - Array of JSON patches
109
+
110
+ ## Usage Examples
111
+
112
+ ### Basic Object Mutations
113
+
114
+ ```typescript
115
+ const state = { count: 0, name: 'test' };
116
+
117
+ const patches = recordPatches(state, (draft) => {
118
+ draft.count = 5;
119
+ draft.name = 'updated';
120
+ });
121
+
122
+ console.log(patches);
123
+ // [
124
+ // { op: 'replace', path: ['count'], value: 5 },
125
+ // { op: 'replace', path: ['name'], value: 'updated' }
126
+ // ]
127
+ ```
128
+
129
+ ### Nested Object Mutations
130
+
131
+ ```typescript
132
+ const state = {
133
+ user: {
134
+ profile: {
135
+ name: 'John',
136
+ age: 30
137
+ }
138
+ }
139
+ };
140
+
141
+ const patches = recordPatches(state, (draft) => {
142
+ draft.user.profile.name = 'Jane';
143
+ draft.user.profile.age = 31;
144
+ });
145
+
146
+ console.log(patches);
147
+ // [
148
+ // { op: 'replace', path: ['user', 'profile', 'name'], value: 'Jane' },
149
+ // { op: 'replace', path: ['user', 'profile', 'age'], value: 31 }
150
+ // ]
151
+ ```
152
+
153
+ ### Array Operations
154
+
155
+ ```typescript
156
+ const state = { items: [1, 2, 3] };
157
+
158
+ const patches = recordPatches(state, (draft) => {
159
+ draft.items.push(4); // add
160
+ draft.items[1] = 10; // replace
161
+ draft.items.shift(); // remove
162
+ });
163
+
164
+ console.log(patches);
165
+ // [
166
+ // { op: 'add', path: ['items', 3], value: 4 },
167
+ // { op: 'replace', path: ['items', 1], value: 10 },
168
+ // { op: 'remove', path: ['items', 0] },
169
+ // { op: 'replace', path: ['items', 0], value: 2 },
170
+ // { op: 'replace', path: ['items', 1], value: 3 },
171
+ // { op: 'replace', path: ['items', 'length'], value: 3 }
172
+ // ]
173
+ ```
174
+
175
+ ### Map Operations
176
+
177
+ ```typescript
178
+ const state = { map: new Map([['a', 1]]) };
179
+
180
+ const patches = recordPatches(state, (draft) => {
181
+ draft.map.set('b', 2); // add
182
+ draft.map.set('a', 10); // replace
183
+ draft.map.delete('b'); // remove
184
+ });
185
+
186
+ console.log(patches);
187
+ // [
188
+ // { op: 'add', path: ['map', 'b'], value: 2 },
189
+ // { op: 'replace', path: ['map', 'a'], value: 10 },
190
+ // { op: 'remove', path: ['map', 'b'] }
191
+ // ]
192
+ ```
193
+
194
+ ### Set Operations
195
+
196
+ ```typescript
197
+ const state = { set: new Set([1, 2]) };
198
+
199
+ const patches = recordPatches(state, (draft) => {
200
+ draft.set.add(3); // add
201
+ draft.set.delete(2); // remove
202
+ });
203
+
204
+ console.log(patches);
205
+ // [
206
+ // { op: 'add', path: ['set', 3], value: 3 },
207
+ // { op: 'remove', path: ['set', 2] }
208
+ // ]
209
+ ```
210
+
211
+ ### Using Options
212
+
213
+ ```typescript
214
+ const state = { value: 1 };
215
+
216
+ // Use string paths instead of arrays
217
+ const patches2 = recordPatches(state, (draft) => {
218
+ draft.value = 3;
219
+ }, { pathAsArray: false });
220
+ console.log(patches2);
221
+ // [{ op: 'replace', path: '/value', value: 3 }]
222
+
223
+ // Optimize patches (merge redundant operations)
224
+ const patches3 = recordPatches(state, (draft) => {
225
+ draft.value = 4;
226
+ draft.value = 5;
227
+ draft.value = 5; // no-op
228
+ }, { optimize: true });
229
+ console.log(patches3);
230
+ // [{ op: 'replace', path: ['value'], value: 5 }]
231
+ ```
232
+
233
+ ## Comparison with Mutative
234
+
235
+ | Feature | Mutative | patch-recorder |
236
+ |---------|----------|----------------|
237
+ | Reference preservation | ❌ No (creates copy) | ✅ Yes (mutates in place) |
238
+ | Memory overhead | ❌ Yes (copies) | ✅ No |
239
+ | Patch accuracy | ✅ Excellent | ✅ Excellent |
240
+ | Type safety | ✅ Excellent | ✅ Excellent |
241
+ | API similarity | ✅ Similar | ✅ Similar |
242
+ | Use case | Immutable state | Mutable with tracking |
243
+
244
+ ### When to Use patch-recorder
245
+
246
+ Use **patch-recorder** when you need:
247
+ - To mutate state in place while tracking changes
248
+ - Zero memory overhead from copying
249
+ - To preserve object references
250
+ - To integrate with systems that require direct mutation
251
+
252
+ Use **mutative** when you need:
253
+ - Immutable state management
254
+ - To create new state versions
255
+ - Functional programming patterns
256
+
257
+ ## Performance
258
+
259
+ patch-recorder provides substantial performance improvements over mutative while maintaining **reference integrity** as its key differentiator. When properly benchmarking just the mutation operations (excluding state creation), patch-recorder shows dramatic speedups:
260
+
261
+ ### Benchmark Results
262
+
263
+ | Operation | Mutative | patch-recorder | Speedup |
264
+ |-----------|----------|----------------|---------|
265
+ | Simple object mutation | 0.0272ms | 0.0110ms | **2.48x** |
266
+ | Medium nested object | 0.0268ms | 0.0114ms | **2.35x** |
267
+ | Large nested object | 0.0094ms | 0.0040ms | **2.38x** |
268
+ | Array push (100k elements) | 3.277ms | 1.155ms | **2.84x** |
269
+ | Array index (100k elements) | 2.966ms | 0.004ms | **826x** |
270
+ | Map operations (100k entries) | 11.384ms | 0.011ms | **1,067x** |
271
+
272
+ **Memory Usage:**
273
+ - **Mutative**: Creates copies (memory overhead proportional to state size)
274
+ - **patch-recorder**: 0 MB overhead (mutates in place, no copying)
275
+
276
+ ### Performance Analysis
277
+
278
+ The benchmark results reveal patch-recorder's massive advantage for operations that would require copying large data structures:
279
+
280
+ - **Object mutations** (2.35-2.48x faster) - Consistent speedups due to simpler proxy overhead
281
+ - **Array push** (2.84x faster) - Avoids copying entire arrays on mutation
282
+ - **Array index assignment** (826x faster) - **Massive speedup** by not copying 100k-element arrays
283
+ - **Map operations** (1,067x faster) - **Incredible speedup** by not copying 100k-entry Maps
284
+
285
+ **Why the dramatic differences?**
286
+ - patch-recorder mutates in place, so array index assignment and Map operations don't require copying
287
+ - mutative's copy-on-write approach is elegant but incurs significant overhead for large collections
288
+ - The advantage scales with data size - the larger the collection, the bigger the speedup
289
+
290
+ **Note on mutative's performance:** Mutative is impressively fast for object mutations and offers excellent immutability guarantees. Its speedups of 2-3x for objects are reasonable trade-offs for immutable state management.
291
+
292
+ ### Run Benchmarks
293
+
294
+ You can run the benchmarks locally:
295
+
296
+ ```bash
297
+ npm run benchmark
298
+ ```
299
+
300
+ ### Memory Usage
301
+
302
+ - **No copying**: Original object mutated in place
303
+ - **Patch storage**: Only patches array stored (minimal overhead)
304
+ - **Proxy creation**: One proxy per object accessed during mutation
305
+
306
+ ### Time Complexity
307
+
308
+ - **Property access**: O(1) for direct access, O(1) for proxy
309
+ - **Property mutation**: O(1) + patch generation
310
+ - **Array operations**: O(n) for some operations (same as native)
311
+ - **Nested mutations**: O(depth) for proxy creation
312
+
313
+ ### When to Choose patch-recorder
314
+
315
+ **Choose patch-recorder if you need:**
316
+ - **Reference integrity** - Objects and arrays maintain their identity
317
+ - **Zero memory overhead** - No copying of state
318
+ - **Direct mutation** - Mutate in place while tracking changes
319
+ - **Integration with mutable systems** - Systems that require direct object manipulation
320
+
321
+ **Choose mutative if you need:**
322
+ - **Immutable state** - Create new state versions
323
+ - **Functional programming patterns** - Prefer immutability
324
+ - **State versioning** - Need to track multiple state versions
325
+
326
+ ### Optimization Tips
327
+
328
+ 1. **Lazy proxy creation**: Only creates proxies for accessed properties
329
+ 2. **Patch compression**: Reduces redundant patches via `optimize` option
330
+
331
+ ## License
332
+
333
+ MIT © [Ronan Sandford](https://github.com/wighawag)
334
+
335
+ ## Contributing
336
+
337
+ Contributions are welcome! Please feel free to submit a Pull Request.
@@ -0,0 +1,6 @@
1
+ import type { RecorderState } from './types.js';
2
+ /**
3
+ * Handle array method calls and property access
4
+ */
5
+ export declare function handleArrayGet(obj: any[], prop: string, path: (string | number)[], state: RecorderState<any>): any;
6
+ //# sourceMappingURL=arrays.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrays.d.ts","sourceRoot":"","sources":["../src/arrays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,YAAY,CAAC;AAK9C;;GAEG;AACH,wBAAgB,cAAc,CAC7B,GAAG,EAAE,GAAG,EAAE,EACV,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EACzB,KAAK,EAAE,aAAa,CAAC,GAAG,CAAC,GACvB,GAAG,CA2DL"}
package/dist/arrays.js ADDED
@@ -0,0 +1,143 @@
1
+ import { generateAddPatch, generateDeletePatch, generateReplacePatch } from './patches.js';
2
+ import { createProxy } from './proxy.js';
3
+ /**
4
+ * Handle array method calls and property access
5
+ */
6
+ export function handleArrayGet(obj, prop, path, state) {
7
+ // Mutating methods
8
+ const mutatingMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse'];
9
+ if (mutatingMethods.includes(prop)) {
10
+ return (...args) => {
11
+ const oldValue = [...obj]; // Snapshot before mutation
12
+ const result = Array.prototype[prop].apply(obj, args);
13
+ // Generate patches based on the method
14
+ generateArrayPatches(state, obj, prop, args, result, path, oldValue);
15
+ return result;
16
+ };
17
+ }
18
+ // Non-mutating methods - just return them bound to the array
19
+ const nonMutatingMethods = [
20
+ 'map',
21
+ 'filter',
22
+ 'reduce',
23
+ 'reduceRight',
24
+ 'forEach',
25
+ 'find',
26
+ 'findIndex',
27
+ 'some',
28
+ 'every',
29
+ 'includes',
30
+ 'indexOf',
31
+ 'lastIndexOf',
32
+ 'slice',
33
+ 'concat',
34
+ 'join',
35
+ 'flat',
36
+ 'flatMap',
37
+ 'at',
38
+ ];
39
+ if (nonMutatingMethods.includes(prop)) {
40
+ return Array.prototype[prop].bind(obj);
41
+ }
42
+ // Property access
43
+ if (prop === 'length') {
44
+ return obj.length;
45
+ }
46
+ const value = obj[prop];
47
+ // For numeric properties (array indices), check if the value is an object/array
48
+ // If so, return a proxy to enable nested mutation tracking
49
+ if (!isNaN(Number(prop)) && typeof value === 'object' && value !== null) {
50
+ const index = Number(prop);
51
+ return createProxy(value, [...path, index], state);
52
+ }
53
+ // For numeric properties (array indices), return the value
54
+ // The main proxy handler's get will handle creating proxies for nested objects
55
+ return value;
56
+ }
57
+ /**
58
+ * Generate patches for array mutations
59
+ */
60
+ function generateArrayPatches(state, obj, method, args, result, path, oldValue) {
61
+ switch (method) {
62
+ case 'push': {
63
+ // Generate add patches for each new element
64
+ const startIndex = oldValue.length;
65
+ args.forEach((value, i) => {
66
+ const index = startIndex + i;
67
+ generateAddPatch(state, [...path, index], value);
68
+ });
69
+ // Generate length patch if option is enabled
70
+ if (state.options.arrayLengthAssignment !== false) {
71
+ generateReplacePatch(state, [...path, 'length'], obj.length);
72
+ }
73
+ break;
74
+ }
75
+ case 'pop': {
76
+ // Generate remove patch for the removed element
77
+ const removedIndex = oldValue.length - 1;
78
+ generateDeletePatch(state, [...path, removedIndex], result);
79
+ // Generate length patch if option is enabled
80
+ if (state.options.arrayLengthAssignment !== false) {
81
+ generateReplacePatch(state, [...path, 'length'], obj.length);
82
+ }
83
+ break;
84
+ }
85
+ case 'shift': {
86
+ // Generate remove patch for the removed element
87
+ generateDeletePatch(state, [...path, 0], result);
88
+ // Shift is complex - we need to update all remaining elements
89
+ // Update all shifted elements (after the shift, each element moves to index - 1)
90
+ for (let i = 0; i < obj.length; i++) {
91
+ generateReplacePatch(state, [...path, i], oldValue[i + 1]);
92
+ }
93
+ // Generate length patch if option is enabled
94
+ if (state.options.arrayLengthAssignment !== false) {
95
+ generateReplacePatch(state, [...path, 'length'], obj.length);
96
+ }
97
+ break;
98
+ }
99
+ case 'unshift': {
100
+ // Add new elements at the beginning
101
+ args.forEach((value, i) => {
102
+ generateAddPatch(state, [...path, i], value);
103
+ });
104
+ // Update all existing elements
105
+ for (let i = 0; i < oldValue.length; i++) {
106
+ generateReplacePatch(state, [...path, i + args.length], oldValue[i]);
107
+ }
108
+ // Generate length patch if option is enabled
109
+ if (state.options.arrayLengthAssignment !== false) {
110
+ generateReplacePatch(state, [...path, 'length'], obj.length);
111
+ }
112
+ break;
113
+ }
114
+ case 'splice': {
115
+ const [start, deleteCount, ...addItems] = args;
116
+ // Generate remove patches for deleted items
117
+ for (let i = 0; i < deleteCount; i++) {
118
+ generateDeletePatch(state, [...path, start], oldValue[start]);
119
+ }
120
+ // Generate add patches for new items
121
+ addItems.forEach((item, i) => {
122
+ generateAddPatch(state, [...path, start + i], item);
123
+ });
124
+ // If there are both deletions and additions, update the shifted elements
125
+ const itemsToShift = oldValue.length - start - deleteCount;
126
+ for (let i = 0; i < itemsToShift; i++) {
127
+ generateReplacePatch(state, [...path, start + addItems.length + i], oldValue[start + deleteCount + i]);
128
+ }
129
+ // Generate length patch if option is enabled
130
+ if (state.options.arrayLengthAssignment !== false) {
131
+ generateReplacePatch(state, [...path, 'length'], obj.length);
132
+ }
133
+ break;
134
+ }
135
+ case 'sort':
136
+ case 'reverse': {
137
+ // These reorder the entire array - generate full replace
138
+ generateReplacePatch(state, path, [...obj]);
139
+ break;
140
+ }
141
+ }
142
+ }
143
+ //# sourceMappingURL=arrays.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrays.js","sourceRoot":"","sources":["../src/arrays.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,gBAAgB,EAAE,mBAAmB,EAAE,oBAAoB,EAAC,MAAM,cAAc,CAAC;AACzF,OAAO,EAAC,WAAW,EAAC,MAAM,YAAY,CAAC;AAEvC;;GAEG;AACH,MAAM,UAAU,cAAc,CAC7B,GAAU,EACV,IAAY,EACZ,IAAyB,EACzB,KAAyB;IAEzB,mBAAmB;IACnB,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;IAEzF,IAAI,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACpC,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;YACzB,MAAM,QAAQ,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,2BAA2B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ACZ,KAAK,SAAS,CAAC,CAAC,CAAC;YAChB,yDAAyD;YACzD,oBAAoB,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;YAC5C,MAAM;QACP,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { NonPrimitive, Draft, RecordPatchesOptions, Patches } from './types.js';
2
+ /**
3
+ * Record JSON patches from mutations applied to an object, array, Map, or Set.
4
+ * Unlike mutative or immer, this mutates the original object in place while recording changes.
5
+ *
6
+ * @param state - The state to record patches from
7
+ * @param mutate - A function that receives a draft of the state and applies mutations
8
+ * @param options - Configuration options
9
+ * @returns Array of JSON patches (RFC 6902 compliant)
10
+ *
11
+ * @example
12
+ * const state = { user: { name: 'John' } };
13
+ * const patches = recordPatches(state, (draft) => {
14
+ * draft.user.name = 'Jane';
15
+ * });
16
+ * console.log(state.user.name); // 'Jane' (mutated in place!)
17
+ * console.log(patches); // [{ op: 'replace', path: ['user', 'name'], value: 'Jane' }]
18
+ */
19
+ export declare function recordPatches<T extends NonPrimitive>(state: T, mutate: (state: Draft<T>) => void, options?: RecordPatchesOptions): Patches<true>;
20
+ export type { NonPrimitive, Draft, RecordPatchesOptions, Patches, Patch, Operation, } from './types.js';
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACX,YAAY,EACZ,KAAK,EACL,oBAAoB,EACpB,OAAO,EAGP,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,YAAY,EACnD,KAAK,EAAE,CAAC,EACR,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,EACjC,OAAO,GAAE,oBAAyB,GAChC,OAAO,CAAC,IAAI,CAAC,CA4Bf;AAGD,YAAY,EACX,YAAY,EACZ,KAAK,EACL,oBAAoB,EACpB,OAAO,EACP,KAAK,EACL,SAAS,GACT,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ import { createProxy } from './proxy.js';
2
+ import { compressPatches } from './optimizer.js';
3
+ /**
4
+ * Record JSON patches from mutations applied to an object, array, Map, or Set.
5
+ * Unlike mutative or immer, this mutates the original object in place while recording changes.
6
+ *
7
+ * @param state - The state to record patches from
8
+ * @param mutate - A function that receives a draft of the state and applies mutations
9
+ * @param options - Configuration options
10
+ * @returns Array of JSON patches (RFC 6902 compliant)
11
+ *
12
+ * @example
13
+ * const state = { user: { name: 'John' } };
14
+ * const patches = recordPatches(state, (draft) => {
15
+ * draft.user.name = 'Jane';
16
+ * });
17
+ * console.log(state.user.name); // 'Jane' (mutated in place!)
18
+ * console.log(patches); // [{ op: 'replace', path: ['user', 'name'], value: 'Jane' }]
19
+ */
20
+ export function recordPatches(state, mutate, options = {}) {
21
+ const internalPatchesOptions = {
22
+ pathAsArray: options.pathAsArray ?? true,
23
+ arrayLengthAssignment: options.arrayLengthAssignment ?? true,
24
+ };
25
+ const recorderState = {
26
+ original: state,
27
+ patches: [],
28
+ basePath: [],
29
+ options: {
30
+ ...options,
31
+ internalPatchesOptions,
32
+ },
33
+ };
34
+ // Create proxy
35
+ const proxy = createProxy(state, [], recorderState);
36
+ // Apply mutations
37
+ mutate(proxy);
38
+ // Return patches (optionally optimized)
39
+ if (options.optimize) {
40
+ return compressPatches(recorderState.patches);
41
+ }
42
+ return recorderState.patches;
43
+ }
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,YAAY,CAAC;AACvC,OAAO,EAAC,eAAe,EAAC,MAAM,gBAAgB,CAAC;AAU/C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,aAAa,CAC5B,KAAQ,EACR,MAAiC,EACjC,UAAgC,EAAE;IAElC,MAAM,sBAAsB,GAAG;QAC9B,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,IAAI;QACxC,qBAAqB,EAAE,OAAO,CAAC,qBAAqB,IAAI,IAAI;KAC5D,CAAC;IAEF,MAAM,aAAa,GAAG;QACrB,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,EAAE;QACX,QAAQ,EAAE,EAAE;QACZ,OAAO,EAAE;YACR,GAAG,OAAO;YACV,sBAAsB;SACtB;KACD,CAAC;IAEF,eAAe;IACf,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,aAAa,CAAa,CAAC;IAEhE,kBAAkB;IAClB,MAAM,CAAC,KAAK,CAAC,CAAC;IAEd,wCAAwC;IACxC,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtB,OAAO,eAAe,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,aAAa,CAAC,OAAwB,CAAC;AAC/C,CAAC"}
package/dist/maps.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { RecorderState } from './types.js';
2
+ /**
3
+ * Handle property access on Map objects
4
+ * Wraps mutating methods (set, delete, clear) to generate patches
5
+ */
6
+ export declare function handleMapGet<K = any, V = any>(obj: Map<K, V>, prop: string | symbol, path: (string | number)[], state: RecorderState<any>): any;
7
+ //# sourceMappingURL=maps.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maps.d.ts","sourceRoot":"","sources":["../src/maps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,YAAY,CAAC;AAM9C;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAC5C,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EACd,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,IAAI,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EACzB,KAAK,EAAE,aAAa,CAAC,GAAG,CAAC,GACvB,GAAG,CAqFL"}