From fb250fb928da576b5649d398272387da72e89e5c Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 17 Sep 2024 02:33:05 +0200 Subject: [PATCH] Fix: search fields in a filter are now wrapped into parentheses, allowing for OR as regex --- src/Models/FilteredLayer.ts | 9 ++++++++- src/Models/ThemeConfig/FilterConfig.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Models/FilteredLayer.ts b/src/Models/FilteredLayer.ts index 2ced53af52..01b5de0b8d 100644 --- a/src/Models/FilteredLayer.ts +++ b/src/Models/FilteredLayer.ts @@ -131,6 +131,13 @@ export default class FilteredLayer { return values } + /** + * import Translations from "../UI/i18n/Translations" + * import { RegexTag } from "../Logic/Tags/RegexTag" + * + * const option: FilterConfigOption = {question: Translations.T("question"), osmTags: undefined, originalTagsSpec: "key~.*{search}.*", fields: [{name: "search", type: "string"}] } + * FilteredLayer.fieldsToTags(option, {search: "value_regex"}) // => new RegexTag("key", /^(.*(value_regex).*)$/) + */ private static fieldsToTags( option: FilterConfigOption, fieldstate: string | Record @@ -153,7 +160,7 @@ export default class FilteredLayer { } for (const key in properties) { - v = (v).replace("{" + key + "}", properties[key]) + v = (v).replace("{" + key + "}", "(" + properties[key] + ")") } return v diff --git a/src/Models/ThemeConfig/FilterConfig.ts b/src/Models/ThemeConfig/FilterConfig.ts index 8e80b581ba..13a994d510 100644 --- a/src/Models/ThemeConfig/FilterConfig.ts +++ b/src/Models/ThemeConfig/FilterConfig.ts @@ -60,8 +60,8 @@ export default class FilterConfig { } const fields: { name: string; type: ValidatorType }[] = (option.fields ?? []).map((f, i) => { - const type = f.type ?? "string" - if(Validators.availableTypes.indexOf(type) < 0){ + const type = f.type ?? "string" + if (Validators.availableTypes.indexOf(type) < 0) { throw `Invalid filter: type is not a valid validator. Did you mean one of ${Utils.sortedByLevenshteinDistance(type, Validators.availableTypes, x => x).slice(0, 3)}` } // Type is validated against 'ValidatedTextField' in Validation.ts, in ValidateFilterConfig @@ -70,12 +70,12 @@ export default class FilterConfig { } return { name: f.name, - type, + type } }) for (const field of fields) { - for (let ln in question.translations) { + for (const ln in question.translations) { const txt = question.translations[ln] if (ln.startsWith("_")) { continue @@ -111,7 +111,7 @@ export default class FilterConfig { question: question, osmTags: osmTags, fields, - originalTagsSpec: option.osmTags, + originalTagsSpec: option.osmTags } }) @@ -223,7 +223,7 @@ export default class FilterConfig { opt.osmTags?.asHumanString() ?? "", opt.fields?.length > 0 ? opt.fields.map((f) => f.name + " (" + f.type + ")").join(" ") - : undefined, + : undefined ]) ) })