Skip to content

Commit

Permalink
[Robustness] use call-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 1, 2021
1 parent 9f84b99 commit fbb61bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';

var callBound = require('call-bind/callBound');
var hasSymbols = require('has-symbols')();
var hasToStringTag = hasSymbols && typeof Symbol.toStringTag === 'symbol';
var hasOwnProperty;
var regexExec;
var has;
var $exec;
var isRegexMarker;
var badStringifier;

if (hasToStringTag) {
hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
regexExec = Function.call.bind(RegExp.prototype.exec);
has = callBound('Object.prototype.hasOwnProperty');
$exec = callBound('RegExp.prototype.exec');
isRegexMarker = {};

var throwRegexMarker = function () {
Expand All @@ -25,7 +26,7 @@ if (hasToStringTag) {
}
}

var toStr = Object.prototype.toString;
var $toString = callBound('Object.prototype.toString');
var gOPD = Object.getOwnPropertyDescriptor;
var regexClass = '[object RegExp]';

Expand All @@ -37,13 +38,13 @@ module.exports = hasToStringTag
}

var descriptor = gOPD(value, 'lastIndex');
var hasLastIndexDataProperty = descriptor && hasOwnProperty(descriptor, 'value');
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
if (!hasLastIndexDataProperty) {
return false;
}

try {
regexExec(value, badStringifier);
$exec(value, badStringifier);
} catch (e) {
return e === isRegexMarker;
}
Expand All @@ -54,5 +55,5 @@ module.exports = hasToStringTag
return false;
}

return toStr.call(value) === regexClass;
return $toString(value) === regexClass;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"expression"
],
"dependencies": {
"call-bind": "^1.0.2",
"has-symbols": "^1.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit fbb61bf

Please sign in to comment.