json-diff-ts 1.2.6 → 1.2.8

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
- - v1.2.6 Improve handling of JSON Path segments that include periods (PR by [EdVinyard](https://github.com/EdVinyard))
284
+
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
@@ -27,7 +27,7 @@ const enrich = (object) => {
27
27
  }, (0, exports.createContainer)({}));
28
28
  case 'Array':
29
29
  return (0, lodash_1.chain)(object)
30
- .map(value => (0, exports.enrich)(value))
30
+ .map((value) => (0, exports.enrich)(value))
31
31
  .reduce((accumulator, value) => {
32
32
  accumulator.value.push(value);
33
33
  return accumulator;
@@ -44,14 +44,14 @@ const enrich = (object) => {
44
44
  exports.enrich = enrich;
45
45
  const applyChangelist = (object, changelist) => {
46
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.') })))
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.') })))
53
53
  .value()
54
- .forEach(entry => {
54
+ .forEach((entry) => {
55
55
  switch (entry.type) {
56
56
  case jsonDiff_1.Operation.ADD:
57
57
  case jsonDiff_1.Operation.UPDATE:
package/lib/jsonDiff.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { Dictionary } from 'lodash';
2
- declare type FunctionKey = (obj: any, getKeyName?: boolean) => any;
1
+ type FunctionKey = (obj: any, getKeyName?: boolean) => any;
3
2
  export declare const getTypeOfObj: (obj: any) => string;
4
- export declare const diff: (oldObj: any, newObj: any, embeddedObjKeys?: Dictionary<string | FunctionKey>) => IChange[];
3
+ export declare const diff: (oldObj: any, newObj: any, embeddedObjKeys?: Record<string, string | FunctionKey>) => IChange[];
5
4
  export declare const applyChangeset: (obj: any, changeset: Changeset) => any;
6
5
  export declare const revertChangeset: (obj: any, changeset: Changeset) => any;
7
6
  export declare enum Operation {
@@ -17,7 +16,7 @@ export interface IChange {
17
16
  oldValue?: any;
18
17
  changes?: IChange[];
19
18
  }
20
- export declare type Changeset = IChange[];
19
+ export type Changeset = IChange[];
21
20
  export interface IFlatChange {
22
21
  type: Operation;
23
22
  key: string;
package/lib/jsonDiff.js CHANGED
@@ -134,7 +134,12 @@ const getObjectKey = (embeddedObjKeys, keyPath) => {
134
134
  };
135
135
  const convertArrayToObj = (arr, uniqKey) => {
136
136
  let obj = {};
137
- if (uniqKey !== '$index') {
137
+ if (uniqKey === '$value') {
138
+ arr.forEach((value) => {
139
+ obj[value] = value;
140
+ });
141
+ }
142
+ else if (uniqKey !== '$index') {
138
143
  obj = (0, lodash_1.keyBy)(arr, uniqKey);
139
144
  }
140
145
  else {
@@ -179,6 +184,9 @@ const removeKey = (obj, key, embeddedKey) => {
179
184
  }
180
185
  };
181
186
  const indexOfItemInArray = (arr, key, value) => {
187
+ if (key === '$value') {
188
+ return arr.indexOf(value);
189
+ }
182
190
  for (let i = 0; i < arr.length; i++) {
183
191
  const item = arr[i];
184
192
  if (item && item[key] ? item[key].toString() === value.toString() : undefined) {
@@ -218,6 +226,12 @@ const applyArrayChange = (arr, change) => (() => {
218
226
  if (change.embeddedKey === '$index') {
219
227
  element = arr[subchange.key];
220
228
  }
229
+ else if (change.embeddedKey === '$value') {
230
+ const index = arr.indexOf(subchange.key);
231
+ if (index !== -1) {
232
+ element = arr[index];
233
+ }
234
+ }
221
235
  else {
222
236
  element = (0, lodash_1.find)(arr, (el) => el[change.embeddedKey].toString() === subchange.key.toString());
223
237
  }
@@ -304,21 +318,31 @@ const flattenChangeset = (obj, path = '$', embeddedKey) => {
304
318
  }
305
319
  else {
306
320
  if (obj.changes || embeddedKey) {
307
- path = embeddedKey
308
- ? embeddedKey === '$index'
309
- ? `${path}[${obj.key}]`
310
- : obj.type === Operation.ADD
311
- ? path
312
- : filterExpression(path, embeddedKey, obj.key)
313
- : (path = append(path, obj.key));
321
+ if (embeddedKey) {
322
+ if (embeddedKey === '$index') {
323
+ path = `${path}[${obj.key}]`;
324
+ }
325
+ else if (embeddedKey === '$value') {
326
+ path = `${path}[?(@='${obj.key}')]`;
327
+ const valueType = (0, exports.getTypeOfObj)(obj.value);
328
+ return [
329
+ Object.assign(Object.assign({}, obj), { path,
330
+ valueType })
331
+ ];
332
+ }
333
+ else if (obj.type !== Operation.ADD) {
334
+ path = `${path}[?(@.${embeddedKey}='${obj.key}')]`;
335
+ }
336
+ }
337
+ else {
338
+ path = `${path}.${obj.key}`;
339
+ }
314
340
  return (0, exports.flattenChangeset)(obj.changes || obj, path, obj.embeddedKey);
315
341
  }
316
342
  else {
317
343
  const valueType = (0, exports.getTypeOfObj)(obj.value);
318
344
  return [
319
- Object.assign(Object.assign({}, obj), { path: valueType === 'Object' || path.endsWith(`[${obj.key}]`)
320
- ? path
321
- : append(path, obj.key), valueType })
345
+ Object.assign(Object.assign({}, obj), { path: valueType === 'Object' || path.endsWith(`[${obj.key}]`) ? path : `${path}.${obj.key}`, valueType })
322
346
  ];
323
347
  }
324
348
  }
@@ -356,7 +380,7 @@ const unflattenChanges = (changes) => {
356
380
  for (let i = 1; i < segments.length; i++) {
357
381
  const segment = segments[i];
358
382
  // check for array
359
- const result = /^(.+)\[\?\(@\.(.+)='(.+)'\)]$|^(.+)\[(\d+)\]/.exec(segment);
383
+ const result = /^(.+)\[\?\(@\.?(.{0,})='(.+)'\)]$|^(.+)\[(\d+)\]/.exec(segment);
360
384
  // array
361
385
  if (result) {
362
386
  let key;
@@ -364,7 +388,7 @@ const unflattenChanges = (changes) => {
364
388
  let arrKey;
365
389
  if (result[1]) {
366
390
  key = result[1];
367
- embeddedKey = result[2];
391
+ embeddedKey = result[2] || '$value';
368
392
  arrKey = result[3];
369
393
  }
370
394
  else {
@@ -440,15 +464,3 @@ const unflattenChanges = (changes) => {
440
464
  return changesArr;
441
465
  };
442
466
  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,12 +1,12 @@
1
1
  {
2
2
  "name": "json-diff-ts",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
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",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
9
+ "format": "prettier --write \"src/**/*.ts\"",
10
10
  "lint": "tslint -p tsconfig.json",
11
11
  "test": "jest --config jest.config.json",
12
12
  "test:watch": "jest --watch --config jest.config.json",
@@ -38,15 +38,15 @@
38
38
  "homepage": "https://github.com/ltwlf/json-diff-ts#readme",
39
39
  "devDependencies": {
40
40
  "@types/jest": "^29.1.1",
41
- "@types/lodash": "^4.14.149",
42
- "jest": "^24.9.0",
41
+ "@types/lodash-es": "^4.14.149",
42
+ "jest": "^29.5.0",
43
43
  "prettier": "^2.5.1",
44
- "ts-jest": "^24.2.0",
44
+ "ts-jest": "^29.0.5",
45
45
  "tslint": "^6.1.3",
46
46
  "tslint-config-prettier": "^1.18.0",
47
47
  "typescript": "^4.5.2"
48
48
  },
49
- "peerDependencies": {
50
- "lodash": "^4.x"
49
+ "dependencies": {
50
+ "lodash": "^4.17.21"
51
51
  }
52
- }
52
+ }