Skip to content

Commit

Permalink
add support for treating NaN values as equivalent (#24)
Browse files Browse the repository at this point in the history
* add support for treating NaN values as equivalent

* Simplify change check

---------

Co-authored-by: Jacob Jackson <[email protected]>
  • Loading branch information
brainthinks and AsyncBanana authored Mar 19, 2024
1 parent ce20559 commit f46daa0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export default function diff(
} else if (
objKey !== newObjKey &&
!(
// treat NaN values as equivalent
Number.isNaN(objKey) &&
tNumber.isNaN(newObjKey) &&
areCompatibleObjects &&
(isNaN(objKey)
? objKey + "" === newObjKey + ""
Expand Down
68 changes: 68 additions & 0 deletions tests/nan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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();

0 comments on commit f46daa0

Please sign in to comment.