json-diff-ts 1.2.4 → 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
@@ -10,7 +10,7 @@ TypeScript diff tool with support for array keys instead of just indexes and com
10
10
 
11
11
  ### diff
12
12
 
13
- If a key is specified for an embedded array, the diff will be generated based on the objects have same keys.
13
+ If a embedded key is specified for an array, the diff will be generated based on the objects with the same keys.
14
14
 
15
15
  #### Examples:
16
16
 
@@ -41,6 +41,8 @@ newObj = {
41
41
  // Assume children is an array of child object and the child object has 'name' as its primary key
42
42
  // keys can also be hierarchical e.g. {children: 'name', 'children.grandChildren', 'age'}
43
43
  // or use functions that return the key of an object e.g. {children: function(obj) { return obj.key; }}
44
+ // when you use a function flatten can not generate the correct path.
45
+ // to fix this, you can add an additional parameter e.g. (obj, getKeyNameFlag) => {...}. getKeyNameFlag will be true when the diff library try to resolve the key name instead of the key value. You can return a static string or use obj to check which key name you should return. obj will be the first object of the array!
44
46
  diffs = changesets.diff(oldObj, newObj, { children: 'name' });
45
47
 
46
48
  expect(diffs).to.eql([
@@ -279,7 +281,8 @@ Blog: https://blog.leitwolf.io
279
281
  Twitter: [@cglessner](https://twitter.com/cglessner)
280
282
 
281
283
  ## Changelog
282
-
284
+ - v1.2.6 Improve handling of JSON Path segments that include periods (PR by [EdVinyard](https://github.com/EdVinyard))
285
+ - v1.2.5 Patch all dependencies; add support for resolving a key name if a function is used to get the key value
283
286
  - v1.2.4 Fix readme (npm install); update TypeScript and Lodash
284
287
  - v1.2.3 Update outdated dependencies; update TypeScript version to 4.5.2
285
288
  - v1.2.2 Add support for functions to resove object keys (PR by [Abraxxa](https://github.com/abraxxa))
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/lib/jsonDiff.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Dictionary } from 'lodash';
2
- declare type FunctionKey = (obj: any) => any;
2
+ declare type FunctionKey = (obj: any, getKeyName?: boolean) => any;
3
3
  export declare const getTypeOfObj: (obj: any) => string;
4
4
  export declare const diff: (oldObj: any, newObj: any, embeddedObjKeys?: Dictionary<string | FunctionKey>) => IChange[];
5
5
  export declare const applyChangeset: (obj: any, changeset: Changeset) => any;
package/lib/jsonDiff.js CHANGED
@@ -28,7 +28,7 @@ const compare = (oldObj, newObj, path, embeddedObjKeys, keyPath) => {
28
28
  }
29
29
  switch (typeOfOldObj) {
30
30
  case 'Date':
31
- changes = changes.concat(comparePrimitives(oldObj.getTime(), newObj.getTime(), path).map(x => (Object.assign(Object.assign({}, x), { value: new Date(x.value), oldValue: new Date(x.oldValue) }))));
31
+ changes = changes.concat(comparePrimitives(oldObj.getTime(), newObj.getTime(), path).map((x) => (Object.assign(Object.assign({}, x), { value: new Date(x.value), oldValue: new Date(x.oldValue) }))));
32
32
  break;
33
33
  case 'Object':
34
34
  const diffs = compareObject(oldObj, newObj, path, embeddedObjKeys, keyPath);
@@ -108,7 +108,7 @@ const compareArray = (oldObj, newObj, path, embeddedObjKeys, keyPath) => {
108
108
  {
109
109
  type: Operation.UPDATE,
110
110
  key: getKey(path),
111
- embeddedKey: uniqKey,
111
+ embeddedKey: typeof uniqKey === 'function' && uniqKey.length === 2 ? uniqKey(newObj[0], true) : uniqKey,
112
112
  changes: diffs
113
113
  }
114
114
  ];
@@ -219,7 +219,7 @@ const applyArrayChange = (arr, change) => (() => {
219
219
  element = arr[subchange.key];
220
220
  }
221
221
  else {
222
- element = (0, lodash_1.find)(arr, el => el[change.embeddedKey].toString() === subchange.key.toString());
222
+ element = (0, lodash_1.find)(arr, (el) => el[change.embeddedKey].toString() === subchange.key.toString());
223
223
  }
224
224
  result.push((0, exports.applyChangeset)(element, subchange.changes));
225
225
  }
@@ -257,7 +257,7 @@ const revertArrayChange = (arr, change) => (() => {
257
257
  element = arr[+subchange.key];
258
258
  }
259
259
  else {
260
- element = (0, lodash_1.find)(arr, el => el[change.embeddedKey].toString() === subchange.key);
260
+ element = (0, lodash_1.find)(arr, (el) => el[change.embeddedKey].toString() === subchange.key);
261
261
  }
262
262
  result.push((0, exports.revertChangeset)(element, subchange.changes));
263
263
  }
@@ -276,7 +276,7 @@ const diff = (oldObj, newObj, embeddedObjKeys) => compare(oldObj, newObj, [], em
276
276
  exports.diff = diff;
277
277
  const applyChangeset = (obj, changeset) => {
278
278
  if (changeset) {
279
- changeset.forEach(change => (change.value !== null && change.value !== undefined) || change.type === Operation.REMOVE
279
+ changeset.forEach((change) => (change.value !== null && change.value !== undefined) || change.type === Operation.REMOVE
280
280
  ? applyLeafChange(obj, change, change.embeddedKey)
281
281
  : applyBranchChange(obj[change.key], change));
282
282
  }
@@ -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
  }
@@ -327,7 +329,7 @@ const unflattenChanges = (changes) => {
327
329
  changes = [changes];
328
330
  }
329
331
  const changesArr = [];
330
- changes.forEach(change => {
332
+ changes.forEach((change) => {
331
333
  const obj = {};
332
334
  let ptr = obj;
333
335
  const segments = change.path.split(/([^@])\./).reduce((acc, curr, i) => {
@@ -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.4",
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",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "homepage": "https://github.com/ltwlf/json-diff-ts#readme",
39
39
  "devDependencies": {
40
- "@types/jest": "^27.0.3",
40
+ "@types/jest": "^29.1.1",
41
41
  "@types/lodash": "^4.14.149",
42
42
  "jest": "^24.9.0",
43
43
  "prettier": "^2.5.1",