Fix: dynamic filtering with non-string values

This commit is contained in:
Pieter Vander Vennet 2023-06-08 13:34:25 +02:00
parent 44b919fec0
commit 58b94c38e5
4 changed files with 42 additions and 34 deletions

View file

@ -147,26 +147,38 @@ export class RegexTag extends TagsFilter {
* new RegexTag("key", new RegExp(".+")).matchesProperties({"key": undefined}) // => false
* new RegexTag("key", new RegExp(".+")).matchesProperties({"key": v}) // => false
* new RegexTag("key", new RegExp(".+")).matchesProperties({"key": ""}) // => false
*
* // Show work with non-string objects
* new RegexTag("key", "true").matchesProperties({"key": true}) // => true
* new RegexTag("key", "true", true).matchesProperties({"key": true}) // => false
*/
matchesProperties(tags: Record<string, string>): boolean {
matchesProperties(tags: Record<string, string | number | boolean>): boolean {
if (typeof this.key === "string") {
const value = tags[this.key]
let value = tags[this.key]
if(!value || value === ""){
// No tag is known, so we assume the empty string
// If this regexTag matches the empty string, we return true, otherwise false
// (Note: if inverted, we must reverse this)
return this.invert !== this.matchesEmpty
}
if(typeof value !== "string"){
if(typeof this.value !== "string"){
const regExp = this.value
if(regExp.source === ".*"){
// We match anything, and we do have a value
return !this.invert
}
return RegexTag.doesMatch(value, JSON.stringify(this.value)) != this.invert
if (typeof value === "string") {
return RegexTag.doesMatch(value, this.value) != this.invert
}
// The value under test is _not_ a string; it can be a culculated tag, thus be e.g. a number or a boolean
// It might also be an object
if (typeof this.value !== "string") {
const regExp = this.value
if (regExp.source === ".*") {
// We match anything, and we do have some value
return !this.invert
}
}
if(typeof value !== "string"){
value = JSON.stringify(value)
}
return RegexTag.doesMatch(value, this.value) != this.invert
}
@ -175,7 +187,10 @@ export class RegexTag extends TagsFilter {
continue
}
if (RegexTag.doesMatch(key, this.key)) {
const value = tags[key] ?? ""
let value = tags[key] ?? ""
if(typeof value !== "string"){
value = JSON.stringify(value)
}
return RegexTag.doesMatch(value, this.value) != this.invert
}
}

View file

@ -180,7 +180,10 @@ export default class FilteredLayer {
}
/**
* Returns true if the given tags match the current filters (and the specified 'global filters')
* Returns true if the given tags match
* - the current filters
* - the specified 'global filters'
* - the 'isShown'-filter set by the layer
*/
public isShown(properties: Record<string, string>, globalFilters?: GlobalFilter[]): boolean {
if (properties._deleted === "yes") {

33
package-lock.json generated
View file

@ -19,13 +19,12 @@
"@turf/distance": "^6.5.0",
"@turf/length": "^6.5.0",
"@turf/turf": "^6.5.0",
"@types/dom-to-image": "^2.6.4",
"@types/mocha": "^10.0.1",
"@types/showdown": "^2.0.0",
"chart.js": "^3.8.0",
"country-language": "^0.1.7",
"csv-parse": "^5.1.0",
"doctest-ts-improved": "^0.8.8",
"dom-to-image": "^2.6.0",
"email-validator": "^2.0.4",
"escape-html": "^1.0.3",
"fake-dom": "^1.0.4",
@ -3646,11 +3645,6 @@
"@types/chai": "*"
}
},
"node_modules/@types/dom-to-image": {
"version": "2.6.4",
"resolved": "https://registry.npmjs.org/@types/dom-to-image/-/dom-to-image-2.6.4.tgz",
"integrity": "sha512-UddUdGF1qulrSDulkz3K2Ypq527MR6ixlgAzqLbxSiQ0icx0XDlIV+h4+edmjq/1dqn0KgN0xGSe1kI9t+vGuw=="
},
"node_modules/@types/estree": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.0.tgz",
@ -3715,6 +3709,11 @@
"@types/pbf": "*"
}
},
"node_modules/@types/mocha": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz",
"integrity": "sha512-/fvYntiO1GeICvqbQ3doGDIP97vWmvFt83GKguJ6prmQM2iXZfFcq6YE8KteFyRtX2/h5Hf91BYvPodJKFYv5Q=="
},
"node_modules/@types/node": {
"version": "18.11.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
@ -5457,11 +5456,6 @@
"url": "https://github.com/fb55/domhandler?sponsor=1"
}
},
"node_modules/dom-to-image": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/dom-to-image/-/dom-to-image-2.6.0.tgz",
"integrity": "sha512-Dt0QdaHmLpjURjU7Tnu3AgYSF2LuOmksSGsUcE6ItvJoCWTBEmiMXcqBdNSAm9+QbbwD7JMoVsuuKX6ZVQv1qA=="
},
"node_modules/dom-walk": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz",
@ -14768,11 +14762,6 @@
"@types/chai": "*"
}
},
"@types/dom-to-image": {
"version": "2.6.4",
"resolved": "https://registry.npmjs.org/@types/dom-to-image/-/dom-to-image-2.6.4.tgz",
"integrity": "sha512-UddUdGF1qulrSDulkz3K2Ypq527MR6ixlgAzqLbxSiQ0icx0XDlIV+h4+edmjq/1dqn0KgN0xGSe1kI9t+vGuw=="
},
"@types/estree": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.0.tgz",
@ -14837,6 +14826,11 @@
"@types/pbf": "*"
}
},
"@types/mocha": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz",
"integrity": "sha512-/fvYntiO1GeICvqbQ3doGDIP97vWmvFt83GKguJ6prmQM2iXZfFcq6YE8KteFyRtX2/h5Hf91BYvPodJKFYv5Q=="
},
"@types/node": {
"version": "18.11.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
@ -16142,11 +16136,6 @@
}
}
},
"dom-to-image": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/dom-to-image/-/dom-to-image-2.6.0.tgz",
"integrity": "sha512-Dt0QdaHmLpjURjU7Tnu3AgYSF2LuOmksSGsUcE6ItvJoCWTBEmiMXcqBdNSAm9+QbbwD7JMoVsuuKX6ZVQv1qA=="
},
"dom-walk": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz",

View file

@ -118,6 +118,7 @@
"@types/chai": "^4.3.0",
"@types/geojson": "^7946.0.10",
"@types/lz-string": "^1.3.34",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/papaparse": "^5.3.1",
"@types/prompt-sync": "^4.1.0",