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,20 +1,19 @@
|
|||
import {Translation} from "../UI/i18n/Translation";
|
||||
import {DenominationConfigJson} from "./ThemeConfig/Json/UnitConfigJson";
|
||||
import Translations from "../UI/i18n/Translations";
|
||||
import {Store} from "../Logic/UIEventSource";
|
||||
import BaseUIElement from "../UI/BaseUIElement";
|
||||
import Toggle from "../UI/Input/Toggle";
|
||||
import { Translation } from "../UI/i18n/Translation"
|
||||
import { DenominationConfigJson } from "./ThemeConfig/Json/UnitConfigJson"
|
||||
import Translations from "../UI/i18n/Translations"
|
||||
import { Store } from "../Logic/UIEventSource"
|
||||
import BaseUIElement from "../UI/BaseUIElement"
|
||||
import Toggle from "../UI/Input/Toggle"
|
||||
|
||||
export class Denomination {
|
||||
public readonly canonical: string;
|
||||
public readonly _canonicalSingular: string;
|
||||
public readonly canonical: string
|
||||
public readonly _canonicalSingular: string
|
||||
public readonly useAsDefaultInput: boolean | string[]
|
||||
public readonly useIfNoUnitGiven : boolean | string[]
|
||||
public readonly prefix: boolean;
|
||||
public readonly alternativeDenominations: string [];
|
||||
private readonly _human: Translation;
|
||||
private readonly _humanSingular?: Translation;
|
||||
|
||||
public readonly useIfNoUnitGiven: boolean | string[]
|
||||
public readonly prefix: boolean
|
||||
public readonly alternativeDenominations: string[]
|
||||
private readonly _human: Translation
|
||||
private readonly _humanSingular?: Translation
|
||||
|
||||
constructor(json: DenominationConfigJson, context: string) {
|
||||
context = `${context}.unit(${json.canonicalDenomination})`
|
||||
|
@ -24,26 +23,24 @@ export class Denomination {
|
|||
}
|
||||
this._canonicalSingular = json.canonicalDenominationSingular?.trim()
|
||||
|
||||
|
||||
json.alternativeDenomination.forEach((v, i) => {
|
||||
if (((v?.trim() ?? "") === "")) {
|
||||
if ((v?.trim() ?? "") === "") {
|
||||
throw `${context}.alternativeDenomination.${i}: invalid alternative denomination: undefined, null or only whitespace`
|
||||
}
|
||||
})
|
||||
|
||||
this.alternativeDenominations = json.alternativeDenomination?.map(v => v.trim()) ?? []
|
||||
this.alternativeDenominations = json.alternativeDenomination?.map((v) => v.trim()) ?? []
|
||||
|
||||
if(json["default"] !== undefined) {
|
||||
if (json["default"] !== undefined) {
|
||||
throw `${context} uses the old 'default'-key. Use "useIfNoUnitGiven" or "useAsDefaultInput" instead`
|
||||
}
|
||||
this.useIfNoUnitGiven = json.useIfNoUnitGiven
|
||||
this.useAsDefaultInput = json.useAsDefaultInput ?? json.useIfNoUnitGiven
|
||||
|
||||
|
||||
this._human = Translations.T(json.human, context + "human")
|
||||
this._humanSingular = Translations.T(json.humanSingular, context + "humanSingular")
|
||||
|
||||
this.prefix = json.prefix ?? false;
|
||||
|
||||
this.prefix = json.prefix ?? false
|
||||
}
|
||||
|
||||
get human(): Translation {
|
||||
|
@ -58,18 +55,14 @@ export class Denomination {
|
|||
if (this._humanSingular === undefined) {
|
||||
return this.human
|
||||
}
|
||||
return new Toggle(
|
||||
this.humanSingular,
|
||||
this.human,
|
||||
isSingular
|
||||
)
|
||||
return new Toggle(this.humanSingular, this.human, isSingular)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a representation of the given value
|
||||
* @param value: the value from OSM
|
||||
* @param actAsDefault: if set and the value can be parsed as number, will be parsed and trimmed
|
||||
*
|
||||
*
|
||||
* const unit = new Denomination({
|
||||
* canonicalDenomination: "m",
|
||||
* alternativeDenomination: ["meter"],
|
||||
|
@ -83,7 +76,7 @@ export class Denomination {
|
|||
* unit.canonicalValue("42 meter", true) // =>"42 m"
|
||||
* unit.canonicalValue("42m", true) // =>"42 m"
|
||||
* unit.canonicalValue("42", true) // =>"42 m"
|
||||
*
|
||||
*
|
||||
* // Should be trimmed if canonical is empty
|
||||
* const unit = new Denomination({
|
||||
* canonicalDenomination: "",
|
||||
|
@ -97,18 +90,18 @@ export class Denomination {
|
|||
* unit.canonicalValue("42 m", true) // =>"42"
|
||||
* unit.canonicalValue("42 meter", true) // =>"42"
|
||||
*/
|
||||
public canonicalValue(value: string, actAsDefault: boolean) : string {
|
||||
public canonicalValue(value: string, actAsDefault: boolean): string {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
const stripped = this.StrippedValue(value, actAsDefault)
|
||||
if (stripped === null) {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
if (stripped === "1" && this._canonicalSingular !== undefined) {
|
||||
return ("1 " + this._canonicalSingular).trim()
|
||||
}
|
||||
return (stripped + " " + this.canonical).trim();
|
||||
return (stripped + " " + this.canonical).trim()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,13 +112,12 @@ export class Denomination {
|
|||
* Returns null if it doesn't match this unit
|
||||
*/
|
||||
public StrippedValue(value: string, actAsDefault: boolean): string {
|
||||
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
|
||||
value = value.toLowerCase()
|
||||
const self = this;
|
||||
const self = this
|
||||
|
||||
function startsWith(key) {
|
||||
if (self.prefix) {
|
||||
|
@ -147,36 +139,39 @@ export class Denomination {
|
|||
return substr(this.canonical)
|
||||
}
|
||||
|
||||
if (this._canonicalSingular !== undefined && this._canonicalSingular !== "" && startsWith(this._canonicalSingular)) {
|
||||
if (
|
||||
this._canonicalSingular !== undefined &&
|
||||
this._canonicalSingular !== "" &&
|
||||
startsWith(this._canonicalSingular)
|
||||
) {
|
||||
return substr(this._canonicalSingular)
|
||||
}
|
||||
|
||||
for (const alternativeValue of this.alternativeDenominations) {
|
||||
if (startsWith(alternativeValue)) {
|
||||
return substr(alternativeValue);
|
||||
return substr(alternativeValue)
|
||||
}
|
||||
}
|
||||
|
||||
if (!actAsDefault) {
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
const parsed = Number(value.trim())
|
||||
if (!isNaN(parsed)) {
|
||||
return value.trim();
|
||||
return value.trim()
|
||||
}
|
||||
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
isDefaultUnit(country: () => string) {
|
||||
if(this.useIfNoUnitGiven === true){
|
||||
if (this.useIfNoUnitGiven === true) {
|
||||
return true
|
||||
}
|
||||
if(this.useIfNoUnitGiven === false){
|
||||
if (this.useIfNoUnitGiven === false) {
|
||||
return false
|
||||
}
|
||||
return this.useIfNoUnitGiven.indexOf(country()) >= 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue