0.2.4 • Published 7 years ago

expr-manager v0.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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"}
array1,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

npm.io

License

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

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago