-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-regexp-x.js
50 lines (41 loc) · 1.25 KB
/
is-regexp-x.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
import isObjectLike from 'is-object-like-x';
import hasToStringTag from 'has-to-string-tag-x';
import has from 'has-own-property-x';
import gOPD from 'object-get-own-property-descriptor-x';
import defineProperty from 'object-define-property-x';
import toStringTag from 'to-string-tag-x';
import methodize from 'simple-methodize-x';
const regexExec = methodize(/none/.exec);
const regexClass = '[object RegExp]';
const tryRegexExecCall = function tryRegexExec(value, descriptor) {
try {
value.lastIndex = 0;
regexExec(value);
return true;
} catch (e) {
return false;
} finally {
defineProperty(value, 'lastIndex', descriptor);
}
};
/**
* This method tests if a value is a regex.
*
* @param {*} value - The value to test.
* @returns {boolean} `true` if value is a regex; otherwise `false`.
*/
const isRegex = function isRegex(value) {
if (isObjectLike(value) === false) {
return false;
}
if (hasToStringTag === false) {
return toStringTag(value) === regexClass;
}
const descriptor = gOPD(value, 'lastIndex');
const hasLastIndexDataProperty = descriptor && has(descriptor, 'value');
if (hasLastIndexDataProperty !== true) {
return false;
}
return tryRegexExecCall(value, descriptor);
};
export default isRegex;