ts: Migrate `alert_words.js` to TypeScript.

Added type annotations for function params, return values and
local variables.
This commit is contained in:
Junyao Chen 2023-07-24 02:21:55 -04:00 committed by Anders Kaseorg
parent 8ab855a3df
commit 8df9828ed1
1 changed files with 20 additions and 11 deletions

View File

@ -1,16 +1,17 @@
import _ from "lodash";
import * as people from "./people";
import type {Message} from "./types";
// For simplicity, we use a list for our internal
// data, since that matches what the server sends us.
let my_alert_words = [];
let my_alert_words: string[] = [];
export function set_words(words) {
export function set_words(words: string[]): void {
my_alert_words = words;
}
export function get_word_list() {
export function get_word_list(): {word: string}[] {
// Returns a array of objects
// (with each alert_word as value and 'word' as key to the object.)
const words = [];
@ -20,11 +21,11 @@ export function get_word_list() {
return words;
}
export function has_alert_word(word) {
export function has_alert_word(word: string): boolean {
return my_alert_words.includes(word);
}
const alert_regex_replacements = new Map([
const alert_regex_replacements = new Map<string, string>([
["&", "&amp;"],
["<", "&lt;"],
[">", "&gt;"],
@ -33,7 +34,7 @@ const alert_regex_replacements = new Map([
["'", "(?:'|&#39;)"],
]);
export function process_message(message) {
export function process_message(message: Message): void {
// Parsing for alert words is expensive, so we rely on the host
// to tell us there any alert words to even look for.
if (!message.alerted) {
@ -41,8 +42,9 @@ export function process_message(message) {
}
for (const word of my_alert_words) {
const clean = _.escapeRegExp(word).replaceAll(/["&'<>]/g, (c) =>
alert_regex_replacements.get(c),
const clean = _.escapeRegExp(word).replaceAll(
/["&'<>]/g,
(c) => alert_regex_replacements.get(c)!,
);
const before_punctuation = "\\s|^|>|[\\(\\\".,';\\[]";
const after_punctuation = "(?=\\s)|$|<|[\\)\\\"\\?!:.,';\\]!]";
@ -50,7 +52,14 @@ export function process_message(message) {
const regex = new RegExp(`(${before_punctuation})(${clean})(${after_punctuation})`, "ig");
message.content = message.content.replace(
regex,
(match, before, word, after, offset, content) => {
(
match: string,
before: string,
word: string,
after: string,
offset: number,
content: string,
) => {
// Logic for ensuring that we don't muck up rendered HTML.
const pre_match = content.slice(0, offset);
// We want to find the position of the `<` and `>` only in the
@ -69,7 +78,7 @@ export function process_message(message) {
}
}
export function notifies(message) {
export function notifies(message: Message): boolean {
// We exclude ourselves from notifications when we type one of our own
// alert words into a message, just because that can be annoying for
// certain types of workflows where everybody on your team, including
@ -77,6 +86,6 @@ export function notifies(message) {
return !people.is_current_user(message.sender_email) && message.alerted;
}
export const initialize = (params) => {
export const initialize = (params: {alert_words: string[]}): void => {
my_alert_words = params.alert_words;
};