forked from MapComplete/MapComplete
Reformat all files with prettier
This commit is contained in:
parent
e22d189376
commit
b541d3eab4
382 changed files with 50893 additions and 35566 deletions
|
@ -1,32 +1,41 @@
|
|||
import BaseUIElement from "../UI/BaseUIElement";
|
||||
import {FixedUiElement} from "../UI/Base/FixedUiElement";
|
||||
import Combine from "../UI/Base/Combine";
|
||||
import {Denomination} from "./Denomination";
|
||||
import UnitConfigJson from "./ThemeConfig/Json/UnitConfigJson";
|
||||
import BaseUIElement from "../UI/BaseUIElement"
|
||||
import { FixedUiElement } from "../UI/Base/FixedUiElement"
|
||||
import Combine from "../UI/Base/Combine"
|
||||
import { Denomination } from "./Denomination"
|
||||
import UnitConfigJson from "./ThemeConfig/Json/UnitConfigJson"
|
||||
|
||||
export class Unit {
|
||||
public readonly appliesToKeys: Set<string>;
|
||||
public readonly denominations: Denomination[];
|
||||
public readonly denominationsSorted: Denomination[];
|
||||
public readonly eraseInvalid: boolean;
|
||||
public readonly appliesToKeys: Set<string>
|
||||
public readonly denominations: Denomination[]
|
||||
public readonly denominationsSorted: Denomination[]
|
||||
public readonly eraseInvalid: boolean
|
||||
|
||||
constructor(appliesToKeys: string[], applicableDenominations: Denomination[], eraseInvalid: boolean) {
|
||||
this.appliesToKeys = new Set(appliesToKeys);
|
||||
this.denominations = applicableDenominations;
|
||||
constructor(
|
||||
appliesToKeys: string[],
|
||||
applicableDenominations: Denomination[],
|
||||
eraseInvalid: boolean
|
||||
) {
|
||||
this.appliesToKeys = new Set(appliesToKeys)
|
||||
this.denominations = applicableDenominations
|
||||
this.eraseInvalid = eraseInvalid
|
||||
|
||||
const seenUnitExtensions = new Set<string>();
|
||||
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
|
||||
throw (
|
||||
"This canonical unit is already defined in another denomination: " +
|
||||
denomination.canonical
|
||||
)
|
||||
}
|
||||
const duplicate = denomination.alternativeDenominations.filter(denom => seenUnitExtensions.has(denom))
|
||||
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))
|
||||
denomination.alternativeDenominations.forEach((d) => seenUnitExtensions.add(d))
|
||||
}
|
||||
this.denominationsSorted = [...this.denominations]
|
||||
this.denominationsSorted.sort((a, b) => b.canonical.length - a.canonical.length)
|
||||
|
@ -51,37 +60,38 @@ export class Unit {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
static fromJson(json: UnitConfigJson, ctx: string) {
|
||||
const appliesTo = json.appliesToKey
|
||||
for (let i = 0; i < appliesTo.length; i++) {
|
||||
let key = appliesTo[i];
|
||||
let key = appliesTo[i]
|
||||
if (key.trim() !== key) {
|
||||
throw `${ctx}.appliesToKey[${i}] is invalid: it starts or ends with whitespace`
|
||||
}
|
||||
}
|
||||
|
||||
if ((json.applicableUnits ?? []).length === 0) {
|
||||
throw `${ctx}: define at least one applicable unit`
|
||||
throw `${ctx}: define at least one applicable unit`
|
||||
}
|
||||
// Some keys do have unit handling
|
||||
|
||||
if(json.applicableUnits.some(denom => denom.useAsDefaultInput !== undefined)){
|
||||
json.applicableUnits.forEach(denom => {
|
||||
if (json.applicableUnits.some((denom) => denom.useAsDefaultInput !== undefined)) {
|
||||
json.applicableUnits.forEach((denom) => {
|
||||
denom.useAsDefaultInput = denom.useAsDefaultInput ?? false
|
||||
})
|
||||
}
|
||||
|
||||
const applicable = json.applicableUnits.map((u, i) => new Denomination(u, `${ctx}.units[${i}]`))
|
||||
|
||||
const applicable = json.applicableUnits.map(
|
||||
(u, i) => new Denomination(u, `${ctx}.units[${i}]`)
|
||||
)
|
||||
return new Unit(appliesTo, applicable, json.eraseInvalidValues ?? false)
|
||||
}
|
||||
|
||||
isApplicableToKey(key: string | undefined): boolean {
|
||||
if (key === undefined) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
return this.appliesToKeys.has(key);
|
||||
return this.appliesToKeys.has(key)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -89,7 +99,7 @@ export class Unit {
|
|||
*/
|
||||
findDenomination(valueWithDenom: string, country: () => string): [string, Denomination] {
|
||||
if (valueWithDenom === undefined) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
const defaultDenom = this.getDefaultDenomination(country)
|
||||
for (const denomination of this.denominationsSorted) {
|
||||
|
@ -103,27 +113,28 @@ export class Unit {
|
|||
|
||||
asHumanLongValue(value: string, country: () => string): BaseUIElement {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
const [stripped, denom] = this.findDenomination(value, country)
|
||||
const human = stripped === "1" ? denom?.humanSingular : denom?.human
|
||||
if (human === undefined) {
|
||||
return new FixedUiElement(stripped ?? value);
|
||||
return new FixedUiElement(stripped ?? value)
|
||||
}
|
||||
|
||||
const elems = denom.prefix ? [human, stripped] : [stripped, human];
|
||||
const elems = denom.prefix ? [human, stripped] : [stripped, human]
|
||||
return new Combine(elems)
|
||||
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
if (
|
||||
denomination.useAsDefaultInput === undefined ||
|
||||
denomination.useAsDefaultInput === false
|
||||
) {
|
||||
continue
|
||||
}
|
||||
let countries: string | string[] = country()
|
||||
|
@ -131,19 +142,22 @@ export class Unit {
|
|||
countries = countries.split(",")
|
||||
}
|
||||
const denominationCountries: string[] = denomination.useAsDefaultInput
|
||||
if (countries.some(country => denominationCountries.indexOf(country) >= 0)) {
|
||||
if (countries.some((country) => denominationCountries.indexOf(country) >= 0)) {
|
||||
return denomination
|
||||
}
|
||||
}
|
||||
return this.denominations[0]
|
||||
}
|
||||
|
||||
public getDefaultDenomination(country: () => string){
|
||||
|
||||
public getDefaultDenomination(country: () => string) {
|
||||
for (const denomination of this.denominations) {
|
||||
if (denomination.useIfNoUnitGiven === true || denomination.canonical === "") {
|
||||
return denomination
|
||||
}
|
||||
if (denomination.useIfNoUnitGiven === undefined || denomination.useIfNoUnitGiven === false) {
|
||||
if (
|
||||
denomination.useIfNoUnitGiven === undefined ||
|
||||
denomination.useIfNoUnitGiven === false
|
||||
) {
|
||||
continue
|
||||
}
|
||||
let countries: string | string[] = country()
|
||||
|
@ -151,11 +165,10 @@ export class Unit {
|
|||
countries = countries.split(",")
|
||||
}
|
||||
const denominationCountries: string[] = denomination.useIfNoUnitGiven
|
||||
if (countries.some(country => denominationCountries.indexOf(country) >= 0)) {
|
||||
if (countries.some((country) => denominationCountries.indexOf(country) >= 0)) {
|
||||
return denomination
|
||||
}
|
||||
}
|
||||
return this.denominations[0]
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue