regex for advanced filter

This commit is contained in:
IgnatzHome 2018-06-14 21:14:16 +02:00
parent b5e6e6ee10
commit ba2012af43

View file

@ -109,12 +109,20 @@ class AdvancedFilter {
const commands = []; const commands = [];
let current = ''; let current = '';
let string = false; let string = false;
let regex = false;
let wasString = false; let wasString = false;
let ignore = false; let ignore = false;
for (let i = 0; i < this._filter.length; i++) { for (let i = 0; i < this._filter.length; i++) {
const char = this._filter.charAt(i); const char = this._filter.charAt(i);
if (ignore) { if (ignore) {
ignore = false; ignore = false;
current += char;
continue;
}
if (char === '/'){
string = !string;
if (string) regex = true;
current += char;
continue; continue;
} }
if (char === '\'') { if (char === '\'') {
@ -122,12 +130,12 @@ class AdvancedFilter {
if (string) wasString = true; if (string) wasString = true;
continue; continue;
} }
if (char === '\\') { if (char === '\\' && !string) {
ignore = true; ignore = true;
continue; continue;
} }
if (char === ' ' && !string) { if (char === ' ' && !string) {
commands.push({ 'cmd': current, 'string': wasString }); commands.push({ 'cmd': current, 'string': wasString, regex});
wasString = false; wasString = false;
current = ''; current = '';
continue; continue;
@ -135,7 +143,7 @@ class AdvancedFilter {
current += char; current += char;
} }
if (current !== '') { if (current !== '') {
commands.push({ 'cmd': current, 'string': wasString }); commands.push({ 'cmd': current, 'string': wasString, regex});
} }
return commands; return commands;
} }
@ -224,7 +232,20 @@ class AdvancedFilter {
{ {
const field = commands[i - 1].cmd; const field = commands[i - 1].cmd;
const str = commands[i + 1].cmd; const str = commands[i + 1].cmd;
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': {$in: [this._fieldValueToId(field, str), parseInt(str, 10)]} }; if (commands[i + 1].regex)
{
const match = str.match(new RegExp('^/(.*?)/([gimy]*)$'));
let regex = null;
if (match.length > 2)
regex = new RegExp(match[1], match[2]);
else
regex = new RegExp(match[1]);
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': regex };
}
else
{
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': {$in: [this._fieldValueToId(field, str), parseInt(str, 10)]} };
}
commands.splice(i - 1, 1); commands.splice(i - 1, 1);
commands.splice(i, 1); commands.splice(i, 1);
//changed = true; //changed = true;
@ -236,7 +257,20 @@ class AdvancedFilter {
{ {
const field = commands[i - 1].cmd; const field = commands[i - 1].cmd;
const str = commands[i + 1].cmd; const str = commands[i + 1].cmd;
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': { $not: {$in: [this._fieldValueToId(field, str), parseInt(str, 10)]} } }; if (commands[i + 1].regex)
{
const match = str.match(new RegExp('^/(.*?)/([gimy]*)$'));
let regex = null;
if (match.length > 2)
regex = new RegExp(match[1], match[2]);
else
regex = new RegExp(match[1]);
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': { $not: regex } };
}
else
{
commands[i] = { 'customFields._id': this._fieldNameToId(field), 'customFields.value': { $not: {$in: [this._fieldValueToId(field, str), parseInt(str, 10)]} } };
}
commands.splice(i - 1, 1); commands.splice(i - 1, 1);
commands.splice(i, 1); commands.splice(i, 1);
//changed = true; //changed = true;