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