Skip to content

Commit

Permalink
Update findMatchingEmoji function too return all matching emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonam Serchan authored and sonam-serchan committed Aug 1, 2023
1 parent 1a9cc0c commit a2a6306
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "index.js",
"scripts": {
"start": "functions-framework --target=xplorersbot",
"test": "task clean && jest --verbose --coverage tests",
"test": "task clean && jest --verbose --coverage --watch",
"start-xplorersbot-server": "task clean && tsc && cd out && npx functions-framework --target=xplorersbot --signature-type=http",
"start-openai-server": "task clean && tsc && cd out && npx functions-framework --target=xplorersbotOpenAI --signature-type=http"
},
Expand Down
27 changes: 27 additions & 0 deletions src/emojis/emojiHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { findMatchingEmojiKeywords } from "./emojiHandler";

const emojis = {
react: ["react", "reactjs"],
javascript: ["js", "javascript"],
programming: ["javascript", "python"],
};

describe("Emoji handler", () => {
it("should return matching emojis for a given keyword", () => {
const keywords = Object.values(emojis).flat();

const matchingEmojis = findMatchingEmojiKeywords(
keywords,
"reactivity",
emojis
);
expect(matchingEmojis).toEqual(["react"]);

const matchingEmojis2 = findMatchingEmojiKeywords(
keywords,
"javascript",
emojis
);
expect(matchingEmojis2).toEqual(["javascript", "programming"]);
});
});
18 changes: 8 additions & 10 deletions src/emojis/emojiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ export function getEmojisToReactWith(text: string): Array<string> {
return Array.from(new Set(emojisToReactWith));
}

function findMatchingEmojiKeywords(
export function findMatchingEmojiKeywords(
keywords: string[],
lowerCaseText: string,
emojis: Record<string, string[]>
): Array<string> {
const matchingEmojiKeywords: Array<string> = [];

) {
const emojiKeys = Object.keys(emojis);
for (const keyword of keywords) {
//this is too deep too
if (lowerCaseText.includes(keyword)) {
matchingEmojiKeywords.push(
Object.keys(emojis).find((key) =>
emojis[key].includes(keyword)
)!
const textHasKeyword = lowerCaseText.includes(keyword);
if (textHasKeyword) {
const matchingEmojis = emojiKeys.filter((key) =>
emojis[key].includes(keyword)
);
return matchingEmojis;
}
}
return matchingEmojiKeywords;
}

0 comments on commit a2a6306

Please sign in to comment.