From 96dcba3bad5b8a393de36ab81d3c9bb36d198cc2 Mon Sep 17 00:00:00 2001 From: Geshan Manandhar Date: Tue, 27 Jun 2023 10:20:08 +1000 Subject: [PATCH] Some refactoring of the code to make it less complex --- src/helpers/slack.ts | 55 ++++++++++++++------------------------------ src/index.ts | 2 +- 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/src/helpers/slack.ts b/src/helpers/slack.ts index a5dba36..12ec017 100644 --- a/src/helpers/slack.ts +++ b/src/helpers/slack.ts @@ -29,45 +29,29 @@ export function getEmojisToReactWith(text: string): Array { } function validateIsEmojisDict(dict: any): boolean { - if (typeof dict !== "object" || dict === null) return false; + if (typeof dict !== "object" || dict === null) { + return false; + } for (const key in dict) { - if (typeof key !== "string") return false; - if (!Array.isArray(dict[key])) return false; - - for (const elem of dict[key]) { - if (typeof elem !== "string") return false; + if(Array.isArray(dict[key])) { + const everyElementInArrayIsString = dict[key].every((element: any) => typeof element === "string"); + return everyElementInArrayIsString; } } - return true; + return false; } function getEmojisFilePath(): string { - let emojisFilePath: string = DEFAULT_EMOJIS_FILE_PATH; - - if (process.env.EMOJIS_FILE_PATH) { - // check if the file path is valid - if (!fs.existsSync(process.env.EMOJIS_FILE_PATH)) { - return ""; - } else { - emojisFilePath = process.env.EMOJIS_FILE_PATH; - } - } + const canUseEmojiFileFromEnv = process.env.EMOJIS_FILE_PATH && fs.existsSync(process.env.EMOJIS_FILE_PATH); - return emojisFilePath; + return canUseEmojiFileFromEnv ? process.env.EMOJIS_FILE_PATH! : DEFAULT_EMOJIS_FILE_PATH; } function readEmojisFromFile(): Record> { const emojisFilePath = getEmojisFilePath(); - if (!emojisFilePath) { - console.error( - `Emojis file path ${process.env.EMOJIS_FILE_PATH} is invalid.` - ); - return {}; - } - try { const data = fs.readFileSync(emojisFilePath, { encoding: "utf8", @@ -94,7 +78,7 @@ function findMatchingEmojiKeywords( ): Array { const matchingEmojiKeywords: Array = []; - for (const keyword of keywords) { + for (const keyword of keywords) { //this is too deep too if (lowerCaseText.includes(keyword)) { matchingEmojiKeywords.push( Object.keys(emojis).find((key) => @@ -143,7 +127,9 @@ export async function reactToSlackPost( ): Promise { const emojisToReactWith: Array = getEmojisToReactWith(text); - if (!emojisToReactWith.length) return; + if (!emojisToReactWith.length) { + return; + } const currentEmojisOnSlackPost: Array = await getCurrentEmojisOnSlackPost( @@ -273,19 +259,12 @@ export const strategies: Record = { export async function handleSlackMessageEvent( slackWebClient: SlackWebClient, slackEvent: SlackEvent -) { - let strategyName: string; - - if (!slackEvent.subtype) { - strategyName = slackEvent.type; - } else { - strategyName = slackEvent.subtype; - } - +): Promise { + const strategyName: string = slackEvent.subtype ?? slackEvent.type; let strategy = strategies[strategyName]; if (strategy) { await strategy.handle(slackWebClient, slackEvent); - } else { - console.log(`No strategy found for event type ${slackEvent.type}`); + return; } + console.log(`No strategy found for event type ${slackEvent.type}`); } diff --git a/src/index.ts b/src/index.ts index 151c75d..5be453c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,7 @@ export const xplorersbot: HttpFunction = async (req, res) => { req: req.body, }; - await writeLog(log, message, LOG_METADATA); + writeLog(log, message, LOG_METADATA); switch (req.body.type) { case "url_verification":