Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createTest(config = {}) {
const mode = config.mode;
let title = config.title;
if ( ! title || ! mode )
return () => false;
if ( mode === 'text' )
title = escape_regex(title);
else if ( mode === 'glob' )
title = glob_to_regex(title);
else if ( mode !== 'raw' )
return () => false;
if ( ! safety(title) )
return () => false;
let regex;
try {
regex = new RegExp(title, `g${config.sensitive ? '' : 'i'}`);
} catch(err) {
return () => false;
}
return ctx => {
regex.lastIndex = 0;
return ctx.title && regex.test(ctx.title);
}
},
either.chain(t.string.validate(from, to), s => {
const advancedSyntaxMatch = s.match(regexWithFlags);
const [regexStr, flags] = advancedSyntaxMatch ? [advancedSyntaxMatch[1], advancedSyntaxMatch[2]] : [s, ""];
const finalFlags = flags
.split("")
.filter(flag => safeRegexAllowedFlags.includes(flag))
.join("");
return safeRegex(regexStr) ? t.success(new RegExp(regexStr, finalFlags)) : t.failure(from, to, "Unsafe regex");
}),
s => `/${s.source}/${s.flags}`,
is_safe() {
const data = this.display,
t = data.t;
let v = data.v;
if ( t === 'text' )
v = escape_regex(v);
else if ( t === 'glob' )
v = glob_to_regex(v);
return safety(v);
},