Skip to content

Commit

Permalink
Use is-regex/is-date-object/is-number-object to properly check …
Browse files Browse the repository at this point in the history
…RegExps/Dates/Numbers, despite `@@toStringTag`.
  • Loading branch information
ljharb committed Jan 28, 2015
1 parent 7ff4bea commit c0be34d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
var ObjectPrototype = Object.prototype;
var toStr = ObjectPrototype.toString;
var has = ObjectPrototype.hasOwnProperty;
var isGenerator = require('is-generator-function');
var isArrowFunction = require('is-arrow-function');
var isDate = require('is-date-object');
var isGenerator = require('is-generator-function');
var isNumber = require('is-number-object');
var isRegex = require('is-regex');

var getPrototypeOf = Object.getPrototypeOf;
if (!getPrototypeOf) {
Expand All @@ -30,10 +33,7 @@ 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]';
var funcType = '[object Function]';
var v8GeneratorFuncType = '[object GeneratorFunction]';
Expand All @@ -51,15 +51,25 @@ module.exports = function isEqual(value, other) {

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

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

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

if (type === dateType) { return value.getTime() === other.getTime(); }
var valIsDate = isDate(value);
var otherIsDate = isDate(other);
if (valIsDate || otherIsDate) {
return valIsDate && otherIsDate && +value === +other;
}

if (type === regexType) { return String(value) === String(other); }
var valIsRegex = isRegex(value);
var otherIsRegex = isRegex(other);
if (valIsRegex || otherIsRegex) {
return valIsRegex && otherIsRegex && String(value) === String(other);
}

if (type === arrayType) {
if (value.length !== other.length) { return false; }
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
],
"dependencies": {
"is-arrow-function": "~2.0.1",
"is-generator-function": "~1.0.2"
"is-date-object": "~1.0.0",
"is-generator-function": "~1.0.2",
"is-number-object": "~1.0.0",
"is-regex": "~1.0.1"
},
"devDependencies": {
"tape": "~3.4.0",
Expand Down

0 comments on commit c0be34d

Please sign in to comment.