source: trip-planner-front/node_modules/regex-not/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.6 KB
Line 
1'use strict';
2
3var extend = require('extend-shallow');
4var safe = require('safe-regex');
5
6/**
7 * The main export is a function that takes a `pattern` string and an `options` object.
8 *
9 * ```js
10 & var not = require('regex-not');
11 & console.log(not('foo'));
12 & //=> /^(?:(?!^(?:foo)$).)*$/
13 * ```
14 *
15 * @param {String} `pattern`
16 * @param {Object} `options`
17 * @return {RegExp} Converts the given `pattern` to a regex using the specified `options`.
18 * @api public
19 */
20
21function toRegex(pattern, options) {
22 return new RegExp(toRegex.create(pattern, options));
23}
24
25/**
26 * Create a regex-compatible string from the given `pattern` and `options`.
27 *
28 * ```js
29 & var not = require('regex-not');
30 & console.log(not.create('foo'));
31 & //=> '^(?:(?!^(?:foo)$).)*$'
32 * ```
33 * @param {String} `pattern`
34 * @param {Object} `options`
35 * @return {String}
36 * @api public
37 */
38
39toRegex.create = function(pattern, options) {
40 if (typeof pattern !== 'string') {
41 throw new TypeError('expected a string');
42 }
43
44 var opts = extend({}, options);
45 if (opts.contains === true) {
46 opts.strictNegate = false;
47 }
48
49 var open = opts.strictOpen !== false ? '^' : '';
50 var close = opts.strictClose !== false ? '$' : '';
51 var endChar = opts.endChar ? opts.endChar : '+';
52 var str = pattern;
53
54 if (opts.strictNegate === false) {
55 str = '(?:(?!(?:' + pattern + ')).)' + endChar;
56 } else {
57 str = '(?:(?!^(?:' + pattern + ')$).)' + endChar;
58 }
59
60 var res = open + str + close;
61 if (opts.safe === true && safe(res) === false) {
62 throw new Error('potentially unsafe regular expression: ' + res);
63 }
64
65 return res;
66};
67
68/**
69 * Expose `toRegex`
70 */
71
72module.exports = toRegex;
Note: See TracBrowser for help on using the repository browser.