Skip to content

add support for treating NaN values as equivalent #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();