1 | 'use strict';
|
---|
2 |
|
---|
3 | var callBound = require('call-bind/callBound');
|
---|
4 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
5 | var has;
|
---|
6 | var $exec;
|
---|
7 | var isRegexMarker;
|
---|
8 | var badStringifier;
|
---|
9 |
|
---|
10 | if (hasToStringTag) {
|
---|
11 | has = callBound('Object.prototype.hasOwnProperty');
|
---|
12 | $exec = callBound('RegExp.prototype.exec');
|
---|
13 | isRegexMarker = {};
|
---|
14 |
|
---|
15 | var throwRegexMarker = function () {
|
---|
16 | throw isRegexMarker;
|
---|
17 | };
|
---|
18 | badStringifier = {
|
---|
19 | toString: throwRegexMarker,
|
---|
20 | valueOf: throwRegexMarker
|
---|
21 | };
|
---|
22 |
|
---|
23 | if (typeof Symbol.toPrimitive === 'symbol') {
|
---|
24 | badStringifier[Symbol.toPrimitive] = throwRegexMarker;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | var $toString = callBound('Object.prototype.toString');
|
---|
29 | var gOPD = Object.getOwnPropertyDescriptor;
|
---|
30 | var regexClass = '[object RegExp]';
|
---|
31 |
|
---|
32 | module.exports = hasToStringTag
|
---|
33 | // eslint-disable-next-line consistent-return
|
---|
34 | ? function isRegex(value) {
|
---|
35 | if (!value || typeof value !== 'object') {
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | var descriptor = gOPD(value, 'lastIndex');
|
---|
40 | var hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
|
---|
41 | if (!hasLastIndexDataProperty) {
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | try {
|
---|
46 | $exec(value, badStringifier);
|
---|
47 | } catch (e) {
|
---|
48 | return e === isRegexMarker;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | : function isRegex(value) {
|
---|
52 | // In older browsers, typeof regex incorrectly returns 'function'
|
---|
53 | if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return $toString(value) === regexClass;
|
---|
58 | };
|
---|