-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
prepare.ts
31 lines (30 loc) · 1.14 KB
/
prepare.ts
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
import { translate } from '../../translate';
import { showWithAt } from '../tmi/showWithAt';
/**
* Prepares strings with replacement attributes
* @param translate Translation key
* @param attr Attributes to replace { 'replaceKey': 'value' }
* @param isTranslationKey consider if translation key to be translate key or pure message
*/
export function prepare(toTranslate: string, attr?: {[x: string]: any }, isTranslationKey = true): string {
attr = attr || {};
let msg = (() => {
if (isTranslationKey) {
return translate(toTranslate);
} else {
return toTranslate;
}
})();
for (const key of Object.keys(attr).sort((a, b) => b.length - a.length)) {
let value = attr[key];
if (['username', 'who', 'winner', 'sender', 'loser'].includes(key.toLowerCase())) {
if (typeof value.username !== 'undefined' || typeof value.userName !== 'undefined') {
value = showWithAt.value ? `@${value.username || value.userName}` : value.username || value.userName;
} else {
value = showWithAt.value ? `@${value}` : value;
}
}
msg = msg.replace(new RegExp('[$]' + key, 'gi'), value);
}
return msg;
}