You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
50 lines
1.1 KiB
4 years ago
|
'use strict';
|
||
|
|
||
|
var hasSymbols = require('has-symbols')();
|
||
|
var hasToStringTag = hasSymbols && typeof Symbol.toStringTag === 'symbol';
|
||
|
var regexExec;
|
||
|
var isRegexMarker;
|
||
|
var badStringifier;
|
||
|
|
||
|
if (hasToStringTag) {
|
||
|
regexExec = Function.call.bind(RegExp.prototype.exec);
|
||
|
isRegexMarker = {};
|
||
|
|
||
|
var throwRegexMarker = function () {
|
||
|
throw isRegexMarker;
|
||
|
};
|
||
|
badStringifier = {
|
||
|
toString: throwRegexMarker,
|
||
|
valueOf: throwRegexMarker
|
||
|
};
|
||
|
|
||
|
if (typeof Symbol.toPrimitive === 'symbol') {
|
||
|
badStringifier[Symbol.toPrimitive] = throwRegexMarker;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var toStr = Object.prototype.toString;
|
||
|
var regexClass = '[object RegExp]';
|
||
|
|
||
|
module.exports = hasToStringTag
|
||
|
// eslint-disable-next-line consistent-return
|
||
|
? function isRegex(value) {
|
||
|
if (!value || typeof value !== 'object') {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
regexExec(value, badStringifier);
|
||
|
} catch (e) {
|
||
|
return e === isRegexMarker;
|
||
|
}
|
||
|
}
|
||
|
: function isRegex(value) {
|
||
|
// In older browsers, typeof regex incorrectly returns 'function'
|
||
|
if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return toStr.call(value) === regexClass;
|
||
|
};
|