From 7d8d4fa074b06762285ae501aed9e1c807abfa78 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 28 Mar 2025 12:49:28 +0100 Subject: [PATCH] Feature: Properly fix postfixdistinguished --- src/Models/ThemeConfig/TagRenderingConfig.ts | 63 +++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/src/Models/ThemeConfig/TagRenderingConfig.ts b/src/Models/ThemeConfig/TagRenderingConfig.ts index a03a8a9b9..668a67b31 100644 --- a/src/Models/ThemeConfig/TagRenderingConfig.ts +++ b/src/Models/ThemeConfig/TagRenderingConfig.ts @@ -5,10 +5,7 @@ import { TagUtils } from "../../Logic/Tags/TagUtils" import { And } from "../../Logic/Tags/And" import { Utils } from "../../Utils" import { Tag } from "../../Logic/Tags/Tag" -import { - MappingConfigJson, - QuestionableTagRenderingConfigJson, -} from "./Json/QuestionableTagRenderingConfigJson" +import { MappingConfigJson, QuestionableTagRenderingConfigJson } from "./Json/QuestionableTagRenderingConfigJson" import Validators, { ValidatorType } from "../../UI/InputElement/Validators" import { TagRenderingConfigJson } from "./Json/TagRenderingConfigJson" import { RegexTag } from "../../Logic/Tags/RegexTag" @@ -688,6 +685,43 @@ export default class TagRenderingConfig { } } + /** + * + * TagRenderingConfig.splitPostfixDistinguished("€10") // => {value: "€10", denomination: ""} + * TagRenderingConfig.splitPostfixDistinguished(" €10 / day ") // => {value: "€10", denomination: "day"} + * + */ + private static splitPostfixDistinguished(part: string): { value: string, denomination: string } { + const i = part.indexOf("/") + if (i < 0) { + return { value: part, denomination: "" } + } + const value = part.substring(0, i).trim() + const denomination = part.substring(i + 1).trim() + return { value, denomination } + } + + /** + * Converts the freeform value into a postfix-table, e.g. + * + * + * const charge = "€5/student;€6/citizen;€7/adult;€4/disabled;€10" + * TagRenderingConfig.buildPostfixTable(charge) // => {"student": "€5", "citizen": "€6", "adult": "€7", "disabled": "€4", "":"€10"} + * + */ + private static buildPostfixTable(v: string): Record { + const allValues = v.split(";").map((s) => s.trim()) + const perPostfix: Record = {} + for (const part of allValues) { + const { denomination, value } = TagRenderingConfig.splitPostfixDistinguished(part) + if (value === "") { + continue + } + perPostfix[denomination] = value.trim() + } + return perPostfix + } + /** * Given a value for the freeform key and an overview of the selected mappings, construct the correct tagsFilter to apply. * Result should be interpreted as "and" @@ -753,12 +787,8 @@ export default class TagRenderingConfig { const pf = this.freeform?.postfixDistinguished if (pf) { const v = currentProperties[this.freeform.key] ?? "" - const allValues = v.split(";").map((s) => s.trim()) - const perPostfix: Record = {} - for (const value of allValues) { - const [v, postfix] = value.split("/").map((s) => s.trim()) - perPostfix[postfix ?? pf] = v.trim() - } + const perPostfix = TagRenderingConfig.buildPostfixTable(v) + if (freeformValue === "" || freeformValue === undefined) { delete perPostfix[this.freeform.postfixDistinguished] } else { @@ -981,17 +1011,8 @@ export default class TagRenderingConfig { if (!distinguish) { return value } - const parts = value.split(";") - for (const part of parts) { - if (part.indexOf("/") < 0) { - continue - } - const [v, denom] = part.split("/").map((s) => s.trim()) - if (denom === distinguish) { - return v - } - } - return "" + const perPostfix = TagRenderingConfig.buildPostfixTable(value) + return perPostfix[distinguish] ?? "" } /**