Spaces:
Running
Running
import { clsx, type ClassValue } from 'clsx'; | |
import { customAlphabet } from 'nanoid'; | |
import { twMerge } from 'tailwind-merge'; | |
export function cn(...inputs: ClassValue[]) { | |
return twMerge(clsx(inputs)); | |
} | |
export const nanoid = customAlphabet( | |
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
7, | |
); // 7-character random string | |
export async function fetcher<JSON = any>( | |
input: RequestInfo, | |
init?: RequestInit, | |
): Promise<JSON> { | |
const res = await fetch(input, init); | |
if (!res.ok) { | |
const json = await res.json(); | |
if (json.error) { | |
const error = new Error(json.error) as Error & { | |
status: number; | |
}; | |
error.status = res.status; | |
throw error; | |
} else { | |
throw new Error('An unexpected error occurred'); | |
} | |
} | |
return res.json(); | |
} | |
/** | |
* Checks if a given string represents a valid date in the format "YYYY-MM-DD". | |
* @param dateString - The string to be validated as a date. | |
* @returns A boolean indicating whether the input string is a valid date. | |
*/ | |
export function isValidDate(dateString: string) { | |
// Check if the format is correct using regex | |
const regex = /^\d{4}-\d{2}-\d{2}$/; | |
if (!dateString.match(regex)) { | |
return false; | |
} | |
// Parse the date parts to integers | |
const parts = dateString.split('-'); | |
const year = parseInt(parts[0], 10); | |
const month = parseInt(parts[1], 10); | |
const day = parseInt(parts[2], 10); | |
// Check if the date parts are valid | |
if (year < 1000 || year > 9999 || month === 0 || month > 12) { | |
return false; | |
} | |
// Create a date object from the parsed parts | |
const date = new Date(year, month - 1, day); | |
// Check if the date object matches the input date | |
if ( | |
date.getFullYear() !== year || | |
date.getMonth() + 1 !== month || | |
date.getDate() !== day | |
) { | |
return false; | |
} | |
return true; | |
} | |