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 Dec 1, 2024
1 parent 20fde50 commit ac86dd7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';

var strValue = String.prototype.valueOf;
var callBound = require('call-bind/callBound');

var $strValueOf = callBound('String.prototype.valueOf');

var tryStringObject = function tryStringObject(value) {
try {
strValue.call(value);
$strValueOf(value);
return true;
} catch (e) {
return false;
}
};
var toStr = Object.prototype.toString;
var $toString = callBound('Object.prototype.toString');
var strClass = '[object String]';
var hasToStringTag = require('has-tostringtag/shams')();

Expand All @@ -20,5 +23,5 @@ module.exports = function isString(value) {
if (!value || typeof value !== 'object') {
return false;
}
return hasToStringTag ? tryStringObject(value) : toStr.call(value) === strClass;
return hasToStringTag ? tryStringObject(value) : $toString(value) === strClass;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"String object"
],
"dependencies": {
"call-bind": "^1.0.7",
"has-tostringtag": "^1.0.2"
},
"devDependencies": {
Expand Down

0 comments on commit ac86dd7

Please sign in to comment.