-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
173 lines (152 loc) · 4.8 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
166
167
168
169
170
171
172
173
/*
* utils.js: Utility functions
*
* This file contains the various utility functions used throughout the bot.
*/
const probe = require('probe-image-size');
const config = require('./config');
module.exports = {
// Basic functions used across the repo.
leftpad(str) {
return (str < 10 ? `0${str}` : `${str}`);
},
toId(str) {
return str.toLowerCase().replace(/[^a-z0-9]/g, '');
},
toRoomId(str) {
return str.trim().toLowerCase().replace(/[^a-z0-9-]/g, '');
},
timestamp() {
let date = new Date();
let parts = [date.getDate(), date.getMonth() + 1, date.getHours(), date.getMinutes(), date.getSeconds()].map(this.leftpad);
return `[${parts.slice(0, 2).join('/')} ${parts.slice(2, 5).join(':')}]`;
},
parseQueryString(url) {
let split = url.split('?');
if (split.length === 1) return {};
let query = split[1];
let parts = query.split('&');
let output = {};
for (let i = 0; i < parts.length; i++) {
let elem = parts[i].split('=');
if (elem.length === 2) {
output[elem[0]] = elem[1];
}
}
return output;
},
async fitImage(url, maxHeight = 300, maxWidth = 400) {
let {height, width} = await probe(url);
let ratio = 1;
if (width <= maxWidth && height <= maxHeight) return [width, height];
if (height * (maxWidth/maxHeight) > width) {
ratio = maxHeight / height;
} else {
ratio = maxWidth / width;
}
return [Math.round(width * ratio), Math.round(height * ratio)];
},
// Utilies from Zarel/Pokemon-Showdown
plural(num, plural = 's', singular = '') {
if (num && typeof num.length === 'number') {
num = num.length;
} else if (num && typeof num.size === 'number') {
num = num.size;
} else {
num = Number(num);
}
return (num !== 1 ? plural : singular);
},
toDurationString(number) {
// TODO: replace by Intl.DurationFormat or equivalent when it becomes available (ECMA-402)
// https://github.com/tc39/ecma402/issues/47
const date = new Date(+number);
const parts = [date.getUTCFullYear() - 1970, date.getUTCMonth(), date.getUTCDate() - 1, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()];
const unitNames = ["second", "minute", "hour", "day", "month", "year"];
const positiveIndex = parts.findIndex(elem => elem > 0);
return parts.slice(positiveIndex).reverse().map((value, index) => value ? `${value} ${unitNames[index]}${this.plural(value)}` : "").reverse().join(" ").trim();
},
levenshtein(s, t, l) {
// Original levenshtein distance function by James Westgate, turned out to be the fastest
let d = [];
// Step 1
let n = s.length;
let m = t.length;
if (n === 0) return m;
if (m === 0) return n;
if (l && Math.abs(m - n) > l) return Math.abs(m - n);
// Create an array of arrays in javascript (a descending loop is quicker)
for (let i = n; i >= 0; i--) d[i] = [];
// Step 2
for (let i = n; i >= 0; i--) d[i][0] = i;
for (let j = m; j >= 0; j--) d[0][j] = j;
// Step 3
for (let i = 1; i <= n; i++) {
let sI = s.charAt(i - 1);
// Step 4
for (let j = 1; j <= m; j++) {
// Check the jagged ld total so far
if (i === j && d[i][j] > 4) return n;
let tJ = t.charAt(j - 1);
let cost = (sI === tJ) ? 0 : 1; // Step 5
// Calculate the minimum
let mi = d[i - 1][j] + 1;
let b = d[i][j - 1] + 1;
let c = d[i - 1][j - 1] + cost;
if (b < mi) mi = b;
if (c < mi) mi = c;
d[i][j] = mi; // Step 6
}
}
// Step 7
return d[n][m];
},
// (Technically came from Zarel/Pokemon-Showdown-Client but shhh)
arrayToPhrase(array, finalSeparator = 'and') {
return (array.length <= 1 ? array.join() : `${array.slice(0, -1).join(", ")}, ${finalSeparator} ${array.slice(-1)[0]}`);
},
// HTML-related functions used by webpages
wrapHTML(title, body) {
return `<!DOCTYPE html>\
<html>\
<head>\
<meta charset="UTF-8">\
<link rel="stylesheet" href="/style.css">\
<link href="https://fonts.googleapis.com/css?family=Ovo" rel="stylesheet">\
<title>${title} - ${config.username}</title>\
</head>\
<body>\
<div id="container">\
<div id="header">\
<h1>${title} - ${config.username}</h1>\
</div>\
<div id="text-area">\
${body}\
</div>\
</div>\
</body>\
</html>`;
},
// Logging-specific utility functions. `output` is exported to allow
// extensibility.
colourMap: new Map([
['STATUS', ['green', 'STATUS'.green]],
['ERROR', ['red', 'ERROR'.red]],
['PM', ['cornflowerblue', 'PM'.cyan]],
]),
stdout: '',
output(messageType, text) {
let [htmlColour, outputColour] = this.colourMap.get(messageType);
this.stdout += `${this.timestamp()} <strong style="color: ${htmlColour};">${messageType}</strong>: ${text}<br/>`;
console.log(`${this.timestamp()} ${outputColour}: ${text}`);
},
statusMsg(text) {
this.output('STATUS', text);
},
errorMsg(text) {
this.output('ERROR', text);
},
pmMsg(text) {
this.output('PM', text);
},
};