json-diff-ts 1.2.5 → 1.2.6

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 CHANGED
@@ -281,7 +281,7 @@ Blog: https://blog.leitwolf.io
281
281
  Twitter: [@cglessner](https://twitter.com/cglessner)
282
282
 
283
283
  ## Changelog
284
-
284
+ - v1.2.6 Improve handling of JSON Path segments that include periods (PR by [EdVinyard](https://github.com/EdVinyard))
285
285
  - v1.2.5 Patch all dependencies; add support for resolving a key name if a function is used to get the key value
286
286
  - v1.2.4 Fix readme (npm install); update TypeScript and Lodash
287
287
  - v1.2.3 Update outdated dependencies; update TypeScript version to 4.5.2
package/lib/jsonDiff.js CHANGED
@@ -309,14 +309,16 @@ const flattenChangeset = (obj, path = '$', embeddedKey) => {
309
309
  ? `${path}[${obj.key}]`
310
310
  : obj.type === Operation.ADD
311
311
  ? path
312
- : `${path}[?(@.${embeddedKey}='${obj.key}')]`
313
- : (path = `${path}.${obj.key}`);
312
+ : filterExpression(path, embeddedKey, obj.key)
313
+ : (path = append(path, obj.key));
314
314
  return (0, exports.flattenChangeset)(obj.changes || obj, path, obj.embeddedKey);
315
315
  }
316
316
  else {
317
317
  const valueType = (0, exports.getTypeOfObj)(obj.value);
318
318
  return [
319
- Object.assign(Object.assign({}, obj), { path: valueType === 'Object' || path.endsWith(`[${obj.key}]`) ? path : `${path}.${obj.key}`, valueType })
319
+ Object.assign(Object.assign({}, obj), { path: valueType === 'Object' || path.endsWith(`[${obj.key}]`)
320
+ ? path
321
+ : append(path, obj.key), valueType })
320
322
  ];
321
323
  }
322
324
  }
@@ -438,3 +440,15 @@ const unflattenChanges = (changes) => {
438
440
  return changesArr;
439
441
  };
440
442
  exports.unflattenChanges = unflattenChanges;
443
+ /** combine a base JSON Path with a subsequent segment */
444
+ function append(basePath, nextSegment) {
445
+ return nextSegment.includes('.')
446
+ ? `${basePath}[${nextSegment}]`
447
+ : `${basePath}.${nextSegment}`;
448
+ }
449
+ /** returns a JSON Path filter expression; e.g., `$.pet[(?name='spot')]` */
450
+ function filterExpression(basePath, filterKey, filterValue) {
451
+ return typeof filterKey === 'string' && filterKey.includes('.')
452
+ ? `${basePath}[?(@[${filterKey}]='${filterValue}')]`
453
+ : `${basePath}[?(@.${filterKey}='${filterValue}')]`;
454
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-diff-ts",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "A diff tool for JavaScript based on https://www.npmjs.com/package/diff-json written in TypeScript.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",