From e2b44cb662c0ffe8effb2b9c34af37b0dcc478e6 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 13 May 2024 17:22:29 +0200 Subject: [PATCH] feat(chore): :sparkles: add utils --- utils.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 utils.ts diff --git a/utils.ts b/utils.ts new file mode 100644 index 0000000..edacc27 --- /dev/null +++ b/utils.ts @@ -0,0 +1,29 @@ +import { Login } from "@/types.ts" + +export function toLogin({ + firstname, + lastname, +}: { + firstname: string + lastname: string +}): Login { + return `${sanitizeString(firstname)}.${sanitizeString(lastname)}` +} + +export function sanitizeString(str: string): string { + return str.toLocaleLowerCase().split('').map(sanitizeChar).join('') +} + +function sanitizeChar(char: string): string { + //decompose unicode and remove diacritical marks + char = char.normalize('NFD').replace(/[\u0300-\u036f]/g, '') + if (char.match(/[a-zA-Z0-9]|-|_/)) return char + return '_' +} + +export const regex = { + uuidV4: /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/, + isoDateString: + /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$/, + mailAddress: /\S+@\S+\.\S+/, +}