Compare commits
3 Commits
8f16112b2e
...
53dda834f9
Author | SHA1 | Date |
---|---|---|
Ceda EI | 53dda834f9 | |
Ceda EI | af56ffbbb3 | |
Ceda EI | 382579ba3f |
|
@ -1,19 +1,35 @@
|
|||
function expand(words, text) {
|
||||
|
||||
const letters = text.trim().toLowerCase().split("");
|
||||
// Build a dictionary with lowercase letters as keys
|
||||
const dict = {};
|
||||
words.forEach(word => {
|
||||
|
||||
if (word == "")
|
||||
return;
|
||||
const initial = word.split("")[0].toLowerCase();
|
||||
if (initial in dict)
|
||||
dict[initial].push(word);
|
||||
else
|
||||
dict[initial] = [word];
|
||||
|
||||
});
|
||||
return letters.map((letter) => {
|
||||
|
||||
const wordsWithLetter = words.filter(i => i.match(RegExp(`^${letter}`, "i")));
|
||||
if (!(letter.toLowerCase() in dict))
|
||||
return letter;
|
||||
const wordsWithLetter = dict[letter.toLowerCase()];
|
||||
const word = wordsWithLetter[Math.floor(Math.random() * wordsWithLetter.length)];
|
||||
return word;
|
||||
|
||||
}).reduce((acc, cur) => acc + " " + cur);
|
||||
}).reduce((acc, cur) => acc + " " + cur).replace(/\s{2,}/g, " ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = (words = []) => (ctx) => {
|
||||
|
||||
words = words.filter(i => ! i.match(/'s$/));
|
||||
const message = ctx.message.text.replace(/^[^ ]+/, "");
|
||||
if (message) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue