Skip to content

Commit 944656d

Browse files
committed
Add support for NaN value
NaN cannot be checked as equality. This commit add a test based on isNaN
1 parent 6296889 commit 944656d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/diff/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { isDate, isEmpty, isObject, properObject } from '../utils';
33
const diff = (lhs, rhs) => {
44
if (lhs === rhs) return {}; // equal return no diff
55

6+
if (typeof lhs === "number" && typeof rhs === "number") {
7+
if (isNaN(lhs) && isNaN(rhs)) return {}; // NaN is equal
8+
}
9+
610
if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs
711

812
const l = properObject(lhs);

src/diff/index.test.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('.diff', () => {
1313
['object', { a: 1 }],
1414
['array', [1]],
1515
['function', () => ({})],
16+
['NaN', NaN],
1617
['date', new Date()],
1718
['date with milliseconds', new Date('2017-01-01T00:00:00.637Z')],
1819
])('returns empty object when given values of type %s are equal', (type, value) => {
@@ -56,7 +57,7 @@ describe('.diff', () => {
5657
});
5758

5859
test('returns subset of right hand side value when nested values differ', () => {
59-
expect(diff({ a: { b: 1, c: 2} }, { a: { b: 1, c: 3 } })).toEqual({ a: { c: 3 } });
60+
expect(diff({ a: { b: 1, c: 2 } }, { a: { b: 1, c: 3 } })).toEqual({ a: { c: 3 } });
6061
});
6162

6263
test('returns subset of right hand side value when nested values differ at multiple paths', () => {
@@ -72,7 +73,7 @@ describe('.diff', () => {
7273
});
7374

7475
test('returns keys as undefined when deleted from right hand side', () => {
75-
expect(diff({ a: 1, b: { c: 2 }}, { a: 1 })).toEqual({ b: undefined });
76+
expect(diff({ a: 1, b: { c: 2 } }, { a: 1 })).toEqual({ b: undefined });
7677
});
7778
});
7879

@@ -139,7 +140,7 @@ describe('.diff', () => {
139140

140141
test('returns subset of right hand side value when nested values differ', () => {
141142
const lhs = Object.create(null);
142-
lhs.a = { b: 1, c: 2};
143+
lhs.a = { b: 1, c: 2 };
143144
const rhs = Object.create(null);
144145
rhs.a = { b: 1, c: 3 };
145146
expect(diff(lhs, rhs)).toEqual({ a: { c: 3 } });
@@ -173,5 +174,15 @@ describe('.diff', () => {
173174
expect(diff(lhs, rhs)).toEqual({ b: 2 });
174175
});
175176
});
177+
178+
describe('nested NaN', () => {
179+
test('returns empty object when there is nested NaN value', () => {
180+
expect(diff({ a: 1, b: NaN }, { a: 1, b: NaN })).toEqual({});
181+
});
182+
183+
test('returns subset of right hand side when a left hand side value is not a NaN', () => {
184+
expect(diff({ a: 1, b: 2 }, { a: 1, b: NaN })).toEqual({ b: NaN });
185+
});
186+
});
176187
});
177188
});

0 commit comments

Comments
 (0)