json-diff-ts
json-diff-ts copied to clipboard
atomizeChangeset produces wrong result when the changes has a new object
Thank you for a great project. Love the way you can use id’s for arrays. I do however have a problem when using atomizeChangeset with objects:
const oldData = () => {
return {
planet: "Tatooine",
characters: [{ id: "LUK", name: null }],
};
};
const newData = () => {
return {
planet: "Tatooine",
characters: [{ id: "LUK", name: { firstName: "Luke", lastName: "Skywalker" } }],
};
};
const options = {
embeddedObjKeys: { ".characters": "id" },
};
// Get the diffs between oldData and newData
const originalDiffs = diff(oldData(), newData(), options);
// Atomize and then unatomize the diffs
const atomizedDiffs = atomizeChangeset(originalDiffs);
const unatomizedDiffs = unatomizeChangeset(atomizedDiffs);
// Applying the original diffs produces the expected result:
// {planet: "Tatooine", characters: [{id: "LUK", name: {firstName: "Luke", lastName: "Skywalker"}}]}
const dataWithOriginalDiffs = applyChangeset(oldData(), originalDiffs);
// Applying the unatomized diffs produces an unexpected result:
// {planet: "Tatooine", characters: [{id: "LUK"}, {firstName: "Luke", lastName: "Skywalker"}]}
// The 'name' object is pushed as a new element in the 'characters' array, which is not expected.
const dataWithUnatomizedDiffs = applyChangeset(oldData(), unatomizedDiffs);
// The expectation was that the unatomized diffs would yield the same result as the original diffs.
console.log(JSON.stringify(dataWithOriginalDiffs) === JSON.stringify(dataWithUnatomizedDiffs));
I need to use atomizeChangeset to have more control over the changes that are applied. Hope you can help me out.