Skip to content

Commit f46daa0

Browse files
add support for treating NaN values as equivalent (#24)
* add support for treating NaN values as equivalent * Simplify change check --------- Co-authored-by: Jacob Jackson <[email protected]>
1 parent ce20559 commit f46daa0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export default function diff(
7373
} else if (
7474
objKey !== newObjKey &&
7575
!(
76+
// treat NaN values as equivalent
77+
Number.isNaN(objKey) &&
78+
tNumber.isNaN(newObjKey) &&
7679
areCompatibleObjects &&
7780
(isNaN(objKey)
7881
? objKey + "" === newObjKey + ""

tests/nan.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { test } from "uvu";
2+
import * as assert from "uvu/assert";
3+
import diff from "../dist/index.js";
4+
5+
test("new NaN value in object", () => {
6+
assert.equal(diff({}, { testNaN: NaN }), [
7+
{
8+
type: "CREATE",
9+
path: ["testNaN"],
10+
value: NaN,
11+
},
12+
]);
13+
});
14+
test("change NaN value in object", () => {
15+
assert.equal(diff({ testNaN: NaN }, { testNaN: 0 }), [
16+
{
17+
type: "CHANGE",
18+
path: ["testNaN"],
19+
value: 0,
20+
oldValue: NaN,
21+
},
22+
]);
23+
});
24+
test("do not change NaN value in object", () => {
25+
assert.equal(diff({ testNaN: NaN }, { testNaN: NaN }), []);
26+
});
27+
test("remove NaN value in object", () => {
28+
assert.equal(diff({ testNaN: NaN }, {}), [
29+
{
30+
type: "REMOVE",
31+
path: ["testNaN"],
32+
oldValue: NaN,
33+
},
34+
]);
35+
});
36+
test("new NaN value in array", () => {
37+
assert.equal(diff([], [ NaN ]), [
38+
{
39+
type: "CREATE",
40+
path: [0],
41+
value: NaN,
42+
},
43+
]);
44+
});
45+
test("change NaN value in object", () => {
46+
assert.equal(diff([ NaN ], [ 0 ]), [
47+
{
48+
type: "CHANGE",
49+
path: [0],
50+
value: 0,
51+
oldValue: NaN,
52+
},
53+
]);
54+
});
55+
test("do not change NaN value in array", () => {
56+
assert.equal(diff([ NaN ], [ NaN ]), []);
57+
});
58+
test("remove NaN value in array", () => {
59+
assert.equal(diff([ NaN ], []), [
60+
{
61+
type: "REMOVE",
62+
path: [0],
63+
oldValue: NaN,
64+
},
65+
]);
66+
});
67+
68+
test.run();

0 commit comments

Comments
 (0)