mapcomplete/Customizations/JSON/Denomination.ts

144 lines
4.6 KiB
TypeScript
Raw Normal View History

import {Translation} from "../../UI/i18n/Translation";
import UnitConfigJson from "./UnitConfigJson";
import Translations from "../../UI/i18n/Translations";
import BaseUIElement from "../../UI/BaseUIElement";
import Combine from "../../UI/Base/Combine";
export class Unit {
public readonly appliesToKeys: Set<string>;
2021-06-22 12:13:44 +02:00
public readonly denominations: Denomination[];
public readonly defaultDenom: Denomination;
2021-06-22 12:13:44 +02:00
public readonly eraseInvalid : boolean;
constructor(appliesToKeys: string[], applicableUnits: Denomination[], eraseInvalid: boolean) {
this.appliesToKeys = new Set(appliesToKeys);
this.denominations = applicableUnits;
2021-06-22 12:13:44 +02:00
this.defaultDenom = applicableUnits.filter(denom => denom.default)[0]
this.eraseInvalid = eraseInvalid
}
2021-06-22 12:13:44 +02:00
isApplicableToKey(key: string | undefined): boolean {
if (key === undefined) {
return false;
}
2021-06-22 12:13:44 +02:00
return this.appliesToKeys.has(key);
}
/**
* Finds which denomination is applicable and gives the stripped value back
*/
2021-06-22 12:13:44 +02:00
findDenomination(valueWithDenom: string): [string, Denomination] {
for (const denomination of this.denominations) {
2021-06-22 12:13:44 +02:00
const bare = denomination.StrippedValue(valueWithDenom)
if (bare !== null) {
return [bare, denomination]
}
2021-06-22 12:13:44 +02:00
}
return [undefined, undefined]
}
asHumanLongValue(value: string): BaseUIElement {
2021-06-22 12:13:44 +02:00
if (value === undefined) {
return undefined;
}
const [stripped, denom] = this.findDenomination(value)
const human = denom.human
2021-06-22 12:13:44 +02:00
const elems = denom.prefix ? [human, stripped] : [stripped, human];
return new Combine(elems)
2021-06-22 12:13:44 +02:00
}
}
export class Denomination {
public readonly canonical: string;
readonly default: boolean;
readonly prefix: boolean;
2021-06-22 12:13:44 +02:00
private readonly _human: Translation;
private readonly alternativeDenominations: string [];
constructor(json: UnitConfigJson, context: string) {
context = `${context}.unit(${json.canonicalDenomination})`
this.canonical = json.canonicalDenomination.trim()
if ((this.canonical ?? "") === "") {
throw `${context}: this unit has no decent canonical value defined`
}
json.alternativeDenomination.forEach((v, i) => {
if (((v?.trim() ?? "") === "")) {
throw `${context}.alternativeDenomination.${i}: invalid alternative denomination: undefined, null or only whitespace`
}
})
this.alternativeDenominations = json.alternativeDenomination?.map(v => v.trim()) ?? []
this.default = json.default ?? false;
this._human = Translations.T(json.human, context + "human")
this.prefix = json.prefix ?? false;
}
2021-06-22 12:13:44 +02:00
get human(): Translation {
return this._human.Clone()
}
public canonicalValue(value: string, actAsDefault?: boolean) {
2021-06-22 12:13:44 +02:00
if (value === undefined) {
return undefined;
}
const stripped = this.StrippedValue(value, actAsDefault)
if (stripped === null) {
return null;
}
return stripped + this.canonical
}
/**
* Returns the core value (without unit) if:
* - the value ends with the canonical or an alternative value (or begins with if prefix is set)
* - the value is a Number (without unit) and default is set
*
* Returns null if it doesn't match this unit
*/
public StrippedValue(value: string, actAsDefault?: boolean): string {
2021-06-22 12:13:44 +02:00
if (value === undefined) {
return undefined;
}
2021-06-22 12:13:44 +02:00
if (this.prefix) {
if (value.startsWith(this.canonical)) {
return value.substring(this.canonical.length).trim();
}
for (const alternativeValue of this.alternativeDenominations) {
if (value.startsWith(alternativeValue)) {
return value.substring(alternativeValue.length).trim();
}
}
} else {
if (value.endsWith(this.canonical)) {
return value.substring(0, value.length - this.canonical.length).trim();
}
for (const alternativeValue of this.alternativeDenominations) {
if (value.endsWith(alternativeValue)) {
return value.substring(0, value.length - alternativeValue.length).trim();
}
}
}
if (this.default || actAsDefault) {
const parsed = Number(value.trim())
if (!isNaN(parsed)) {
return value.trim();
}
}
return null;
}
}