Commit 852a67a8 authored by Mathias Chouet's avatar Mathias Chouet :spaghetti:
Browse files

Remplacement de NumericalString par une fonction isNumeric()

Showing with 5 additions and 62 deletions
+5 -62
......@@ -55,3 +55,8 @@ export function round(val: number, prec: number) {
const m = Math.pow(10, prec || 0);
return Math.round(val * m) / m;
}
export function isNumeric(s: string): boolean {
const trimmed = s.trim();
return trimmed !== "" && !isNaN(Number(trimmed));
}
......@@ -16,7 +16,6 @@ export * from "./section/section_trapez";
export * from "./section/section_rectang";
export * from "./section/section_circulaire";
export * from "./section/section_puissance";
export * from "./util/numericalstring";
export * from "./util/message";
export * from "./util/log";
export * from "./util/result";
......
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);
}
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment