-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace-message-text.ts
executable file
·91 lines (89 loc) · 2.64 KB
/
replace-message-text.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
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
/**
* @license
* Copyright FabricElements. All Rights Reserved.
*/
import hashId from './hash-id.js';
import specialCharToRegular from './special-char-to-regular.js';
import {toCamelCase} from './strings.js';
/**
* Random Reply Stop by language
*
* @param {string} language
* @return {string}
*/
const replyStop = (language: string): string => {
const messages: Record<string, string[]> = {
// Max length: 26 characters
'en': [
'Reply STOP to end',
'Reply STOP to remove',
'Reply STOP to unsubscribe',
'Reply OPT OUT to end',
'Reply OPT OUT to remove',
'Reply QUIT to end',
'Reply QUIT to remove',
'Reply QUIT to unsubscribe',
'Reply UNSUBSCRIBE to end',
'Send QUIT to end',
'Send QUIT to remove',
'Send QUIT to unsubscribe',
'Send STOP to end',
'Send STOP to remove',
'Send STOP to unsubscribe',
'Send UNSUBSCRIBE to end',
'Send OPT OUT to end',
'Send OPT OUT to remove',
],
'es': [
'Responde STOP para anular',
'Contesta STOP para anular',
'Envia STOP para anular',
'Envia STOP para cancelar',
'Envia STOP para detener',
'Envia ELIMINAR para anular',
'Envia CANCELAR para anular',
'Envia PARAR para anular',
'Envia PARAR para detener',
'Envia ELIMINAR para anular',
],
};
const messagesByLanguage = Object.prototype.hasOwnProperty.call(messages, language) ? messages[language] : messages.en;
const random = Math.floor((Math.random() * messagesByLanguage.length));
return messagesByLanguage[random];
};
/**
* Replace message text with custom keys
*
* @param {any} options
* @return {string}
*/
export default (options: {
data?: object,
domains?: string[],
language?: string,
text: string,
}): string => {
let final = options.text ? options.text.replace(/ +(?= )/g, '') : '';
const matches = final.match(/{.*?}/g);
const length = matches ? matches.length : 0;
const language = options.language ?? 'en';
for (let i = 0; i < length; i++) {
const match = matches[i];
const clean = match.toLowerCase().replace(/[{}]/gi, '');
const key = toCamelCase(clean);
let replaceValue = options.data[key] ? options.data[key] : '';
switch (key) {
case 'r': // Replaces random hash id
replaceValue = hashId();
break;
case 'replyStopUnsubscribe': // Replaces random unsubscribe message
replaceValue = replyStop(language);
break;
default:
replaceValue = specialCharToRegular(replaceValue);
break;
}
final = final.replace(matches[i], replaceValue);
}
return final.replace(/[{}]/gmi, '');
};