Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixing NaN comparison #5

Closed
wants to merge 1 commit into from

1 participant

@thlorenz
  • added test comparing NaNs that failed intitially
  • fixed equal to make test pass
@thlorenz thlorenz fixing NaN comparison
- added test comparing NaNs that failed intitially
- fixed equal to make test pass
b32ee66
@thlorenz thlorenz referenced this pull request in thlorenz/scriptie-talkie
Closed

Variable expressions coerce `NaN` to `undefined` #1

@thlorenz

Will publish as deep-is since I got convinced that deep-equal needs to stay consistent with node core and related modules.

@thlorenz thlorenz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 10, 2013
  1. @thlorenz

    fixing NaN comparison

    thlorenz authored
    - added test comparing NaNs that failed intitially
    - fixed equal to make test pass
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 0 deletions.
  1. +9 −0 index.js
  2. +14 −0 test/cmp.js
View
9 index.js
@@ -16,6 +16,9 @@ var deepEqual = module.exports = function (actual, expected) {
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
+ } else if (isNumberNaN(actual)) {
+ return isNumberNaN(expected);
+
// 7.3. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') {
@@ -40,9 +43,15 @@ function isArguments(object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
+function isNumberNaN(value) {
+ // NaN === NaN -> false
+ return typeof value == 'number' && value !== value;
+}
+
function objEquiv(a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
+
// an identical 'prototype' property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
View
14 test/cmp.js
@@ -21,3 +21,17 @@ test('nested nulls', function (t) {
t.ok(equal([ null, null, null ], [ null, null, null ]));
t.end();
});
+
+test('NaN and 0 values', function (t) {
+ t.ok(equal(NaN, NaN));
+ t.notOk(equal(0, NaN));
+ t.ok(equal(0, 0));
+ t.notOk(equal(0, 1));
+ t.end();
+});
+
+
+test('nested NaN values', function (t) {
+ t.ok(equal([ NaN, 1, NaN ], [ NaN, 1, NaN ]));
+ t.end();
+});
Something went wrong with that request. Please try again.