forked from MapComplete/MapComplete
Fix tests, fix units
This commit is contained in:
parent
8b1b843dbe
commit
fb3e3a040b
7 changed files with 52 additions and 17 deletions
|
@ -37,6 +37,7 @@ export class Unit {
|
|||
|
||||
const possiblePostFixes = new Set<string>()
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
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)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -287,13 +287,21 @@ export default class ValidatedTextField {
|
|||
input = new CombinedInputElement(
|
||||
input,
|
||||
unitDropDown,
|
||||
// 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")
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
]);
|
||||
|
|
Loading…
Reference in a new issue