Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce RangeFilter and the corresponding operator "<=x<=" #275

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions examples/parsersample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Style, StyleParser, UnsupportedProperties } from 'index';
import { Style, StyleParser, UnsupportedProperties } from '../index';
import { ReadStyleResult, WriteStyleResult } from '../style';

export class SampleParser implements StyleParser {

Expand All @@ -20,15 +21,21 @@ export class SampleParser implements StyleParser {

title = 'Sample Parser';

writeStyle(geoStylerStyle: Style): Promise<string> {
return new Promise<string>(() => 'sample');
writeStyle(geoStylerStyle: Style): Promise<WriteStyleResult> {
return new Promise<WriteStyleResult>(() => {
return {
output: 'sample'
};
});
}

readStyle(sldString: string): Promise<Style> {
return new Promise<Style>(() => {
readStyle(sldString: string): Promise<ReadStyleResult> {
return new Promise<ReadStyleResult>(() => {
return {
name: 'Samplestyle',
rules: []
output: {
name: 'Samplestyle',
rules: []
}
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rastersample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Style } from 'index';
import { Style } from '../index';

const sampleRasterStyle: Style = {
name: 'Sample Raster Style',
Expand Down
5 changes: 3 additions & 2 deletions examples/sample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Style } from 'index';
import { Style } from '../index';

const sampleStyle: Style = {
name: 'Sample Point Style',
Expand All @@ -7,7 +7,8 @@ const sampleStyle: Style = {
name: 'Young Peter',
filter: ['&&',
['==', 'name', 'Peter'],
['<=', 'age', 12]
['<=', 'age', 12],
['<=x<=', 'height', 1, 2]
],
scaleDenominator: {
min: 500,
Expand Down
9 changes: 7 additions & 2 deletions style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type PropertyValue = string | number | boolean | null;
/**
* The possible Operators used for comparison Filters.
*/
export type ComparisonOperator = '==' | '*=' | '!=' | '<' | '<=' | '>' | '>=';
export type ComparisonOperator = '==' | '*=' | '!=' | '<' | '<=' | '>' | '>=' | '<=x<=';

/**
* The possible Operators used for combination Filters.
Expand Down Expand Up @@ -69,6 +69,11 @@ export type StrMatchesFunctionFilter = [
*/
export type FunctionFilter = StrMatchesFunctionFilter;

/**
* A Filter that checks if a property is in a range of two values (inclusive).
*/
export type RangeFilter = ['<=x<=', PropertyName | FunctionFilter, number, number];

/**
* A ComparisonFilter compares a value of an object (by key) with an expected
* value.
Expand All @@ -77,7 +82,7 @@ export type ComparisonFilter = [
ComparisonOperator,
PropertyName | FunctionFilter,
PropertyValue
];
] | RangeFilter;

/**
* A CombinationFilter combines N Filters with a logical OR / AND operator.
Expand Down
8 changes: 5 additions & 3 deletions typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const isOperator = (got: any): got is Operator => {
isNegationOperator(got);
};
export const isComparisonOperator = (got: any): got is ComparisonOperator => {
return ['==', '*=' , '!=' , '<' , '<=' , '>' , '>='].includes(got);
return ['==', '*=' , '!=' , '<' , '<=' , '>' , '>=', '<=x<='].includes(got);
};
export const isCombinationOperator = (got: any): got is CombinationOperator => {
return ['&&', '||'].includes(got);
Expand All @@ -72,11 +72,13 @@ export const isFilter = (got: any): got is Filter => {
isNegationFilter(got);
};
export const isComparisonFilter = (got: any): got is ComparisonFilter => {
const expectedLength = got[0] === '<=x<=' ? 4 : 3;
return Array.isArray(got) &&
got.length === 3 &&
got.length === expectedLength &&
isComparisonOperator(got[0]) &&
(isFunctionFilter(got[1]) || _isString(got[1])) &&
isPropertyValue(got[2]);
isPropertyValue(got[2]) &&
(got[0] !== '<=x<=' || _isNumber(got[3]));
};
export const isCombinationFilter = (got: any): got is CombinationFilter => {
return Array.isArray(got) &&
Expand Down