Skip to content

Commit

Permalink
Adding support for boxed primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 23, 2014
1 parent 675fc60 commit a074e04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ if (!getPrototypeOf) {
}
}

var boolType = '[object Boolean]';
var numberType = '[object Number]';
var stringType = '[object String]';
var dateType = '[object Date]';
var regexType = '[object RegExp]';
var arrayType = '[object Array]';
Expand All @@ -39,6 +42,14 @@ module.exports = function isEqual(value, other) {
var type = toString.call(value);
if (type !== toString.call(other)) { return false; }

if (type === boolType) { return value.valueOf() === other.valueOf(); }

if (type === numberType) {
return (Number(value) === Number(other)) || (isNaN(value) && isNaN(other));
}

if (type === stringType) { return String(value) === String(other); }

if (type === dateType) { return value.getTime() === other.getTime(); }

if (type === regexType) { return String(value) === String(other); }
Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ test('primitives', function (t) {
t.ok(isEqual('foo', 'foo'), 'strings are equal');
t.ok(isEqual(42, 42), 'numbers are equal');
t.ok(isEqual(0 / Infinity, -0 / Infinity), 'opposite sign zeroes are equal');
t.ok(isEqual(Infinity, Infinity), 'infinities are equal');
t.end();
});

test('NaN', function (t) {
t.ok(isEqual(NaN, NaN), 'NaNs are equal');
t.end();
});

test('boxed primitives', function (t) {
t.ok(isEqual(new String(''), ''), 'Empty String and empty string are equal');
t.ok(isEqual(new String('foo'), 'foo'), 'String and string are equal');
t.ok(isEqual(new Boolean(true), true), 'Boolean true and boolean true are equal');
t.ok(isEqual(new Boolean(false), false), 'Boolean false and boolean false are equal');
t.ok(isEqual(new Number(42), 42), 'Number and number literal are equal');
t.end();
});

Expand Down

0 comments on commit a074e04

Please sign in to comment.