Skip to content

Commit

Permalink
enforcing +0 !== -0
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Apr 17, 2013
1 parent cfd09a2 commit fea4ddd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Expand Up @@ -9,8 +9,12 @@ var Object_keys = typeof Object.keys === 'function'
;

var deepEqual = module.exports = function (actual, expected) {
// enforce Object.is +0 !== -0
if (actual === 0 && expected === 0) {
return areZerosEqual(actual, expected);

// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
} else if (actual === expected) {
return true;

} else if (actual instanceof Date && expected instanceof Date) {
Expand All @@ -33,7 +37,7 @@ var deepEqual = module.exports = function (actual, expected) {
} else {
return objEquiv(actual, expected);
}
}
};

function isUndefinedOrNull(value) {
return value === null || value === undefined;
Expand All @@ -48,6 +52,11 @@ function isNumberNaN(value) {
return typeof value == 'number' && value !== value;
}

function areZerosEqual(zeroA, zeroB) {
// (1 / +0|0) -> Infinity, but (1 / -0) -> -Infinity and (Infinity !== -Infinity)
return (1 / zeroA) === (1 / zeroB);
}

function objEquiv(a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
Expand Down
15 changes: 15 additions & 0 deletions test/neg-vs-pos-0.js
@@ -0,0 +1,15 @@
var test = require('tape');
var equal = require('../');

test('0 values', function (t) {
t.ok(equal( 0, 0), ' 0 === 0');
t.ok(equal( 0, +0), ' 0 === +0');
t.ok(equal(+0, +0), '+0 === +0');
t.ok(equal(-0, -0), '-0 === -0');

t.notOk(equal(-0, 0), '-0 !== 0');
t.notOk(equal(-0, +0), '-0 !== +0');

t.end();
});

0 comments on commit fea4ddd

Please sign in to comment.