em

expr-manager

Parse, validate, calc, and manage expression in javascript.

Showing:

Popularity

Downloads/wk

1

GitHub Stars

6

Maintenance

Last Commit

3yrs ago

Contributors

1

Package

Dependencies

2

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

ExprManager

NPM version NPM downloads MIT License Travis Status Appveyor status Circleci status Coverage Status

Installation

npm install expr-manager

Usage

Simple calculation

var exprManager = new ExprManager();

var expr = "0.1 + 0.2";
var v = exprManager.calc(expr);
if (!v.errorMsg) {
    console.log(v.toValue());
    // => 0.3
} else {
    console.log(v.errorMsg);
}

var calcData = {v1: "hello", v2: "world"};
expr = "v1 + ' ' + v2 + '!'";
v = exprManager.calc(expr, calcData);
console.log(v.toValue());
// => "hello world!"

expr = "IIf(1 > 2, 'a', 'b')";
v = exprManager.calc(expr);
console.log(v.toValue());
// => "b"

expr = "123.ToString()"
v = exprManager.calc(expr);
console.log(v.toValue());
// => "123"

Advanced calculation

var dataContext = {
    "Table": {
        fields: {
            "Field0": { type: "number", primaryKey: true },
            "Field1": { type: "string" },
            "Field2": { type: "object" },
            "Field3": { type: "array" },
            "Field4": { type: "date" },
            "Field5": { type: "boolean" },
            "CalcField0": { type: "string" },
            "CalcField1": { type: "string" }
        },
        childs: {
            "SubTable": {
                fields: {
                    "Field0": { type: "number", primaryKey: true },
                    "Field1": { type: "string" },
                    "Field2": { type: "object" },
                    "Field3": { type: "array" },
                    "Field4": { type: "date" },
                    "Field5": { type: "boolean" }
                }
            }
        }
    }
};
var data = {
    Table: [{
        Field0: 0,
        Field1: "Hello",
        Field2: {key: "i", value: 0},
        Field3: [0, 1],
        Field4: new Date(),
        Field5: false,
        SubTable: [{
            Field0: 0,
            Field1: "Wrold",
            Field2: {key: "j", value: 10},
            Field3: [2, 3],
            Field4: new Date(),
            Field5: true
        }]
    }]
};
var context = {
    Field0: "!"
};

exprManager.init(data, dataContext, context);

var tableName = "Table";
var dataCursor = {
    "Table": 0,
    "Table.SubTable": 0
};
expr = "Field1 + ' ' + SubTable[0].Field1 + $C.Field0";
v = exprManager.calcExpr(expr, tableName, dataCursor);
console.log(v.toValue());
// => "Hello World!"

var t = exprManager.calcDependencies(expr, tableName);
console.log(t.dependencies);
// => ["Table.Field1", "Table.SubTable", "Table.SubTable.Field1"]

Advanced dependent calculation

exprManager.resetExpression();

var doCalc = function(type, info) {
    console.log(type);
};

exprManager.addExpression("Field1 + ' ' + SubTable[0].Field1",
    "Table", "CalcField0", ["load", "add", "update", "remove"],
    doCalc, null);
exprManager.addExpression("CalcField0 + SubTable.Count().ToString()",
    "Table", "CalcField1", ["load", "add", "update", "remove"],
    doCalc, null);

var errorMsg = exprManager.checkAndSort();
if (!errorMsg) {
    exprManager.calcExpression("load", {
        entityName: "Table"
    });
    // doCalc => "load"

    exprManager.calcExpression("add", {
        entityName: "Table"
    });
    // doCalc => "add"

    exprManager.calcExpression("update", {
        entityName: "Table",
        propertyName: "Field1"
    });
    // doCalc => "update"

    exprManager.calcExpression("remove", {
        entityName: "Table.SubTable"
    });
    // doCalc => "remove"
} else {
    console.log(errorMsg)
}

Value type

TypeValue
string"value1" 'value2'
number1 -1 1.23 -1.23
booleantrue false
dateNow() "2017-10-24T10:10:10.037Z".ToDate()
object{a: 1, b: 2} {a: "1", b: "2"}
array[1,2]["1","2"]
nullnull

Operator precedence

OperatorDescription
. [] () {}Member access, array, grouping, object
+ - !Unary operators, logical NOT
* / %Multiplication, division, modulo division
+ -Addition, subtraction
< <= > >=Less than, less than or equal, greater than, greater than or equal
== !=Equality, inequality
&&Logical AND
||Logical OR
:Colon operator
,Multiple evaluation

System functions

OwnerFunctions
FieldDisplayName FieldName FieldValue IIf IfNull Now Parent PropValue Random RecNo Root
arrayAverage Count Distinct Max Min Sum Where
booleanToString
dateDateOf DayOf DayOfWeek DaysBetween HourOf HoursBetween IncDay IncHour IncMinute IncMonth IncSecond IncWeek IncYear MilliSecondOf MilliSecondsBetween MinuteOf MinutesBetween MonthOf MonthsBetween SecondOf SecondsBetween ToString WeekOf WeeksBetween YearOf YearsBetween
numberAbs Ceil Cos Exp Floor Ln Log Power Round Sin Sqrt Tan ToRMB ToString Trunc
objectParent RecNo
stringLeftString Length Lower Pos Replace ReplaceReg RightString SubString ToDate ToNumber ToString Trim TrimLeft TrimRight Upper

Example

npm install
npm start
open example/index.html

preview

License

expr-manager.js is freely distributable under the terms of the MIT license.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial