2020-01-12 07:50:11 +00:00
|
|
|
import { CONFIG_VALIDATION } from '../constants/error-messages';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { logger } from '../logger';
|
2019-08-25 12:29:51 +00:00
|
|
|
|
2020-05-07 08:23:45 +00:00
|
|
|
let RegEx: RegExpConstructor;
|
2019-09-09 10:21:01 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const RE2 = require('re2');
|
|
|
|
// Test if native is working
|
|
|
|
new RE2('.*').exec('test');
|
|
|
|
logger.debug('Using RE2 as regex engine');
|
|
|
|
RegEx = RE2;
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ err }, 'RE2 not usable, falling back to RegExp');
|
|
|
|
RegEx = RegExp;
|
2019-08-25 12:29:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
export function regEx(pattern: string, flags?: string): RegExp {
|
2019-08-25 12:29:51 +00:00
|
|
|
try {
|
2019-09-09 10:21:01 +00:00
|
|
|
return new RegEx(pattern, flags);
|
2019-08-25 12:29:51 +00:00
|
|
|
} catch (err) {
|
2020-01-12 07:50:11 +00:00
|
|
|
const error = new Error(CONFIG_VALIDATION);
|
2019-09-09 10:21:01 +00:00
|
|
|
error.configFile = pattern;
|
|
|
|
error.validationError = 'Invalid regular expression: ' + err.toString();
|
|
|
|
throw error;
|
2019-08-25 12:29:51 +00:00
|
|
|
}
|
2019-09-09 10:21:01 +00:00
|
|
|
}
|
2020-04-14 11:47:58 +00:00
|
|
|
|
|
|
|
export function escapeRegExp(input: string): string {
|
|
|
|
return input.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
|
|
}
|