-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
370 lines (329 loc) · 8.35 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var maths = require("mathjs");
var dayjs = require('dayjs')
let handlebars = require("mustache")//Handlebars sucks eggs with svelte
async function flatten(data, sep = ".") {
data = await new Promise((resolve) => {
var result = {};
function recurse(cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for (var i = 0, l = cur.length; i < l; i++)
recurse(cur[i], prop ? prop + sep + i : "" + i);
if (l == 0)
result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop + sep + p : p);
}
if (isEmpty)
result[prop] = {};
}
}
recurse(data, "");
return resolve(result);
})
return data
}
module.exports.flat = (obj) => {
const flatten = (obj, roots=[], sep='.') => Object.keys(obj).reduce((memo, prop) => Object.assign({}, memo, Object.prototype.toString.call(obj[prop]) === '[object Object]' ? flatten(obj[prop], roots.concat([prop]), sep) : {[roots.concat([prop]).join(sep)]: obj[prop]}), {})
const data = flatten(data)
return data
//return await flatten(obj)
}
async function unflatten(obj) {
let data = await new Promise((resolve) => {
Object.unflatten_ = function (data) {
"use strict";
if (Object(data) !== data || Array.isArray(data))
return data;
var regex = /\.?([^.\[\]]+)|\[(\d+)\]/g,
resultholder = {};
for (var p in data) {
var cur = resultholder,
prop = "",
m;
while (m = regex.exec(p)) {
cur = cur[prop] || (cur[prop] = (m[2] ? [] : {}));
prop = m[2] || m[1];
}
cur[prop] = data[p];
}
return resultholder[""] || resultholder;
};
return resolve(Object.unflatten_(obj))
})
return data
}
module.exports.run2 = async (rules, data) => {
var t1 = new Date();
let r = await render(rules, data)
data = await flatten(data)
const result = await new Promise((resolve) => {
module.exports.process(r, data, (r, label, type, order) => {
return resolve(r, label, type, order)
})
})
var t2 = new Date();
var dif = t2.getTime() - t1.getTime();
console.log(dif, "milliseconds")
data = await unflatten(data)
return { ...result, data }
}
module.exports.run = (rules, data, cb) => {
//console.log(rules)
handlebar(rules, data, (r) => {
module.exports.process(r, data, cb)
})
}
const handlebar = (rules, data, cb) => {
// const template = handlebars.compile(rules)
// const result = template(data)
data.calc = function () {
return function (text, render) {
const y = render(text)
return maths.evaluate(y)
}
}
const result = handlebars.render(rules, data)
delete data.calc
return cb(result)
}
const render = async (rules, data) => {
data = await new Promise((resolve) => {
handlebar(rules, data, (r) => {
return resolve(r)
})
})
return data
}
/*Process*/
module.exports.process = (rules, data, cb) => {
const start = dayjs()
const a = rules.split("\n");
//first line is the operand
//second line is the field
const operands = a[0].split(",");
const fields = a[2].split(",");
//console.log(fields)
const type = a[1].split(",");
var results = {};
const v = a;
const reserved = ["required", "optional"]
var boolean = (value) => {
if (reserved.includes(value)) {
return value
} else {
switch (value) {
case true:
case "true":
case "True":
case "TRUE":
case "on":
case "On":
case "ON":
case "yes":
case "Yes":
return true;
break;
default:
return false;
}
}
};
function String_(value) {
let s = String(value)
if (s === 'undefined')
s = null
if (s === 'null')
s = null
return s
}
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
function Number_(m) {
return reserved.includes(m) ? m : Number(m)
}
function parseFloat_(m) {
return reserved.includes(m) ? m : parseFloat(m)
}
function date_(m) {
return reserved.includes(m) ? m : dayjs(m).unix()
}
var type_ = (t_, _t) => {
switch (t_) {
case "required":
case "mandatory":
case "enter":
return t_
break;
default:
switch (_t) {
case "double":
case "calc":
return parseFloat_(t_);
break;
case "bool":
t_ = t_ || false
// console.log("Test Bool",t_,_t)
return boolean(t_);
break;
case "number":
return Number_(t_);
break;
case "date":
switch (t_) {
case "NOW":
return dayjs().unix();
break;
default:
return date_(t_)
}
case "json":
//console.log("Note",t_,_t)
if (t_ && isString(t_))
return JSON.parse("{" + t_.split(";").join(",") + "}")
else
return (t_ || "")
break;
case 'string':
case 'signature':
default:
// code
//return null;
return String_(t_);
break;
}
}
};
const isCalc = (type, field, value) => {
var f = field
switch (type) {
case "calc":
f = maths.evaluate(field, data)
// console.log("CALC",f)
break;
case "date":
switch (value) {
case "NOW":
f = dayjs().unix();
break;
default:
f = dayjs(value).unix()
}
default:
f = value
}
return f
}
var match = (n, i, cb) => {
var t_ = true;
const f = type_(isCalc(type[i], fields[i], data[fields[i]]), type[i]);//const f = type_(data[fields[i]], type[i]);
const v = n[i] && n[i].length > 0 ? type_(n[i], type[i]) : null;
const field = fields[i]
const o = operands[i];
const match_ = v//||(type[i]==="bool")
//console.log("match",n[i],type_(n[i], type[i]))
i++;
if (match_) {
switch (o) {
case 'result':
case 'return':
results.matches = n
data[field] = v;
//console.log("Return", v,field)
break;
case "equals":
case "=":
case "===":
///console.log(field,f,v)
t_ = f === v;
break;
case "not":
case "!=":
case "!==":
t_ = f !== v;
break;
case "<":
t_ = f < v;
break;
case "<=":
t_ = f <= v;
break;
case ">":
t_ = f > v;
break;
case ">=":
t_ = f >= v;
break;
case "startsWith":
t_ = f.indexOf(v) >= 0
break;
case "contains":
t_ = f.includes(v)
break;
case "endsWith":
t_ = f.endsWith(v)
break;
case "exists":
t_ = (String_(f)?.length > 0) === boolean(v)
break;
default:
t_ = false;
}
} else {
t_ = true;
}
if (t_ && i < n.length) {
match(n, i, function () {
return cb();
});
} else {
return cb();
}
};
var determine = (m, i, cb) => {
const y = m[i].split(",")
// console.log("determine",i)
var t = 1;
match(y, t, () => {
i++;
if (i < m.length) {
determine(m, i, () => {
return cb();
});
} else {
return cb();
}
});
};
var step = 3
// console.log("LBAEL",v[step])
let labels = []
const cc = v[step].split(",")
if (cc[0] === "label") {
labels = [...cc]
labels.shift()
step++
} else {
labels = []
}
determine(v, step, () => {
const c = dayjs().diff(start, "millisecond")
console.log("Length of exec", c)
results.execution = c
let rete_results = results;
let objects = type.reduce(function (result, field, index) {
result[fields[index]] = field;
return result;
}, {})
labels = labels.reduce(function (result, field, index) {
result[fields[index + 1]] = field.length > 0 ? field : fields[index + 1];
return result;
}, {})
return cb(rete_results, labels, objects, fields)
});
}