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+/, +}