0.2.4 • Published 7 years ago
expr-manager v0.2.4
ExprManager
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
Type | Value |
---|---|
string | "value1" 'value2' |
number | 1 -1 1.23 -1.23 |
boolean | true false |
date | Now() "2017-10-24T10:10:10.037Z".ToDate() |
object | {a: 1, b: 2} {a: "1", b: "2"} |
array | 1,2 |
null | null |
Operator precedence
Operator | Description |
---|---|
. [] () {} | 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
Owner | Functions |
---|---|
FieldDisplayName FieldName FieldValue IIf IfNull Now Parent PropValue Random RecNo Root | |
array | Average Count Distinct Max Min Sum Where |
boolean | ToString |
date | DateOf 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 |
number | Abs Ceil Cos Exp Floor Ln Log Power Round Sin Sqrt Tan ToRMB ToString Trunc |
object | Parent RecNo |
string | LeftString Length Lower Pos Replace ReplaceReg RightString SubString ToDate ToNumber ToString Trim TrimLeft TrimRight Upper |
Example
npm install
npm start
open example/index.html
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