All files / NCalc/Domain ValueExpression.ts

92.59% Statements 25/27
88.88% Branches 16/18
100% Functions 3/3
92.59% Lines 25/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                      268x 268x 268x 268x 33x 33x     26x 26x     1x           1x   1x   6x 6x             268x 235x       268x 268x     257x       2x 2x 2x 2x 2x 2x 2x    
import { EvaluationException, LogicalExpression, LogicalExpressionVisitor } from '@/NCalc/Domain';
 
export class ValueExpression extends LogicalExpression {
    public constructor();
    public constructor(value: string);
    public constructor(value: number);
    public constructor(value: boolean);
    public constructor(value: string, valueType: ValueType);
    public constructor(value: number, valueType: ValueType);
    public constructor(value: boolean, valueType: ValueType);
    public constructor(value?: any, valueType?: ValueType) {
        super();
        if (value !== null && value != undefined) {
            this.Value = value;
            if (valueType === null || valueType === undefined) {
                const detectedType = typeof value;
                switch (detectedType) {
                case 'number':
                case 'bigint':
                    this.Type = ValueType.Integer;
                    break;
                case 'string':
                    // Attempt to check if it is a date
                    Iif (
                        (new Date(value) as unknown as string) !== 'Invalid Date' &&
              !isNaN(new Date(value) as unknown as number)
                    ) {
                        this.Type = ValueType.DateTime;
                    } else {
                        this.Type = ValueType.String;
                    }
                    break;
                case 'boolean':
                    this.Type = ValueType.Boolean;
                    break;
                default:
                    throw new EvaluationException('This value could not be handled: ' + value);
                }
            }
        }
 
        if (valueType) {
            this.Type = valueType;
        }
    }
 
    public Value: any = '';
    public Type: ValueType = ValueType.Boolean;
 
    public Accept(visitor: LogicalExpressionVisitor) {
        visitor.Visit(this);
    }
}
 
export enum ValueType {
  None = 0,
  Integer = 1,
  String = 2,
  DateTime = 3,
  Float = 4,
  Boolean = 5
}