Skip to content

Restyle add support for treating NaN values as equivalent #25

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions tests/nan.js
Original file line number Diff line number Diff line change
@@ -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();