json-diff-ts 1.2.4 → 1.2.5
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 +4 -1
- package/lib/index.js +5 -1
- package/lib/jsonDiff.d.ts +1 -1
- package/lib/jsonDiff.js +6 -6
- package/package.json +2 -2
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
|
|
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([
|
|
@@ -280,6 +282,7 @@ Twitter: [@cglessner](https://twitter.com/cglessner)
|
|
|
280
282
|
|
|
281
283
|
## Changelog
|
|
282
284
|
|
|
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.
|
|
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
|
}
|
|
@@ -327,7 +327,7 @@ const unflattenChanges = (changes) => {
|
|
|
327
327
|
changes = [changes];
|
|
328
328
|
}
|
|
329
329
|
const changesArr = [];
|
|
330
|
-
changes.forEach(change => {
|
|
330
|
+
changes.forEach((change) => {
|
|
331
331
|
const obj = {};
|
|
332
332
|
let ptr = obj;
|
|
333
333
|
const segments = change.path.split(/([^@])\./).reduce((acc, curr, i) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-diff-ts",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
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": "^
|
|
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",
|