-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateEmojiPasta-es6.js
48 lines (41 loc) · 1.25 KB
/
generateEmojiPasta-es6.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
const getSettings = require("./getSettings");
function generateEmojipasta(text) {
const blocks = splitIntoBlocks(text);
const 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) {
const trimmedBlock = trimNonAlphanumericalChars(block);
const matchingEmojis = getMatchingEmojis(trimmedBlock);
const emojis = [];
if (matchingEmojis) {
const 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) {
const key = getAlphanumericPrefix(word.toLowerCase());
if (key in EMOJI_MAPPINGS) {
return EMOJI_MAPPINGS[key];
}
return [];
}
function getAlphanumericPrefix(s) {
return s.match(/^[a-z0-9]*/i);
}