Skip to content

Commit

Permalink
[Refactor] reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 29, 2024
1 parent d939332 commit 2f76f26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@
var callBound = require('call-bind/callBound');
var hasToStringTag = require('has-tostringtag/shams')();
var hasOwn = require('hasown');
var $exec;
var isRegexMarker;
var badStringifier;
var gOPD = require('gopd');

var fn;
if (hasToStringTag) {
$exec = callBound('RegExp.prototype.exec');
isRegexMarker = {};
var $exec = callBound('RegExp.prototype.exec');
var isRegexMarker = {};

var throwRegexMarker = function () {
throw isRegexMarker;
};
badStringifier = {
var badStringifier = {
toString: throwRegexMarker,
valueOf: throwRegexMarker
};

if (typeof Symbol.toPrimitive === 'symbol') {
badStringifier[Symbol.toPrimitive] = throwRegexMarker;
}
}

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

module.exports = hasToStringTag
// eslint-disable-next-line consistent-return
? function isRegex(value) {
fn = function isRegex(value) {
if (!value || typeof value !== 'object') {
return false;
}
Expand All @@ -46,12 +39,19 @@ module.exports = hasToStringTag
} catch (e) {
return e === isRegexMarker;
}
}
: function isRegex(value) {
};
} else {
var $toString = callBound('Object.prototype.toString');
var regexClass = '[object RegExp]';

fn = function isRegex(value) {
// In older browsers, typeof regex incorrectly returns 'function'
if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
return false;
}

return $toString(value) === regexClass;
};
}

module.exports = fn;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
],
"dependencies": {
"call-bind": "^1.0.7",
"gopd": "^1.0.1",
"has-tostringtag": "^1.0.2",
"hasown": "^2.0.2"
},
Expand Down

0 comments on commit 2f76f26

Please sign in to comment.