refactor: use switch case instead

This commit is contained in:
Methapon2001 2024-12-19 10:45:00 +07:00
parent d8844e7599
commit 5e524ec91d
2 changed files with 23 additions and 22 deletions

View file

@ -15,6 +15,9 @@ export function dateFormatJS(opts: {
dayStyle?: 'numeric' | '2-digit';
monthStyle?: 'numeric' | '2-digit' | 'long' | 'short';
timeStyle?: 'full' | 'long' | 'medium' | 'short';
noDay?: boolean;
noMonth?: boolean;
noYear?: boolean;
}) {
const dt = opts.date ? new Date(opts.date) : new Date();
@ -25,10 +28,10 @@ export function dateFormatJS(opts: {
}
let formatted = new Intl.DateTimeFormat(opts.locale, {
day: opts.dayStyle || 'numeric',
month: opts.monthStyle || 'short',
day: opts.noDay ? undefined : opts.dayStyle || 'numeric',
month: opts.noMonth ? undefined : opts.monthStyle || 'short',
timeStyle: opts.timeStyle,
year: 'numeric',
year: opts.noYear ? undefined : 'numeric',
}).format(dt);
switch (opts.locale) {
@ -42,9 +45,8 @@ export function dateFormatJS(opts: {
}
/**
* @deprecated Please use dateFormatJS.
* @deprecated use dateFormatJS instead.
*/
export function dateFormat(
date?: string | Date | null,
fullmonth = false,

View file

@ -25,17 +25,17 @@ export function initTheme(): Theme {
* **Warning:** Must be called after initialize vue and quasar.
*/
export function setTheme(theme: Theme): Theme {
localStorage.setItem('currentTheme', theme);
if (theme !== Theme.Auto) {
Dark.set(theme === Theme.Dark);
switch (theme) {
case Theme.Light:
case Theme.Dark:
localStorage.setItem('currentTheme', theme);
Dark.set(theme !== Theme.Dark);
return theme;
default:
localStorage.setItem('currentTheme', Theme.Auto);
Dark.set(window.matchMedia('(prefers-color-scheme: dark)').matches);
return Theme.Auto;
}
if (theme === Theme.Auto) {
Dark.set(window.matchMedia('(prefers-color-scheme: dark)').matches);
}
return theme;
}
export enum Lang {
@ -49,16 +49,15 @@ export enum Lang {
* **Warning:** Must be used after initialize vue and vue-i18n
*/
export function initLang(): Lang {
const { locale } = i18n.global;
const current = localStorage.getItem('currentLanguage') as Lang | null;
if (current !== Lang.English && current !== Lang.Thai) {
return setLang(Lang.Thai);
switch (current) {
case Lang.English:
case Lang.Thai:
return setLang(current);
default:
return setLang(Lang.Thai);
}
if (!current) return setLang(locale.value as Lang);
return setLang(current);
}
/**