source: trip-planner-front/node_modules/safe-regex/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[6a3a178]1var parse = require('ret');
2var types = parse.types;
3
4module.exports = function (re, opts) {
5 if (!opts) opts = {};
6 var replimit = opts.limit === undefined ? 25 : opts.limit;
7
8 if (isRegExp(re)) re = re.source;
9 else if (typeof re !== 'string') re = String(re);
10
11 try { re = parse(re) }
12 catch (err) { return false }
13
14 var reps = 0;
15 return (function walk (node, starHeight) {
16 if (node.type === types.REPETITION) {
17 starHeight ++;
18 reps ++;
19 if (starHeight > 1) return false;
20 if (reps > replimit) return false;
21 }
22
23 if (node.options) {
24 for (var i = 0, len = node.options.length; i < len; i++) {
25 var ok = walk({ stack: node.options[i] }, starHeight);
26 if (!ok) return false;
27 }
28 }
29 var stack = node.stack || (node.value && node.value.stack);
30 if (!stack) return true;
31
32 for (var i = 0; i < stack.length; i++) {
33 var ok = walk(stack[i], starHeight);
34 if (!ok) return false;
35 }
36
37 return true;
38 })(re, 0);
39};
40
41function isRegExp (x) {
42 return {}.toString.call(x) === '[object RegExp]';
43}
Note: See TracBrowser for help on using the repository browser.