-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.ts
37 lines (29 loc) · 1.08 KB
/
util.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
export const dateDayDiff = (d: Date, days: number): Date => {
const res = new Date(d);
res.setDate(res.getDate() + days);
return res;
};
export const codeToFiles = (code: Record<string, unknown>): Record<string, string> => {
return Object.entries(code).reduce((res, [key, value]) => {
if (typeof value === 'string') {
res[key] = value;
}
else if (typeof value === 'object' && value !== null) {
const subfolder = codeToFiles(value as Record<string, unknown>);
Object.entries(subfolder).forEach(([filePath, content]) => res[`${key}/${filePath}`] = content);
}
return res;
}, {} as Record<string, string>);
};
export const localize = (obj: Record<string, string> | string | undefined, locale: string): string => {
if (!obj)
return '';
if (typeof obj === 'string')
return obj;
if (locale in obj)
return obj[locale];
if (locale.substring(0, 2) in obj)
return obj[locale.substring(0, 2)];
const [firstLocale] = Object.keys(obj);
return obj[firstLocale];
};