-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.js
76 lines (70 loc) · 2.17 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
'use strict'
module.exports = {
padString: padString,
parseDiscordUserId: parseDiscordUserId,
replaceDiscordMentionIdsWithNames: replaceDiscordMentionIdsWithNames,
toTitleCase: toTitleCase,
truncateStr: truncateStr
}
/**
* Pads a string with characters. Used mainly for keeping table columns aligned for price and position displays
* e.g we want the price column to always be a consistent width
* ticker | price | 24hr % chg
* + XMR | $60.17 | 0.1
* + BTC | $8868.77 | 0.55
* @param {string} pad String we're padding with, usually a number of spaces, e.g. ' '
* @param {string} str String that we're padding e.g. '60.17'
* @param {boolean} padLeft If true, then padding is on the left of the string, otherwise it's on the right
* @returns {string} The padded string e.g. '$60.17 '
*/
function padString(pad, str, padLeft) {
if (typeof str === 'undefined')
return pad;
if (padLeft) {
return (pad + str).slice(-pad.length);
} else {
return (str + pad).substring(0, pad.length);
}
}
/**
* See if a string matches a discord user identifier
* @param {string} str String to check
* @returns {string} The parsed discord id if a match is found, e.g. 676989462969974844, otherwise returns null if it ain't found
*/
function parseDiscordUserId(str) {
let pattern = /<@![0-9]+>/g;
let matches = str.match(pattern);
if (matches) {
return matches[0].split('<@!').join('').split('>').join('');
}
else {
return null;
}
}
function replaceDiscordMentionIdsWithNames(str, mentionsUsers) {
for (let [userId, userObj] of mentionsUsers) {
str = str.split('<@' + userId + '>').join('@' + userObj.username).split('<@!' + userId + '>').join('@' + userObj.username);
}
return str;
}
/**
* Change a string to proper case, e.g. 'heya there' becomes 'Heya There'
* @param {string} str String to check
* @returns {string}
*/
function toTitleCase(str) {
return str.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
);
}
function truncateStr(str, num) {
if (str.length > num) {
return str.slice(0, num) + '...';
}
else {
return str;
}
}