From 89c5c78f414b65c97f50e325f0fca54a8cc64ada Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Fri, 25 Jun 2021 18:30:56 +0200 Subject: [PATCH] Small improvements to units --- Customizations/JSON/Denomination.ts | 29 ++++++++++++++++++++++++---- UI/Popup/TagRenderingQuestion.ts | 10 ++++++++-- assets/themes/climbing/climbing.json | 17 ++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Customizations/JSON/Denomination.ts b/Customizations/JSON/Denomination.ts index dc65e4142..421cb0605 100644 --- a/Customizations/JSON/Denomination.ts +++ b/Customizations/JSON/Denomination.ts @@ -7,14 +7,31 @@ import Combine from "../../UI/Base/Combine"; export class Unit { public readonly appliesToKeys: Set; public readonly denominations: Denomination[]; + public readonly denominationsSorted: Denomination[]; public readonly defaultDenom: Denomination; - public readonly eraseInvalid : boolean; + public readonly eraseInvalid: boolean; constructor(appliesToKeys: string[], applicableUnits: Denomination[], eraseInvalid: boolean) { this.appliesToKeys = new Set(appliesToKeys); this.denominations = applicableUnits; this.defaultDenom = applicableUnits.filter(denom => denom.default)[0] this.eraseInvalid = eraseInvalid + + const seenUnitExtensions = new Set(); + for (const denomination of this.denominations) { + if(seenUnitExtensions.has(denomination.canonical)){ + throw "This canonical unit is already defined in another denomination: "+denomination.canonical + } + const duplicate = denomination.alternativeDenominations.filter(denom => seenUnitExtensions.has(denom)) + if(duplicate.length > 0){ + throw "A denomination is used multiple times: "+duplicate.join(", ") + } + + seenUnitExtensions.add(denomination.canonical) + denomination.alternativeDenominations.forEach(d => seenUnitExtensions.add(d)) + } + this.denominationsSorted = [...this.denominations] + this.denominationsSorted.sort((a, b) => b.canonical.length - a.canonical.length) } isApplicableToKey(key: string | undefined): boolean { @@ -29,7 +46,10 @@ export class Unit { * Finds which denomination is applicable and gives the stripped value back */ findDenomination(valueWithDenom: string): [string, Denomination] { - for (const denomination of this.denominations) { + if(valueWithDenom === undefined){ + return undefined; + } + for (const denomination of this.denominationsSorted) { const bare = denomination.StrippedValue(valueWithDenom) if (bare !== null) { return [bare, denomination] @@ -56,12 +76,12 @@ export class Denomination { readonly default: boolean; readonly prefix: boolean; private readonly _human: Translation; - private readonly alternativeDenominations: string []; + public readonly alternativeDenominations: string []; constructor(json: UnitConfigJson, context: string) { context = `${context}.unit(${json.canonicalDenomination})` this.canonical = json.canonicalDenomination.trim() - if ((this.canonical ?? "") === "") { + if (this.canonical === undefined) { throw `${context}: this unit has no decent canonical value defined` } @@ -109,6 +129,7 @@ export class Denomination { return undefined; } + value = value.toLowerCase() if (this.prefix) { if (value.startsWith(this.canonical)) { return value.substring(this.canonical.length).trim(); diff --git a/UI/Popup/TagRenderingQuestion.ts b/UI/Popup/TagRenderingQuestion.ts index 97627fa4b..6f655814b 100644 --- a/UI/Popup/TagRenderingQuestion.ts +++ b/UI/Popup/TagRenderingQuestion.ts @@ -354,8 +354,14 @@ export default class TagRenderingQuestion extends UIElement { input = new CombinedInputElement( input, unitDropDown, - (text, denom) => denom?.canonicalValue(text, true) ?? text, - (valueWithDenom: string) => unit.findDenomination(valueWithDenom) + (text, denom) => { + console.log("text:", text, "denom:", denom, "canon: ",denom?.canonicalValue(text, true)) + return denom?.canonicalValue(text, true) ?? text; + }, + (valueWithDenom: string) => { + console.log("ToSplit: ", valueWithDenom, "becomes", unit.findDenomination(valueWithDenom)) + return unit.findDenomination(valueWithDenom) ?? [valueWithDenom, undefined]; + } ).SetClass("flex") } diff --git a/assets/themes/climbing/climbing.json b/assets/themes/climbing/climbing.json index 3f4382bb0..a8d2b9491 100644 --- a/assets/themes/climbing/climbing.json +++ b/assets/themes/climbing/climbing.json @@ -842,12 +842,15 @@ "units": [ { "appliesToKey": [ - "climbing:length" + "climbing:length", + "climbing:length:min", + "climbing:length:max" ], "applicableUnits": [ { - "canonicalDenomination": "m", + "canonicalDenomination": "", "alternativeDenomination": [ + "m", "meter", "meters" ], @@ -1363,7 +1366,7 @@ "overrideAll": { "titleIcons": [ { - "render": "
{climbing:length}
", + "render": "
{climbing:length}m
", "freeform": { "key": "climbing:length" } @@ -1372,7 +1375,13 @@ "render": "
{climbing:bolted}
", "freeform": { "key": "climbing:bolted" - } + }, + "mappings": [ + { + "if": "climbing:bolted=yes", + "then": "" + } + ] }, "defaults" ],