Skip to content

Commit

Permalink
[Robustness] use call-bind to avoid a dependency on .call
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 6, 2020
1 parent 5cb2405 commit 76d87ae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict';

var boolToStr = Boolean.prototype.toString;
var callBound = require('call-bind/callBound');
var $boolToStr = callBound('Boolean.prototype.toString');
var $toString = callBound('Object.prototype.toString');

var tryBooleanObject = function booleanBrandCheck(value) {
try {
boolToStr.call(value);
$boolToStr(value);
return true;
} catch (e) {
return false;
}
};
var toStr = Object.prototype.toString;
var boolClass = '[object Boolean]';
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';

Expand All @@ -21,5 +22,5 @@ module.exports = function isBoolean(value) {
if (value === null || typeof value !== 'object') {
return false;
}
return hasToStringTag && Symbol.toStringTag in value ? tryBooleanObject(value) : toStr.call(value) === boolClass;
return hasToStringTag && Symbol.toStringTag in value ? tryBooleanObject(value) : $toString(value) === boolClass;
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"false",
"is-boolean"
],
"dependencies": {},
"dependencies": {
"call-bind": "^1.0.0"
},
"devDependencies": {
"@ljharb/eslint-config": "^17.3.0",
"aud": "^1.1.3",
Expand Down

0 comments on commit 76d87ae

Please sign in to comment.