You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -168,8 +169,8 @@ observableDiff(lhs, rhs, function (d) {
168
169
169
170
A standard import of `var diff = require('deep-diff')` is assumed in all of the code examples. The import results in an object having the following public properties:
170
171
171
-
* `diff(lhs, rhs, prefilter, acc)` — calculates the differences between two objects, optionally prefiltering elements for comparison, and optionally using the specified accumulator.
172
-
* `observableDiff(lhs, rhs, observer, prefilter)` — calculates the differences between two objects and reports each to an observer function, optionally, prefiltering elements for comparison.
172
+
*`diff(lhs, rhs[, options, acc])`— calculates the differences between two objects, optionally using the specified accumulator.
173
+
*`observableDiff(lhs, rhs, observer[, options])`— calculates the differences between two objects and reports each to an observer function.
173
174
*`applyDiff(target, source, filter)`— applies any structural differences from a source object to a target object, optionally filtering each difference.
174
175
*`applyChange(target, source, change)`— applies a single change record to a target object. NOTE: `source` is unused and may be removed.
175
176
*`revertChange(target, source, change)` reverts a single change record to a target object. NOTE: `source` is unused and may be removed.
@@ -178,11 +179,14 @@ A standard import of `var diff = require('deep-diff')` is assumed in all of the
178
179
179
180
The `diff` function calculates the difference between two objects.
180
181
182
+
181
183
#### Arguments
182
184
183
185
*`lhs` - the left-hand operand; the origin object.
184
186
*`rhs` - the right-hand operand; the object being compared structurally with the origin object.
185
-
* `prefilter` - an optional function that determines whether difference analysis should continue down the object graph.
187
+
*`options` - A configuration object that can have the following properties:
188
+
-`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.
189
+
-`normalize`: function that pre-processes every _leaf_ of the tree.
186
190
*`acc` - an optional accumulator/array (requirement is that it have a `push` function). Each difference is pushed to the specified accumulator.
187
191
188
192
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:
assert.equal(two.length, 2, 'should reflect two differences');
228
232
assert.ok(typeof none ==='undefined', 'should reflect no differences');
229
233
```
234
+
#### Normalizing object properties
235
+
236
+
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.
237
+
238
+
```javascript
239
+
constdiff=require('deep-diff');
240
+
constassert=require('assert');
241
+
242
+
constdata= {
243
+
pull:149,
244
+
submittedBy:'saveman71',
245
+
};
246
+
247
+
constclone=JSON.parse(JSON.stringify(data));
248
+
clone.issue=42;
249
+
250
+
consttwo=diff(data, clone);
251
+
constnone=diff(data, clone, {
252
+
normalize: (path, key, lhs, rhs) => {
253
+
if (lhs ===149) {
254
+
lhs =42;
255
+
}
256
+
if (rhs ===149) {
257
+
rhs =42;
258
+
}
259
+
return [lsh, rhs];
260
+
}
261
+
});
262
+
263
+
assert.equal(two.length, 1, 'should reflect one difference');
264
+
assert.ok(typeof none ==='undefined', 'should reflect no difference');
265
+
```
266
+
267
+
### `observableDiff`
268
+
269
+
The `observableDiff` function calculates the difference between two objects and reports each to an observer function.
270
+
271
+
#### Argmuments
272
+
273
+
*`lhs` - the left-hand operand; the origin object.
274
+
*`rhs` - the right-hand operand; the object being compared structurally with the origin object.
275
+
*`observer` - The observer to report to.
276
+
*`options` - A configuration object that can have the following properties:
277
+
-`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.
278
+
-`normalize`: function that pre-processes every _leaf_ of the tree.
0 commit comments