mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
1721034a40
PR-URL: https://github.com/nodejs/node/pull/28525 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
40 lines
918 B
JavaScript
40 lines
918 B
JavaScript
'use strict';
|
|
|
|
var has = require('has');
|
|
var regexExec = RegExp.prototype.exec;
|
|
var gOPD = Object.getOwnPropertyDescriptor;
|
|
|
|
var tryRegexExecCall = function tryRegexExec(value) {
|
|
try {
|
|
var lastIndex = value.lastIndex;
|
|
value.lastIndex = 0;
|
|
|
|
regexExec.call(value);
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
} finally {
|
|
value.lastIndex = lastIndex;
|
|
}
|
|
};
|
|
var toStr = Object.prototype.toString;
|
|
var regexClass = '[object RegExp]';
|
|
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
|
|
|
module.exports = function isRegex(value) {
|
|
if (!value || typeof value !== 'object') {
|
|
return false;
|
|
}
|
|
if (!hasToStringTag) {
|
|
return toStr.call(value) === regexClass;
|
|
}
|
|
|
|
var descriptor = gOPD(value, 'lastIndex');
|
|
var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
|
|
if (!hasLastIndexDataProperty) {
|
|
return false;
|
|
}
|
|
|
|
return tryRegexExecCall(value);
|
|
};
|