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
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);
// /undefine
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;
}
// /define
// parse args
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) {
// Set definition
dictionary[word.toLowerCase()] = {
word: word,
definition: definition,
};
res.send(`*${word}*: ${definition}`);
return;
}
// Get definition
// Check for exact match
const exactMatch = dictionary[word.toLowerCase()];
if (exactMatch) {
res.send(`${exactMatch.word}: _${exactMatch.definition}_`);
return;
}
// Check for what they might have meant
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;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023