-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.js
190 lines (169 loc) · 5.59 KB
/
cluster.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
/*jshint esversion: 6*/
var Cluster = {
'version': '0.0.1',
'versiontype': 'alpha',
'className': 'cluster',
'path': 'cluster/',
'mainFileName': 'cluster.js',
'Utils': {
uppath(path, level) {
var r = path.split('/'), f='';
r.forEach( (e, i) => {
if(i<level){
f+=e;
}
});
return f;
},
downpath(path, level) {
var r = path.split('/').reverse(), f='';
r.forEach( (e, i) => {
if(i<level){
f=e+'/'+f;
}
});
return f;
},
goodTime(ms){
if(ms >= 1000) return (ms/1000).toFixed(3)+'s';
else if(ms < 1 && ms > 0.001) return (ms*1000).toFixed(3)+'µs';
else if(ms <= 0.001) return (ms*1000000).toFixed(3)+'ns';
else return ms.toFixed(3)+'ms';
}
}
};
Cluster.whereShouldBe = function(needed){
var parsed_propertie, result='', ppath;
parsed_propertie = needed.split('.');
var last = Cluster[parsed_propertie[0]];
if(last){
parsed_propertie.forEach( (e) => {
if(last){
ppath = (last.path + last.mainFileName )|| e;
result+= '/'+ppath;
last = last[e];
} else return false;
});
} else return false;
return result;
};
Cluster.Benchmark = class Benchmark{
constructor(options={}){
this.iterations = options.iterations;
this.ms_start = options.start;
this.ms_end = options.end;
this.ms_delta = options.end-options.start;
this.ms_average = this.ms_delta/this.iterations;
this.delta = Cluster.Utils.goodTime(this.ms_delta);
this.average = Cluster.Utils.goodTime(this.ms_average);
this.mode = options.mode;
}
};
Cluster.randomHash = function(len=32, security=true){
var saltAlpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz"+ (security ? './-+_%*£\\ç^äâëêùàèé$§!()[]{}:,?\'"' : '');
var salt = 'rh$';
for(var i = 0; i < len; ++i) {
salt += saltAlpha.charAt(
Math.floor(Math.random() * saltAlpha.length));
}
return salt;
};
Cluster.benchmark = function (callback, options={}) {
var dc = // default config
{
iterations: 1,
mode: 'simple'
};
var it=1, bt=0, at=0, mode ;
if(typeof options == 'number'){
it = options;
mode = dc.mode;
}
else{
if(typeof options.iterations == 'number') it = options.iterations;
else it = dc.iterations;
if(typeof options.mode == 'string') mode = options.mode;
else mode = dc.mode;
}
if(mode == 'simple'){
bt = performance.now();
for(let i = 0; i < it ; i++){
callback();
}
at = performance.now();
}
return new Cluster.Benchmark({
iterations: it,
start: bt,
end: at,
mode: mode
});
};
Cluster.NeedError = class NeedError extends Error{
constructor(needed='some unknow', message=''){
super(message);
this.name = 'ClusterNeedError';
this.splittedStack = this.stack.split('\n');
this.fileOriginPath = this.splittedStack[this.splittedStack.length-1].replace(/at | /g,'');
this.fileOriginName = Cluster.Utils.downpath(this.fileOriginPath, 2).replace(/:[0-9]+:[0-9]+/,'');
this.stack = this.splittedStack[this.splittedStack.length-1];
this.needed = needed;
var whereis = needed+" should be in \'"+Cluster.whereShouldBe(needed)+"\'\n";
this.message = 'A cluster object in \''+this.fileOriginName+'\' need '+this.needed+' element...\n'+whereis+message;
}
};
Cluster.need = function(needed, message){
var parsed_propertie;
parsed_propertie = needed.split('.');
if(parsed_propertie.length == 1){
if(!Cluster[parsed_propertie[0]]) throw new Cluster.NeedError(needed, message);
}
else if(parsed_propertie.length == 2){
if(Cluster[parsed_propertie[0]]){
if(!Cluster[parsed_propertie[0]][parsed_propertie[1]]) throw new Cluster.NeedError(needed, message);
}
else{
throw new Cluster.NeedError(parsed_propertie[0], message);
}
}
};
Cluster.Ui = {
'classSuffix': 'ui',
'path': Cluster.path+"cluster-ui/",
'mainFileName': "component.js",
'define': function (object){
if(window.customElements){
window.customElements.define('cluster-'+object.tag, object);
}
else{
document.registerElement('cluster-'+object.tag, object);
}
}
};
Cluster.cssList = [];
/**
* css - Add css file to header dynamicly.
*
* @param {string} filename The file name
* @return {bool}
*/
Cluster.css = function(filename){
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename);
if (typeof fileref!="undefined" && Cluster.cssList.indexOf(filename) == -1){
try{
document.getElementsByTagName("head")[0].appendChild(fileref);
Cluster.cssList.push(fileref);
return true;
}
catch(err){
throw err;
}
}
return false;
}
Cluster.Ui.className = Cluster.className+'-'+Cluster.Ui.classSuffix;
export default Cluster;