From 58b94c38e50c5a9c66ac248fe8117b19bac7d05a Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 8 Jun 2023 13:34:25 +0200 Subject: [PATCH] Fix: dynamic filtering with non-string values --- Logic/Tags/RegexTag.ts | 37 ++++++++++++++++++++++++++----------- Models/FilteredLayer.ts | 5 ++++- package-lock.json | 33 +++++++++++---------------------- package.json | 1 + 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/Logic/Tags/RegexTag.ts b/Logic/Tags/RegexTag.ts index 805438c81..22435255c 100644 --- a/Logic/Tags/RegexTag.ts +++ b/Logic/Tags/RegexTag.ts @@ -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): boolean { + matchesProperties(tags: Record): 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 } } diff --git a/Models/FilteredLayer.ts b/Models/FilteredLayer.ts index ee07c2d50..b84ebd663 100644 --- a/Models/FilteredLayer.ts +++ b/Models/FilteredLayer.ts @@ -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, globalFilters?: GlobalFilter[]): boolean { if (properties._deleted === "yes") { diff --git a/package-lock.json b/package-lock.json index e2834882a..ed1fe6f69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 857080eb0..b8817e610 100644 --- a/package.json +++ b/package.json @@ -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",