Skip to content

Commit ad26c81

Browse files
committed
Use fast-deep-equal
1 parent adab44b commit ad26c81

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
"@niivue/niivue": "^0.39.0",
3232
"react": "^17 || ^18",
3333
"typescript": "^5.0.0"
34+
},
35+
"dependencies": {
36+
"fast-deep-equal": "^3.1.3"
3437
}
3538
}

pnpm-lock.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/diff.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { HasUrlObject } from "./model.ts";
1+
import { HasUrlObject } from "./model";
2+
import equal from "fast-deep-equal/es6";
23

34
/**
45
* A special value which indicates that the difference between two objects is Irreconcilable.
@@ -186,30 +187,14 @@ function diffPrimitive<T extends { [key: string]: any }>(
186187
const yKeys = Object.keys(y);
187188
const deletedKeys = setDifference(xKeys, yKeys);
188189
const addedKeys = setDifference(yKeys, xKeys);
189-
const changedKeys = xKeys.filter((key) => !deepishEqual(x[key], y[key]));
190+
const changedKeys = xKeys.filter((key) => !equal(x[key], y[key]));
190191
const diffKeys = addedKeys.concat(changedKeys);
191192
return {
192193
...Object.fromEntries(deletedKeys.map((key) => [key, undefined])),
193194
...Object.fromEntries(diffKeys.map((key) => [key, y[key]])),
194195
};
195196
}
196197

197-
/**
198-
* Equality for arrays and primitives.
199-
*/
200-
function deepishEqual<T>(x: T, y: T): boolean {
201-
if (Array.isArray(x)) {
202-
if (!Array.isArray(y)) {
203-
return false;
204-
}
205-
return zipArrays(x, y).reduce(
206-
(same, [a, b]) => same && deepishEqual(a, b),
207-
true,
208-
);
209-
}
210-
return x === y;
211-
}
212-
213198
function zipArrays<X, Y>(x: X[], y: Y[]): [X, Y][] {
214199
return x.map((v, i) => [v, y[i]]);
215200
}

0 commit comments

Comments
 (0)