Skip to content

Commit d5fce21

Browse files
authored
Add old value to changing difference (#17)
Closes #15
1 parent 0b4c00e commit d5fce21

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ const diff = require("microdiff").default;
5252
There are three different types of changes. `CREATE`, `REMOVE`, and `CHANGE`.
5353
The `path` property gives a path to the property in the new object (or the old object in the case of `REMOVE`).
5454
Each element in the paths is a key to the next property a level deeper until you get to the property changed, and it is string or a number, depending on whether the object is an Array or Object (Objects with number keys will still be strings).
55-
The `value` property exists in types `CREATE` and `CHANGE`, and it contains the value of the property added/changed.
56-
55+
The `value` (`deletedValue` in the case of `REMOVE`) property exists in types `CREATE`, `CHANGE` and `REMOVE`, and it contains the value of the property added/changed/deleted.
56+
The `oldValue` property exists in the type `CHANGE`, and it contains the old value of the property changed.
5757
# Cycles support
5858

5959
By default cycles are supported, but if you are sure that the object has no cycles (for example if you are parsing JSON) you can disable cycles using the `cyclesFix` option.

index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ interface Difference {
22
type: "CREATE" | "REMOVE" | "CHANGE";
33
path: (string | number)[];
44
value?: any;
5+
oldValue?: any;
6+
deletedValue?: any;
57
}
68
interface Options {
79
cyclesFix: boolean;
@@ -26,6 +28,7 @@ export default function diff(
2628
diffs.push({
2729
type: "REMOVE",
2830
path: [path],
31+
deletedValue: obj[key]
2932
});
3033
continue;
3134
}
@@ -65,6 +68,7 @@ export default function diff(
6568
path: [path],
6669
type: "CHANGE",
6770
value: newObjKey,
71+
oldValue: objKey
6872
});
6973
}
7074
}

0 commit comments

Comments
 (0)