patch-recorder 0.1.0 → 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/README.md +1 -75
- package/dist/arrays.d.ts +2 -2
- package/dist/arrays.d.ts.map +1 -1
- package/dist/arrays.js +12 -20
- package/dist/arrays.js.map +1 -1
- package/dist/index.d.ts +1 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -34
- package/dist/index.js.map +1 -1
- package/dist/maps.d.ts +2 -2
- package/dist/maps.d.ts.map +1 -1
- package/dist/maps.js +4 -20
- package/dist/maps.js.map +1 -1
- package/dist/optimizer.d.ts +16 -1
- package/dist/optimizer.d.ts.map +1 -1
- package/dist/optimizer.js +114 -15
- package/dist/optimizer.js.map +1 -1
- package/dist/patches.d.ts +5 -5
- package/dist/patches.d.ts.map +1 -1
- package/dist/patches.js +5 -5
- package/dist/patches.js.map +1 -1
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js +24 -52
- package/dist/proxy.js.map +1 -1
- package/dist/sets.d.ts +2 -2
- package/dist/sets.d.ts.map +1 -1
- package/dist/sets.js +4 -20
- package/dist/sets.js.map +1 -1
- package/dist/types.d.ts +14 -33
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +28 -13
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +76 -20
- package/dist/utils.js.map +1 -1
- package/package.json +2 -1
- package/src/arrays.ts +22 -30
- package/src/index.ts +9 -54
- package/src/maps.ts +12 -30
- package/src/optimizer.ts +146 -28
- package/src/patches.ts +20 -21
- package/src/proxy.ts +31 -60
- package/src/sets.ts +11 -30
- package/src/types.ts +16 -40
- package/src/utils.ts +81 -28
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {GetItemIdConfig, GetItemIdFunction, PatchPath} from './types.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Type guard to check if a value is a plain object (not null, not array, not Map/Set)
|
|
@@ -37,25 +37,12 @@ export function isSet(value: unknown): value is Set<unknown> {
|
|
|
37
37
|
/**
|
|
38
38
|
* Format a path array to either array format or JSON Pointer string format
|
|
39
39
|
*/
|
|
40
|
-
export function formatPath(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
options.internalPatchesOptions &&
|
|
46
|
-
typeof options.internalPatchesOptions === 'object' &&
|
|
47
|
-
options.internalPatchesOptions.pathAsArray === false
|
|
48
|
-
) {
|
|
49
|
-
// Convert to JSON Pointer string format (RFC 6901)
|
|
50
|
-
if (path.length === 0) {
|
|
51
|
-
return '';
|
|
52
|
-
}
|
|
53
|
-
return '/' + path
|
|
54
|
-
.map((part) => String(part).replace(/~/g, '~0').replace(/\//g, '~1'))
|
|
55
|
-
.join('/');
|
|
40
|
+
export function formatPath(path: PatchPath): string {
|
|
41
|
+
// Convert to JSON Pointer string format (RFC 6901)
|
|
42
|
+
if (path.length === 0) {
|
|
43
|
+
return '';
|
|
56
44
|
}
|
|
57
|
-
|
|
58
|
-
return path;
|
|
45
|
+
return '/' + path.map((part) => String(part).replace(/~/g, '~0').replace(/\//g, '~1')).join('/');
|
|
59
46
|
}
|
|
60
47
|
|
|
61
48
|
/**
|
|
@@ -148,16 +135,16 @@ export function cloneIfNeeded<T>(value: T): T {
|
|
|
148
135
|
}
|
|
149
136
|
|
|
150
137
|
/**
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
138
|
+
* Find a getItemId function for a given path.
|
|
139
|
+
* The function is looked up by traversing the getItemId config object
|
|
140
|
+
* using the parent path (all elements except the last one).
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* // For path ['items', 3] with config { items: (item) => item.id }
|
|
144
|
+
* // Returns the function (item) => item.id
|
|
145
|
+
*/
|
|
159
146
|
export function findGetItemIdFn(
|
|
160
|
-
path:
|
|
147
|
+
path: PatchPath,
|
|
161
148
|
getItemIdConfig: GetItemIdConfig | undefined,
|
|
162
149
|
): GetItemIdFunction | undefined {
|
|
163
150
|
if (!getItemIdConfig || path.length === 0) {
|
|
@@ -190,6 +177,11 @@ export function findGetItemIdFn(
|
|
|
190
177
|
return undefined;
|
|
191
178
|
}
|
|
192
179
|
|
|
180
|
+
if (typeof key === 'object' || typeof key === 'symbol') {
|
|
181
|
+
// there is no way to match an object or symbol key in the config
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
193
185
|
current = (current as GetItemIdConfig)[key];
|
|
194
186
|
}
|
|
195
187
|
|
|
@@ -200,3 +192,64 @@ export function findGetItemIdFn(
|
|
|
200
192
|
|
|
201
193
|
return undefined;
|
|
202
194
|
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Convert a path array or string to a string key for optimized lookup.
|
|
198
|
+
* Uses null character (\x00) as delimiter since it's unlikely in property names.
|
|
199
|
+
* This is significantly faster than JSON.stringify for the common case.
|
|
200
|
+
*
|
|
201
|
+
* @param path - The path array or string to convert
|
|
202
|
+
* @returns A string key representation of the path
|
|
203
|
+
*/
|
|
204
|
+
export function pathToKey(path: PatchPath): string {
|
|
205
|
+
// If path is already a string, use it directly
|
|
206
|
+
if (typeof path === 'string') {
|
|
207
|
+
return path;
|
|
208
|
+
}
|
|
209
|
+
// Otherwise convert array to string
|
|
210
|
+
if (path.length === 0) {
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
if (path.length === 1) {
|
|
214
|
+
const elem = path[0];
|
|
215
|
+
if (typeof elem === 'symbol') {
|
|
216
|
+
return elem.toString();
|
|
217
|
+
}
|
|
218
|
+
if (typeof elem === 'object') {
|
|
219
|
+
return JSON.stringify(elem);
|
|
220
|
+
}
|
|
221
|
+
return String(elem);
|
|
222
|
+
}
|
|
223
|
+
return path.map((elem) => {
|
|
224
|
+
if (typeof elem === 'symbol') {
|
|
225
|
+
return elem.toString();
|
|
226
|
+
}
|
|
227
|
+
if (typeof elem === 'object') {
|
|
228
|
+
return JSON.stringify(elem);
|
|
229
|
+
}
|
|
230
|
+
return String(elem);
|
|
231
|
+
}).join('\x00');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Convert a string key back to a path array.
|
|
236
|
+
* This is the inverse of pathToKey.
|
|
237
|
+
*
|
|
238
|
+
* @param key - The string key to convert
|
|
239
|
+
* @returns The path array
|
|
240
|
+
*/
|
|
241
|
+
export function keyToPath(key: string): (string | number)[] {
|
|
242
|
+
if (key === '') {
|
|
243
|
+
return [];
|
|
244
|
+
}
|
|
245
|
+
if (key.indexOf('\x00') === -1) {
|
|
246
|
+
// No delimiter, single element
|
|
247
|
+
// Try to parse as number for consistency
|
|
248
|
+
const num = Number(key);
|
|
249
|
+
return isNaN(num) ? [key] : [num];
|
|
250
|
+
}
|
|
251
|
+
return key.split('\x00').map((part) => {
|
|
252
|
+
const num = Number(part);
|
|
253
|
+
return isNaN(num) ? part : num;
|
|
254
|
+
});
|
|
255
|
+
}
|