From cd0de79fbce8853767c75c0345dd578d08097d8e Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:39:43 +0200 Subject: [PATCH] normalize labels --- apps/web/utils/gmail/label.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/web/utils/gmail/label.ts b/apps/web/utils/gmail/label.ts index 3e8a693e7..1b8a27746 100644 --- a/apps/web/utils/gmail/label.ts +++ b/apps/web/utils/gmail/label.ts @@ -203,14 +203,26 @@ export async function getLabels(gmail: gmail_v1.Gmail) { return response.data.labels; } +function normalizeLabel(name: string) { + return name + .toLowerCase() + .replace(/[-_.]/g, " ") // replace hyphens, underscores, dots with spaces + .replace(/\s+/g, " ") // multiple spaces to single space + .replace(/^\/+|\/+$/g, "") // trim slashes + .trim(); +} + export async function getLabel(options: { gmail: gmail_v1.Gmail; name: string; }) { const { gmail, name } = options; const labels = await getLabels(gmail); + + const normalizedSearch = normalizeLabel(name); + return labels?.find( - (label) => label.name?.toLowerCase() === name.toLowerCase(), + (label) => label.name && normalizeLabel(label.name) === normalizedSearch, ); }