datum-merge 1.3.0 → 1.5.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datum-merge",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "author": "Rohit Kulkarni <rohk@live.com>",
5
5
  "license": "MIT",
6
6
  "description": "Simplified diff and merging for deeply nested objects",
@@ -27,7 +27,7 @@
27
27
  "scripts": {
28
28
  "lint": "npx eslint src",
29
29
  "pretty": "prettier -c ./src",
30
- "scantask": "findstr /s /i todo src\\*.*",
30
+ "scantask": "findstr /s /i todo src\\*.ts",
31
31
  "test": "jest",
32
32
  "build:dts": "tsc --outDir dist/dts --declaration --declarationMap --emitDeclarationOnly",
33
33
  "build:cjs": "tsc --outDir dist/cjs",
@@ -43,26 +43,26 @@
43
43
  },
44
44
  "bugs": "https://github.com/therohk/datum-merge/issues",
45
45
  "devDependencies": {
46
- "@types/node": "^16.11.6",
46
+ "@types/node": "^25.2.3",
47
47
  "@typescript-eslint/eslint-plugin": "8.53.1",
48
48
  "@typescript-eslint/parser": "8.53.1",
49
- "@types/lodash": "^4.17.23",
49
+ "@types/lodash": "^4.17.24",
50
50
  "@types/lodash-es": "^4.17.12",
51
51
  "@types/jest": "^29.5.2",
52
52
  "eslint": "^8.57.1",
53
53
  "prettier": "^3.1.0",
54
- "npm-run-all2": "^6.2.0",
55
- "dpdm": "^3.14.0",
54
+ "npm-run-all2": "^6.2.6",
55
+ "dpdm": "^4.2.0",
56
56
  "jest": "^29.7.0",
57
57
  "ts-jest": "^29.1.0",
58
58
  "ts-node": "^10.9.2",
59
- "tslib": "2.4.0",
60
- "typescript": "5.4.5",
61
- "lodash": "^4.17.23",
59
+ "tslib": "2.8.1",
60
+ "typescript": "5.7.3",
61
+ "lodash": "4.18.1",
62
62
  "esbuild": "0.25.6"
63
63
  },
64
64
  "dependencies": {
65
- "fast-deep-equal": "^3.1.3",
66
- "lodash-es": "^4.17.23"
65
+ "fast-deep-equal": "3.1.3",
66
+ "lodash-es": "4.18.1"
67
67
  }
68
68
  }
@@ -27,6 +27,10 @@ export function createValueKeys<T>(
27
27
  return Object.fromEntries(keys.map((k) => [k, value]));
28
28
  }
29
29
 
30
+ export function shallowEquals(lhs: any, rhs: any): boolean {
31
+ return lhs === rhs;
32
+ }
33
+
30
34
  export function deepEquals(lhs: any, rhs: any): boolean {
31
35
  if (lhs === rhs)
32
36
  return true;
@@ -1,11 +1,26 @@
1
+ # datum-diff
2
+
3
+ module recently published as a standalone package [datum-diff](https://www.npmjs.com/package/datum-diff)
4
+
5
+ works as a drop-in replacement for the deprecated package [deep-diff](https://www.npmjs.com/package/deep-diff)
6
+
7
+ simply use `datum-diff` instead of the `datum-merge` libary in the examples below .
8
+
9
+ for usage on browser environments:
10
+ ```
11
+ <script src="https://unpkg.com/datum-diff@1.0.3/dist-diff/umd/deep-diff.min.js"></script>
12
+ ```
13
+
1
14
  ## sources
2
15
 
3
- the source code of the [deep-diff](https://github.com/flitbit/diff) js library has been migrated to typescript .
16
+ the source code of the [deep-diff](https://www.npmjs.com/package/deep-diff) js library has been migrated to typescript .
4
17
  it remains unmaintained and contains some serious bugs that made it unreliable for this project .
5
18
 
6
- * tag:v1.0.2 [flitbit/diff](https://github.com/flitbit/diff/blob/master/index.js)
7
- * tag:v1.0.2 [flitbit/diff/tests](https://github.com/flitbit/diff/blob/master/test/tests.js)
19
+ * tag:v1.0.2 flitbit/diff (was github.com/flitbit/diff/blob/master/index.js)
20
+ * tag:v1.0.2 flitbit/diff/tests (was github.com/flitbit/diff/blob/master/test/tests.js)
8
21
  * tag:v1.0.5 [@types/deep-diff](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/deep-diff/index.d.ts)
22
+ * tag:v1.0.2 flitbit/Readme.md (was github.com/flitbit/diff/blob/master/Readme.md)
23
+ * as of 2026 the links to the original github library are no longer working
9
24
 
10
25
  same interfaces have been exposed to maintain compatibility with the last version .
11
26
 
@@ -36,20 +51,7 @@ const patch: PatchResult[] = diffToPatchLog(customDiff, true);
36
51
  applyPatchLog(patch, target);
37
52
  ```
38
53
 
39
- see the [readme](https://github.com/flitbit/diff/blob/master/Readme.md#api-documentation) in the original library for detailed examples .
40
-
41
- ## datum-diff
42
-
43
- module recently published as a standalone package [datum-diff](https://www.npmjs.com/package/datum-diff)
44
-
45
- works as a drop-in replacement for the package [deep-diff](https://www.npmjs.com/package/deep-diff)
46
-
47
- simply use `datum-diff` instead of the `datum-merge` libary in the examples above .
48
-
49
- for usage on browser environments:
50
- ```
51
- <script src="https://unpkg.com/datum-diff@1.0.3/dist-diff/umd/deep-diff.min.js"></script>
52
- ```
54
+ see the [readme](dd-README.md) migrated from the original library for detailed examples .
53
55
 
54
56
  ## changes
55
57
 
@@ -0,0 +1,238 @@
1
+ # deep-diff
2
+
3
+ **deep-diff** is a javascript/node.js module providing utility functions for determining the structural differences between objects and includes some utilities for applying differences across objects.
4
+
5
+ [![NPM](https://nodei.co/npm/datum-diff.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/datum-diff)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install datum-diff
11
+ ```
12
+
13
+ ## Features
14
+
15
+ * Get the structural differences between two objects.
16
+ * Observe the structural differences between two objects.
17
+ * When structural differences represent change, apply change from one object to another.
18
+ * When structural differences represent change, selectively apply change from one object to another.
19
+
20
+ ## Simple Examples
21
+
22
+ In order to describe differences, change revolves around an `origin` object. For consistency, the `origin` object is always the operand on the `left-hand-side` of operations. The `comparand`, which may contain changes, is always on the `right-hand-side` of operations.
23
+
24
+ ``` javascript
25
+ const diff = require('datum-diff').diff;
26
+
27
+ const lhs = {
28
+ name: 'my object',
29
+ description: 'it\'s an object!',
30
+ details: {
31
+ it: 'has',
32
+ an: 'array',
33
+ with: ['a', 'few', 'elements']
34
+ }
35
+ };
36
+
37
+ const rhs = {
38
+ name: 'updated object',
39
+ description: 'it\'s an object!',
40
+ details: {
41
+ it: 'has',
42
+ an: 'array',
43
+ with: ['a', 'few', 'more', 'elements', { than: 'before' }]
44
+ }
45
+ };
46
+
47
+ const differences = diff(lhs, rhs);
48
+ ```
49
+
50
+ The code snippet above would result in the following structure describing the differences:
51
+
52
+ ``` javascript
53
+ [ { kind: 'E',
54
+ path: [ 'name' ],
55
+ lhs: 'my object',
56
+ rhs: 'updated object' },
57
+ { kind: 'E',
58
+ path: [ 'details', 'with', 2 ],
59
+ lhs: 'elements',
60
+ rhs: 'more' },
61
+ { kind: 'A',
62
+ path: [ 'details', 'with' ],
63
+ index: 3,
64
+ item: { kind: 'N', rhs: 'elements' } },
65
+ { kind: 'A',
66
+ path: [ 'details', 'with' ],
67
+ index: 4,
68
+ item: { kind: 'N', rhs: { than: 'before' } } }, ]
69
+ ```
70
+
71
+ ### Differences
72
+
73
+ Differences are reported as one or more change records. Change records have the following structure:
74
+
75
+ * `kind` - indicates the kind of change; will be one of the following:
76
+ * `N` - indicates a newly added property/element
77
+ * `D` - indicates a property/element was deleted
78
+ * `E` - indicates a property/element was edited
79
+ * `A` - indicates a change occurred within an array
80
+ * `path` - the property path (from the left-hand-side root)
81
+ * `lhs` - the value on the left-hand-side of the comparison (undefined if kind === 'N')
82
+ * `rhs` - the value on the right-hand-side of the comparison (undefined if kind === 'D')
83
+ * `index` - when kind === 'A', indicates the array index where the change occurred
84
+ * `item` - when kind === 'A', contains a nested change record indicating the change that occurred at the array index
85
+
86
+ Change records are generated for all structural differences between `origin` and `comparand`. The methods only consider an object's own properties and array elements; those inherited from an object's prototype chain are not considered.
87
+
88
+ Changes to arrays are recorded simplistically. We care most about the shape of the structure; therefore we don't take the time to determine if an object moved from one slot in the array to another. Instead, we only record the structural
89
+ differences. If the structural differences are applied from the `comparand` to the `origin` then the two objects will compare as "deep equal" using most `isEqual` implementations such as found in [lodash](https://lodash.com/) or [underscore](http://underscorejs.org/).
90
+
91
+ ### Changes
92
+
93
+ When two objects differ, you can observe the differences as they are calculated and selectively apply those changes to the origin object (left-hand-side).
94
+
95
+ ``` javascript
96
+ const observableDiff = require('datum-diff').observableDiff;
97
+ const applyChange = require('datum-diff').applyChange;
98
+
99
+ const lhs = {
100
+ name: 'my object',
101
+ description: 'it\'s an object!',
102
+ details: {
103
+ it: 'has',
104
+ an: 'array',
105
+ with: ['a', 'few', 'elements']
106
+ }
107
+ };
108
+
109
+ const rhs = {
110
+ name: 'updated object',
111
+ description: 'it\'s an object!',
112
+ details: {
113
+ it: 'has',
114
+ an: 'array',
115
+ with: ['a', 'few', 'more', 'elements', { than: 'before' }]
116
+ }
117
+ };
118
+
119
+ observableDiff(lhs, rhs, function (d) {
120
+ // Apply all changes except to the name property...
121
+ if (d.path.at(-1) !== 'name') {
122
+ applyChange(lhs, rhs, d);
123
+ }
124
+ });
125
+ ```
126
+
127
+ ## API Documentation
128
+
129
+ A standard import of the library is assumed in all of the code examples. The import results in an object having the following public properties:
130
+
131
+ * `diff(lhs, rhs[, options, acc])` &mdash; calculates the differences between two objects, optionally using the specified accumulator.
132
+ * `observableDiff(lhs, rhs, observer[, options])` &mdash; calculates the differences between two objects and reports each to an observer function.
133
+ * `applyDiff(target, source, filter)` &mdash; applies any structural differences from a source object to a target object, optionally filtering each difference.
134
+ * `applyChange(target, unused, change)` &mdash; applies a single change record to a target object.
135
+ * `revertChange(target, unused, change)` reverts a single change record to a target object.
136
+
137
+ ### `diff`
138
+
139
+ The `diff` function calculates the difference between two objects.
140
+
141
+ #### Arguments
142
+
143
+ * `lhs` - the left-hand operand; the origin object.
144
+ * `rhs` - the right-hand operand; the object being compared structurally with the origin object.
145
+ * `options` - A configuration object that can have the following properties:
146
+ * `prefilter` - function that determines whether difference analysis should continue down the object graph. This function can also replace the `options` object in the parameters for backward compatibility.
147
+ * `normalize` - function that pre-processes every _leaf_ of the tree.
148
+ * `acc` - an optional accumulator/array (requirement is that it have a `push` function). Each difference is pushed to the specified accumulator.
149
+
150
+ Returns either an array of changes or, if there are no changes, `undefined`. This was originally chosen so the result would be pass a truthy test:
151
+
152
+ ```javascript
153
+ const changes = diff(obja, objb);
154
+ if (changes) {
155
+ // do something with the changes.
156
+ }
157
+ ```
158
+
159
+ #### Pre-filtering Object Properties
160
+
161
+ The `prefilter`'s signature should be `function(path, key)` and it should return a truthy value for any `path`-`key` combination that should be filtered. If filtered, the difference analysis does no further analysis of on the identified object-property path.
162
+
163
+ ```javascript
164
+ const diff = require('datum-diff');
165
+ const assert = require('assert');
166
+
167
+ const data = {
168
+ issue: 126,
169
+ submittedBy: 'abuzarhamza',
170
+ title: 'readme.md need some additional example prefilter',
171
+ posts: [
172
+ {
173
+ date: '2018-04-16',
174
+ text: `additional example for prefilter for deep-diff would be great.
175
+ stackoverflow.com/questions/38364639`
176
+ }
177
+ ]
178
+ };
179
+
180
+ const clone = JSON.parse(JSON.stringify(data));
181
+ clone.title = 'README.MD needs additional example illustrating how to prefilter';
182
+ clone.disposition = 'completed';
183
+
184
+ const two = diff(data, clone);
185
+ const none = diff(data, clone,
186
+ (path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key)
187
+ );
188
+
189
+ assert.equal(two.length, 2, 'should reflect two differences');
190
+ assert.ok(typeof none === 'undefined', 'should reflect no differences');
191
+ ```
192
+ #### Normalizing object properties
193
+
194
+ The `normalize`'s signature should be `function(path, key, lhs, rhs)` and it should return either a falsy value if no normalization has occured, or a `[lhs, rhs]` array to replace the original values. This step doesn't occur if the path was filtered out in the `prefilter` phase.
195
+
196
+ ```javascript
197
+ const diff = require('datum-diff');
198
+ const assert = require('assert');
199
+
200
+ const data = {
201
+ pull: 149,
202
+ submittedBy: 'saveman71',
203
+ };
204
+
205
+ const clone = JSON.parse(JSON.stringify(data));
206
+ clone.issue = 42;
207
+
208
+ const two = diff(data, clone);
209
+ const none = diff(data, clone, {
210
+ normalize: (path, key, lhs, rhs) => {
211
+ if (lhs === 149) {
212
+ lhs = 42;
213
+ }
214
+ if (rhs === 149) {
215
+ rhs = 42;
216
+ }
217
+ return [lsh, rhs];
218
+ }
219
+ });
220
+
221
+ assert.equal(two.length, 1, 'should reflect one difference');
222
+ assert.ok(typeof none === 'undefined', 'should reflect no difference');
223
+ ```
224
+
225
+ ### `observableDiff`
226
+
227
+ The `observableDiff` function calculates the difference between two objects and reports each to an observer function.
228
+
229
+ #### Argmuments
230
+
231
+ * `lhs` - the left-hand operand; the origin object.
232
+ * `rhs` - the right-hand operand; the object being compared structurally with the origin object.
233
+ * `observer` - The observer to report to.
234
+ * `options` - A configuration object that can have the following properties:
235
+ * `prefilter` - function that determines whether difference analysis should continue down the object graph. This function can also replace the `options` object in the parameters for backward compatibility.
236
+ * `normalize` - function that pre-processes every _leaf_ of the tree.
237
+
238
+ ---
@@ -1,26 +1,26 @@
1
1
  {
2
2
  "name": "datum-diff",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "datum-diff",
9
- "version": "1.0.3",
9
+ "version": "1.0.4",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "@types/jest": "^29.5.2",
13
- "@types/node": "^16.11.6",
13
+ "@types/node": "^25.2.3",
14
14
  "@typescript-eslint/eslint-plugin": "8.53.1",
15
15
  "@typescript-eslint/parser": "8.53.1",
16
16
  "esbuild": "0.25.6",
17
17
  "eslint": "^8.57.1",
18
18
  "jest": "^29.7.0",
19
- "npm-run-all2": "^6.2.0",
19
+ "npm-run-all2": "^6.2.6",
20
20
  "ts-jest": "^29.1.0",
21
21
  "ts-node": "^10.9.2",
22
- "tslib": "2.4.0",
23
- "typescript": "5.4.5"
22
+ "tslib": "2.8.1",
23
+ "typescript": "5.7.3"
24
24
  },
25
25
  "engines": {
26
26
  "node": ">=16",
@@ -1821,11 +1821,14 @@
1821
1821
  }
1822
1822
  },
1823
1823
  "node_modules/@types/node": {
1824
- "version": "16.18.126",
1825
- "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.126.tgz",
1826
- "integrity": "sha512-OTcgaiwfGFBKacvfwuHzzn1KLxH/er8mluiy8/uM3sGXHaRe73RrSIj01jow9t4kJEW633Ov+cOexXeiApTyAw==",
1824
+ "version": "25.2.3",
1825
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-25.2.3.tgz",
1826
+ "integrity": "sha512-m0jEgYlYz+mDJZ2+F4v8D1AyQb+QzsNqRuI7xg1VQX/KlKS0qT9r1Mo16yo5F/MtifXFgaofIFsdFMox2SxIbQ==",
1827
1827
  "dev": true,
1828
- "license": "MIT"
1828
+ "license": "MIT",
1829
+ "dependencies": {
1830
+ "undici-types": "~7.16.0"
1831
+ }
1829
1832
  },
1830
1833
  "node_modules/@types/stack-utils": {
1831
1834
  "version": "2.0.3",
@@ -5682,9 +5685,9 @@
5682
5685
  }
5683
5686
  },
5684
5687
  "node_modules/tslib": {
5685
- "version": "2.4.0",
5686
- "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
5687
- "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==",
5688
+ "version": "2.8.1",
5689
+ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
5690
+ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
5688
5691
  "dev": true,
5689
5692
  "license": "0BSD"
5690
5693
  },
@@ -5725,9 +5728,9 @@
5725
5728
  }
5726
5729
  },
5727
5730
  "node_modules/typescript": {
5728
- "version": "5.4.5",
5729
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz",
5730
- "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==",
5731
+ "version": "5.7.3",
5732
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz",
5733
+ "integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==",
5731
5734
  "dev": true,
5732
5735
  "license": "Apache-2.0",
5733
5736
  "bin": {
@@ -5738,6 +5741,13 @@
5738
5741
  "node": ">=14.17"
5739
5742
  }
5740
5743
  },
5744
+ "node_modules/undici-types": {
5745
+ "version": "7.16.0",
5746
+ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz",
5747
+ "integrity": "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==",
5748
+ "dev": true,
5749
+ "license": "MIT"
5750
+ },
5741
5751
  "node_modules/update-browserslist-db": {
5742
5752
  "version": "1.1.3",
5743
5753
  "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.3.tgz",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datum-diff",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "author": "Rohit Kulkarni <rohk@live.com>",
5
5
  "license": "MIT",
6
6
  "description": "deep-diff js library rewritten in typescript",
@@ -40,17 +40,17 @@
40
40
  },
41
41
  "bugs": "https://github.com/therohk/datum-merge/issues",
42
42
  "devDependencies": {
43
- "@types/node": "^16.11.6",
43
+ "@types/node": "^25.2.3",
44
44
  "@typescript-eslint/eslint-plugin": "8.53.1",
45
45
  "@typescript-eslint/parser": "8.53.1",
46
46
  "@types/jest": "^29.5.2",
47
47
  "eslint": "^8.57.1",
48
- "npm-run-all2": "^6.2.0",
48
+ "npm-run-all2": "^6.2.6",
49
49
  "jest": "^29.7.0",
50
50
  "ts-jest": "^29.1.0",
51
51
  "ts-node": "^10.9.2",
52
- "tslib": "2.4.0",
53
- "typescript": "5.4.5",
52
+ "tslib": "2.8.1",
53
+ "typescript": "5.7.3",
54
54
  "esbuild": "0.25.6"
55
55
  }
56
56
  }
package/src/index.ts CHANGED
@@ -10,13 +10,15 @@ export { flattenObject, unflattenObject } from "./diff-high";
10
10
  export { type PatchResult } from "./patch-low";
11
11
  export { diffToPatchLog, deepPatchLog } from "./patch-low";
12
12
  export { applyPatchLog, revertPatchLog } from "./patch-low";
13
- export { immutablePatch, getPointerValue } from "./patch-low";
13
+ export { forcePatchLog, getPointerValue } from "./patch-low";
14
+ export { immutablePatch } from "./patch-low";
14
15
 
15
16
  export { UpdateCode } from "./merge-low";
16
17
  export { UpdateCode as MC } from "./merge-low";
17
18
  export { type MergeCode } from "./merge-low";
18
19
  export { updateCodeInfo } from "./merge-high";
19
20
 
21
+ export { mergeVectors } from "./merge-low";
20
22
  export { shallowMerge, immutableMerge } from "./merge-high";
21
23
  export { deepMerge, immutableDeepMerge } from "./merge-high";
22
24
  export { diffFromMerge, patchFromMerge } from "./merge-high";
package/src/merge-low.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { concat, differenceWith, intersectionWith, unionWith, isEqual } from "lodash-es";
1
+ import { concat, differenceWith, intersectionWith, unionWith } from "lodash-es";
2
2
  import { emptyValue, isArrayOfAny, isNullish, isPrimitive, typeOfValue } from "./type-utils";
3
- import { areArraysEqual, deepClone } from "./datum-utils";
3
+ import { areArraysEqual, deepClone, deepEquals } from "./datum-utils";
4
4
 
5
5
  export const UpdateCode = {
6
6
  T: "T", //touch, blank update
@@ -132,14 +132,14 @@ export function mergeVectorField(
132
132
  break;
133
133
  case UpdateCode.B:
134
134
  case UpdateCode.XM:
135
- targetVals = unionWith(target[label], sourceVals, isEqual);
135
+ targetVals = unionWith(target[label], sourceVals, deepEquals);
136
136
  break;
137
137
  // case UpdateCode.D:
138
138
  case UpdateCode.XD:
139
- targetVals = differenceWith(target[label], sourceVals, isEqual);
139
+ targetVals = differenceWith(target[label], sourceVals, deepEquals);
140
140
  break;
141
141
  case UpdateCode.XI:
142
- targetVals = intersectionWith(target[label], sourceVals, isEqual);
142
+ targetVals = intersectionWith(target[label], sourceVals, deepEquals);
143
143
  break;
144
144
  default:
145
145
  return false;
@@ -154,3 +154,25 @@ export function mergeVectorField(
154
154
  target[label] = !changed ? targetVals : deepClone(targetVals);
155
155
  return changed;
156
156
  };
157
+
158
+ export function mergeVectors<T>(
159
+ mergeCode: "XM" | "XI" | "XD" | "XS",
160
+ arr1: readonly T[],
161
+ arr2: readonly T[],
162
+ equalsCond?: (v1: T, v2: T) => boolean,
163
+ ): T[] {
164
+ if (!arr1?.length && !arr2?.length)
165
+ return [];
166
+ equalsCond = equalsCond ?? deepEquals;
167
+ switch (mergeCode) {
168
+ case UpdateCode.XM:
169
+ return unionWith<T>(arr1, arr2, equalsCond);
170
+ case UpdateCode.XI:
171
+ return intersectionWith<T>(arr1, arr2, equalsCond);
172
+ case UpdateCode.XD:
173
+ return differenceWith<T, T>(arr1, arr2, equalsCond);
174
+ case UpdateCode.XS:
175
+ return concat(arr1 ?? [], arr2 ?? []);
176
+ }
177
+ throw new Error("invalid merge code " + mergeCode);
178
+ }
package/src/patch-low.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { get, set, toPath, unset } from "lodash-es";
2
+ import { isNullish, isPrimitive } from "./type-utils";
2
3
  import { deepClone, deepEquals } from "./datum-utils";
3
4
  import { Diff } from "./diff-lib/deep-diff";
4
5
  import { deepDiffLow } from "./diff-high";
@@ -60,6 +61,7 @@ export function deepPatchLog(
60
61
  };
61
62
 
62
63
  /**
64
+ * subset of standard json patch format
63
65
  * reapply patch on a fresh target
64
66
  */
65
67
  export function applyPatchLog(
@@ -78,10 +80,11 @@ export function applyPatchLog(
78
80
  continue;
79
81
  }
80
82
  const targetVal = get(target, difPath);
81
- if (!targetVal && !patchItem.value)
83
+ const sourceVal = patchItem.value;
84
+ if (isNullish(targetVal) && isNullish(sourceVal))
82
85
  continue;
83
- if (!targetVal || !deepEquals(targetVal, patchItem.value)) {
84
- set(target, difPath, patchItem.value);
86
+ if (isNullish(targetVal) || !deepEquals(targetVal, sourceVal)) {
87
+ set(target, difPath, deepClone(sourceVal));
85
88
  changed = true;
86
89
  }
87
90
  }
@@ -100,24 +103,52 @@ export function revertPatchLog(
100
103
  let changed = false;
101
104
  for (const patchItem of patchLog) {
102
105
  const difPath: string[] = asLodashPath(patchItem.path);
106
+ if (patchItem.op === "test")
107
+ continue;
103
108
  if (patchItem.op === "add") {
104
109
  changed = unset(target, difPath) || changed;
105
- } else if (patchItem.op !== "test") {
106
- set(target, difPath, patchItem.prev);
107
- changed = true;
110
+ continue;
108
111
  }
112
+ set(target, difPath, patchItem.prev);
113
+ changed = true;
109
114
  }
110
115
  return changed;
111
116
  }
112
117
 
118
+ /**
119
+ * unofficial json merge-patch format
120
+ * op is ignored and nulls are removed
121
+ */
122
+ export function forcePatchLog(
123
+ patchLog: { path: string, value?: unknown }[],
124
+ target: object = {},
125
+ ): void {
126
+ for (const patchItem of patchLog) {
127
+ const difPath: string[] = patchItem.path.startsWith("/")
128
+ ? asLodashPath(patchItem.path) : toPath(patchItem.path);
129
+ if (!difPath?.length)
130
+ continue;
131
+ const value = patchItem.value;
132
+ if (isNullish(value)) {
133
+ unset(target, difPath);
134
+ } if (isPrimitive(value)) {
135
+ set(target, difPath, value);
136
+ } else {
137
+ set(target, difPath, deepClone(value));
138
+ }
139
+ }
140
+ }
141
+
113
142
  export function immutablePatch(
114
143
  target: object,
115
144
  patchSrc: PatchResult[],
116
- patchDir?: "apply" | "revert",
145
+ patchDir?: "apply" | "revert" | "force",
117
146
  ): object {
118
147
  const targetCopy = deepClone(target ?? {});
119
148
  if (patchDir === "revert") {
120
149
  revertPatchLog(patchSrc, targetCopy);
150
+ } else if (patchDir === "force") {
151
+ forcePatchLog(patchSrc, targetCopy);
121
152
  } else {
122
153
  applyPatchLog(patchSrc, targetCopy);
123
154
  }