2020-08-25 23:57:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
var callBound = require('call-bind/callBound');
|
2020-08-25 23:57:08 +00:00
|
|
|
var hasSymbols = require('has-symbols')();
|
|
|
|
var hasToStringTag = hasSymbols && typeof Symbol.toStringTag === 'symbol';
|
2021-02-26 03:58:33 +00:00
|
|
|
var has;
|
|
|
|
var $exec;
|
2020-08-25 23:57:08 +00:00
|
|
|
var isRegexMarker;
|
|
|
|
var badStringifier;
|
|
|
|
|
|
|
|
if (hasToStringTag) {
|
2021-02-26 03:58:33 +00:00
|
|
|
has = callBound('Object.prototype.hasOwnProperty');
|
|
|
|
$exec = callBound('RegExp.prototype.exec');
|
2020-08-25 23:57:08 +00:00
|
|
|
isRegexMarker = {};
|
|
|
|
|
|
|
|
var throwRegexMarker = function () {
|
|
|
|
throw isRegexMarker;
|
|
|
|
};
|
|
|
|
badStringifier = {
|
|
|
|
toString: throwRegexMarker,
|
|
|
|
valueOf: throwRegexMarker
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof Symbol.toPrimitive === 'symbol') {
|
|
|
|
badStringifier[Symbol.toPrimitive] = throwRegexMarker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
var $toString = callBound('Object.prototype.toString');
|
2020-08-25 23:57:08 +00:00
|
|
|
var gOPD = Object.getOwnPropertyDescriptor;
|
|
|
|
var regexClass = '[object RegExp]';
|
|
|
|
|
|
|
|
module.exports = hasToStringTag
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
? function isRegex(value) {
|
|
|
|
if (!value || typeof value !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptor = gOPD(value, 'lastIndex');
|
2021-02-26 03:58:33 +00:00
|
|
|
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
|
2020-08-25 23:57:08 +00:00
|
|
|
if (!hasLastIndexDataProperty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-02-26 03:58:33 +00:00
|
|
|
$exec(value, badStringifier);
|
2020-08-25 23:57:08 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
return $toString(value) === regexClass;
|
2020-08-25 23:57:08 +00:00
|
|
|
};
|