-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
77 lines (67 loc) · 1.75 KB
/
utils.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
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import axios, { AxiosError } from 'axios';
import { format } from 'date-fns';
import { da } from 'date-fns/locale';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function filter<T>(arr: T[], callback: (item: T) => boolean) {
return arr.filter(callback);
}
function groupBy<T, K extends keyof any>(
array: T[],
getKey: (item: T) => K
): Record<K, T[]> {
return array.reduce((accumulator, item) => {
const key = getKey(item);
if (!accumulator[key]) {
accumulator[key] = [];
}
accumulator[key].push(item);
return accumulator;
}, {} as Record<K, T[]>);
}
// from english to danish
// key value map
const translations = {
CLOSED: 'Lukket',
OPEN: 'Åben',
};
export class Translator {
static translate(text: string) {
return translations[text as keyof typeof translations] || text;
}
}
export class DateHelper {
static parse(dateStr: string) {
return new Date(dateStr);
}
static formatPretty(dateStr: string) {
const date = this.parse(dateStr);
if (isNaN(date.getTime())) {
return 'Invalid date';
}
return format(date, 'dd/MM/yyyy HH:mm', { locale: da });
}
static extractMonth(dateStr: string) {
const date = this.parse(dateStr);
if (isNaN(date.getTime())) {
return 'Invalid date';
}
return format(date, 'MMMM', { locale: da });
}
}
export function parseNumber(value: string) {
return Number(value.replace(/[^0-9.-]+/g, ''));
}
export function placeValueIntoPlaceholder<T>(
str: string,
placeholder: string,
value: T
) {
return str.replace(placeholder, String(value));
}
export function lower(str: string) {
return str.toLowerCase();
}