diff --git a/package.json b/package.json index c7054b4..73d5e9c 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,8 @@ "@niivue/niivue": "^0.39.0", "react": "^17 || ^18", "typescript": "^5.0.0" + }, + "dependencies": { + "fast-deep-equal": "^3.1.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4a1d03c..535c8ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + fast-deep-equal: + specifier: ^3.1.3 + version: 3.1.3 react: specifier: ^17 || ^18 version: 18.2.0 @@ -922,6 +925,10 @@ packages: strip-final-newline: 3.0.0 dev: true + /fast-deep-equal@3.1.3: + resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} + dev: false + /fflate@0.7.4: resolution: {integrity: sha512-5u2V/CDW15QM1XbbgS+0DfPxVB+jUKhWEKuuFuHncbk3tEEqzmoXL+2KyOFuKGqOnmdIy0/davWF1CkuwtibCw==} dev: true diff --git a/src/diff.ts b/src/diff.ts index 17c099b..11ece71 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -1,4 +1,5 @@ -import { HasUrlObject } from "./model.ts"; +import { HasUrlObject } from "./model"; +import equal from "fast-deep-equal/es6"; /** * A special value which indicates that the difference between two objects is Irreconcilable. @@ -186,7 +187,7 @@ function diffPrimitive( const yKeys = Object.keys(y); const deletedKeys = setDifference(xKeys, yKeys); const addedKeys = setDifference(yKeys, xKeys); - const changedKeys = xKeys.filter((key) => !deepishEqual(x[key], y[key])); + const changedKeys = xKeys.filter((key) => !equal(x[key], y[key])); const diffKeys = addedKeys.concat(changedKeys); return { ...Object.fromEntries(deletedKeys.map((key) => [key, undefined])), @@ -194,22 +195,6 @@ function diffPrimitive( }; } -/** - * Equality for arrays and primitives. - */ -function deepishEqual(x: T, y: T): boolean { - if (Array.isArray(x)) { - if (!Array.isArray(y)) { - return false; - } - return zipArrays(x, y).reduce( - (same, [a, b]) => same && deepishEqual(a, b), - true, - ); - } - return x === y; -} - function zipArrays(x: X[], y: Y[]): [X, Y][] { return x.map((v, i) => [v, y[i]]); }