Skip to content

Commit

Permalink
Merge pull request #25 from hwbllmnn/fix-write
Browse files Browse the repository at this point in the history
Fixes a couple of filter writing issues
  • Loading branch information
hwbllmnn authored Mar 8, 2019
2 parents ac83514 + 83e970d commit 929c138
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
40 changes: 38 additions & 2 deletions src/CqlParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'`;
Expand Down Expand Up @@ -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', () => {
Expand All @@ -80,7 +105,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];
Expand All @@ -102,12 +127,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);
});
});
});
17 changes: 3 additions & 14 deletions src/CqlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 '*=':
Expand All @@ -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:
Expand Down

0 comments on commit 929c138

Please sign in to comment.