json-diff-ts 1.2.2 → 1.2.3

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
@@ -280,6 +280,7 @@ Twitter: [@cglessner](https://twitter.com/cglessner)
280
280
 
281
281
  ## Changelog
282
282
 
283
+ - v1.2.3 Update outdated dependencies; update TypeScript version to 4.5.2
283
284
  - v1.2.2 Add support for functions to resove object keys (PR by [Abraxxa](https://github.com/abraxxa))
284
285
 
285
286
  ## Credits
package/lib/index.js CHANGED
@@ -1,7 +1,14 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./jsonDiff"));
7
- __export(require("./jsonCompare"));
13
+ __exportStar(require("./jsonDiff"), exports);
14
+ __exportStar(require("./jsonCompare"), exports);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compare = exports.applyChangelist = exports.enrich = exports.createContainer = exports.createValue = exports.CompareOperation = void 0;
3
4
  const lodash_1 = require("lodash");
4
5
  const jsonDiff_1 = require("./jsonDiff");
5
6
  var CompareOperation;
@@ -7,54 +8,57 @@ var CompareOperation;
7
8
  CompareOperation["CONTAINER"] = "CONTAINER";
8
9
  CompareOperation["UNCHANGED"] = "UNCHANGED";
9
10
  })(CompareOperation = exports.CompareOperation || (exports.CompareOperation = {}));
10
- exports.createValue = (value) => ({ type: CompareOperation.UNCHANGED, value });
11
- exports.createContainer = (value) => ({
11
+ const createValue = (value) => ({ type: CompareOperation.UNCHANGED, value });
12
+ exports.createValue = createValue;
13
+ const createContainer = (value) => ({
12
14
  type: CompareOperation.CONTAINER,
13
15
  value
14
16
  });
15
- exports.enrich = (object) => {
16
- const objectType = jsonDiff_1.getTypeOfObj(object);
17
+ exports.createContainer = createContainer;
18
+ const enrich = (object) => {
19
+ const objectType = (0, jsonDiff_1.getTypeOfObj)(object);
17
20
  switch (objectType) {
18
21
  case 'Object':
19
- return lodash_1.keys(object)
20
- .map((key) => ({ key, value: exports.enrich(object[key]) }))
22
+ return (0, lodash_1.keys)(object)
23
+ .map((key) => ({ key, value: (0, exports.enrich)(object[key]) }))
21
24
  .reduce((accumulator, entry) => {
22
25
  accumulator.value[entry.key] = entry.value;
23
26
  return accumulator;
24
- }, exports.createContainer({}));
27
+ }, (0, exports.createContainer)({}));
25
28
  case 'Array':
26
- return lodash_1.chain(object)
27
- .map(value => exports.enrich(value))
29
+ return (0, lodash_1.chain)(object)
30
+ .map(value => (0, exports.enrich)(value))
28
31
  .reduce((accumulator, value) => {
29
32
  accumulator.value.push(value);
30
33
  return accumulator;
31
- }, exports.createContainer([]))
34
+ }, (0, exports.createContainer)([]))
32
35
  .value();
33
36
  case 'Function':
34
37
  return undefined;
35
38
  case 'Date':
36
39
  default:
37
40
  // Primitive value
38
- return exports.createValue(object);
41
+ return (0, exports.createValue)(object);
39
42
  }
40
43
  };
41
- exports.applyChangelist = (object, changelist) => {
42
- lodash_1.chain(changelist)
43
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, '$.', '.') })))
44
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, /(\[(?<array>\d)\]\.)/g, 'ARRVAL_START$<array>ARRVAL_END') })))
45
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, /(?<dot>\.)/g, '.value$<dot>') })))
46
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, /\./, '') })))
47
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, /ARRVAL_START/g, '.value[') })))
48
- .map(entry => (Object.assign(Object.assign({}, entry), { path: lodash_1.replace(entry.path, /ARRVAL_END/g, '].value.') })))
44
+ exports.enrich = enrich;
45
+ const applyChangelist = (object, changelist) => {
46
+ (0, lodash_1.chain)(changelist)
47
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, '$.', '.') })))
48
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, /(\[(?<array>\d)\]\.)/g, 'ARRVAL_START$<array>ARRVAL_END') })))
49
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, /(?<dot>\.)/g, '.value$<dot>') })))
50
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, /\./, '') })))
51
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, /ARRVAL_START/g, '.value[') })))
52
+ .map(entry => (Object.assign(Object.assign({}, entry), { path: (0, lodash_1.replace)(entry.path, /ARRVAL_END/g, '].value.') })))
49
53
  .value()
50
54
  .forEach(entry => {
51
55
  switch (entry.type) {
52
56
  case jsonDiff_1.Operation.ADD:
53
57
  case jsonDiff_1.Operation.UPDATE:
54
- lodash_1.set(object, entry.path, { type: entry.type, value: entry.value, oldValue: entry.oldValue });
58
+ (0, lodash_1.set)(object, entry.path, { type: entry.type, value: entry.value, oldValue: entry.oldValue });
55
59
  break;
56
60
  case jsonDiff_1.Operation.REMOVE:
57
- lodash_1.set(object, entry.path, { type: entry.type, value: undefined, oldValue: entry.value });
61
+ (0, lodash_1.set)(object, entry.path, { type: entry.type, value: undefined, oldValue: entry.value });
58
62
  break;
59
63
  default:
60
64
  throw new Error();
@@ -62,6 +66,8 @@ exports.applyChangelist = (object, changelist) => {
62
66
  });
63
67
  return object;
64
68
  };
65
- exports.compare = (oldObject, newObject) => {
66
- return exports.applyChangelist(exports.enrich(oldObject), jsonDiff_1.flattenChangeset(jsonDiff_1.diff(oldObject, newObject)));
69
+ exports.applyChangelist = applyChangelist;
70
+ const compare = (oldObject, newObject) => {
71
+ return (0, exports.applyChangelist)((0, exports.enrich)(oldObject), (0, jsonDiff_1.flattenChangeset)((0, jsonDiff_1.diff)(oldObject, newObject)));
67
72
  };
73
+ exports.compare = compare;
package/lib/jsonDiff.d.ts CHANGED
@@ -26,6 +26,6 @@ export interface IFlatChange {
26
26
  value?: any;
27
27
  oldValue?: any;
28
28
  }
29
- export declare const flattenChangeset: (obj: IChange | Changeset, path?: string, embeddedKey?: string | FunctionKey) => IFlatChange[];
29
+ export declare const flattenChangeset: (obj: Changeset | IChange, path?: string, embeddedKey?: string | FunctionKey) => IFlatChange[];
30
30
  export declare const unflattenChanges: (changes: IFlatChange | IFlatChange[]) => IChange[];
31
31
  export {};
package/lib/jsonDiff.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unflattenChanges = exports.flattenChangeset = exports.Operation = exports.revertChangeset = exports.applyChangeset = exports.diff = exports.getTypeOfObj = void 0;
3
4
  const lodash_1 = require("lodash");
4
- exports.getTypeOfObj = (obj) => {
5
+ const getTypeOfObj = (obj) => {
5
6
  if (typeof obj === 'undefined') {
6
7
  return 'undefined';
7
8
  }
@@ -10,14 +11,15 @@ exports.getTypeOfObj = (obj) => {
10
11
  }
11
12
  return Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
12
13
  };
14
+ exports.getTypeOfObj = getTypeOfObj;
13
15
  const getKey = (path) => {
14
16
  const left = path[path.length - 1];
15
17
  return left != null ? left : '$root';
16
18
  };
17
19
  const compare = (oldObj, newObj, path, embeddedObjKeys, keyPath) => {
18
20
  let changes = [];
19
- const typeOfOldObj = exports.getTypeOfObj(oldObj);
20
- const typeOfNewObj = exports.getTypeOfObj(newObj);
21
+ const typeOfOldObj = (0, exports.getTypeOfObj)(oldObj);
22
+ const typeOfNewObj = (0, exports.getTypeOfObj)(newObj);
21
23
  // if type of object changes, consider it as old obj has been deleted and a new object has been added
22
24
  if (typeOfOldObj !== typeOfNewObj) {
23
25
  changes.push({ type: Operation.REMOVE, key: getKey(path), value: oldObj });
@@ -64,7 +66,7 @@ const compareObject = (oldObj, newObj, path, embeddedObjKeys, keyPath, skipPath
64
66
  let changes = [];
65
67
  const oldObjKeys = Object.keys(oldObj);
66
68
  const newObjKeys = Object.keys(newObj);
67
- const intersectionKeys = lodash_1.intersection(oldObjKeys, newObjKeys);
69
+ const intersectionKeys = (0, lodash_1.intersection)(oldObjKeys, newObjKeys);
68
70
  for (k of intersectionKeys) {
69
71
  newPath = path.concat([k]);
70
72
  newKeyPath = skipPath ? keyPath : keyPath.concat([k]);
@@ -73,7 +75,7 @@ const compareObject = (oldObj, newObj, path, embeddedObjKeys, keyPath, skipPath
73
75
  changes = changes.concat(diffs);
74
76
  }
75
77
  }
76
- const addedKeys = lodash_1.difference(newObjKeys, oldObjKeys);
78
+ const addedKeys = (0, lodash_1.difference)(newObjKeys, oldObjKeys);
77
79
  for (k of addedKeys) {
78
80
  newPath = path.concat([k]);
79
81
  newKeyPath = skipPath ? keyPath : keyPath.concat([k]);
@@ -83,7 +85,7 @@ const compareObject = (oldObj, newObj, path, embeddedObjKeys, keyPath, skipPath
83
85
  value: newObj[k]
84
86
  });
85
87
  }
86
- const deletedKeys = lodash_1.difference(oldObjKeys, newObjKeys);
88
+ const deletedKeys = (0, lodash_1.difference)(oldObjKeys, newObjKeys);
87
89
  for (k of deletedKeys) {
88
90
  newPath = path.concat([k]);
89
91
  newKeyPath = skipPath ? keyPath : keyPath.concat([k]);
@@ -133,7 +135,7 @@ const getObjectKey = (embeddedObjKeys, keyPath) => {
133
135
  const convertArrayToObj = (arr, uniqKey) => {
134
136
  let obj = {};
135
137
  if (uniqKey !== '$index') {
136
- obj = lodash_1.keyBy(arr, uniqKey);
138
+ obj = (0, lodash_1.keyBy)(arr, uniqKey);
137
139
  }
138
140
  else {
139
141
  for (let i = 0; i < arr.length; i++) {
@@ -217,9 +219,9 @@ const applyArrayChange = (arr, change) => (() => {
217
219
  element = arr[subchange.key];
218
220
  }
219
221
  else {
220
- element = 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());
221
223
  }
222
- result.push(exports.applyChangeset(element, subchange.changes));
224
+ result.push((0, exports.applyChangeset)(element, subchange.changes));
223
225
  }
224
226
  }
225
227
  return result;
@@ -229,7 +231,7 @@ const applyBranchChange = (obj, change) => {
229
231
  return applyArrayChange(obj, change);
230
232
  }
231
233
  else {
232
- return exports.applyChangeset(obj, change.changes);
234
+ return (0, exports.applyChangeset)(obj, change.changes);
233
235
  }
234
236
  };
235
237
  const revertLeafChange = (obj, change, embeddedKey = '$index') => {
@@ -255,9 +257,9 @@ const revertArrayChange = (arr, change) => (() => {
255
257
  element = arr[+subchange.key];
256
258
  }
257
259
  else {
258
- element = 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);
259
261
  }
260
- result.push(exports.revertChangeset(element, subchange.changes));
262
+ result.push((0, exports.revertChangeset)(element, subchange.changes));
261
263
  }
262
264
  }
263
265
  return result;
@@ -267,11 +269,12 @@ const revertBranchChange = (obj, change) => {
267
269
  return revertArrayChange(obj, change);
268
270
  }
269
271
  else {
270
- return exports.revertChangeset(obj, change.changes);
272
+ return (0, exports.revertChangeset)(obj, change.changes);
271
273
  }
272
274
  };
273
- exports.diff = (oldObj, newObj, embeddedObjKeys) => compare(oldObj, newObj, [], embeddedObjKeys, []);
274
- exports.applyChangeset = (obj, changeset) => {
275
+ const diff = (oldObj, newObj, embeddedObjKeys) => compare(oldObj, newObj, [], embeddedObjKeys, []);
276
+ exports.diff = diff;
277
+ const applyChangeset = (obj, changeset) => {
275
278
  if (changeset) {
276
279
  changeset.forEach(change => (change.value !== null && change.value !== undefined) || change.type === Operation.REMOVE
277
280
  ? applyLeafChange(obj, change, change.embeddedKey)
@@ -279,7 +282,8 @@ exports.applyChangeset = (obj, changeset) => {
279
282
  }
280
283
  return obj;
281
284
  };
282
- exports.revertChangeset = (obj, changeset) => {
285
+ exports.applyChangeset = applyChangeset;
286
+ const revertChangeset = (obj, changeset) => {
283
287
  if (changeset) {
284
288
  changeset
285
289
  .reverse()
@@ -287,15 +291,16 @@ exports.revertChangeset = (obj, changeset) => {
287
291
  }
288
292
  return obj;
289
293
  };
294
+ exports.revertChangeset = revertChangeset;
290
295
  var Operation;
291
296
  (function (Operation) {
292
297
  Operation["REMOVE"] = "REMOVE";
293
298
  Operation["ADD"] = "ADD";
294
299
  Operation["UPDATE"] = "UPDATE";
295
300
  })(Operation = exports.Operation || (exports.Operation = {}));
296
- exports.flattenChangeset = (obj, path = '$', embeddedKey) => {
301
+ const flattenChangeset = (obj, path = '$', embeddedKey) => {
297
302
  if (Array.isArray(obj)) {
298
- return obj.reduce((memo, change) => [...memo, ...exports.flattenChangeset(change, path, embeddedKey)], []);
303
+ return obj.reduce((memo, change) => [...memo, ...(0, exports.flattenChangeset)(change, path, embeddedKey)], []);
299
304
  }
300
305
  else {
301
306
  if (obj.changes || embeddedKey) {
@@ -306,17 +311,18 @@ exports.flattenChangeset = (obj, path = '$', embeddedKey) => {
306
311
  ? path
307
312
  : `${path}[?(@.${embeddedKey}='${obj.key}')]`
308
313
  : (path = `${path}.${obj.key}`);
309
- return exports.flattenChangeset(obj.changes || obj, path, obj.embeddedKey);
314
+ return (0, exports.flattenChangeset)(obj.changes || obj, path, obj.embeddedKey);
310
315
  }
311
316
  else {
312
- const valueType = exports.getTypeOfObj(obj.value);
317
+ const valueType = (0, exports.getTypeOfObj)(obj.value);
313
318
  return [
314
319
  Object.assign(Object.assign({}, obj), { path: valueType === 'Object' || path.endsWith(`[${obj.key}]`) ? path : `${path}.${obj.key}`, valueType })
315
320
  ];
316
321
  }
317
322
  }
318
323
  };
319
- exports.unflattenChanges = (changes) => {
324
+ exports.flattenChangeset = flattenChangeset;
325
+ const unflattenChanges = (changes) => {
320
326
  if (!Array.isArray(changes)) {
321
327
  changes = [changes];
322
328
  }
@@ -431,3 +437,4 @@ exports.unflattenChanges = (changes) => {
431
437
  });
432
438
  return changesArr;
433
439
  };
440
+ exports.unflattenChanges = unflattenChanges;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-diff-ts",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
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,14 +37,14 @@
37
37
  },
38
38
  "homepage": "https://github.com/ltwlf/json-diff-ts#readme",
39
39
  "devDependencies": {
40
- "@types/jest": "^24.0.23",
40
+ "@types/jest": "^27.0.3",
41
41
  "@types/lodash": "^4.14.149",
42
42
  "jest": "^24.9.0",
43
- "prettier": "^1.19.1",
43
+ "prettier": "^2.5.1",
44
44
  "ts-jest": "^24.2.0",
45
- "tslint": "^5.20.1",
45
+ "tslint": "^6.1.3",
46
46
  "tslint-config-prettier": "^1.18.0",
47
- "typescript": "^3.7.2"
47
+ "typescript": "^4.5.2"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "lodash": "^4.x"