Registry Mirror
Skip to content

Conversation

@lee3Q

@lee3Q lee3Q commented Nov 3, 2026

Copy link
Copy Markdown

diff(a, clone(a)) returns a non-empty delta when an array contains an object (or array) next to any repeated primitive.

import { create } from "jsondiffpatch";
const j = create();
const a = [{ x: 1 }, "a", "a"];
j.diff(a, structuredClone(a));
// {"0":[{"x":1}],"_t":"a","_0":[{"x":1},0,0]}   expected: undefined

The input from #420 ({ A: [["B"], 1, "C", 2, "C"] }) hits the same path.

arraysHaveMatchByRef() returns true when array1[i] === array2[j] for any i !== j. For repeated primitives ("a" at two positions) that is always true, so matchByPosition is switched off. matchByPosition is only read for objects in matchItems, so same-position objects then stop matching and show up as a delete plus an insert. The same thing makes a change inside an object look like a replacement:

j.diff([{ id: 1, v: "a" }, "x", { id: 2 }, "x"], [{ id: 1, v: "b" }, "x", { id: 2 }, "x"]);
// before: delete + insert of both objects
// after:  {"0":{"v":["a","b"]},"_t":"a"}

The fix only counts non-null object values in arraysHaveMatchByRef(). Detection of shared object references is unchanged, and arrays of primitives behave as before.

Tests: two new array examples (identical array with repeated primitives, and a nested change next to repeated primitives). They fail without the change (4 failed, 420 passed) and pass with it. npx vitest run in packages/jsondiffpatch: 5 files, 424 tests pass. tsc --noEmit and biome check are clean.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant