File size: 1,860 Bytes
c3e8f3d
 
 
3ba9c0c
 
f80b091
3ba9c0c
 
 
f80b091
 
c3e8f3d
3ba9c0c
 
f80b091
 
3ba9c0c
f80b091
3ba9c0c
f80b091
 
 
 
 
 
 
 
 
 
 
 
3ba9c0c
f80b091
3ba9c0c
1006c22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}