Skip to content

Commit 284ec0b

Browse files
meliteleHarelM
andauthored
Fix passing reference to global-state object when creating filter (#1279)
* pass reference to `global-state` object when creating feature filter * Add global-state parameter to featureFilter function --------- Co-authored-by: Harel M <[email protected]>
1 parent c8581e8 commit 284ec0b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## main
22

33
### ✨ Features and improvements
4+
- Added `global-state` parameter to `featureFilter` function ([#1279](https://github.com/maplibre/maplibre-style-spec/pull/1279))
45
- _...Add new stuff here..._
56

67
### 🐞 Bug fixes

src/feature_filter/feature_filter.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ describe('filter', () => {
9797
expect(withinFilter.filter({zoom: 3}, featureInTile, canonical)).toBe(false);
9898
});
9999

100+
test('expression, global-state', () => {
101+
const {filter} = featureFilter(['==', ['global-state', 'x'], ['get', 'x']], {x: 1});
102+
expect(filter(undefined, {properties: {x: 1}} as any as Feature)).toBe(true);
103+
expect(filter(undefined, {properties: {x: 2}} as any as Feature)).toBe(false);
104+
});
105+
100106
legacyFilterTests(featureFilter);
101107

102108
});

src/feature_filter/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createExpression, findGlobalStateRefs} from '../expression';
22
import type {GlobalProperties, Feature} from '../expression';
33
import {ICanonicalTileID} from '../tiles_and_coordinates';
44
import {StylePropertySpecification} from '..';
5-
import {ExpressionFilterSpecification} from '../types.g';
5+
import {ExpressionFilterSpecification, type FilterSpecification} from '../types.g';
66

77
type FilterExpression = (
88
globalProperties: GlobalProperties,
@@ -75,19 +75,20 @@ const filterSpec = {
7575
* passes its test.
7676
*
7777
* @private
78-
* @param {Array} filter MapLibre filter
79-
* @returns {Function} filter-evaluating function
78+
* @param filter MapLibre filter
79+
* @param [globalState] Global state object to be used for evaluating 'global-state' expressions
80+
* @returns filter-evaluating function
8081
*/
81-
export function featureFilter(filter: any): FeatureFilter {
82+
export function featureFilter(filter: FilterSpecification | void, globalState?: Record<string, any>): FeatureFilter {
8283
if (filter === null || filter === undefined) {
8384
return {filter: () => true, needGeometry: false, getGlobalStateRefs: () => new Set()};
8485
}
8586

8687
if (!isExpressionFilter(filter)) {
87-
filter = convertFilter(filter);
88+
filter = convertFilter(filter) as ExpressionFilterSpecification;
8889
}
8990

90-
const compiled = createExpression(filter, filterSpec as StylePropertySpecification);
91+
const compiled = createExpression(filter, filterSpec as StylePropertySpecification, globalState);
9192
if (compiled.result === 'error') {
9293
throw new Error(compiled.value.map(err => `${err.key}: ${err.message}`).join(', '));
9394
} else {
@@ -114,7 +115,7 @@ function geometryNeeded(filter) {
114115
return false;
115116
}
116117

117-
function convertFilter(filter?: Array<any> | null): unknown {
118+
function convertFilter(filter?: Array<any> | null | void): unknown {
118119
if (!filter) return true;
119120
const op = filter[0];
120121
if (filter.length <= 1) return (op !== 'any');

0 commit comments

Comments
 (0)