-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwf.js
312 lines (279 loc) · 11.9 KB
/
bwf.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
/**
* Class responsible to create and valuate another classes
* @param [optional] element: a string to parse to a class
* @param [optional] container: the container object to put classes in (default is window)
* TODO @param [optional] options: parsing and valuating options
*/
function Bwf(elem, container, options) {
var bwf = this;
var elem = elem;
var container = container || window;
var result = {};
/**
* A template to create classes
* @param className: the name of the class (as string)
* @param options: the properties to insert into class
*/
var classTemplate = function(className, options) {
var keys = options ?
Object.keys(options).filter(function(k) {
return k !== '__types';
}) : '';
var code = 'this.f = function ' + className + '(options) {\
var c = this;\
var k = [];\
"' + keys + '".split(/,/).forEach(function(key) {\
k.push(key);\
});\
var inList = function(val, lst) {\
return lst.indexOf(val) > -1;\
};\
var keys = Object.keys(options);\
keys.forEach(function(key) {\
if (inList(key, k)) {\
c[key] = options[key];\
}\
});\
c[\'instanceof\'] = function(klass) {\
return (klass.prototype.constructor.name === this.constructor.name);\
};\
return c;\
};';
var klass = eval(code);
// class dynamic data
keys.forEach(function(key) {
klass.prototype[key] = options[key];
});
// Class static data
klass.keys = keys;
klass.__types = options.__types;
klass.toString = function() {
return 'function ' + className + '() { [native code] }';
};
return klass;
};
/**
* Used to split a string in a given pattern
* @param str: the string
* @param pattern: the pattern to use
*/
var splitOn = function(str, pattern) {
var trimParts = function(lst, pattern) {
var parts = [];
lst.forEach(function(part) {
parts.push(part.trim());
});
return parts.filter(function(p) {
return !p.match(pattern);
});
};
var t = trimParts(str.toString().trim().split(pattern), pattern);
var result = [];
t.forEach(function(r) {
if (r !== '') {
result.push(r);
}
});
return result;
};
/**
* Setup specific types used in Bwf
*/
var setupInnerTypes = function() {
if (!container['Password']) {
container['Password'] = class Password {
constructor(obj) {
this.value = btoa(obj.value);
}
getPass() {
return atob(this.value);
}
instanceof() {
return (Password.prototype.constructor.name === this.constructor.name);
}
static toString() {
return 'function Password() { [native code] }';
}
};
}
Password.__types = { value: String };
}
// the Bwf itself
bwf.prototype = {
init: function() {
setupInnerTypes();
return this;
},
/**
* Function to validate instance of Beowulf
*/
instanceof: function(klass) {
return (klass.prototype.constructor.name === 'Bwf');
},
/**
* Creates a class for the given string
* @param el: the Beowulf class notation, as string
*/
create: function(el) {
elem = el || elem;
var parts = splitOn(elem, /:/);
// can split?
if ((parts !== undefined) && (parts[0] !== '')) {
// Verify if is a class
if (parts[0] !== parts[0].toLowerCase()) {
var className = parts[0];
result[className] = new Object();
var klass = result[className];
// remove first element
parts.shift();
// Parse as Bwf JSON notation
klass = JSON.parse(parts.join(':').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim());
klass.__types = new Object();
var kk = JSON.parse(parts.join(':').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim());
var deep = function(kk, klass) {
// Set variable types
Object.keys(kk).forEach(function(arg) {
if (klass[arg] instanceof Array) {
switch (klass[arg][0].toString().toLowerCase()) {
case 'string':
klass[arg] = [new String()];
klass.__types[arg] = [String];
break;
case 'number':
klass[arg] = [new Number()];
klass.__types[arg] = [Number];
break;
case 'boolean':
klass[arg] = [new Boolean()];
klass.__types[arg] = [Boolean];
break;
case 'list':
klass[arg] = [new Array()];
klass.__types[arg] = [Array];
break;
case 'object':
klass[arg] = [new Object()];
klass.__types[arg] = [Object];
break;
case 'function':
klass[arg] = [new Function()];
klass.__types[arg] = [Function];
break;
default:
// Have to match PERFECTLY and CASE SENSITIVE
if (container[klass[arg]]) {
klass[arg] = [new container[klass[arg][0]]({})];
klass.__types[arg] = [container[klass[arg][0].constructor.name]];
} else {
klass[arg] = [new Object()];
klass.__types[arg] = [Object];
}
break;
}
} else if (klass[arg] instanceof Object) {
// Set __types for deep subclass
klass[arg].__types = new Object();
// run deep mapping
deep(kk[arg], klass[arg]);
// get deep mappings back to superclass
klass.__types[arg] = klass[arg].__types;
delete klass[arg].__types;
} else {
switch (klass[arg].toString().toLowerCase()) {
case 'string':
klass[arg] = new String();
klass.__types[arg] = String;
break;
case 'number':
klass[arg] = new Number();
klass.__types[arg] = Number;
break;
case 'boolean':
klass[arg] = new Boolean();
klass.__types[arg] = Boolean;
break;
case 'list':
klass[arg] = new Array();
klass.__types[arg] = Array;
break;
case 'object':
klass[arg] = new Object();
klass.__types[arg] = Object;
break;
case 'function':
klass[arg] = new Function();
klass.__types[arg] = Function;
break;
default:
// Have to match PERFECTLY and CASE SENSITIVE
if (container[klass[arg]]) {
klass[arg] = [new container[klass[arg][0]]({})];
klass.__types[arg] = [container[klass[arg][0].constructor.name]];
} else {
klass[arg] = new Object();
klass.__types[arg] = Object;
}
break;
}
}
});
}
// Deeply map class
deep(kk, klass);
// return it
container[className] = classTemplate(className, klass);
return container[className];
}
}
return result;
},
/**
* Used to give values to a created class
* @param val: the values in Beowulf notation
*/
valuate: function(val) {
var parts = splitOn(val, / /);
// can split?
if ((parts !== undefined) && (parts[0] !== '')) {
// Verify if is a class
if (parts[0] !== parts[0].toLowerCase()) {
var className = parts[0].split(/:/)[0];
// class exists ?
if (container[className]) {
// remove first element
parts.shift();
var p = [];
var pp = parts.join(' ').trim().split('');
pp.forEach(
function(k, i) {
if (i !== 0 && i !== pp.length - 1) {
p.push(k);
}
}
);
var json = JSON.parse('{' + p.join('').trim().replace(/[a-zA-Z0-9_]+[a-zA-Z0-9\-_ ]*/g,
function(val) {
return '"' + val.trim() + '"';
}
).trim() + '}');
// parse rest of the string
return new container[className](json);
}
}
}
return result;
}
};
Bwf.toString = function() {
return 'function Bwf() { [native code] }';
};
return bwf.prototype.init();
}