Shorten the code by using Array.prototype.find in the Styler.

This commit is contained in:
Christian Paul 2017-11-24 00:01:05 -08:00
parent 554ae593be
commit fd40b57f51

View File

@ -77,73 +77,44 @@ module.exports = class Styler {
//TODO Better translation of the long cases. //TODO Better translation of the long cases.
_compileFilter(filter) { _compileFilter(filter) {
var filters; let filters;
switch (filter != null ? filter[0] : void 0) { switch (filter != null ? filter[0] : void 0) {
case 'all': case 'all':
filter = filter.slice(1); filter = filter.slice(1);
filters = (() => { filters = (() => {
return filter.map((sub) => this._compileFilter(sub)); return filter.map((sub) => this._compileFilter(sub));
}).call(this); }).call(this);
//TODO Use Array.prototype.find return (feature) => !!filters.find((appliesTo) => {
return (feature) => { return !appliesTo(feature);
for (const appliesTo of filters) { });
if (!appliesTo(feature)) {
return false;
}
}
return true;
};
case 'any': case 'any':
filter = filter.slice(1); filter = filter.slice(1);
filters = (() => { filters = (() => {
return filter.map((sub) => this._compileFilter(sub)); return filter.map((sub) => this._compileFilter(sub));
}).call(this); }).call(this);
//TODO Use Array.prototype.find return (feature) => !!filters.find((appliesTo) => {
return (feature) => { return appliesTo(feature);
for (const appliesTo of filters) { });
if (appliesTo(feature)) {
return true;
}
}
return false;
};
case 'none': case 'none':
filter = filter.slice(1); filter = filter.slice(1);
filters = (() => { filters = (() => {
return filter.map((sub) => this._compileFilter(sub)); return filter.map((sub) => this._compileFilter(sub));
}).call(this); }).call(this);
return (feature) => { return (feature) => !filters.find((appliesTo) => {
for (const appliesTo of filters) { return !appliesTo(feature);
if (appliesTo(feature)) { });
return false;
}
}
return true;
};
case '==': case '==':
return (feature) => feature.properties[filter[1]] === filter[2]; return (feature) => feature.properties[filter[1]] === filter[2];
case "!=": case "!=":
return (feature) => feature.properties[filter[1]] !== filter[2]; return (feature) => feature.properties[filter[1]] !== filter[2];
case "in": case "in":
return (feature) => { return (feature) => !!filter.slice(2).find((value) => {
filter = filter.slice(2); return feature.properties[filter[1]] === value;
for (const value of filter) { });
if (feature.properties[filter[1]] === value) {
return true;
}
}
return false;
};
case '!in': case '!in':
return (feature) => { return (feature) => !filter.slice(2).find((value) => {
filter = filter.slice(2); return feature.properties[filter[1]] === value;
for (const value of filter) { });
if (feature.properties[filter[1]] === value) {
return false;
}
}
return true;
};
case "has": case "has":
return (feature) => !!feature.properties[filter[1]]; return (feature) => !!feature.properties[filter[1]];
case "!has": case "!has":