recursive-diff
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -16,2 +16,3 @@ - 0.1.1 - Added support of **null** value for any key in a object. | ||
- 1.0.6 - Added support for including `oldVal` in the diff object using an optional third parameter in the `getDiff` API | ||
- 1.0.7 - Improving docs | ||
@@ -18,0 +19,0 @@ [1]: https://github.com/IsabellaCerbino |
{ | ||
"name": "recursive-diff", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Find diff between any two variables where variables be any valid JavaScript data type like string, numeric, array or object", | ||
@@ -5,0 +5,0 @@ "main": "src/recursive-diff.js", |
@@ -22,3 +22,3 @@ [![NPM Version][npm-image]][npm-url] | ||
const rdiff = require('recursive-diff'); | ||
const x = { | ||
const initialVal = { | ||
a: { | ||
@@ -30,3 +30,3 @@ b: 1, | ||
} | ||
const y = { | ||
const changedVal = { | ||
a: { | ||
@@ -38,5 +38,5 @@ b: 2, | ||
const diff = rdiff.getDiff(x, y); | ||
const diff = rdiff.getDiff(initialVal, changedVal); | ||
/*** | ||
Diff of x and y is: [ | ||
Diff of initialVal and changedVal is: [ | ||
{ | ||
@@ -69,4 +69,4 @@ "path": [ | ||
const c = rdiff.applyDiff(x, diff); | ||
assert.deepEqual(c, y); | ||
const c = rdiff.applyDiff(initialVal, diff); | ||
assert.deepEqual(c, changedVal); | ||
@@ -77,11 +77,12 @@ ``` | ||
- **`getDiff(x, y)`:** `getDiff` takes two arguments `x` and `y` and return their diff. `x` and `y` can be Array/Object or even other primitive types such as number, boolean or string. | ||
- **`getDiff(oldVal, newVal, keepOldVal?)`:** `getDiff` takes following parameters | ||
- `oldVal (required)`: initial value, can be Array/Object or even primitive type such as number, boolean or string | ||
- `newVal (required)`: changed value ( required ), can be Array/Object or even primitive type such as number, boolean or string | ||
- `keepOldValueInDiff (optional)`: boolean parameter which if set to true, every diff value will have an additional property `oldValue` which will denote the old value before mutation | ||
> Notes: `getDiff` also takes a third optional boolen parameter, `keepOldValueInDiff` which if set to true, every diff value will have an additional property `oldValue` which will denote the old value before mutation. | ||
- **`applyDiff (oldVal, diff, visitorCallbackFn?)`** `applyDiff` takes three arguments: | ||
- `oldVal (required)`: original value, | ||
- `diff (required)`: diff returned by `getDiff` API | ||
- `visitorCallbackFn (optional)`: This callback function is called at each depth level while applying the diff. It can be used to mark the mutation path with some meta properties eg: `{ isMutated: true }`. For more details, please check the examples directory of this repo. | ||
- **`applyDiff (x, diff, visitorCallbackFn)`** `applyDiff` takes three arguments: | ||
- x: original value, | ||
- diff: diff returned by `getDiff` API | ||
- visitorCallbackFn (optional): This callback function is called at each depth level while applying the diff. It can be used to mark the mutation path with some meta properties eg: `{ isMutated: true }`. For more details, please check the examples directory of this repo. | ||
## Using recursive diff library in Node | ||
@@ -94,7 +95,7 @@ | ||
const diff = require('recursive-diff'); | ||
const ob1 = {a:1}; | ||
const ob2 = {a:2}; | ||
const delta = diff.getDiff(ob1,ob2); | ||
const ob3 = diff.applyDiff(ob1, delta); | ||
assert.deepEqual(ob3, ob2); | ||
const oldVal = {a:1}; | ||
const newVal = {a:2}; | ||
const delta = diff.getDiff(oldVal, newVal); | ||
const ob3 = diff.applyDiff(oldVal, delta); | ||
assert.deepEqual(ob3, newVal); | ||
@@ -109,6 +110,6 @@ ``` | ||
<script type="text/javascript"> | ||
const ob1 = {a:1}; | ||
const ob2 = {a:2}; | ||
const delta = recursiveDiff.getDiff(ob1,ob2); | ||
const ob3 = recursiveDiff.applyDiff(ob1, delta); //expect ob3 is deep equal to ob2 | ||
const oldVal = { a: 1 }; | ||
const newVal = { a: 2 }; | ||
const delta = recursiveDiff.getDiff(oldVal, newVal); | ||
const ob3 = recursiveDiff.applyDiff(oldVal, delta); //expect ob3 is deep equal to newVal | ||
</script> | ||
@@ -120,8 +121,8 @@ | ||
const x = [1, 2]; | ||
const y = [2, 3, 4]; | ||
const diff:rdiffResult[] = getDiff(x, y); | ||
const oldVal = [1, 2]; | ||
const newVal = [2, 3, 4]; | ||
const diff:rdiffResult[] = getDiff(oldVal, newVal); | ||
console.log('diff', diff); | ||
const final = applyDiff(x, diff); | ||
console.log('applydiff', final); | ||
const final = applyDiff(oldVal, diff); | ||
console.log('applydiff', final); // final should deep equal to newVal | ||
@@ -128,0 +129,0 @@ ## Tests |
32627
248