-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
106 lines (92 loc) · 2.61 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const expr = require('./index.js');
function runTest(test) {
test.xps.forEach(t=>{
let xp = expr[t.type](t.xp);
console.log(xp(test.obj));
});
}
/*
let fn1 = expr.eval('(${a} + ${b}) / ${this.c} + ${d.e}');
console.log(fn1({a:4,b:6,c:10,d:{e:4}}));
var expr1 = expr.expr({time : "${client}/${address.host}:${address.port}", data : {request : {headers : "${headers}"}}});
var input1 = {client : "HOST001", address : {host : "localhost", port : 8080}, headers : "Content-Type: application/json"}
var output1 = {time: 'HOST001/localhost:8080', data: {request: {headers: 'Content-Type: application/json'}}}
var input2 = {client : "HOST002", address : {host : "localhost", port : 8443}, headers : "Content-Type: text/html"}
var output2 = {time: 'HOST002/localhost:8443', data: {request: {headers: 'Content-Type: text/html'}}}
console.log(JSON.stringify(output1,null,2));
console.log(JSON.stringify(expr1(input1),null,2));
console.log(JSON.stringify(output2,null,2));
console.log(JSON.stringify(expr1(input2),null,2));
var assign = expr.assign("a.b.c[0]");
var input3 = {a:{b:{c:[1,2,3,4,5]}}};
assign(input3,"10");
console.log(JSON.stringify(input3,null,2));
let nexpr = expr.newInstance('@');
fn1 = nexpr.eval('(@{a} + @{b}) / @{c} + @{d.e}');
console.log(fn1({a:4,b:6,c:10,d:{e:4}}));
*/
let test04 = {
obj : {client : "HOST001", address : {host : "localhost", port : 8080}, headers : "Content-Type: application/json"},
xps : [
{type:"expr", xp:"${JSON}"},
{type:"expr", xp:"${JSON:address}"},
{type:"expr", xp:"${JSON:address:0}"},
{type:"expr", xp:"${JSON:0}"}
]
}
let test05 = {
obj : {test:false},
xps : [
{type:'expr', xp:"${!this.test}"},
{type:'expr', xp:"${this.test}"},
{type:'expr', xp:"${test}"},
{type:'expr', xp:"${!test}"},
{type:'expr', xp:"${this}"}
]
}
let test06 = {
obj : {a:{b:{c:"hi"}}},
xps : [
{type:'expr', xp:"${a['b']['c']}"},
{type:'expr', xp:'${a["b"]["c"]}'},
{type:'eval', xp:"${a['b']['c']}=='hi'"},
{type:'eval', xp:'${a["b"]["c"]}=="hi"'}
]
};
let test07 = {
obj : {a:new Date()},
xps : [
{type:'expr', xp:"${a.toLocaleTimeString('en')}"},
]
};
let test08 = {
obj : {
date : "2020-01-01"
},
xps : [
{type:'expr', xp:"Date is: ${DATE:date}"},
{type:'expr', xp:"${DATE:date:YYYY-MM-DD}"},
{type:'expr', xp:"${DATE:date:YYYY-MM-DD|YYYY-MM-DD}"},
{type:'expr', xp:"${DATE:date:YYYY-MM-DD|HH:mm:ss}"},
{type:'expr', xp:"Year is: ${SUBSTR:date:0|4}"},
]
}
runTest(test04);
runTest(test05);
runTest(test06);
runTest(test07);
runTest(test08);
/*
expr.traverse({
a: {
b: 1,
c: [1,2,3],
d: {
e : "f"
}
}
},(obj,key,val)=>{
console.log(key,val);
return val;
});
*/