-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
157 lines (157 loc) · 3.93 KB
/
util.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
import { onValue, onChildAdded, onChildChanged, onChildRemoved, onChildMoved } from "firebase/database";
import https from 'https';
import readline from 'readline';
function createTask() {
const task = {
done: false
};
task.promise = new Promise((resolve, reject)=>{
task.cancel = (err)=>{
if (!task.done) {
task.done = true;
reject(err);
}
};
task.finish = (result)=>{
if (!task.done) {
task.done = true;
resolve(result);
}
};
});
return task;
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function sleep(ms) {
return new Promise((resolve)=>setTimeout(resolve, ms)
);
}
function listenForAll(dbRef) {
onValue(dbRef, (snapshot)=>{
const data = snapshot.val();
console.log("Vdata: ", data);
});
onChildAdded(dbRef, (snapshot)=>{
const data = snapshot.val();
console.log("CAdata:", data);
});
onChildChanged(dbRef, (snapshot)=>{
const data = snapshot.val();
console.log("CCdata:", data);
});
onChildMoved(dbRef, (snapshot)=>{
const data = snapshot.val();
console.log("CMdata:", data);
});
onChildRemoved(dbRef, (snapshot)=>{
const data = snapshot.val();
console.log("CRdata:", data);
});
}
function withTimeout(promise, timeout) {
return new Promise((resolve, reject)=>{
promise.then((...data)=>resolve(...data)
);
setTimeout(()=>{
reject();
}, timeout);
});
}
function normalize(str) {
return str.normalize('NFD').replace(/(<:ph0t0shop:910908399275876362>|[\u0300-\u036f ])/g, "").toLowerCase().trim();
}
function readFileFromURL(url, mapper) {
return new Promise((resolve)=>{
https.get(url, async (res)=>{
res.setEncoding('utf8');
const result = [];
const rl = readline.createInterface({
input: res,
crlfDelay: Infinity
});
for await (const line of rl){
result.push(mapper(line));
}
resolve(result);
});
});
}
function extractAmount(str) {
let i = 0;
let char;
for(; i < str.length; i++){
char = str[i];
if (char < '0' || char > '9') {
break;
}
}
if (i == 0 || str[i] != 'x') {
return [
str,
1
];
}
return [
str.substring(i + 1).trim(),
parseInt(str)
] // TODO: change this to parseInt(str)
;
}
function formatNum(num) {
return num.toLocaleString('en-US', {
maximumFractionDigits: 0
});
}
var tmp = Symbol.iterator;
class ObjectSet {
get size() {
return Object.keys(this.dict).length;
}
add(value) {
this.dict[value.id] = value;
return this;
}
clear() {
this.dict = {
};
}
delete(value1) {
return delete this.dict[value1.id];
}
map(mapFunc) {
const res = [];
for (const value of this){
res.push(mapFunc(value));
}
return res;
}
getById(id) {
return this.dict[id];
}
has(value2) {
return value2.id in this.dict;
}
*[tmp]() {
for(const item in this.dict){
yield this.dict[item];
}
}
constructor(items){
this.dict = {
};
if (items) {
for (const item of items){
this.add(item);
}
}
}
}
function UTF8ToBase64(str) {
return Buffer.from(encodeURIComponent(str), 'utf-8').toString('base64');
}
function base64ToUTF8(str) {
return decodeURIComponent(Buffer.from(str, 'base64').toString("utf-8"));
}
export { getRandomInt, sleep, listenForAll, createTask, withTimeout, readFileFromURL, formatNum, normalize, extractAmount, ObjectSet, UTF8ToBase64, base64ToUTF8 };