total5 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/macros.js ADDED
@@ -0,0 +1,222 @@
1
+ // Supported:
2
+ // - condition: IF, ELSE, ELSE IF, FI
3
+ // - custom helpers: HELPERNAME(arg1, arg2)
4
+ // - user defined values: "1" (number), "TEXT" (text), "TRUE" (boolean), "FALSE" boolean, "YES" boolean, "NO" boolean
5
+ // - returning via return keyword
6
+ // - supports lower/upper case (but properties in the model/helpers must be in the lower case)
7
+ // - #temporary
8
+ /*
9
+ IF something >= 10
10
+ RETURN something * 10;
11
+ FI
12
+ */
13
+
14
+ const AsyncFunction = async function () {}.constructor;
15
+
16
+ function findkeywords(line, keywords, replace, allowedbeg, allowedend) {
17
+
18
+ var white = [' ', '\t', ';'];
19
+
20
+ for (var keyword of keywords) {
21
+
22
+ var reg = new RegExp(keyword, 'gi');
23
+ var match = line.match(reg);
24
+
25
+ if (!match)
26
+ continue;
27
+
28
+ var index = 0;
29
+
30
+ for (var m of match) {
31
+
32
+ index = line.indexOf(m);
33
+
34
+ if (index === -1)
35
+ break;
36
+
37
+ var beg = line.substring(index - 1, index);
38
+
39
+ if (index === 0 || white.includes(beg) || (allowedbeg && allowedbeg.includes(beg))) {
40
+ var length = index + m.length;
41
+ var end = line.substring(length, length + 1);
42
+
43
+ if (!end || white.includes(end) || (allowedend && allowedend.includes(end))) {
44
+ var output = replace(m);
45
+ line = line.substring(0, index) + output + line.substring(length);
46
+ }
47
+ }
48
+ }
49
+ }
50
+ return line;
51
+ }
52
+
53
+ function prepareline(str, meta, isasync) {
54
+
55
+ str = str.trim();
56
+
57
+ if (str.substring(0, 2) === '//')
58
+ return;
59
+
60
+ // User defined values
61
+ str = str.replace(/(".*?")|('.*?')/g, function(text) {
62
+
63
+ var key = '@' + meta.indexer + '@';
64
+
65
+ text = text.substring(1, text.length - 1);
66
+ if ((/^[0-9.,]+$/).test(text)) {
67
+ text = text.parseFloat();
68
+ } else {
69
+ var boolean = text.toLowerCase();
70
+ if (boolean === 'true' || boolean === 'false')
71
+ text = boolean === 'true';
72
+ else
73
+ text = '"' + text + '"';
74
+ }
75
+
76
+ meta.keywords[key] = text;
77
+ meta.indexer++;
78
+ return key;
79
+ });
80
+
81
+ if (str.indexOf(';') !== -1) {
82
+ var lines = str.split(';');
83
+ for (var m of lines)
84
+ m = prepareline(m, meta, isasync);
85
+ return lines.join('\n');
86
+ }
87
+
88
+ // Return
89
+ str = findkeywords(str, ['return'], function(text) {
90
+ var key = '@' + meta.indexer + '@';
91
+ meta.keywords[key] = text.toLowerCase();
92
+ meta.indexer++;
93
+ return key;
94
+ });
95
+
96
+ // End condition
97
+ str = findkeywords(str, ['fi'], function() {
98
+ return '}';
99
+ });
100
+
101
+ // Boolean
102
+ str = findkeywords(str, ['true', 'false', 'yes', 'no', 'ok'], function(text) {
103
+ text = text.toLowerCase();
104
+ return text === 'yes' || text === 'true' || text === 'ok';
105
+ }, ['('], [')']);
106
+
107
+ // AND OR
108
+ str = findkeywords(str, ['and', 'or'], function(text) {
109
+ return text.toLowerCase().replace(/and/g, '&&').replace(/or/g, '||');
110
+ });
111
+
112
+ // Conditions
113
+ var lower = str.toLowerCase();
114
+ var index = lower.indexOf('else');
115
+ if (index !== -1) {
116
+ var tmp = str.substring(index);
117
+ var tmplower = tmp.toLowerCase();
118
+ str = str.substring(0, index) + '}' + tmp + (tmplower.indexOf('else if') === -1 ? '{' : '');
119
+ }
120
+
121
+ if (lower.indexOf('if ') !== -1) {
122
+ str = str.replace(/.=./g, function(text) {
123
+ if (text[0] === '>' || text[0] === '<' || text[0] === '=' || (text[1] === '=' && text[2] === '='))
124
+ return text;
125
+ if (text[2] === '>' || text[2] === '<')
126
+ return text[2] + text[1] + text[0];
127
+ if (text[1] === '=')
128
+ return text[0] + '=' + text[1] + text[2];
129
+ return text;
130
+ }) + '){';
131
+ }
132
+
133
+ // Conditions
134
+ str = str.replace(/(\s)?(else|else\sif|if)(\s)?/ig, function(text) {
135
+ var key = '@' + meta.indexer + '@';
136
+ meta.keywords[key] = text.replace(/if(\s)/i, 'if(').replace(/else/i, 'else');
137
+ meta.indexer++;
138
+ return key;
139
+ });
140
+
141
+ // Conditions
142
+ str = findkeywords(str, ['else', 'else if', 'if'], function(text) {
143
+ text = text.toLowerCase();
144
+ var key = '@' + meta.indexer + '@';
145
+ meta.keywords[key] = text.replace(/if(\s)/i, 'if(').replace(/else/i, 'else');
146
+ meta.indexer++;
147
+ return key;
148
+ });
149
+
150
+ // Null
151
+ str = findkeywords(str, ['null'], function(text) {
152
+ var key = '@' + meta.indexer + '@';
153
+ text = text.toLowerCase();
154
+ meta.keywords[key] = text.replace(/null/i, 'null');
155
+ meta.indexer++;
156
+ return key;
157
+ });
158
+
159
+ // Helpers
160
+ str = str.replace(/[a-z0-9_]+\((\))?/ig, function(text) {
161
+ var key = '@' + meta.indexer + '@';
162
+ var index = text.indexOf('(');
163
+ meta.keywords[key] = (isasync ? 'await ' : '') + 'helpers.' + (text.substring(0, index) + '.call(model' + (text.substring(index) === '()' ? ')' : ',')).toLowerCase();
164
+ meta.indexer++;
165
+ return key;
166
+ });
167
+
168
+ // Temporary variables
169
+ str = str.replace(/#[a-z0-9_.]+./ig, function(text) {
170
+
171
+ var last = text[text.length - 1];
172
+ if (last === '@')
173
+ text = text.substring(0, text.length - 1);
174
+ else
175
+ last = '';
176
+
177
+ var key = '@' + meta.indexer + '@';
178
+ meta.keywords[key] = text.substring(1).toLowerCase().replace(/[a-z]/i, text => 'tmp.' + text);
179
+ meta.indexer++;
180
+ return key + last;
181
+ });
182
+
183
+ // Properties & fixed values
184
+ str = str.replace(/.[a-z0-9_.]+./ig, function(text) {
185
+
186
+ if ((/@[0-9]+@|true|false(\))?/).test(text))
187
+ return text;
188
+
189
+ if ((/^[0-9.]+$/i).test(text)) {
190
+ return text;
191
+ }
192
+
193
+ text = text.toLowerCase().replace(/[a-z]/i, function(text) {
194
+ return 'model.' + text;
195
+ });
196
+
197
+ return text;
198
+ });
199
+
200
+ if (str)
201
+ meta.builder.push(str.replace(/(\s)?(=|>|<|\+|-)(\s)?/g, n => n.trim()) + ';');
202
+
203
+ }
204
+
205
+ exports.compile = function(str, nocompile, isasync) {
206
+
207
+ var meta = {};
208
+ meta.keywords = {};
209
+ meta.indexer = 0;
210
+ meta.builder = [];
211
+
212
+ var lines = str.split('\n');
213
+
214
+ for (var line of lines)
215
+ prepareline(line, meta, isasync);
216
+
217
+ var compiled = meta.builder.join('\n').replace(/@\d+@/gi, function(text) {
218
+ return meta.keywords[text];
219
+ });
220
+
221
+ return nocompile ? compiled : new (isasync ? AsyncFunction : Function)('model', 'helpers', 'var tmp={};\n' + compiled);
222
+ };