From e850edcdda9362a95839617fc17547b74b848634 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Thu, 9 Feb 2017 16:11:32 +0530 Subject: [PATCH] Fix. Do not mutate the input raw Query --- index.js | 16 ++++++++++------ test.js | 10 ++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7e5db10..bfbe4ee 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ var functionOperators = [ /* ---- */ 'OR', 'AND', + 'EQ', + '=', /* ---- */ 'OR_BETWEEN', 'OR_IN', @@ -20,6 +22,8 @@ var functionOperatorMap = { /* ---- */ OR: 'orWhere', AND: 'where', + EQ: 'where', + '=': 'where', /* ---- */ OR_BETWEEN: 'orWhereBetween', OR_IN: 'orWhereIn', @@ -33,19 +37,19 @@ var functionOperatorMap = { function addCondition (q, field, val) { if (Array.isArray(val[0])) { return q.where(function () { - return val.forEach(addCondition.bind(null, this, field)) - }) + return val.forEach(addCondition.bind(null, this, field)); + }); } if (!Array.isArray(val)) { - val = ['AND', field, val ] + val = ['AND', field, val ]; } else if (functionOperators.indexOf(val[0]) !== -1) { - val.splice(1, 0, field) + val = [ val[0], field ].concat(val.slice(1)); } else { - val = [ 'AND', field ].concat(val) + val = [ 'AND', field ].concat(val); } - return q[functionOperatorMap[val[0]]].apply(q, val.slice(1)) + return q[functionOperatorMap[val[0]]].apply(q, val.slice(1)); } diff --git a/test.js b/test.js index b213d1e..258b40c 100644 --- a/test.js +++ b/test.js @@ -12,6 +12,16 @@ var conditions= [ input:{ f1: 10, f2: 20, f3: 30 }, output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)' }, + { + name: 'handle simple and conditions', + input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]}, + output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))' + }, + { + name: 'handle simple and conditions', + input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 }, + output: 'select * where (("f1" = \'A\' and "f1" LIKE \'B\') and "f2" = 20 and "f3" = 30)' + }, { name: 'handle basic operators :: greater-than less-than', input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 },