@maptalks/feature-filter
Advanced tools
+66
-3
@@ -10,2 +10,6 @@ /*! | ||
| function isObject(obj) { | ||
| return typeof obj === 'object' && !!obj; | ||
| } | ||
| /** | ||
@@ -23,2 +27,39 @@ * Given a filter expressed as nested arrays, return a new function | ||
| export function isFeatureFilter(filter) { | ||
| if (filter === true || filter === false) { | ||
| return true; | ||
| } | ||
| if (!Array.isArray(filter) || filter.length === 0) { | ||
| return false; | ||
| } | ||
| switch (filter[0]) { | ||
| case 'has': | ||
| case '!has': | ||
| return filter.length === 2 && (typeof filter[1] === 'string' || filter[1].property && filter[1].op); | ||
| case 'in': | ||
| case '!in': | ||
| return filter.length >= 2 && (typeof filter[1] === 'string' || filter[1].property && filter[1].op); | ||
| case '==': | ||
| case '!=': | ||
| case '>': | ||
| case '>=': | ||
| case '<': | ||
| case '<=': | ||
| return filter.length === 3 && (typeof filter[1] === 'string' || filter[1].property && filter[1].op); | ||
| case 'none': | ||
| case 'any': | ||
| case 'all': | ||
| for (const f of filter.slice(1)) { | ||
| if (!isFeatureFilter(f) && typeof f !== 'boolean') { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| case 'contains': | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| function compile(filter) { | ||
@@ -33,4 +74,4 @@ if (!filter) return 'true'; | ||
| op === '>' || | ||
| op === '<=' || | ||
| op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) : | ||
| op === '<=' || | ||
| op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) : | ||
| op === 'any' ? compileLogicalOp(filter.slice(1), '||') : | ||
@@ -49,2 +90,17 @@ op === 'all' ? compileLogicalOp(filter.slice(1), '&&') : | ||
| function compileFunction(propertyObj, value, op, checkType) { | ||
| const property = propertyObj.property, funName = propertyObj.op; | ||
| let left = compilePropertyReference(property); | ||
| if (funName === 'length') { | ||
| left = `((${left}+='').length)`; | ||
| } else { | ||
| // TODO | ||
| // other functions | ||
| console.error(`not support ${funName} op`); | ||
| return 'false'; | ||
| } | ||
| return getComparisonCode(left, property, value, op, checkType); | ||
| } | ||
| function compileContains(property, str, index) { | ||
@@ -73,3 +129,10 @@ const prop = compilePropertyReference(property); | ||
| function compileComparisonOp(property, value, op, checkType) { | ||
| if (isObject(property) && property.op) { | ||
| return compileFunction(property, value, op, checkType); | ||
| } | ||
| const left = compilePropertyReference(property); | ||
| return getComparisonCode(left, property, value, op, checkType); | ||
| } | ||
| function getComparisonCode(left, property, value, op, checkType) { | ||
| const right = property === '$type' ? types.indexOf(value) : JSON.stringify(value); | ||
@@ -151,3 +214,3 @@ return (checkType ? `typeof ${left}=== typeof ${right}&&` : '') + left + op + right; | ||
| compiled.push(extend({}, styles[i], { | ||
| filter : filter | ||
| filter: filter | ||
| })); | ||
@@ -154,0 +217,0 @@ } |
+1
-1
| { | ||
| "name": "@maptalks/feature-filter", | ||
| "version": "1.2.0", | ||
| "version": "1.3.0", | ||
| "main": "./index.js", | ||
@@ -5,0 +5,0 @@ "module": "./index.js", |
Unidentified License
LicenseSomething that seems like a license was found, but its contents could not be matched with a known license.
Unidentified License
LicenseSomething that seems like a license was found, but its contents could not be matched with a known license.
92064
2.11%1729
3.47%