-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.js
48 lines (41 loc) · 1.36 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
String.prototype.replaceAll = function(str1, str2, ignore) {
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}
exports.cyrb53 = function(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909);
h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1>>>0);
};
exports.timeToText = function(time) {
let text = ""
let days = Math.floor(time / 86400)
time %= 86400;
let hours = Math.floor(time / 3600);
time %= 3600;
let minutes = Math.floor(time / 60);
if (days > 0) {
text += days + ' day'
days > 1 ? text+='s ':text+=' '
}
if (hours > 0){
text += hours + ' hour'
hours > 1 ? text+='s ':text+=' '
}
if (minutes > 0) {
text += minutes + ' minute'
minutes > 1 ? text+='s ':text+=' '
}
if (text === '') {
return Math.round(time % 60) + ' seconds'
}
return text.slice(0, -1)
}
exports.sleep = (time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}