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/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {PatchesOptions, GetItemIdConfig, GetItemIdFunction} from './types.js';
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
- path: (string | number)[],
42
- options: {internalPatchesOptions: PatchesOptions},
43
- ): string | (string | number)[] {
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
- * Find a getItemId function for a given path.
152
- * The function is looked up by traversing the getItemId config object
153
- * using the parent path (all elements except the last one).
154
- *
155
- * @example
156
- * // For path ['items', 3] with config { items: (item) => item.id }
157
- * // Returns the function (item) => item.id
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: (string | number)[],
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
+ }