forked from Tricked-dev/texter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojimixer.js
50 lines (44 loc) · 1.14 KB
/
emojimixer.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
const pastamap = require('./pastamap');
let MAX_EMOJIS_PER_BLOCK = 2;
module.exports = function (text) {
let blocks = splitIntoBlocks(text);
let newBlocks = [];
blocks.forEach((block) => {
newBlocks.push(block);
emojis = generateEmojisFrom(block);
if (emojis) {
newBlocks.push(' ' + emojis);
}
});
return newBlocks.join('');
};
function splitIntoBlocks(text) {
return text.match(/\s*[^\s]*/g);
}
function generateEmojisFrom(block) {
let trimmedBlock = trimNonAlphanumericalChars(block);
let matchingEmojis = getMatchingEmojis(trimmedBlock);
let emojis = [];
if (matchingEmojis) {
let numEmojis = Math.floor(Math.random() * (MAX_EMOJIS_PER_BLOCK + 1));
for (let i = 0; i < numEmojis; i++) {
emojis.push(
matchingEmojis[Math.floor(Math.random() * matchingEmojis.length)]
);
}
}
return emojis.join('');
}
function trimNonAlphanumericalChars(text) {
return text.replace(/^\W*/, '').replace(/\W*$/, '');
}
function getMatchingEmojis(word) {
let key = getAlphanumericPrefix(word.toLowerCase());
if (key in pastamap) {
return pastamap[key];
}
return [];
}
function getAlphanumericPrefix(s) {
return s.match(/^[a-z0-9]*/i);
}