forked from MapComplete/MapComplete
187 lines
6.4 KiB
TypeScript
187 lines
6.4 KiB
TypeScript
import BaseUIElement from "../UI/BaseUIElement"
|
|
import { Denomination } from "./Denomination"
|
|
import { Validator } from "../UI/InputElement/Validator"
|
|
import FloatValidator from "../UI/InputElement/Validators/FloatValidator"
|
|
|
|
export class Unit {
|
|
public readonly appliesToKeys: Set<string>
|
|
public readonly denominations: Denomination[]
|
|
public readonly denominationsSorted: Denomination[]
|
|
public readonly eraseInvalid: boolean
|
|
public readonly quantity: string
|
|
public readonly inverted: boolean
|
|
|
|
constructor(
|
|
quantity: string,
|
|
appliesToKeys: string[],
|
|
applicableDenominations: Denomination[],
|
|
eraseInvalid: boolean,
|
|
validator: Validator,
|
|
inverted: boolean = false
|
|
) {
|
|
this.quantity = quantity
|
|
if (
|
|
!inverted &&
|
|
["string", "text", "key", "icon", "translation", "fediverse", "id"].indexOf(
|
|
validator.name
|
|
) >= 0
|
|
) {
|
|
console.trace("Unit error")
|
|
throw (
|
|
"Invalid unit configuration. The validator is of a forbidden type: " +
|
|
validator.name +
|
|
"; set a (number) type to your freeform key or set inverted. Hint: this unit is applied onto keys: " +
|
|
appliesToKeys.join("; ")
|
|
)
|
|
}
|
|
this.inverted = inverted
|
|
this.appliesToKeys = new Set(appliesToKeys)
|
|
this.denominations = applicableDenominations
|
|
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)
|
|
|
|
const possiblePostFixes = new Set<string>()
|
|
|
|
function addPostfixesOf(str) {
|
|
if (str === undefined) {
|
|
return
|
|
}
|
|
str = str.toLowerCase()
|
|
for (let i = 0; i < str.length + 1; i++) {
|
|
const substr = str.substring(0, i)
|
|
possiblePostFixes.add(substr)
|
|
}
|
|
}
|
|
|
|
for (const denomination of this.denominations) {
|
|
addPostfixesOf(denomination.canonical)
|
|
addPostfixesOf(denomination._canonicalSingular)
|
|
denomination.alternativeDenominations.forEach(addPostfixesOf)
|
|
}
|
|
}
|
|
|
|
isApplicableToKey(key: string | undefined): boolean {
|
|
if (key === undefined) {
|
|
return false
|
|
}
|
|
|
|
return this.appliesToKeys.has(key)
|
|
}
|
|
|
|
/**
|
|
* Finds which denomination is applicable and gives the stripped value back
|
|
*/
|
|
findDenomination(valueWithDenom: string, country: () => string): [string, Denomination] {
|
|
if (valueWithDenom === undefined) {
|
|
return undefined
|
|
}
|
|
const defaultDenom = this.getDefaultDenomination(country)
|
|
for (const denomination of this.denominationsSorted) {
|
|
const bare = denomination.StrippedValue(
|
|
valueWithDenom,
|
|
defaultDenom === denomination,
|
|
this.inverted
|
|
)
|
|
if (bare !== null) {
|
|
return [bare, denomination]
|
|
}
|
|
}
|
|
return [undefined, undefined]
|
|
}
|
|
|
|
asHumanLongValue(value: string | number, country: () => string): BaseUIElement | string {
|
|
if (value === undefined) {
|
|
return undefined
|
|
}
|
|
value = "" + value
|
|
const [stripped, denom] = this.findDenomination(value, country)
|
|
const human = denom?.human
|
|
if (this.inverted) {
|
|
return human.Subs({ quantity: stripped + "/" })
|
|
}
|
|
if (stripped === "1") {
|
|
return denom?.humanSingular ?? stripped
|
|
}
|
|
if (human === undefined) {
|
|
return stripped ?? value
|
|
}
|
|
|
|
return human.Subs({ quantity: stripped })
|
|
}
|
|
|
|
public toOsm(value: string, denomination: string) {
|
|
const denom = this.denominations.find((d) => d.canonical === denomination)
|
|
if (this.inverted) {
|
|
return value + "/" + denom._canonicalSingular
|
|
}
|
|
|
|
const space = denom.addSpace ? " " : ""
|
|
if (denom.prefix) {
|
|
return denom.canonical + space + value
|
|
}
|
|
return value + space + denom.canonical
|
|
}
|
|
|
|
public getDefaultDenomination(country: () => string): Denomination {
|
|
for (const denomination of this.denominations) {
|
|
if (denomination.useIfNoUnitGiven === true) {
|
|
return denomination
|
|
}
|
|
if (
|
|
denomination.useIfNoUnitGiven === undefined ||
|
|
denomination.useIfNoUnitGiven === false
|
|
) {
|
|
continue
|
|
}
|
|
let countries: string | string[] = country() ?? []
|
|
if (typeof countries === "string") {
|
|
countries = countries.split(",")
|
|
}
|
|
const denominationCountries: string[] = denomination.useIfNoUnitGiven
|
|
if (countries.some((country) => denominationCountries.indexOf(country) >= 0)) {
|
|
return denomination
|
|
}
|
|
}
|
|
for (const denomination of this.denominations) {
|
|
if (denomination.canonical === "") {
|
|
return denomination
|
|
}
|
|
}
|
|
return this.denominations[0]
|
|
}
|
|
|
|
/**
|
|
* Gets the value in the canonical denomination;
|
|
* e.g. "1cm -> 0.01" as it is 0.01meter
|
|
* @param v
|
|
*/
|
|
public valueInCanonical(value: string, country: () => string): number {
|
|
const denom = this.findDenomination(value, country)
|
|
if (!denom) {
|
|
return undefined
|
|
}
|
|
const [v, d] = denom
|
|
const vf = new FloatValidator().reformat(v)
|
|
return Number(vf) * (d.factorToCanonical ?? 1)
|
|
}
|
|
}
|