Skip to content

Commit

Permalink
Do some refactoring in Helpers.getNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jul 31, 2024
1 parent 525d17e commit a639976
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
34 changes: 4 additions & 30 deletions src/expressions/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ export class Const extends Operand {
public setVariables(variables: Array<string>) {}
protected getCorrectValue(value: any): any {
if (!value || typeof value != "string") return value;
if (this.isBooleanValue(value)) return value.toLowerCase() === "true";
if (OperandMaker.isBooleanValue(value)) return value.toLowerCase() === "true";
if (
value.length > 1 &&
this.isQuote(value[0]) &&
this.isQuote(value[value.length - 1])
)
return value.substring(1, value.length - 1);
if (OperandMaker.isNumeric(value)) {
if (Helpers.isNumber(value)) {
if (value.indexOf("0x") == 0) return parseInt(value);
if (value.length > 1 && value[0] == "0" && (value.length < 2 || (value[1] !== "." && value[1] !== ","))) return value;
return parseFloat(value);
Expand All @@ -284,12 +284,6 @@ export class Const extends Operand {
private isQuote(ch: string): boolean {
return ch == "'" || ch == '"';
}
private isBooleanValue(value: any): boolean {
return (
value &&
(value.toLowerCase() === "true" || value.toLowerCase() === "false")
);
}
}

export class Variable extends Const {
Expand Down Expand Up @@ -416,7 +410,7 @@ export class FunctionOperand extends Operand {
}

export class OperandMaker {
static throwInvalidOperatorError(op: string) {
static throwInvalidOperatorError(op: string): void {
throw new Error("Invalid operator: '" + op + "'");
}

Expand All @@ -427,32 +421,12 @@ export class OperandMaker {
static toOperandString(value: string): string {
if (
!!value &&
!OperandMaker.isNumeric(value) &&
!Helpers.isNumber(value) &&
!OperandMaker.isBooleanValue(value)
)
value = "'" + value + "'";
return value;
}

static isSpaceString(str: string): boolean {
return !!str && !str.replace(" ", "");
}

static isNumeric(value: string): boolean {
if (
!!value &&
(value.indexOf("-") > -1 ||
value.indexOf("+") > 1 ||
value.indexOf("*") > -1 ||
value.indexOf("^") > -1 ||
value.indexOf("/") > -1 ||
value.indexOf("%") > -1)
)
return false;
if (OperandMaker.isSpaceString(value)) return false;
return Helpers.isNumber(value);
}

static isBooleanValue(value: string): boolean {
return (
!!value &&
Expand Down
21 changes: 14 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,25 @@ export class Helpers {
return !isNaN(this.getNumber(value));
}
public static getNumber(value: any): number {
if (
typeof value == "string" &&
!!value &&
value.indexOf("0x") == 0 &&
value.length > 32
)
return NaN;
if (typeof value == "string") {
if(!value.replace(" ", "")) return NaN;
if(value.indexOf("0x") == 0 && value.length > 32) return NaN;
if(Helpers.isStringHasOperator(value)) return NaN;
}
value = this.prepareStringToNumber(value);
const res = parseFloat(value);
if(isNaN(res) || !isFinite(value)) return NaN;
return res;
}
private static isStringHasOperator(str: string): boolean {
if(str.lastIndexOf("-") > 0) return false;
if(str.lastIndexOf("+") > 0) return false;
const operators = "*^/%";
for(let i = 0; i < operators.length; i ++) {
if(str.indexOf(operators[i]) > -1) return true;
}
return false;
}
private static prepareStringToNumber(val: any): any {
if(typeof val !== "string" || !val) return val;
let i = val.indexOf(",");
Expand Down

0 comments on commit a639976

Please sign in to comment.