Skip to content

Commit

Permalink
normalize labels
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Dec 31, 2024
1 parent ea6ab6a commit cd0de79
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion apps/web/utils/gmail/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down

1 comment on commit cd0de79

@vercel
Copy link

@vercel vercel bot commented on cd0de79 Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.