Skip to content

Commit

Permalink
Use try/catch for a more reliable isString check, even with ES6 @@toS…
Browse files Browse the repository at this point in the history
…tringTag
  • Loading branch information
ljharb committed Jan 28, 2015
1 parent c7c47c1 commit 2e64fe6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ var isBoolean = function isBoolean(value) {
return false;
}
};
var stringType = '[object String]';

var strToStr = String.prototype.valueOf;
var isString = function isString(value) {
try {
strToStr.call(value);
return true;
} catch (e) {
return false;
}
};
var arrayType = '[object Array]';
var objType = '[object Object]';

Expand Down Expand Up @@ -73,7 +82,11 @@ module.exports = function isEqual(value, other) {
return valIsNumber && otherIsNumber && (Number(value) === Number(other) || (isNaN(value) && isNaN(other)));
}

if (type === stringType) { return String(value) === String(other); }
var valIsString = isString(value);
var otherIsString = isString(other);
if (valIsString || otherIsString) {
return valIsString && otherIsString && String(value) === String(other);
}

var valIsDate = isDate(value);
var otherIsDate = isDate(other);
Expand Down

0 comments on commit 2e64fe6

Please sign in to comment.