Skip to content

Commit 14db63e

Browse files
committed
Add parser to the expression builder with functuion support
1 parent 2ab1e40 commit 14db63e

10 files changed

Lines changed: 513 additions & 383 deletions

demo/index.js

Lines changed: 32 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
let funcs = {
3+
Add: function (x, y) {
4+
return x + y;
5+
},
6+
Sub: function(x, y) {
7+
return x - y;
8+
},
9+
Substr: function (str, from) {
10+
if (!str)
11+
throw new Error("In the 'Substr' function the first parameter is not defined!");
12+
13+
if (typeof str !== "string")
14+
throw new Error("The 'Substr' function accepts a string for the first parameter which is not a string!");
15+
16+
return str.substr(from);
17+
},
18+
majid: function () {
19+
return "majid";
20+
},
21+
test: function (x) {
22+
return x * 2;
23+
}
24+
};
25+
26+
27+
$(function () {
28+
expressionBuilder2('#txt', {
29+
funcs: funcs,
30+
variables: [
31+
{
32+
variableId: 1,
33+
name: 'Age'
34+
},
35+
{
36+
variableId: 2,
37+
name: 'Gender'
38+
},
39+
{
40+
variableId: 3,
41+
name: '1TEST'
42+
}
43+
]
44+
});
45+
expressionBuilder2('#txt2', {
46+
variables: [
47+
{
48+
variableId: 1,
49+
name: 'FirstName'
50+
},
51+
{
52+
variableId: 2,
53+
name: 'LastName'
54+
}
55+
],
56+
suggestions: "up",
57+
expression: "[2] + 35"
58+
});
59+
60+
$('#txt').keypress(function () {
61+
setTimeout(function () {
62+
let txtExp = expressionBuilder2('#txt');
63+
let exp = txtExp.getExpression();
64+
$('.res-1-1').html("Expression: " + exp);
65+
66+
let input = txtExp.getInput();
67+
$('.res-1-2').html("Input: " + input);
68+
}, 100);
69+
});
70+
71+
$('#btn-1-2').click(function () {
72+
let txtExp = expressionBuilder2('#txt2');
73+
txtExp.setExpression($('#txt-1-2').val());
74+
});
75+
});

parser/index.html

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@
1919
<script src="script.js"></script>
2020
<script>
2121
function main() {
22+
23+
var data = {
24+
"1name_2": "Majid Akbari",
25+
x: 6
26+
};
27+
28+
2229
let p = parser($('#formula').val());
2330

24-
let v = p.validate();
31+
let v = p.validate(data);
2532

2633
if (v != "") {
2734
document.getElementById('result').innerHTML = v;
2835
return;
2936
}
3037

31-
3238
let tree = p.getExpressionTree();
3339

34-
var data = {
35-
"name_2": "Majid Akbari",
36-
x: 6
37-
};
38-
3940
let result = p.runExpressionTree(data);
4041
document.getElementById('result').innerHTML = $('#formula').val() + " = " + result + "<br />" + tree.toString();
4142
//console.log(JSON.stringify(tree, null, 2));

0 commit comments

Comments
 (0)