From 8541f8f07f4a65c9ac6261c977d89a7169430dab Mon Sep 17 00:00:00 2001 From: Andreas Schmitz Date: Thu, 7 Mar 2019 13:25:30 +0100 Subject: [PATCH 1/2] Fixes a couple of filter writing issues --- src/CqlParser.spec.ts | 15 +++++++++++++-- src/CqlParser.ts | 17 +++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/CqlParser.spec.ts b/src/CqlParser.spec.ts index 4ea147f..e257cd9 100644 --- a/src/CqlParser.spec.ts +++ b/src/CqlParser.spec.ts @@ -80,7 +80,7 @@ describe('CqlParser', () => { it('can write String Comparison Filters', () => { const geoStylerFilter = ['==', 'Name', 'Peter']; const got = cqlParser.write(geoStylerFilter); - expect(got).toEqual('Name = Peter'); + expect(got).toEqual('Name = \'Peter\''); }); it('can write Number Comparison Filters', () => { const geoStylerFilter1 = ['==', 'Age', 12]; @@ -102,12 +102,23 @@ describe('CqlParser', () => { ['==', 'Name', 'Peter Schmidt'], ['==', 'Height', 1.75] ]; - const cqlFilter1 = 'Age = 12 AND Name = Peter'; + const cqlFilter1 = 'Age = 12 AND Name = \'Peter\''; const cqlFilter2 = 'Name = \'Peter Schmidt\' OR Height = 1.75'; const got1 = cqlParser.write(geoStylerFilter1); const got2 = cqlParser.write(geoStylerFilter2); expect(got1).toEqual(cqlFilter1); expect(got2).toEqual(cqlFilter2); }); + it('can write multiple Combination Filters', () => { + const geoStylerFilter1 = [ + '&&', + ['==', 'Age', 12], + ['==', 'Name', 'Peter'], + ['==', 'Car', 'Bentley'] + ]; + const cqlFilter1 = 'Age = 12 AND Name = \'Peter\' AND Car = \'Bentley\''; + const got1 = cqlParser.write(geoStylerFilter1); + expect(got1).toEqual(cqlFilter1); + }); }); }); diff --git a/src/CqlParser.ts b/src/CqlParser.ts index e01fddf..5bef2f7 100644 --- a/src/CqlParser.ts +++ b/src/CqlParser.ts @@ -150,7 +150,7 @@ export class CqlParser { tryToken(text: any, pattern: Pattern) { if (pattern instanceof RegExp) { return pattern.exec(text); - } else { + } else if (pattern) { return pattern(text); } } @@ -360,15 +360,7 @@ export class CqlParser { case '||': let cqlFilter: string = ''; const cqlCombinationOperator = combinationOperatorsReverseMap[operator]; - filter.forEach((filterElement, index: number) => { - if (index > 0) { - if (index === 1) { - cqlFilter += `${write(filter[index])} `; - } else { - cqlFilter += `${cqlCombinationOperator} ${write(filter[index])}`; - } - } - }); + cqlFilter += filter.slice(1).map(write).join(` ${cqlCombinationOperator} `); return cqlFilter; case '==': case '*=': @@ -380,10 +372,7 @@ export class CqlParser { const valueIsString = _isString(filter[2]); let value = filter[2]; if (valueIsString) { - const containWhiteSpaces = value.includes(' '); - if (containWhiteSpaces) { - value = `'${value}'`; - } + value = `'${value}'`; } return `${filter[1]} ${cqlOperator} ${value}`; case undefined: From 83e970d792ce224af4d2b62d871be2a8c0840a50 Mon Sep 17 00:00:00 2001 From: Andreas Schmitz Date: Thu, 7 Mar 2019 15:21:14 +0100 Subject: [PATCH 2/2] Add more test cases --- src/CqlParser.spec.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/CqlParser.spec.ts b/src/CqlParser.spec.ts index e257cd9..6c364a2 100644 --- a/src/CqlParser.spec.ts +++ b/src/CqlParser.spec.ts @@ -27,6 +27,11 @@ describe('CqlParser', () => { const got = cqlParser.read(cqlFilter); expect(got).toEqual(['==', 'Name', 'Peter']); }); + it('can read number Comparison Filters', () => { + const cqlFilter = 'Age = 12.3'; + const got = cqlParser.read(cqlFilter); + expect(got).toEqual(['==', 'Age', 12.3]); + }); it('can read Strings with quotation marks Comparison Filters', () => { const cqlFilter1 = `Name = "Peter"`; const cqlFilter2 = `Name = 'Peter'`; @@ -63,6 +68,26 @@ describe('CqlParser', () => { ] ); }); + it('can read Combination Filters with parens', () => { + const cqlFilter1 = '( Age = 12 AND Name = Peter )'; + const cqlFilter2 = '( Name = "Peter Schmidt" OR Height = 1.75 )'; + const got1 = cqlParser.read(cqlFilter1); + const got2 = cqlParser.read(cqlFilter2); + expect(got1).toEqual( + [ + '&&', + ['==', 'Age', 12], + ['==', 'Name', 'Peter'] + ] + ); + expect(got2).toEqual( + [ + '||', + ['==', 'Name', 'Peter Schmidt'], + ['==', 'Height', 1.75] + ] + ); + }); }); describe('#write', () => {