diff --git a/index.ts b/index.ts index f4feb2f..5dfbf91 100644 --- a/index.ts +++ b/index.ts @@ -55,6 +55,11 @@ export default function diff( ); } else if ( objKey !== newObjKey && + !( + // treat NaN values as equivalent + typeof objKey === "number" && isNaN(objKey) && + typeof newObjKey === "number" && isNaN(newObjKey) + ) && !( areObjects && (isNaN(objKey) diff --git a/tests/nan.js b/tests/nan.js new file mode 100644 index 0000000..e55ea84 --- /dev/null +++ b/tests/nan.js @@ -0,0 +1,69 @@ +import { test } from "uvu"; +import * as assert from "uvu/assert"; + +import diff from "../dist/index.js"; + +test("new NaN value in object", () => { + assert.equal(diff({}, { testNaN: NaN }), [ + { + type: "CREATE", + path: ["testNaN"], + value: NaN, + }, + ]); +}); +test("change NaN value in object", () => { + assert.equal(diff({ testNaN: NaN }, { testNaN: 0 }), [ + { + type: "CHANGE", + path: ["testNaN"], + value: 0, + oldValue: NaN, + }, + ]); +}); +test("do not change NaN value in object", () => { + assert.equal(diff({ testNaN: NaN }, { testNaN: NaN }), []); +}); +test("remove NaN value in object", () => { + assert.equal(diff({ testNaN: NaN }, {}), [ + { + type: "REMOVE", + path: ["testNaN"], + oldValue: NaN, + }, + ]); +}); +test("new NaN value in array", () => { + assert.equal(diff([], [NaN]), [ + { + type: "CREATE", + path: [0], + value: NaN, + }, + ]); +}); +test("change NaN value in object", () => { + assert.equal(diff([NaN], [0]), [ + { + type: "CHANGE", + path: [0], + value: 0, + oldValue: NaN, + }, + ]); +}); +test("do not change NaN value in array", () => { + assert.equal(diff([NaN], [NaN]), []); +}); +test("remove NaN value in array", () => { + assert.equal(diff([NaN], []), [ + { + type: "REMOVE", + path: [0], + oldValue: NaN, + }, + ]); +}); + +test.run();