From a652bbc84d1dc47d48c791a6582117536787660a Mon Sep 17 00:00:00 2001 From: Antoine Lanoe Date: Mon, 16 Nov 2026 21:14:30 +0100 Subject: [PATCH] fix: getlistdiff references --- README.md | 2 +- src/lib/list-diff/index.ts | 163 +--- src/lib/list-diff/list-diff.test.ts | 1325 ++++++++++++++++++++++++--- src/lib/utils/index.ts | 98 +- src/lib/utils/utils.test.ts | 22 + 5 files changed, 1324 insertions(+), 286 deletions(-) diff --git a/README.md b/README.md index 6af3ca9..b1d0602 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ Compares two arrays and returns a diff for each entry. Supports duplicate values nextList: T[]; options?: { showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default - referenceKey?: string, // "" by default + referenceKey?: string, // undefined by default ignoreArrayOrder?: boolean, // false by default, considerMoveAsUpdate?: boolean // false by default } diff --git a/src/lib/list-diff/index.ts b/src/lib/list-diff/index.ts index 9ab8572..416b447 100644 --- a/src/lib/list-diff/index.ts +++ b/src/lib/list-diff/index.ts @@ -1,10 +1,9 @@ -import { isEqual, isObject } from "@lib/utils"; +import { isEqual, isObject, stableStringify } from "@lib/utils"; import { DEFAULT_LIST_DIFF_OPTIONS, ListStatus, ListDiff, ListDiffOptions, - ListData, } from "@models/list"; function getDiffStatus(statusMap: Set): ListStatus { @@ -102,153 +101,85 @@ export const getListDiff = ( } const diff: ListDiff["diff"] = []; - const previousMap = new Map>(); + const previousMap = new Map(); const statusMap = new Set(); const { referenceKey, ignoreArrayOrder } = options; - let containsArrays = false; - - const stableStringifyCache = new Map(); - - function stableStringifyCached(value: unknown): string { - const cached = stableStringifyCache.get(value); + const stringifyCache = new Map(); + const unorderedCache = ignoreArrayOrder + ? new Map() + : stringifyCache; + + function stableStringifyCached(value: unknown, unordered = ignoreArrayOrder) { + const cache = unordered ? unorderedCache : stringifyCache; + const cached = cache.get(value); if (cached !== undefined) return cached; - const result = stableStringify(value); - stableStringifyCache.set(value, result); + const result = stableStringify(value, unordered); + cache.set(value, result); return result; } - function stableStringify(value: unknown): string { - if (isObject(value)) { - const keys = Object.keys(value).sort(); - let res = "{"; - for (let i = 0; i < keys.length; i++) { - const data = keys[i]; - res += JSON.stringify(data) + ":" + stableStringify(value[data]); - if (i < keys.length - 1) res += ","; - } - return res + "}"; - } - if (Array.isArray(value)) { - let res = "["; - for (let i = 0; i < value.length; i++) { - res += stableStringify(value[i]); - if (i < value.length - 1) res += ","; - } - return res + "]"; + function getMatchKey(value: T): unknown { + if ( + referenceKey !== undefined && + isObject(value) && + referenceKey in value + ) { + return `key:${stableStringifyCached(value[referenceKey], false)}`; } - if (value === undefined) return "undefined"; - return JSON.stringify(value); + + return (typeof value === "object" && value !== null) || + typeof value === "string" + ? stableStringifyCached(value) + : value; } for (let i = 0; i < prevList.length; i++) { - const prevValue = prevList[i]; - if (ignoreArrayOrder && !containsArrays && Array.isArray(prevValue)) { - containsArrays = true; - } - let reference: string; - - if (referenceKey && isObject(prevValue) && referenceKey in prevValue) { - reference = stableStringifyCached(prevValue[referenceKey]); - } else { - reference = stableStringifyCached(prevValue); - } - + const reference = getMatchKey(prevList[i]); const match = previousMap.get(reference); if (match) { match.indexes.push(i); } else { - previousMap.set(reference, { value: prevValue, indexes: [i] }); + previousMap.set(reference, { indexes: [i], cursor: 0 }); } } - const getPreviousMatch = (nextValue: T) => { - let reference: string; - let previousMatch: ListData | undefined; - let isStrictEqual = false; - - if (referenceKey && isObject(nextValue) && referenceKey in nextValue) { - reference = stableStringifyCached(nextValue[referenceKey]); - previousMatch = previousMap.get(reference); - if (previousMatch) { - isStrictEqual = isEqual(previousMatch.value, nextValue, { - ignoreArrayOrder, - }); - } - } else { - reference = stableStringifyCached(nextValue); - previousMatch = previousMap.get(reference); - - if (!previousMatch && ignoreArrayOrder && containsArrays) { - for (const prev of previousMap.values()) { - if (isEqual(prev.value, nextValue, { ignoreArrayOrder })) { - previousMatch = prev; - break; - } - } - } - - if (previousMatch) { - isStrictEqual = true; - } - } - - return { previousMatch, reference, isStrictEqual }; - }; + for (let i = 0; i < nextList.length; i++) { + const nextValue = nextList[i]; + const reference = getMatchKey(nextValue); + const previousMatch = previousMap.get(reference); - const injectNextValueInDiff = ( - nextValue: T, - previousMatch: ListData | undefined, - nextIndex: number, - reference: string, - isStrictEqual: boolean, - ) => { if (previousMatch) { - const previousIndex = previousMatch.indexes[0]; - const indexDiff = nextIndex - previousIndex; - const nextStatus = getNextStatus(indexDiff, isStrictEqual, options); - - diff.push({ - value: nextValue, - previousIndex, - index: nextIndex, - status: nextStatus, - }); - statusMap.add(nextStatus); - - previousMatch.indexes.shift(); - if (previousMatch.indexes.length === 0) { + const previousIndex = previousMatch.indexes[previousMatch.cursor++]; + const isStrictEqual = + referenceKey === undefined || + !isObject(nextValue) || + !(referenceKey in nextValue) || + isEqual(prevList[previousIndex], nextValue, options); + const status = getNextStatus(i - previousIndex, isStrictEqual, options); + diff.push({ value: nextValue, previousIndex, index: i, status }); + statusMap.add(status); + + if (previousMatch.cursor === previousMatch.indexes.length) { previousMap.delete(reference); } } else { diff.push({ value: nextValue, previousIndex: null, - index: nextIndex, + index: i, status: ListStatus.ADDED, }); statusMap.add(ListStatus.ADDED); } - }; - - for (let i = 0; i < nextList.length; i++) { - const nextValue = nextList[i]; - const { previousMatch, reference, isStrictEqual } = - getPreviousMatch(nextValue); - injectNextValueInDiff( - nextValue, - previousMatch, - i, - reference, - isStrictEqual, - ); } - for (const prevData of previousMap.values()) { + for (const previousMatch of previousMap.values()) { statusMap.add(ListStatus.DELETED); - for (let i = 0; i < prevData.indexes.length; i++) { + for (let i = previousMatch.cursor; i < previousMatch.indexes.length; i++) { + const previousIndex = previousMatch.indexes[i]; diff.push({ - value: prevData.value, - previousIndex: prevData.indexes[i], + value: prevList[previousIndex], + previousIndex, index: null, status: ListStatus.DELETED, }); diff --git a/src/lib/list-diff/list-diff.test.ts b/src/lib/list-diff/list-diff.test.ts index 1ea8f2f..3428f01 100644 --- a/src/lib/list-diff/list-diff.test.ts +++ b/src/lib/list-diff/list-diff.test.ts @@ -471,257 +471,1284 @@ describe("getListDiff", () => { ], }); }); - it("consider object updated if a referenceKey is given and this key hasn't changed", () => { + it("adds a number and deletes an object with a colliding referenceKey", () => { expect( - getListDiff( - [ - "hello", - { id: 37, isCool: true, hobbies: ["golf", "ski"] }, - { id: 38, isCool: false, hobbies: ["football"] }, - undefined, - { id: 8, age: 77 }, - { id: 55, character: { strength: 66 } }, - ], - [ - { id: 8, age: 77 }, - { id: 37, isCool: false, hobbies: ["golf", "ski"] }, - { id: 38, isCool: false, hobbies: ["football"] }, - undefined, - { id: 99, character: { strength: 69 } }, - ], - { - referenceKey: "id", - }, + getListDiff( + [{ id: 1, name: "Ada" }], + [1], + { referenceKey: "id" }, ), ).toStrictEqual({ type: "list", status: "updated", diff: [ { - value: { id: 8, age: 77 }, + value: 1, + previousIndex: null, index: 0, - previousIndex: 4, - status: "moved", + status: "added", }, { - value: { id: 37, isCool: false, hobbies: ["golf", "ski"] }, - index: 1, - previousIndex: 1, - status: "updated", + value: { id: 1, name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", }, + ], + }); + }); + it("adds an object and deletes a number with a colliding referenceKey", () => { + expect( + getListDiff( + [1], + [{ id: 1, name: "Ada" }], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: { id: 38, isCool: false, hobbies: ["football"] }, - index: 2, - previousIndex: 2, - status: "equal", + value: { id: 1, name: "Ada" }, + previousIndex: null, + index: 0, + status: "added", }, { - value: undefined, - index: 3, - previousIndex: 3, - status: "equal", + value: 1, + previousIndex: 0, + index: null, + status: "deleted", }, + ], + }); + }); + it("adds a string and deletes an object with a colliding referenceKey", () => { + expect( + getListDiff( + [{ id: "1", name: "Ada" }], + ["1"], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: { id: 99, character: { strength: 69 } }, - index: 4, + value: "1", previousIndex: null, + index: 0, status: "added", }, { - value: "hello", - index: null, + value: { id: "1", name: "Ada" }, previousIndex: 0, - status: "deleted", - }, - { - value: { id: 55, character: { strength: 66 } }, index: null, - previousIndex: 5, status: "deleted", }, ], }); }); - it("consider moved values as updated if the considerMoveAsUpdate option is true", () => { + it("adds an object and deletes a string with a colliding referenceKey", () => { expect( - getListDiff(["mbappe", "messi"], ["mbappe", "mbappe", "messi"], { - considerMoveAsUpdate: true, - }), + getListDiff( + ["1"], + [{ id: "1", name: "Ada" }], + { referenceKey: "id" }, + ), ).toStrictEqual({ type: "list", status: "updated", diff: [ { - value: "mbappe", + value: { id: "1", name: "Ada" }, + previousIndex: null, index: 0, + status: "added", + }, + { + value: "1", previousIndex: 0, - status: "equal", + index: null, + status: "deleted", }, + ], + }); + }); + it("adds a boolean and deletes an object with a colliding referenceKey", () => { + expect( + getListDiff( + [{ id: true, name: "Ada" }], + [true], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: "mbappe", - index: 1, + value: true, previousIndex: null, + index: 0, status: "added", }, { - value: "messi", - index: 2, - previousIndex: 1, - status: "updated", + value: { id: true, name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", }, ], }); + }); + it("adds an object and deletes a boolean with a colliding referenceKey", () => { expect( - getListDiff( - [ - "hello", - { id: 37, isCool: true, hobbies: ["golf", "ski"] }, - { id: 38, isCool: false, hobbies: ["football"] }, - undefined, - { id: 8, age: 77 }, - { id: 55, character: { strength: 66 } }, - ], - [ - { id: 8, age: 77 }, - { id: 37, isCool: false, hobbies: ["golf", "ski"] }, - { id: 38, isCool: false, hobbies: ["football"] }, - undefined, - { id: 99, character: { strength: 69 } }, - ], - { - referenceKey: "id", - considerMoveAsUpdate: true, - }, + getListDiff( + [true], + [{ id: true, name: "Ada" }], + { referenceKey: "id" }, ), ).toStrictEqual({ type: "list", status: "updated", diff: [ { - value: { id: 8, age: 77 }, + value: { id: true, name: "Ada" }, + previousIndex: null, index: 0, - previousIndex: 4, - status: "updated", + status: "added", }, { - value: { id: 37, isCool: false, hobbies: ["golf", "ski"] }, - index: 1, - previousIndex: 1, - status: "updated", + value: true, + previousIndex: 0, + index: null, + status: "deleted", }, + ], + }); + }); + it("adds null and deletes an object with a colliding referenceKey", () => { + expect( + getListDiff([{ id: null, name: "Ada" }], [null], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: { id: 38, isCool: false, hobbies: ["football"] }, - index: 2, - previousIndex: 2, - status: "equal", + value: null, + previousIndex: null, + index: 0, + status: "added", }, { - value: undefined, - index: 3, - previousIndex: 3, - status: "equal", + value: { id: null, name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", }, + ], + }); + }); + it("adds an object and deletes null with a colliding referenceKey", () => { + expect( + getListDiff([null], [{ id: null, name: "Ada" }], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: { id: 99, character: { strength: 69 } }, - index: 4, + value: { id: null, name: "Ada" }, previousIndex: null, + index: 0, status: "added", }, { - value: "hello", - index: null, + value: null, previousIndex: 0, + index: null, status: "deleted", }, + ], + }); + }); + it("adds undefined and deletes an object with a colliding referenceKey", () => { + expect( + getListDiff([{ id: undefined, name: "Ada" }], [undefined], { + referenceKey: "id", + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ { - value: { id: 55, character: { strength: 66 } }, + value: undefined, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: undefined, name: "Ada" }, + previousIndex: 0, index: null, - previousIndex: 5, status: "deleted", }, ], }); }); - it("consider moved values as equal if they have not changed and ignoreArrayOrder option is true", () => { + it("adds an object and deletes undefined with a colliding referenceKey", () => { expect( - getListDiff( - [ - { id: 3, name: "nina", hobbies: ["swiming"] }, - { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, - { id: 2, name: "jack", hobbies: ["coding"] }, - ], - [ - { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, - { id: 2, name: "jack", hobbies: ["coding"] }, - { id: 3, name: "nina", hobbies: ["swiming"] }, - ], - { - ignoreArrayOrder: true, - }, - ), + getListDiff([undefined], [{ id: undefined, name: "Ada" }], { + referenceKey: "id", + }), ).toStrictEqual({ type: "list", - status: "equal", + status: "updated", diff: [ { - value: { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + value: { id: undefined, name: "Ada" }, + previousIndex: null, index: 0, - previousIndex: 1, - status: "equal", - }, - { - value: { id: 2, name: "jack", hobbies: ["coding"] }, - index: 1, - previousIndex: 2, - status: "equal", + status: "added", }, { - value: { id: 3, name: "nina", hobbies: ["swiming"] }, - index: 2, + value: undefined, previousIndex: 0, - status: "equal", + index: null, + status: "deleted", }, ], }); }); - it("consider moved values as updated if they have changed and ignoreArrayOrder option is true", () => { + it("adds a keyless object and deletes an object with a colliding referenceKey", () => { expect( - getListDiff( - [ - { id: 3, name: "nina", hobbies: ["swiming"] }, - { id: 1, name: "joseph", hobbies: ["golf", "fishing"] }, - { id: 2, name: "jack", hobbies: ["coding"] }, - ], - [ - { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, - { id: 2, name: "jack", hobbies: ["coding"] }, - { id: 3, name: "nina", hobbies: ["swiming"] }, - ], - { - ignoreArrayOrder: true, - referenceKey: "id", - }, - ), + getListDiff([{ id: { k: 1 }, name: "Ada" }], [{ k: 1 }], { + referenceKey: "id", + }), ).toStrictEqual({ type: "list", status: "updated", diff: [ { - value: { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + value: { k: 1 }, + previousIndex: null, index: 0, - previousIndex: 1, - status: "updated", + status: "added", }, { - value: { id: 2, name: "jack", hobbies: ["coding"] }, - index: 1, + value: { id: { k: 1 }, name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("adds an object and deletes a keyless object with a colliding referenceKey", () => { + expect( + getListDiff([{ k: 1 }], [{ id: { k: 1 }, name: "Ada" }], { + referenceKey: "id", + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: { k: 1 }, name: "Ada" }, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { k: 1 }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("adds an array and deletes an object with a colliding referenceKey", () => { + expect( + getListDiff([{ id: [1], name: "Ada" }], [[1]], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: [1], + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: [1], name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("adds an object and deletes an array with a colliding referenceKey", () => { + expect( + getListDiff([[1]], [{ id: [1], name: "Ada" }], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: [1], name: "Ada" }, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: [1], + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("matches duplicate objects and colliding primitive values independently", () => { + expect( + getListDiff( + [{ id: 1, name: "Ada" }, 1, { id: 1, name: "Ada" }, 1], + [1, { id: 1, name: "Ada" }, 1, { id: 1, name: "Ada" }], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: 1, previousIndex: 1, index: 0, status: "moved" }, + { + value: { id: 1, name: "Ada" }, + previousIndex: 0, + index: 1, + status: "moved", + }, + { value: 1, previousIndex: 3, index: 2, status: "moved" }, + { + value: { id: 1, name: "Ada" }, + previousIndex: 2, + index: 3, + status: "moved", + }, + ], + }); + }); + it("matches duplicate objects and colliding keyless object values independently", () => { + expect( + getListDiff( + [ + { id: { k: 1 }, name: "Ada" }, + { k: 1 }, + { id: { k: 1 }, name: "Ada" }, + { k: 1 }, + ], + [ + { k: 1 }, + { id: { k: 1 }, name: "Ada" }, + { k: 1 }, + { id: { k: 1 }, name: "Ada" }, + ], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: { k: 1 }, previousIndex: 1, index: 0, status: "moved" }, + { + value: { id: { k: 1 }, name: "Ada" }, + previousIndex: 0, + index: 1, + status: "moved", + }, + { value: { k: 1 }, previousIndex: 3, index: 2, status: "moved" }, + { + value: { id: { k: 1 }, name: "Ada" }, + previousIndex: 2, + index: 3, + status: "moved", + }, + ], + }); + }); + it("distinguishes a missing referenceKey from an undefined referenceKey", () => { + expect( + getListDiff([{ id: undefined }], [{}], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: {}, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: undefined }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("distinguishes an undefined referenceKey from a missing referenceKey", () => { + expect( + getListDiff([{}], [{ id: undefined }], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: undefined }, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: {}, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("does not match an array against a referenceKey when ignoring array order", () => { + expect( + getListDiff([{ id: [1], name: "Ada" }], [[1]], { + referenceKey: "id", + ignoreArrayOrder: true, + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: [1], + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: [1], name: "Ada" }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + it("keeps updates, moves, additions and deletions separate in a mixed list", () => { + expect( + getListDiff( + [{ id: 1, name: "Ada" }, 1, { id: 2, name: "Grace" }, 2], + [1, { id: 1, name: "Augusta" }, 2, 3], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: 1, previousIndex: 1, index: 0, status: "moved" }, + { + value: { id: 1, name: "Augusta" }, + previousIndex: 0, + index: 1, + status: "updated", + }, + { value: 2, previousIndex: 3, index: 2, status: "moved" }, + { value: 3, previousIndex: null, index: 3, status: "added" }, + { + value: { id: 2, name: "Grace" }, + previousIndex: 2, + index: null, + status: "deleted", + }, + ], + }); + }); + it("deletes unmatched duplicates from both referenceKey and full-value matches", () => { + expect( + getListDiff( + [1, { id: 1, name: "Ada" }, 1, { id: 1, name: "Ada" }], + [{ id: 1, name: "Ada" }, 1], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 1, name: "Ada" }, + previousIndex: 1, + index: 0, + status: "moved", + }, + { value: 1, previousIndex: 0, index: 1, status: "moved" }, + { value: 1, previousIndex: 2, index: null, status: "deleted" }, + { + value: { id: 1, name: "Ada" }, + previousIndex: 3, + index: null, + status: "deleted", + }, + ], + }); + }); + it("consider object updated if a referenceKey is given and this key hasn't changed", () => { + expect( + getListDiff( + [ + "hello", + { id: 37, isCool: true, hobbies: ["golf", "ski"] }, + { id: 38, isCool: false, hobbies: ["football"] }, + undefined, + { id: 8, age: 77 }, + { id: 55, character: { strength: 66 } }, + ], + [ + { id: 8, age: 77 }, + { id: 37, isCool: false, hobbies: ["golf", "ski"] }, + { id: 38, isCool: false, hobbies: ["football"] }, + undefined, + { id: 99, character: { strength: 69 } }, + ], + { + referenceKey: "id", + }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 8, age: 77 }, + index: 0, + previousIndex: 4, + status: "moved", + }, + { + value: { id: 37, isCool: false, hobbies: ["golf", "ski"] }, + index: 1, + previousIndex: 1, + status: "updated", + }, + { + value: { id: 38, isCool: false, hobbies: ["football"] }, + index: 2, + previousIndex: 2, + status: "equal", + }, + { + value: undefined, + index: 3, + previousIndex: 3, + status: "equal", + }, + { + value: { id: 99, character: { strength: 69 } }, + index: 4, + previousIndex: null, + status: "added", + }, + { + value: "hello", + index: null, + previousIndex: 0, + status: "deleted", + }, + { + value: { id: 55, character: { strength: 66 } }, + index: null, + previousIndex: 5, + status: "deleted", + }, + ], + }); + }); + it("consider moved values as updated if the considerMoveAsUpdate option is true", () => { + expect( + getListDiff(["mbappe", "messi"], ["mbappe", "mbappe", "messi"], { + considerMoveAsUpdate: true, + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: "mbappe", + index: 0, + previousIndex: 0, + status: "equal", + }, + { + value: "mbappe", + index: 1, + previousIndex: null, + status: "added", + }, + { + value: "messi", + index: 2, + previousIndex: 1, + status: "updated", + }, + ], + }); + expect( + getListDiff( + [ + "hello", + { id: 37, isCool: true, hobbies: ["golf", "ski"] }, + { id: 38, isCool: false, hobbies: ["football"] }, + undefined, + { id: 8, age: 77 }, + { id: 55, character: { strength: 66 } }, + ], + [ + { id: 8, age: 77 }, + { id: 37, isCool: false, hobbies: ["golf", "ski"] }, + { id: 38, isCool: false, hobbies: ["football"] }, + undefined, + { id: 99, character: { strength: 69 } }, + ], + { + referenceKey: "id", + considerMoveAsUpdate: true, + }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 8, age: 77 }, + index: 0, + previousIndex: 4, + status: "updated", + }, + { + value: { id: 37, isCool: false, hobbies: ["golf", "ski"] }, + index: 1, + previousIndex: 1, + status: "updated", + }, + { + value: { id: 38, isCool: false, hobbies: ["football"] }, + index: 2, + previousIndex: 2, + status: "equal", + }, + { + value: undefined, + index: 3, + previousIndex: 3, + status: "equal", + }, + { + value: { id: 99, character: { strength: 69 } }, + index: 4, + previousIndex: null, + status: "added", + }, + { + value: "hello", + index: null, + previousIndex: 0, + status: "deleted", + }, + { + value: { id: 55, character: { strength: 66 } }, + index: null, + previousIndex: 5, + status: "deleted", + }, + ], + }); + }); + it("consider moved values as equal if they have not changed and ignoreArrayOrder option is true", () => { + expect( + getListDiff( + [ + { id: 3, name: "nina", hobbies: ["swiming"] }, + { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + { id: 2, name: "jack", hobbies: ["coding"] }, + ], + [ + { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + { id: 2, name: "jack", hobbies: ["coding"] }, + { id: 3, name: "nina", hobbies: ["swiming"] }, + ], + { + ignoreArrayOrder: true, + }, + ), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + index: 0, + previousIndex: 1, + status: "equal", + }, + { + value: { id: 2, name: "jack", hobbies: ["coding"] }, + index: 1, + previousIndex: 2, + status: "equal", + }, + { + value: { id: 3, name: "nina", hobbies: ["swiming"] }, + index: 2, + previousIndex: 0, + status: "equal", + }, + ], + }); + }); + it("consider moved values as updated if they have changed and ignoreArrayOrder option is true", () => { + expect( + getListDiff( + [ + { id: 3, name: "nina", hobbies: ["swiming"] }, + { id: 1, name: "joseph", hobbies: ["golf", "fishing"] }, + { id: 2, name: "jack", hobbies: ["coding"] }, + ], + [ + { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + { id: 2, name: "jack", hobbies: ["coding"] }, + { id: 3, name: "nina", hobbies: ["swiming"] }, + ], + { + ignoreArrayOrder: true, + referenceKey: "id", + }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 1, name: "joe", hobbies: ["golf", "fishing"] }, + index: 0, + previousIndex: 1, + status: "updated", + }, + { + value: { id: 2, name: "jack", hobbies: ["coding"] }, + index: 1, previousIndex: 2, status: "equal", }, { - value: { id: 3, name: "nina", hobbies: ["swiming"] }, + value: { id: 3, name: "nina", hobbies: ["swiming"] }, + index: 2, + previousIndex: 0, + status: "equal", + }, + ], + }); + }); + it("adds an unordered duplicate after its only previous match is consumed", () => { + expect( + getListDiff( + [[1, 2]], + [ + [2, 1], + [2, 1], + ], + { ignoreArrayOrder: true }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: [2, 1], previousIndex: 0, index: 0, status: "equal" }, + { value: [2, 1], previousIndex: null, index: 1, status: "added" }, + ], + }); + }); + + it("consumes equivalent unordered arrays in previous-index order", () => { + expect( + getListDiff( + [ + [1, 2], + [2, 1], + ], + [ + [2, 1], + [1, 2], + ], + { + ignoreArrayOrder: true, + }, + ), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { value: [2, 1], previousIndex: 0, index: 0, status: "equal" }, + { value: [1, 2], previousIndex: 1, index: 1, status: "equal" }, + ], + }); + }); + + it("compares nested arrays without considering array or object property order", () => { + expect( + getListDiff( + [{ items: [[1, 2], { a: 1, b: 2 }] }], + [{ items: [{ b: 2, a: 1 }, [2, 1]] }], + { ignoreArrayOrder: true }, + ), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: { items: [{ b: 2, a: 1 }, [2, 1]] }, + previousIndex: 0, + index: 0, + status: "equal", + }, + ], + }); + }); + + it("compares nested unordered arrays consistently when matching by ID", () => { + expect( + getListDiff( + [{ id: 1, items: [[1, 2], { a: 1, b: 2 }] }], + [{ id: 1, items: [{ b: 2, a: 1 }, [2, 1]] }], + { referenceKey: "id", ignoreArrayOrder: true }, + ), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: { id: 1, items: [{ b: 2, a: 1 }, [2, 1]] }, + previousIndex: 0, + index: 0, + status: "equal", + }, + ], + }); + }); + + it("preserves duplicate counts inside unordered arrays", () => { + expect( + getListDiff([[1, 1, 2]], [[1, 2, 2]], { ignoreArrayOrder: true }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: [1, 2, 2], previousIndex: null, index: 0, status: "added" }, + { value: [1, 1, 2], previousIndex: 0, index: null, status: "deleted" }, + ], + }); + }); + + it("keeps array-valued reference IDs ordered when ignoring list array order", () => { + expect( + getListDiff([{ id: [1, 2] }], [{ id: [2, 1] }], { + referenceKey: "id", + ignoreArrayOrder: true, + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: [2, 1] }, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: [1, 2] }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + + it("keeps unordered full values separate from array-valued reference IDs", () => { + expect( + getListDiff([[9], { id: [1, 2] }], [[9], [2, 1]], { + referenceKey: "id", + ignoreArrayOrder: true, + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: [9], previousIndex: 0, index: 0, status: "equal" }, + { value: [2, 1], previousIndex: null, index: 1, status: "added" }, + { + value: { id: [1, 2] }, + previousIndex: 1, + index: null, + status: "deleted", + }, + ], + }); + }); + + it("keeps ordered arrays distinct by default", () => { + expect(getListDiff([[1, 2]], [[2, 1]])).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: [2, 1], previousIndex: null, index: 0, status: "added" }, + { value: [1, 2], previousIndex: 0, index: null, status: "deleted" }, + ], + }); + }); + + it("compares each object with a duplicate ID against its own previous value", () => { + expect( + getListDiff( + [ + { id: 1, name: "Ada" }, + { id: 1, name: "Grace" }, + ], + [ + { id: 1, name: "Ada" }, + { id: 1, name: "Grace" }, + ], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: { id: 1, name: "Ada" }, + previousIndex: 0, + index: 0, + status: "equal", + }, + { + value: { id: 1, name: "Grace" }, + previousIndex: 1, + index: 1, + status: "equal", + }, + ], + }); + }); + + it("deletes the actual unmatched object when IDs are duplicated", () => { + expect( + getListDiff( + [ + { id: 1, name: "Ada" }, + { id: 1, name: "Grace" }, + ], + [{ id: 1, name: "Ada" }], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 1, name: "Ada" }, + previousIndex: 0, + index: 0, + status: "equal", + }, + { + value: { id: 1, name: "Grace" }, + previousIndex: 1, + index: null, + status: "deleted", + }, + ], + }); + }); + + it("consumes duplicate IDs in order while distinguishing updates and additions", () => { + expect( + getListDiff( + [ + { id: 1, name: "Ada" }, + { id: 1, name: "Grace" }, + ], + [ + { id: 1, name: "Augusta" }, + { id: 1, name: "Grace" }, + { id: 1, name: "Lin" }, + ], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 1, name: "Augusta" }, + previousIndex: 0, + index: 0, + status: "updated", + }, + { + value: { id: 1, name: "Grace" }, + previousIndex: 1, + index: 1, + status: "equal", + }, + { + value: { id: 1, name: "Lin" }, + previousIndex: null, index: 2, + status: "added", + }, + ], + }); + }); + + it("distinguishes null from all non-finite numbers", () => { + expect( + getListDiff([NaN, Infinity, -Infinity], [null]), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: null, previousIndex: null, index: 0, status: "added" }, + { value: NaN, previousIndex: 0, index: null, status: "deleted" }, + { value: Infinity, previousIndex: 1, index: null, status: "deleted" }, + { value: -Infinity, previousIndex: 2, index: null, status: "deleted" }, + ], + }); + }); + + it("matches non-finite numbers to their own values", () => { + expect( + getListDiff([NaN, Infinity, -Infinity], [-Infinity, NaN, Infinity]), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: -Infinity, previousIndex: 2, index: 0, status: "moved" }, + { value: NaN, previousIndex: 0, index: 1, status: "moved" }, + { value: Infinity, previousIndex: 1, index: 2, status: "moved" }, + ], + }); + }); + + it("does not conflate non-finite reference IDs", () => { + expect( + getListDiff([{ id: NaN }], [{ id: Infinity }], { referenceKey: "id" }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: Infinity }, + previousIndex: null, + index: 0, + status: "added", + }, + { + value: { id: NaN }, + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + + it("distinguishes dates with different timestamps", () => { + expect( + getListDiff([new Date("2020-01-01")], [new Date("2021-01-01")]), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: new Date("2021-01-01"), + previousIndex: null, + index: 0, + status: "added", + }, + { + value: new Date("2020-01-01"), + previousIndex: 0, + index: null, + status: "deleted", + }, + ], + }); + }); + + it("matches distinct date instances with the same timestamp", () => { + expect( + getListDiff([new Date("2020-01-01")], [new Date("2020-01-01")]), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: new Date("2020-01-01"), + previousIndex: 0, + index: 0, + status: "equal", + }, + ], + }); + }); + + it("detects a changed nested date when matching by ID", () => { + expect( + getListDiff( + [{ id: 1, date: new Date("2020-01-01") }], + [{ id: 1, date: new Date("2021-01-01") }], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: 1, date: new Date("2021-01-01") }, + previousIndex: 0, + index: 0, + status: "updated", + }, + ], + }); + }); + + it("distinguishes dates from timestamps, strings and empty objects", () => { + expect( + getListDiff( + [new Date(0), 0, "date:0", {}], + [{}, "date:0", 0, new Date(0)], + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: {}, previousIndex: 3, index: 0, status: "moved" }, + { value: "date:0", previousIndex: 2, index: 1, status: "moved" }, + { value: 0, previousIndex: 1, index: 2, status: "moved" }, + { value: new Date(0), previousIndex: 0, index: 3, status: "moved" }, + ], + }); + }); + + it("compares date-valued reference IDs by timestamp", () => { + expect( + getListDiff( + [{ id: new Date(0), name: "Ada" }], + [{ id: new Date(0), name: "Grace" }], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { id: new Date(0), name: "Grace" }, + previousIndex: 0, + index: 0, + status: "updated", + }, + ], + }); + }); + + it("supports an empty property name as the referenceKey", () => { + expect( + getListDiff([{ "": 1, name: "Ada" }], [{ "": 1, name: "Grace" }], { + referenceKey: "", + }), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { + value: { "": 1, name: "Grace" }, + previousIndex: 0, + index: 0, + status: "updated", + }, + ], + }); + }); + + it("distinguishes bigints from numbers and strings", () => { + expect(getListDiff([1n, 1, "1"], ["1", 1n, 1])).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: "1", previousIndex: 2, index: 0, status: "moved" }, + { value: 1n, previousIndex: 0, index: 1, status: "moved" }, + { value: 1, previousIndex: 1, index: 2, status: "moved" }, + ], + }); + }); + + it("matches functions and symbols by identity", () => { + const callback = () => 1; + const symbol = Symbol("id"); + expect( + getListDiff([callback, symbol], [symbol, callback]), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: symbol, previousIndex: 1, index: 0, status: "moved" }, + { value: callback, previousIndex: 0, index: 1, status: "moved" }, + ], + }); + }); + + it("keeps serialized-looking strings separate from objects and reference IDs", () => { + expect( + getListDiff( + ['{"id":1}', { id: 1 }, "key:1"], + [{ id: 1 }, "key:1", '{"id":1}'], + { referenceKey: "id" }, + ), + ).toStrictEqual({ + type: "list", + status: "updated", + diff: [ + { value: { id: 1 }, previousIndex: 1, index: 0, status: "moved" }, + { value: "key:1", previousIndex: 2, index: 1, status: "moved" }, + { value: '{"id":1}', previousIndex: 0, index: 2, status: "moved" }, + ], + }); + }); + + it("allows repeated references to the same nested object", () => { + const shared = { count: 1 }; + expect( + getListDiff([[shared, shared]], [[{ count: 1 }, { count: 1 }]]), + ).toStrictEqual({ + type: "list", + status: "equal", + diff: [ + { + value: [{ count: 1 }, { count: 1 }], previousIndex: 0, + index: 0, status: "equal", }, ], diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 0d10a8f..b9860fb 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -14,11 +14,12 @@ export function isEqual( ): boolean { if (a === b) return true; if (typeof a !== typeof b) return false; + if (typeof a === "number") return Number.isNaN(a) && Number.isNaN(b); + if (a === null || b === null || typeof a !== "object") return false; + const aIsArray = Array.isArray(a); const bIsArray = Array.isArray(b); if (aIsArray !== bIsArray) return false; - if (a === null || b === null) return a === b; - if (typeof a !== "object") return a === b; if (aIsArray && bIsArray) { if (a.length !== b.length) return false; @@ -31,11 +32,17 @@ export function isEqual( const counts = new Map(); for (const item of a) { - const key = JSON.stringify(item); + const key = + typeof item === "object" || typeof item === "string" + ? stableStringify(item, options.ignoreArrayOrder) + : item; counts.set(key, (counts.get(key) || 0) + 1); } for (const item of b) { - const key = JSON.stringify(item); + const key = + typeof item === "object" || typeof item === "string" + ? stableStringify(item, options.ignoreArrayOrder) + : item; const count = counts.get(key); if (!count) return false; if (count === 1) counts.delete(key); @@ -44,24 +51,28 @@ export function isEqual( return counts.size === 0; } - if (a && b) { - const aObj = a as Record; - const bObj = b as Record; - let bKeyCount = 0; - for (const _ in bObj) { - if (Object.prototype.hasOwnProperty.call(bObj, _)) bKeyCount++; - } - let matchedKeys = 0; - for (const key in aObj) { - if (!Object.prototype.hasOwnProperty.call(aObj, key)) continue; - matchedKeys++; - if (!Object.prototype.hasOwnProperty.call(bObj, key)) return false; - if (!isEqual(aObj[key], bObj[key], options)) return false; - } - return matchedKeys === bKeyCount; + if (a instanceof Date || b instanceof Date) { + return ( + a instanceof Date && + b instanceof Date && + Object.is(a.getTime(), b.getTime()) + ); } - return true; + const aObj = a as Record; + const bObj = b as Record; + let bKeyCount = 0; + for (const _ in bObj) { + if (Object.prototype.hasOwnProperty.call(bObj, _)) bKeyCount++; + } + let matchedKeys = 0; + for (const key in aObj) { + if (!Object.prototype.hasOwnProperty.call(aObj, key)) continue; + matchedKeys++; + if (!Object.prototype.hasOwnProperty.call(bObj, key)) return false; + if (!isEqual(aObj[key], bObj[key], options)) return false; + } + return matchedKeys === bKeyCount; } /** @@ -72,3 +83,50 @@ export function isEqual( export function isObject(value: unknown): value is Record { return typeof value === "object" && !!value && !Array.isArray(value); } + +/** + * Serializes values with stable object keys and optional recursive array ordering. + * @param {unknown} value - The data to check. + * @param {boolean} ignoreArrayOrder - Whether to ignore array order recursively. + * @returns string + */ +export function stableStringify( + value: unknown, + ignoreArrayOrder = false, +): string { + if (value === null || typeof value !== "object") { + if (typeof value === "number") return String(value); + if (typeof value === "bigint") return `${value}n`; + if (value === undefined) return "undefined"; + return JSON.stringify(value); + } + if (Array.isArray(value)) { + if (ignoreArrayOrder) { + const items = new Array(value.length); + for (let i = 0; i < value.length; i++) { + items[i] = stableStringify(value[i], ignoreArrayOrder); + } + return `[${items.sort().join(",")}]`; + } + let result = "["; + for (let i = 0; i < value.length; i++) { + result += stableStringify(value[i], ignoreArrayOrder); + if (i < value.length - 1) result += ","; + } + return result + "]"; + } + if (value instanceof Date) return `date:${value.getTime()}`; + + const objectValue = value as Record; + const keys = Object.keys(objectValue).sort(); + let result = "{"; + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + result += + JSON.stringify(key) + + ":" + + stableStringify(objectValue[key], ignoreArrayOrder); + if (i < keys.length - 1) result += ","; + } + return result + "}"; +} diff --git a/src/lib/utils/utils.test.ts b/src/lib/utils/utils.test.ts index c94afba..751df39 100644 --- a/src/lib/utils/utils.test.ts +++ b/src/lib/utils/utils.test.ts @@ -1,6 +1,28 @@ import { isEqual, isObject } from "."; describe("isEqual", () => { + it("compares NaN consistently with native Map keys", () => { + expect(isEqual(NaN, NaN)).toBe(true); + expect(isEqual(NaN, null)).toBe(false); + expect(isEqual(NaN, Infinity)).toBe(false); + }); + it("compares dates by timestamp", () => { + expect(isEqual(new Date(0), new Date(0))).toBe(true); + expect(isEqual(new Date(0), new Date(1))).toBe(false); + expect(isEqual(new Date(0), {})).toBe(false); + expect(isEqual({}, new Date(0))).toBe(false); + }); + it("ignores nested array and object property order while preserving duplicate counts", () => { + expect( + isEqual([[1, 2], { a: 1, b: 2 }], [{ b: 2, a: 1 }, [2, 1]], { + ignoreArrayOrder: true, + }), + ).toBe(true); + expect(isEqual([[1, 1, 2]], [[1, 2, 2]], { ignoreArrayOrder: true })).toBe( + false, + ); + expect(isEqual([NaN], [null], { ignoreArrayOrder: true })).toBe(false); + }); it("return true if data are the same", () => { expect(isEqual(null, null)).toBeTruthy(); expect(isEqual(undefined, undefined)).toBeTruthy();