Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Js - Alternatives to eval mathematical expression with operator as string

Tags:

I have a library that have the feature of filtering objects based on some fields (each field has a specific kind of type - more info on https://github.com/jy95/mediaScan/wiki )

The biggest problem is to handle number properties from multiple source

// to handle number operations
export interface NumberExpressionObject {
    operator: "==" | ">" | "<" | ">=" | "<=";
    number: number;
}

// additional Properties
export interface AdditionalProperties {
    type: AdditionalPropertiesType;
    name: string;
    value: boolean | string | string[] | number | NumberSearchSyntax;
}

For example :

expect((libInstance.filterTvSeries({
            additionalProperties: [
                {type: "number", name: "whateverFieldThatDoesn'tExist", value: "<50"},
                {type: "number", name: "AnotherField", value: undefined},
                {type: "number", name: "AnotherField2", value: "<=25"},
                {type: "number", name: "AnotherField3", value: ">25"},
                {type: "number", name: "AnotherField4", value: "==25"},
            ],
            season: ">=4",
        }))).toEqual(
            new Map(),
        );

Each one must first corresponds the following regex :

const validExpression = /^(==|>|<|>=|<=)(\d+)$/;

and then will be eval by this function :

function resolveExpression(property: string,
                           expressionObject: MediaScanTypes.NumberExpressionObject,
                           object: MediaScanTypes.TPN | MediaScanTypes.TPN_Extended): boolean {
    return eval(`${object[property]}${expressionObject.operator}${expressionObject.number}`);
}

I wonder what could be a more cleaner way to do that ... Please avoid easy answer like a switch case : I tested it but it was still slower in my tests than eval ...

Function constructor is for me the same thing than eval ..

Previous posts I read :
Evaluating a string as a mathematical expression in JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
...

like image 397
jy95 Avatar asked Apr 10 '18 01:04

jy95


2 Answers

  1. implement functions for operators

    ops = { '>': (a, b) => a > b, '>=': (a, b) => a >= b };

    ops[expressionObject.operator](object[property], expressionObject.number)

  2. if the expression is always valid as expected. then following should faster as no parsing.

    eval(${object[property]}${expression})

like image 199
aaron.xiao Avatar answered Sep 20 '22 07:09

aaron.xiao


You can have some mapping from operator to function

const ops = {'==':(a,b)=> a == b};
return ops[${expressionObject.operator}](${object[property]},${expressionObject.number})
like image 44
JohanP Avatar answered Sep 21 '22 07:09

JohanP