-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.js
165 lines (145 loc) · 3.64 KB
/
utils.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
function wait(predicate, options){
if (predicate()) return;
options = options || {};
options.sample_time = options.sample_time || 100;
options.timeout = isFinite(options.timeout)?(options.timeout - options.sample_time): 3000;
options.timeout_callback= options.timeout_callback || function(){};
if (options.timeout > 0) {
window.setTimeout(wait, options.sample_time, predicate, options);
}else {
options.timeout_callback();
}
}
function is_too_fast(event_name, timeout){
timeout = timeout || 1000;
event_name = event_name || arguments.callee.caller.name + "_last_call";
var last_time = storage.get(event_name, 0);
if (Date.now() - last_time < timeout){
console.log("Too fast:" + arguments.callee.caller.name);
return true;
}else{
localStorage[event_name] = Date.now();
return false;
}
}
function flatten(o) {
var prefix = arguments[1] || "", out = arguments[2] || {}, name;
for (name in o) {
if (o.hasOwnProperty(name)) {
typeof o[name] === "object" ? flatten(o[name], prefix + name + '.', out) :
out[prefix + name] = o[name];
}
}
return out;
}
var storage = {
get: function(key, def_val){
if(typeof(def_val) === "string")
return localStorage[key] || def_val;
var value = def_val;
try{
value = JSON.parse(localStorage[key]);
}catch(e){}
return value;
},
set: function(key, val){
localStorage[key] = typeof(val) === "string"? val: JSON.stringify(val);
},
unshift: function(key, value){
var old_val = this.get(key, []);
var tmp = old_val.unshift(value);
this.set(key, old_val);
return tmp;
},
shift: function(key){
var old_val = this.get(key, []);
var tmp = old_val.shift();
this.set(key, old_val);
return tmp;
},
push: function(key, new_val){
var old_val = this.get(key, []);
old_val.push(new_val);
this.set(key, old_val);
},
pop: function(key){
var old_val = this.get(key, []);
var value = old_val.pop();
this.set(key, old_val);
return value;
},
assign: function(key, val){
if(typeof(val) !== "object") throw new Error("Assign for Objects only!");
var old_val = this.get(key, {});
this.set(key, Object.assign(old_val, val));
},
maybe_set: function(key, val){
if (!(key in localStorage)) {
this.set(key, val);
}
}
};
function executeWithDelay(listOfFuncs, delay){
var foo = listOfFuncs.shift();
if (typeof foo === "function") {
try{
foo();
}catch(e){
console.error("Error in %o: %o", foo.name, e);
}
window.setTimeout(()=>{
executeWithDelay(listOfFuncs, delay);
}, delay);
}
}
function substract(a, b){
if(!Array.isArray(a)) a = toArray(a);
if(!Array.isArray(b)) b = toArray(b);
return a.filter((x)=>{return !b.includes(x);});
}
function intersection(a, b){
if(!Array.isArray(a)) a = toArray(a);
if(!Array.isArray(b)) b = toArray(b);
return a.filter((x)=>{return b.includes(x);});
}
function union(a, b){
if(!Array.isArray(a)) a = toArray(a);
if(!Array.isArray(b)) b = toArray(b);
return a.concat(substract(b, a));
}
function difference(a, b){
if(!Array.isArray(a)) a = toArray(a);
if(!Array.isArray(b)) b = toArray(b);
return substract(union(a,b), intersection(a,b));
}
function product(a, b){
var result = [];
a.forEach((x)=>{
b.forEach((y)=>{
result.push([x, y]);
result.push([y, x]);
});
});
return result;
}
function remove_repeates(a){
if(!Array.isArray(a)) a = toArray(a);
return a.reduce((acc, x)=>{
if (!acc.includes(x)) acc.push(x);
return acc;
}, []);
}
function toArray(o){
var result = [];
switch(true){
case Array.isArray(o):
result = o;
break;
default:
for(var x in o){
if (!o.hasOwnProperty(x)) continue;
result.push({key: x, val: o[x]});
}
}
return result;
}