From 1c0c5bde0f39a1f3b7b6a125148f6dbda6803658 Mon Sep 17 00:00:00 2001 From: IgnatzHome Date: Sun, 20 May 2018 11:15:57 +0200 Subject: More conditions and logic Operators, Also Brackets. --- client/lib/filter.js | 152 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 148 insertions(+), 4 deletions(-) diff --git a/client/lib/filter.js b/client/lib/filter.js index c087ca78..db2dd89f 100644 --- a/client/lib/filter.js +++ b/client/lib/filter.js @@ -158,15 +158,61 @@ class AdvancedFilter { console.log('Parts: ', JSON.stringify(commands)); try { //let changed = false; - this._processConditions(commands); - console.log('Conditions: ', JSON.stringify(commands)); - this._processLogicalOperators(commands); - console.log('Operator: ', JSON.stringify(commands)); + this._processSubCommands(commands); } catch (e){return { $in: [] };} return {$or: commands}; } + _processSubCommands(commands) + { + console.log('SubCommands: ', JSON.stringify(commands)); + const subcommands = []; + let level = 0; + let start = -1; + for (let i = 0; i < commands.length; i++) + { + if (!commands[i].string && commands[i].cmd) + { + switch (commands[i].cmd) + { + case '(': + { + level++; + if (start === -1) start = i; + continue; + } + case ')': + { + level--; + commands.splice(i, 1); + i--; + continue; + } + default: + { + if (level > 0) + { + subcommands.push(commands[i]); + commands.splice(i, 1); + i--; + continue; + } + } + } + } + } + if (start !== -1) + { + this._processSubCommands(subcommands); + commands.splice(start, 0, subcommands); + } + this._processConditions(commands); + console.log('Conditions: ', JSON.stringify(commands)); + this._processLogicalOperators(commands); + console.log('Operator: ', JSON.stringify(commands)); + } + _processConditions(commands) { for (let i = 0; i < commands.length; i++) @@ -188,6 +234,76 @@ class AdvancedFilter { i--; break; } + case '!=': + case '!==': + { + const field = commands[i-1].cmd; + const str = commands[i+1].cmd; + commands[i] = {$not: {'customFields._id':this._fieldNameToId(field), 'customFields.value':str}}; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } + case '>': + case 'gt': + case 'Gt': + case 'GT': + { + const field = commands[i-1].cmd; + const str = commands[i+1].cmd; + commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $gt: str } }; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } + case '>=': + case '>==': + case 'gte': + case 'Gte': + case 'GTE': + { + const field = commands[i-1].cmd; + const str = commands[i+1].cmd; + commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $gte: str } }; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } + case '<': + case 'lt': + case 'Lt': + case 'LT': + { + const field = commands[i-1].cmd; + const str = commands[i+1].cmd; + commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $lt: str } }; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } + case '<=': + case '<==': + case 'lte': + case 'Lte': + case 'LTE': + { + const field = commands[i-1].cmd; + const str = commands[i+1].cmd; + commands[i] = {'customFields._id':this._fieldNameToId(field), 'customFields.value': { $lte: str } }; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } } } @@ -217,6 +333,34 @@ class AdvancedFilter { i--; break; } + case 'and': + case 'And': + case 'AND': + case '&': + case '&&': + { + const op1 = commands[i-1]; + const op2 = commands[i+1]; + commands[i] = {$and: [op1, op2]}; + commands.splice(i-1, 1); + commands.splice(i, 1); + //changed = true; + i--; + break; + } + + case 'not': + case 'Not': + case 'NOT': + case '!': + { + const op1 = commands[i+1]; + commands[i] = {$not: op1}; + commands.splice(i+1, 1); + //changed = true; + i--; + break; + } } } -- cgit v1.2.3-1-g7c22