2022-10-15 20:15:55 +00:00
|
|
|
const cache = {
|
|
|
|
currency: "",
|
|
|
|
};
|
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
export function resetCurrency() {
|
2022-10-15 20:15:55 +00:00
|
|
|
cache.currency = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function useFormatCurrency() {
|
2023-01-01 21:50:48 +00:00
|
|
|
if (cache.currency === "") {
|
2022-10-15 20:15:55 +00:00
|
|
|
const client = useUserApi();
|
|
|
|
|
|
|
|
const { data: group } = await client.group.get();
|
|
|
|
|
|
|
|
if (group) {
|
|
|
|
cache.currency = group.currency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (value: number | string) => fmtCurrency(value, cache.currency);
|
|
|
|
}
|
2023-01-01 21:50:48 +00:00
|
|
|
|
|
|
|
export type DateTimeFormat = "relative" | "long" | "short" | "human";
|
2023-02-28 04:52:56 +00:00
|
|
|
export type DateTimeType = "date" | "time" | "datetime";
|
2023-01-01 21:50:48 +00:00
|
|
|
|
|
|
|
function ordinalIndicator(num: number) {
|
|
|
|
if (num > 3 && num < 21) return "th";
|
|
|
|
switch (num % 10) {
|
|
|
|
case 1:
|
|
|
|
return "st";
|
|
|
|
case 2:
|
|
|
|
return "nd";
|
|
|
|
case 3:
|
|
|
|
return "rd";
|
|
|
|
default:
|
|
|
|
return "th";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 04:52:56 +00:00
|
|
|
export function fmtDate(value: string | Date, fmt: DateTimeFormat = "human", fmtType: DateTimeType): string {
|
2023-01-01 21:50:48 +00:00
|
|
|
const months = [
|
|
|
|
"January",
|
|
|
|
"February",
|
|
|
|
"March",
|
|
|
|
"April",
|
|
|
|
"May",
|
|
|
|
"June",
|
|
|
|
"July",
|
|
|
|
"August",
|
|
|
|
"September",
|
|
|
|
"October",
|
|
|
|
"November",
|
|
|
|
"December",
|
|
|
|
];
|
|
|
|
|
|
|
|
const dt = typeof value === "string" ? new Date(value) : value;
|
|
|
|
if (!dt) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validDate(dt)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-02-28 04:52:56 +00:00
|
|
|
if (fmtType === "date") {
|
|
|
|
// Offset local time
|
|
|
|
dt.setHours(dt.getHours() + dt.getTimezoneOffset() / 60);
|
|
|
|
}
|
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
switch (fmt) {
|
|
|
|
case "relative":
|
2023-01-28 22:32:39 +00:00
|
|
|
return useTimeAgo(dt).value + useDateFormat(dt, " (YYYY-MM-DD)").value;
|
2023-01-01 21:50:48 +00:00
|
|
|
case "long":
|
2023-01-28 22:32:39 +00:00
|
|
|
return useDateFormat(dt, "YYYY-MM-DD (dddd)").value;
|
2023-01-01 21:50:48 +00:00
|
|
|
case "short":
|
2023-01-28 22:32:39 +00:00
|
|
|
return useDateFormat(dt, "YYYY-MM-DD").value;
|
2023-01-01 21:50:48 +00:00
|
|
|
case "human":
|
|
|
|
// January 1st, 2021
|
|
|
|
return `${months[dt.getMonth()]} ${dt.getDate()}${ordinalIndicator(dt.getDate())}, ${dt.getFullYear()}`;
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|