-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
68 lines (57 loc) · 1.86 KB
/
common.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
var Keyboard = new Set();
document.addEventListener("keydown", function(e) {Keyboard.add(e.keyCode);});
document.addEventListener("keyup", function(e) {Keyboard.delete(e.keyCode)});
var Timing = {
stamp: window.performance.now(),
delta: 1000/60,
refresh: function() {
let temp = window.performance.now();
this.delta = temp - this.stamp;
return this.stamp = temp;
}
}
let deepSerializedClone = function() {
return JSON.parse(JSON.stringify(this));
}
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
String.prototype.formatApply = function() {
var args = arguments[0];
return this.replace(/{([\w\d]+)}/g, function(match, number) {
return typeof args[number] != 'undefined' && !match.match(/^{_/) ? args[number] : match;
});
};
String.prototype.leftpad = function(padding, length) {
let output = this;
while(output.length < length) output = padding + output;
return output;
}
String.prototype.rightpad = function(padding, length) {
let output = this;
while(output.length < length) output = output + padding;
return output;
}
Boolean.xor = function(a, b) {return ( a || b ) && !( a && b );}
Math.limit = function(val, min, max) {return val < min ? min : (val > max ? max : val);}
Math.randomRange = function(a, b) {
if(typeof a === "undefined") {
min = 0; max = Number.MAX_SAFE_INTEGER;
} else if(typeof b === "undefined") {
min = 0; max = a
} else {
min = a; max = b;
}
return Math.random() * (max - min) + min;
}
Element.prototype.remove = function() {this.parentElement.removeChild(this);}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
for(var i = this.length - 1; i >= 0; i--) {
if(this[i] && this[i].parentElement) {
this[i].parentElement.removeChild(this[i]);
}
}
}