-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
97 lines (87 loc) · 2.35 KB
/
util.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const axios = require("axios");
const levenshtein = require("fast-levenshtein");
const getJSON = async (url) => {
return axios
.get(url)
.then((res) => res.json())
.then((json) => json)
.catch((err) => {
throw err;
});
};
const cleanTitle = (title, blocklist) => {
let newTitle = title;
blocklist.forEach((emoji) => {
newTitle = newTitle.replace(emoji, "");
});
return newTitle;
};
const titleSplit = (title, emojiRegex) => {
const emoji = title.match(emojiRegex);
const text = title
.split(emojiRegex)
.map((s) => s.trim())
.filter(String);
const concat = (...arrays) => [].concat(...arrays.filter(Array.isArray));
return concat(emoji, text);
};
const getRandomEmoji = (allEmojis, blocklist) => {
const allowedEmojis = allEmojis.filter((emoji) => !blocklist.includes(emoji));
if (allowedEmojis.length === 0) return null;
return allowedEmojis[Math.floor(Math.random() * allowedEmojis.length)];
};
const getMappedEmoji = (title, map, blocklist, useFuzzy, defaultEmoji) => {
const caselessTitle = title.toLowerCase();
const tokens = caselessTitle.split(/[ ,.'"!?-_]/g);
for (const token of tokens) {
for (const mapKey in map) {
if (useFuzzy) {
const distance = levenshtein.get(mapKey, token);
if (distance < 3) {
const emoji = getRandomEmoji(map[mapKey], blocklist);
return emoji ? emoji : defaultEmoji;
}
}
if (mapKey.toLowerCase() == token) {
const emoji = getRandomEmoji(map[mapKey], blocklist);
return emoji ? emoji : defaultEmoji;
}
}
}
return defaultEmoji;
};
const genNewTitle = (
title,
useMap,
map,
allEmojis,
blocklist,
useFuzzy = true
) => {
const randomEmoji = getRandomEmoji(allEmojis, blocklist);
const emoji = useMap
? getMappedEmoji(title, map, blocklist, useFuzzy, randomEmoji)
: randomEmoji;
return emoji ? `${emoji} ${title}` : null;
};
const reduceTitle = (processedTitle, er) => {
let firstEmoji = "";
let text = "";
processedTitle.forEach((token) => {
const emojiRegex = er();
if (emojiRegex.test(token)) {
if (firstEmoji === "") firstEmoji = token;
} else {
text += `${token.trim()} `;
}
});
return `${firstEmoji} ${text.trim()}`;
};
module.exports = {
getJSON,
cleanTitle,
titleSplit,
getRandomEmoji,
reduceTitle,
genNewTitle,
};