Small improvements to units

This commit is contained in:
pietervdvn 2021-06-25 18:30:56 +02:00
parent e9df77db58
commit 89c5c78f41
3 changed files with 46 additions and 10 deletions

View file

@ -7,14 +7,31 @@ import Combine from "../../UI/Base/Combine";
export class Unit {
public readonly appliesToKeys: Set<string>;
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<string>();
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();

View file

@ -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")
}

View file

@ -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": "<div style='display:block ruby;' class='m-1 '><img src='./assets/themes/climbing/height.svg' style='width:2rem; height:2rem'/>{climbing:length}</div>",
"render": "<div style='display:block ruby;' class='m-1 '><img src='./assets/themes/climbing/height.svg' style='width:2rem; height:2rem'/>{climbing:length}m</div>",
"freeform": {
"key": "climbing:length"
}
@ -1372,7 +1375,13 @@
"render": "<div style='display:block ruby;' class='m-1 '><img src='./assets/themes/climbing/carabiner.svg' style='width:2rem; height:2rem'/>{climbing:bolted}</div>",
"freeform": {
"key": "climbing:bolted"
},
"mappings": [
{
"if": "climbing:bolted=yes",
"then": "<img src='./assets/themes/climbing/carabiner.svg' style='width:2rem; height:2rem'/>"
}
]
},
"defaults"
],