import { DefinedBoolean, DefinedString } from "./definedvalue"; export class NumericalString extends DefinedString { private get isNumericalFlag(): DefinedBoolean { if (this._isNumericalFlag === undefined) { this._isNumericalFlag = new DefinedBoolean(); } return this._isNumericalFlag; } get isNumerical(): boolean { this.updateNumericalFlag(); return this.isNumericalFlag.value; } /** * nécessaire car si on surcharge un des accesseurs, il faut surcharger les 2 (merci Microsoft) */ get value() { return this.getValue(); } set value(v: string) { this.isNumericalFlag.undefine(); this.setValue(v); // this.updateNumericalFlag(); } get numericalValue(): number { if (!this.isNumerical) { throw new Error("invalid NumericalString '" + this.uncheckedValue + "' value"); } return +this.value; } get uncheckedValueString(): string { if (this.isDefined) { return this.uncheckedValue; } return ""; } private _isNumericalFlag: DefinedBoolean; constructor(s?: any) { super(s); } public toString(): string { return super.toString() + (this.isNumerical ? " [numerical]" : " [NOT numerical]"); } private updateNumericalFlag() { if (!this.isNumericalFlag.isDefined) { this.isNumericalFlag.value = false; if (this.isDefined) { if (typeof this.value === "string") { this.isNumericalFlag.value = String(this.value).trim() !== "" && !isNaN(+this.value); } } } } }