forked from MapComplete/MapComplete
Refactoring: allow to reuse units, move all units into central file
This commit is contained in:
parent
067fb549c1
commit
94e07d5b13
30 changed files with 1495 additions and 1307 deletions
|
@ -3,18 +3,23 @@ import { FixedUiElement } from "../UI/Base/FixedUiElement"
|
|||
import Combine from "../UI/Base/Combine"
|
||||
import { Denomination } from "./Denomination"
|
||||
import UnitConfigJson from "./ThemeConfig/Json/UnitConfigJson"
|
||||
import unit from "../../assets/layers/unit/unit.json"
|
||||
|
||||
export class Unit {
|
||||
private static allUnits = this.initUnits()
|
||||
public readonly appliesToKeys: Set<string>
|
||||
public readonly denominations: Denomination[]
|
||||
public readonly denominationsSorted: Denomination[]
|
||||
public readonly eraseInvalid: boolean
|
||||
public readonly quantity: string
|
||||
|
||||
constructor(
|
||||
quantity: string,
|
||||
appliesToKeys: string[],
|
||||
applicableDenominations: Denomination[],
|
||||
eraseInvalid: boolean
|
||||
) {
|
||||
this.quantity = quantity
|
||||
this.appliesToKeys = new Set(appliesToKeys)
|
||||
this.denominations = applicableDenominations
|
||||
this.eraseInvalid = eraseInvalid
|
||||
|
@ -60,12 +65,24 @@ export class Unit {
|
|||
}
|
||||
}
|
||||
|
||||
static fromJson(
|
||||
json:
|
||||
| UnitConfigJson
|
||||
| Record<string, string | { quantity: string; denominations: string[] }>,
|
||||
ctx: string
|
||||
): Unit[] {
|
||||
if (!json.appliesToKey && !json.quantity) {
|
||||
return this.loadFromLibrary(<any>json, ctx)
|
||||
}
|
||||
return [this.parse(<UnitConfigJson>json, ctx)]
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* // Should detect invalid defaultInput
|
||||
* let threwError = false
|
||||
* try{
|
||||
* Unit.fromJson({
|
||||
* Unit.parse({
|
||||
* appliesToKey: ["length"],
|
||||
* defaultInput: "xcm",
|
||||
* applicableUnits: [
|
||||
|
@ -82,7 +99,7 @@ export class Unit {
|
|||
* threwError // => true
|
||||
*
|
||||
* // Should work
|
||||
* Unit.fromJson({
|
||||
* Unit.parse({
|
||||
* appliesToKey: ["length"],
|
||||
* defaultInput: "xcm",
|
||||
* applicableUnits: [
|
||||
|
@ -98,9 +115,9 @@ export class Unit {
|
|||
* ]
|
||||
* }, "test")
|
||||
*/
|
||||
static fromJson(json: UnitConfigJson, ctx: string) {
|
||||
private static parse(json: UnitConfigJson, ctx: string): Unit {
|
||||
const appliesTo = json.appliesToKey
|
||||
for (let i = 0; i < appliesTo.length; i++) {
|
||||
for (let i = 0; i < (appliesTo ?? []).length; i++) {
|
||||
let key = appliesTo[i]
|
||||
if (key.trim() !== key) {
|
||||
throw `${ctx}.appliesToKey[${i}] is invalid: it starts or ends with whitespace`
|
||||
|
@ -112,15 +129,8 @@ export class Unit {
|
|||
}
|
||||
// Some keys do have unit handling
|
||||
|
||||
const applicable = json.applicableUnits.map(
|
||||
(u, i) =>
|
||||
new Denomination(
|
||||
u,
|
||||
u.canonicalDenomination === undefined
|
||||
? undefined
|
||||
: u.canonicalDenomination.trim() === json.defaultInput,
|
||||
`${ctx}.units[${i}]`
|
||||
)
|
||||
const applicable = json.applicableUnits.map((u, i) =>
|
||||
Denomination.fromJson(u, `${ctx}.units[${i}]`)
|
||||
)
|
||||
|
||||
if (
|
||||
|
@ -133,7 +143,85 @@ export class Unit {
|
|||
.map((denom) => denom.canonical)
|
||||
.join(", ")}`
|
||||
}
|
||||
return new Unit(appliesTo, applicable, json.eraseInvalidValues ?? false)
|
||||
return new Unit(
|
||||
json.quantity ?? "",
|
||||
appliesTo,
|
||||
applicable,
|
||||
json.eraseInvalidValues ?? false
|
||||
)
|
||||
}
|
||||
|
||||
private static initUnits(): Map<string, Unit> {
|
||||
const m = new Map<string, Unit>()
|
||||
const units = (<UnitConfigJson[]>unit.units).map((json, i) =>
|
||||
this.parse(json, "unit.json.units." + i)
|
||||
)
|
||||
|
||||
for (const unit of units) {
|
||||
m.set(unit.quantity, unit)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
private static getFromLibrary(name: string, ctx: string): Unit {
|
||||
const loaded = this.allUnits.get(name)
|
||||
if (loaded === undefined) {
|
||||
throw (
|
||||
"No unit with quantity name " +
|
||||
name +
|
||||
" found (at " +
|
||||
ctx +
|
||||
"). Try one of: " +
|
||||
Array.from(this.allUnits.keys()).join(", ")
|
||||
)
|
||||
}
|
||||
return loaded
|
||||
}
|
||||
|
||||
private static loadFromLibrary(
|
||||
spec: Record<
|
||||
string,
|
||||
string | { quantity: string; denominations: string[]; canonical?: string }
|
||||
>,
|
||||
ctx: string
|
||||
): Unit[] {
|
||||
const units: Unit[] = []
|
||||
for (const key in spec) {
|
||||
const toLoad = spec[key]
|
||||
if (typeof toLoad === "string") {
|
||||
const loaded = this.getFromLibrary(toLoad, ctx)
|
||||
units.push(
|
||||
new Unit(loaded.quantity, [key], loaded.denominations, loaded.eraseInvalid)
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
const loaded = this.getFromLibrary(toLoad.quantity, ctx)
|
||||
const quantity = toLoad.quantity
|
||||
function fetchDenom(d: string): Denomination {
|
||||
const found = loaded.denominations.find(
|
||||
(denom) => denom.canonical.toLowerCase() === d
|
||||
)
|
||||
if (!found) {
|
||||
throw (
|
||||
`Could not find a denomination \`${d}\`for quantity ${quantity} at ${ctx}. Perhaps you meant to use on of ` +
|
||||
loaded.denominations.map((d) => d.canonical).join(", ")
|
||||
)
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
const denoms = toLoad.denominations
|
||||
.map((d) => d.toLowerCase())
|
||||
.map((d) => fetchDenom(d))
|
||||
|
||||
if (toLoad.canonical) {
|
||||
const canonical = fetchDenom(toLoad.canonical)
|
||||
denoms.unshift(canonical.withBlankCanonical())
|
||||
}
|
||||
units.push(new Unit(loaded.quantity, [key], denoms, loaded.eraseInvalid))
|
||||
}
|
||||
return units
|
||||
}
|
||||
|
||||
isApplicableToKey(key: string | undefined): boolean {
|
||||
|
@ -161,47 +249,34 @@ export class Unit {
|
|||
return [undefined, undefined]
|
||||
}
|
||||
|
||||
asHumanLongValue(value: string, country: () => string): BaseUIElement {
|
||||
asHumanLongValue(value: string, country: () => string): BaseUIElement | string {
|
||||
if (value === undefined) {
|
||||
return undefined
|
||||
}
|
||||
const [stripped, denom] = this.findDenomination(value, country)
|
||||
const human = stripped === "1" ? denom?.humanSingular : denom?.human
|
||||
if (stripped === "1") {
|
||||
return denom?.humanSingular ?? stripped
|
||||
}
|
||||
const human = denom?.human
|
||||
if (human === undefined) {
|
||||
return new FixedUiElement(stripped ?? value)
|
||||
return stripped ?? value
|
||||
}
|
||||
|
||||
const elems = denom.prefix ? [human, stripped] : [stripped, human]
|
||||
return new Combine(elems)
|
||||
return human.Subs({ quantity: value })
|
||||
}
|
||||
|
||||
public getDefaultInput(country: () => string | string[]) {
|
||||
console.log("Searching the default denomination for input", country)
|
||||
for (const denomination of this.denominations) {
|
||||
if (denomination.useAsDefaultInput === true) {
|
||||
return denomination
|
||||
}
|
||||
if (
|
||||
denomination.useAsDefaultInput === undefined ||
|
||||
denomination.useAsDefaultInput === false
|
||||
) {
|
||||
continue
|
||||
}
|
||||
let countries: string | string[] = country()
|
||||
if (typeof countries === "string") {
|
||||
countries = countries.split(",")
|
||||
}
|
||||
const denominationCountries: string[] = denomination.useAsDefaultInput
|
||||
if (countries.some((country) => denominationCountries.indexOf(country) >= 0)) {
|
||||
return denomination
|
||||
}
|
||||
public toOsm(value: string, denomination: string) {
|
||||
const denom = this.denominations.find((d) => d.canonical === denomination)
|
||||
const space = denom.addSpace ? " " : ""
|
||||
if (denom.prefix) {
|
||||
return denom.canonical + space + value
|
||||
}
|
||||
return this.denominations[0]
|
||||
return value + space + denom.canonical
|
||||
}
|
||||
|
||||
public getDefaultDenomination(country: () => string) {
|
||||
for (const denomination of this.denominations) {
|
||||
if (denomination.useIfNoUnitGiven === true || denomination.canonical === "") {
|
||||
if (denomination.useIfNoUnitGiven === true) {
|
||||
return denomination
|
||||
}
|
||||
if (
|
||||
|
@ -219,6 +294,11 @@ export class Unit {
|
|||
return denomination
|
||||
}
|
||||
}
|
||||
for (const denomination of this.denominations) {
|
||||
if (denomination.canonical === "") {
|
||||
return denomination
|
||||
}
|
||||
}
|
||||
return this.denominations[0]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue