-
Notifications
You must be signed in to change notification settings - Fork 0
/
isRegex.js
57 lines (45 loc) · 1.41 KB
/
isRegex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @file Is this value a JS regex?
* @copyright Copyright (c) 2018-present, Graham Fairweather
* @module isRegex
*/
import attempt from './attempt';
import toStringTag from './toStringTag';
import isObjectLike from './isObjectLike';
import isToStringTagSupported from './isToStringTagSupported';
import _hasOwnProperty from './.internal/_hasOwnProperty';
import _exec from './.internal/_exec';
import _getOwnPropertyDescriptor from './.internal/_getOwnPropertyDescriptor';
const regexClass = '[object RegExp]';
const tryRegexExecCall = function _tryRegexExecCall(value) {
const result = attempt(function _attemptee() {
const storedLastIndex = value.lastIndex;
// eslint-disable-next-line no-param-reassign
value.lastIndex = 0;
_exec(value);
return storedLastIndex;
});
/* istanbul ignore next */
if (result.threw) {
return false;
}
// eslint-disable-next-line no-param-reassign
value.lastIndex = result.value;
return true;
};
export default function isRegex(value) {
if (!isObjectLike(value)) {
return false;
}
/* istanbul ignore next */
if (!isToStringTagSupported) {
return toStringTag(value) === regexClass;
}
const descriptor = _getOwnPropertyDescriptor(value, 'lastIndex');
const hasLastIndexDataProperty =
descriptor && _hasOwnProperty(descriptor, 'value');
if (!hasLastIndexDataProperty) {
return false;
}
return tryRegexExecCall(value);
}