fbla26/src/lib/shared.ts
2026-01-24 11:43:11 -06:00

42 lines
1.1 KiB
TypeScript

import type { User } from '$lib/types';
export const getCookieValue = (name: string): string =>
document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '';
export const getUserFromJWT = (jwt: string): User => JSON.parse(atob(jwt.split('.')[1]));
// export const userData = () => {
// let userData: User | null = null;
// const cookieValue = getCookieValue('jwt');
// if (cookieValue) {
// userData = getUserFromJWT(cookieValue);
// }
// return userData;
// };
export function getFormString(data: FormData, key: string): string | undefined {
const value = data.get(key);
if (value === null) {
return undefined;
}
if (typeof value !== 'string') {
throw Error(`Incorrect input in field ${key}.`);
}
return value.trim();
}
export function getRequiredFormString(data: FormData, key: string) {
const value = data.get(key);
if (typeof value !== 'string') {
throw Error(`Missing required field ${key}.`);
}
return value.trim();
}
export const dateFormatOptions: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric'
};