diff --git a/Customizations/JSON/Denomination.ts b/Customizations/JSON/Denomination.ts index fd9308319..09c5ab977 100644 --- a/Customizations/JSON/Denomination.ts +++ b/Customizations/JSON/Denomination.ts @@ -37,6 +37,7 @@ export class Unit { const possiblePostFixes = new Set() function addPostfixesOf(str){ + str = str.toLowerCase() for (let i = 0; i < str.length + 1; i++) { const substr = str.substring(0,i) possiblePostFixes.add(substr) @@ -178,11 +179,11 @@ export class Denomination { } } } else { - if (value.endsWith(this.canonical) && this.canonical !== "") { + if (value.endsWith(this.canonical.toLowerCase()) && this.canonical !== "") { return value.substring(0, value.length - this.canonical.length).trim(); } for (const alternativeValue of this.alternativeDenominations) { - if (value.endsWith(alternativeValue)) { + if (value.endsWith(alternativeValue.toLowerCase())) { return value.substring(0, value.length - alternativeValue.length).trim(); } } diff --git a/Customizations/SharedTagRenderings.ts b/Customizations/SharedTagRenderings.ts index b3af01c83..0e9bc494f 100644 --- a/Customizations/SharedTagRenderings.ts +++ b/Customizations/SharedTagRenderings.ts @@ -1,6 +1,7 @@ import TagRenderingConfig from "./JSON/TagRenderingConfig"; import * as questions from "../assets/tagRenderings/questions.json"; import * as icons from "../assets/tagRenderings/icons.json"; +import {Utils} from "../Utils"; export default class SharedTagRenderings { @@ -14,7 +15,10 @@ export default class SharedTagRenderings { try { dict.set(key, new TagRenderingConfig(store[key], undefined, `SharedTagRenderings.${key}`)) } catch (e) { - console.error("BUG: could not parse", key, " from questions.json or icons.json - this error happened during the build step of the SharedTagRenderings", e) + if(!Utils.runningFromConsole){ + console.error("BUG: could not parse", key, " from questions.json or icons.json - this error happened during the build step of the SharedTagRenderings", e) + + } } } diff --git a/Logic/SimpleMetaTagger.ts b/Logic/SimpleMetaTagger.ts index f78d0e49d..c6269e222 100644 --- a/Logic/SimpleMetaTagger.ts +++ b/Logic/SimpleMetaTagger.ts @@ -94,7 +94,7 @@ export default class SimpleMetaTagger { } const value = feature.properties[key] const [, denomination] = unit.findDenomination(value) - let canonical = denomination.canonicalValue(value) ?? undefined; + let canonical = denomination?.canonicalValue(value) ?? undefined; console.log("Rewritten ", key, " from", value, "into", canonical) if(canonical === undefined && !unit.eraseInvalid) { break; diff --git a/UI/Input/ValidatedTextField.ts b/UI/Input/ValidatedTextField.ts index 37ee2ae1d..8ea3fb948 100644 --- a/UI/Input/ValidatedTextField.ts +++ b/UI/Input/ValidatedTextField.ts @@ -287,13 +287,21 @@ export default class ValidatedTextField { input = new CombinedInputElement( input, unitDropDown, - (text, denom) => denom?.canonicalValue(text, true) ?? undefined, + // combine the value from the textfield and the dropdown into the resulting value that should go into OSM + (text, denom) => denom?.canonicalValue(text, true) ?? undefined, (valueWithDenom: string) => { - const [text, denom] = unit.findDenomination(valueWithDenom) ?? [valueWithDenom, undefined]; - if(text === undefined){ - return [valueWithDenom, undefined] + // Take the value from OSM and feed it into the textfield and the dropdown + const withDenom = unit.findDenomination(valueWithDenom); + if(withDenom === undefined) + { + // Not a valid value at all - we give it undefined and leave the details up to the other elements + return [undefined, undefined] } - return [text, denom] + const [strippedText, denom] = withDenom + if(strippedText === undefined){ + return [undefined, undefined] + } + return [strippedText, denom] } ).SetClass("flex") } diff --git a/UI/Popup/SaveButton.ts b/UI/Popup/SaveButton.ts index f8ad9ba50..c9b5df65c 100644 --- a/UI/Popup/SaveButton.ts +++ b/UI/Popup/SaveButton.ts @@ -19,7 +19,7 @@ export class SaveButton extends Toggle { const text = Translations.t.general.save const saveEnabled = text.Clone().SetClass(`btn`); - const saveDisabled = text.SetClass(`btn btn-disabled`); + const saveDisabled = text.Clone().SetClass(`btn btn-disabled`); const save = new Toggle( saveEnabled, saveDisabled, diff --git a/test/TestAll.ts b/test/TestAll.ts index 5174e2ad0..95449ec40 100644 --- a/test/TestAll.ts +++ b/test/TestAll.ts @@ -11,6 +11,7 @@ import {FixedUiElement} from "../UI/Base/FixedUiElement"; import Combine from "../UI/Base/Combine"; import OsmObjectSpec from "./OsmObject.spec"; import ScriptUtils from "../scripts/ScriptUtils"; +import UnitsSpec from "./Units.spec"; @@ -37,7 +38,8 @@ const allTests = [ new GeoOperationsSpec(), new ImageSearcherSpec(), new ThemeSpec(), - new UtilsSpec() + new UtilsSpec(), + new UnitsSpec() ] diff --git a/test/Units.spec.ts b/test/Units.spec.ts index 6433881c2..11ea7af33 100644 --- a/test/Units.spec.ts +++ b/test/Units.spec.ts @@ -1,5 +1,5 @@ import T from "./TestHelper"; -import {Denomination} from "../Customizations/JSON/Denomination"; +import {Denomination, Unit} from "../Customizations/JSON/Denomination"; import {equal} from "assert"; export default class UnitsSpec extends T { @@ -17,13 +17,33 @@ export default class UnitsSpec extends T { } }, "test") - equal(unit.canonicalValue("42m"), "42m") - equal(unit.canonicalValue("42"), "42m") - equal(unit.canonicalValue("42 m"), "42m") - equal(unit.canonicalValue("42 meter"), "42m") + equal(unit.canonicalValue("42m"), "42 m") + equal(unit.canonicalValue("42"), "42 m") + equal(unit.canonicalValue("42 m"), "42 m") + equal(unit.canonicalValue("42 meter"), "42 m") - }] + }], + ["Advanced canonicalize and back", () => { + + const unit = new Denomination({ + "canonicalDenomination": "MW", + "alternativeDenomination": ["megawatts", "megawatt"], + "human": { + "en": " megawatts", + "nl": " megawatt" + }, + "default": true + }, "test"); + + const canonical = unit.canonicalValue("5") + equal(canonical, "5 MW") + const units = new Unit(["key"], [unit], false) + const [detected, detectedDenom] = units.findDenomination("5 MW") + equal(detected, "5") + equal(detectedDenom, unit) + } + ] ]);