import { dictionary } from "https://esm.town/v/fields/dictionary";
export let defineSlackApp = async (req: express.Request, res: express.Response) => {
const text = req.body.text.trim();
res.status(200);
if (req.body.command == "/undefine") {
if (dictionary[text.toLowerCase()]) {
delete dictionary[text.toLowerCase()];
res.send(`Removed definition for *${text}*`);
}
res.send(`There is no definition for *${text}*`);
return;
}
let word;
let definition;
if (!text.includes('"')) {
word = text;
}
else {
const quote = text.indexOf('"');
word = text.slice(0, quote).trim();
definition = text.slice(quote + 1).replace('"', "");
}
if (definition) {
dictionary[word.toLowerCase()] = {
word: word,
definition: definition,
};
res.send(`*${word}*: ${definition}`);
return;
}
const exactMatch = dictionary[word.toLowerCase()];
if (exactMatch) {
res.send(`${exactMatch.word}: _${exactMatch.definition}_`);
return;
}
const { default: didYouMean, ReturnTypeEnums, ThresholdTypeEnums } =
await import("npm:didyoumean2");
const fuzzyFind = didYouMean(
word.toLowerCase(),
Object.values(dictionary),
{
matchPath: ["word"],
returnType: ReturnTypeEnums.ALL_SORTED_MATCHES,
},
);
const suggestOtherWords = (fuzzyFind.length === 0)
? "Feel free to"
: `Try ${
fuzzyFind.slice(0, 3).map((entry) => `*${entry.word}*`).join(", ")
}. or feel free to`;
res.send(
`I don't know that term. ${suggestOtherWords} define it:\n\`/define ${word} ["definition"]\``,
);
return;
};