viruschidai / diff-json Goto Github PK
View Code? Open in Web Editor NEWA javascript object diff tool
A javascript object diff tool
Just a typo on your change item!
Hi,
The dependency on lodash
increases the bundle size of consuming applications. Could we switch to the packages for the individual lodash
functions to lodash.intersection
, lodash.difference
etc.?
The update process is fairly trivial, would be happy to do a PR for it.
Thanks.
Need to be able to show that two items in an array switched order for example
{ a : [ { id: b, value: 1}, {id: a, value: 2}] }
{ a : [ { id: a, value: 2}, {id: b, value: 1}] }
should show a difference as order matters in an array
Hi, could you possibly publish an updated version of diff-json
to npm? The dependency on lodash was updated here: 7a1ac6a#diff-b9cfc7f2cdf78a7f4b91a753d10865a2 but that doesn't seem to be included in the most recent version on npm. Would be great to get a new version on npm with that updated lodash dependency.
I'm getting the following output if I run npm run test
✓ should return correct diff for object with embedded array object that does not have key specified
✓ should return correct diff for object with embedded array that has key specified
applyChanges()
1) should transfer oldObj to newObj with changeset
✓ should transfer oldObj to newObj with changesetWithoutEmbeddedKey
revertChanges()
✓ should transfer newObj to oldObj with changeset
✓ should transfer newObj to oldObj with changesetWithoutEmbeddedKey
5 passing (20ms)
1 failing
1) changesets
applyChanges()
should transfer oldObj to newObj with changeset:
Error: expected { name: 'smith',
nested: { inner: 2 },
date: Mon, 13 Oct 2014 09:13:00 GMT,
coins: [ 2, 5, 1 ],
toys: [],
pets: [],
children:
[ { name: 'kid1', age: 0, subset: [Object] },
{ name: 'kid2', age: 2 },
{ name: 'kid3', age: 3 } ],
mixed: '10' } to sort of equal { name: 'smith',
mixed: '10',
nested: { inner: 2 },
date: Mon, 13 Oct 2014 09:13:00 GMT,
coins: [ 2, 5, 1 ],
toys: [],
pets: [],
children:
[ { name: 'kid3', age: 3 },
{ name: 'kid1', age: 0, subset: [Object] },
{ name: 'kid2', age: 2 } ] }
+ expected - actual
{
"children": [
{
+ "age": 3
+ "name": "kid3"
+ }
+ {
"age": 0
"name": "kid1"
"subset": [
{
--
{
"age": 2
"name": "kid2"
}
- {
- "age": 3
- "name": "kid3"
- }
]
"coins": [
2
5
at Assertion.assert (node_modules/expect.js/index.js:96:13)
at Assertion.eql (node_modules/expect.js/index.js:230:10)
at Context.<anonymous> (test/changesets.coffee:143:25)
at processImmediate (internal/timers.js:464:21)
Is there an advantage of diff-json over changeset?
I have the following code:
var diffJson = require("diff-json")
oldArray = [
{
"id": "a",
"count": 1
},
{
"id": "b",
"count": 0
}
];
newArray = [
{
"id": "b",
"count": 1
},
{
"id": "a",
"count": 3
}
];
var diffs = diffJson.diff(oldArray, newArray);
And the result is showing as:
[Object {changes: [, ], embededKey: "$index", key: "$root", type: "update"}]
What should the call to .diff
be?
What do the other params (path, embededObjKeys, keyPath) mean?
It seems that the logic around removing items from an array is seriously dodgy.
For example, in the test you go from toys ["car", "doll", "car"] to [] by doing 3 remove actions on the array. But the actions are not looking at the items in the array, they will try to remove an item from the index where the original item was positioned, and if that index doesn't have an element, it will just remove the last element from the array, whatever it is.
Here's what happens if you log the applyLeafChange arguments. The first is the array contents it operates on, and the second is the change to be applied.
As you can see the first mutation is valid, because there is a "car" at index 0, but 2 and 3 are not. Step two should remove the doll, but instead removes the car at the end. Then step 3 should remove the car at index 2 but there is no car and not index 2 anymore, and instead it just removes the last item.
The end result is still [], and that's why the test passes, but this all seems wrong to me.
I think what you intended to do with the indexOfItemInArray
function is to look for the actual value in the array. But for that to work you need to pass the value to removeKey
which is currently not happening.
This is what I ended up with:
function removeKey(obj: JsonObject | JsonArray, key: string, value: JsonValue) {
if (Array.isArray(obj)) {
if (!obj[Number(key)]) {
const index = obj.findIndex((x) => x === value);
return obj.splice(index, 1);
} else {
return obj.splice(Number(key), 1);
}
} else {
return delete obj[key];
}
}
In this implementation, if the item can not be found, it will still remove the last item from the array whatever it is, so it's probably better to catch the -1 returned from findIndex
and throw an error.
This code is the result of converting your library to Typescript and in the process, I have also stripped the logic around embeddedKeys because I didn't need that complexity and it was difficult enough trying to port this.
In the future, I would probably move to a JSON Patch compatible implementation, but my production code was already dependent on this library so I decided to convert it to something I can understand. If enough people are interested I might release it in the future.
A simple test case:
it 'should properly handle new null property', ->
before = {}
after = { newProp: null }
changesets.applyChanges before, changesets.diff before, after
expect(after).to.eql before
Fails with "TypeError: Cannot read property 'length' of undefined"
diff-json/src/changesets.coffee
Line 212 in 4356e69
should probably be:
if !change.changes
In its current form applyChanges calls applyBranchChanges for the add of a new property with a value of null. applyBranchChanges expects a changes array to exist on the change and calls length on undefined.
I would expect that a different order of items in an array would also produce a diff. It doesn't.
Here's an example:
const changesets = require('diff-json')
const newObj = {
children: [
// kid2 is first here
{name: 'kid2', age: 2},
{name: 'kid1', age: 1}
]}
const oldObj = {
// kid1 is first here
children: [
{name: 'kid1', age: 1},
{name: 'kid2', age: 2}
]}
const diffs = changesets.diff(oldObj, newObj, {children: 'name'})
console.log(JSON.stringify(diffs, null, 2))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.