source: imaps-frontend/node_modules/is-regex/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[d565449]1'use strict';
2
3var callBound = require('call-bind/callBound');
4var hasToStringTag = require('has-tostringtag/shams')();
5var has;
6var $exec;
7var isRegexMarker;
8var badStringifier;
9
10if (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
28var $toString = callBound('Object.prototype.toString');
29var gOPD = Object.getOwnPropertyDescriptor;
30var regexClass = '[object RegExp]';
31
32module.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 };
Note: See TracBrowser for help on using the repository browser.