Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patches modify previous patches with nested objects #377

Open
jacklj opened this issue Nov 18, 2024 · 0 comments
Open

Patches modify previous patches with nested objects #377

jacklj opened this issue Nov 18, 2024 · 0 comments

Comments

@jacklj
Copy link

jacklj commented Nov 18, 2024

Applying a series of patches can modify the patch before it, if the first patch is adding a nested object and the second is modifying it. cloneDiffValues: true doesn't help.

Minimal repro:

const { create } = await import("jsondiffpatch");

const jdp = create();

const createPatch = jdp.diff.bind(jdp);
const applyPatch = jdp.patch.bind(jdp);

const p1 = createPatch({}, { stages: [] }); // create stages array
const p2 = createPatch({stages: []}, { stages: [{ id: '2' }] }); // add item

console.log("p1", JSON.stringify(p1)); // {"stages":[[]]}
console.log("p2", JSON.stringify(p2)); // {"stages":{"0":[{"id":"2"}],"_t":"a"}}

let results = applyPatch({}, p1);
results = applyPatch(results, p2);

console.log("results", JSON.stringify(results)); // {"stages":[{"id":"2"}]}
console.log("p1", JSON.stringify(p1)); // {"stages":[[{"id":"2"}]]} // **it's changed!!**
console.log("p2", JSON.stringify(p2)); // {"stages":{"0":[{"id":"2"}],"_t":"a"}}

// now apply the two patches again to see the bug in the results object
let results2 = applyPatch({}, p1);
results2 = applyPatch(results2, p2);
console.log("results2", JSON.stringify(results2)); // { stages: [ { id: '2' }, { id: '2' } ] } // results has 2 items
console.log("p1", JSON.stringify(p1)); // {"stages":[[{"id":"2"},{"id":"2"}]]}  // now p1 has 2 items
console.log("p2", JSON.stringify(p2)); // {"stages":{"0":[{"id":"2"}],"_t":"a"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant