This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelpers.ts
110 lines (98 loc) · 2.21 KB
/
helpers.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useRouter } from "next/router";
import useSWR, { SWRConfiguration } from "swr";
import { LocaleSchema, mapLocaleToJSON } from "./shared/LocalesMap";
/**
* The SWR config used by the application
*/
export const swrConfig: SWRConfiguration = {
fetcher: (resource, init) =>
fetch(`${process.env.NEXT_PUBLIC_API_ROOT}/${resource}`, init).then((res) =>
res.json()
),
};
/**
* The SWR fetcher for fetching data from the application
*
* @param args
* @returns JSON
*
* @reference https://swr.vercel.app/docs/data-fetching
*/
export const appSWRFetcher = (...args) =>
//@ts-expect-error Ignore this error
fetch(...args).then((res) => res.json());
/**
* Check if a object is empty
*
* @param obj The object
*
* @returns Boolean
*/
export const isEmpty = (obj: Object) => {
return Object.keys(obj).length === 0;
};
/**
* Check if a string is a number
*
* @reference https://stackoverflow.com/a/1779019/12241836
*/
export const isNum = (val: string) => /^\d+$/.test(val);
export const cleanObj = (obj: Object) =>
Object.keys(obj).forEach((key) => (!obj[key] ? delete obj[key] : {}));
/**
* The interface for the response of the `useTranslation` hook
*/
interface IUseTranslationResponse {
data: LocaleSchema;
}
/**
* Hook used to get the translation data
*
* @returns Interface with translation data in it
*/
export const useTranslation = (): IUseTranslationResponse => {
const { locale } = useRouter();
const { data } = useSWR(`/locales/${locale}.json`, {
//@ts-expect-error locale does not expect the correct types
initialData: mapLocaleToJSON(locale),
fetcher: appSWRFetcher,
});
return {
data: data,
};
};
/**
* Get the day from the date
*
* @param date The date object
*
* @returns The day
*/
export const getDay = (date: Date) => {
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[date.getDay()];
};
/**
* Get the month from the date
*
* @param date The date object
*
* @returns The month
*/
export const getMonth = (date: Date) => {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec",
];
return months[date.getMonth()];
};