129 lines
3.1 KiB
TypeScript
129 lines
3.1 KiB
TypeScript
// * Packages
|
|
import { usePage } from "@inertiajs/react";
|
|
// * Other
|
|
import useTrans from "@/hooks/use-translations";
|
|
import useLocale from "@/store/use-locale-store";
|
|
import { AppEnv } from "@/types/global";
|
|
|
|
/**
|
|
* Format a date.
|
|
* @param timestamp string
|
|
* @return string
|
|
*/
|
|
export function useFormatedDate(
|
|
timestamp: string,
|
|
timezone?: string,
|
|
time: boolean = true,
|
|
): string {
|
|
const { locale } = usePage().props.appEnv as AppEnv;
|
|
let currentTimezone = timezone;
|
|
if (!timestamp) return "Date inconnue";
|
|
|
|
const options: Intl.DateTimeFormatOptions = {
|
|
day: "2-digit",
|
|
month: "long",
|
|
year: "numeric",
|
|
};
|
|
|
|
if (time) {
|
|
options.minute = "2-digit";
|
|
options.hour = "2-digit";
|
|
}
|
|
|
|
if (currentTimezone === undefined) {
|
|
currentTimezone = locale;
|
|
}
|
|
|
|
return new Date(timestamp).toLocaleDateString(currentTimezone, options);
|
|
}
|
|
|
|
/**
|
|
* Format a time.
|
|
* @param time number
|
|
* @return string
|
|
*/
|
|
export function useFormatedTime(time: number): string {
|
|
const units = [
|
|
{
|
|
name: useTrans("uwil-common::validation.attributes.year"),
|
|
seconds: 31536000,
|
|
},
|
|
{
|
|
name: useTrans("uwil-common::validation.attributes.month"),
|
|
seconds: 2592000,
|
|
},
|
|
{
|
|
name: useTrans("uwil-common::validation.attributes.day"),
|
|
seconds: 86400,
|
|
},
|
|
{
|
|
name: useTrans("uwil-common::validation.attributes.hour"),
|
|
seconds: 3600,
|
|
},
|
|
{
|
|
name: useTrans("uwil-common::validation.attributes.minute"),
|
|
seconds: 60,
|
|
},
|
|
{ name: useTrans("uwil-common::validation.attributes.second"), seconds: 1 },
|
|
];
|
|
|
|
const result: Array<string> = [];
|
|
let remainingSeconds = time;
|
|
|
|
Object.values(units).forEach((unit) => {
|
|
const count = Math.floor(remainingSeconds / unit.seconds);
|
|
if (count > 0) {
|
|
result.push(`${count} ${unit.name}${count > 1 ? "s" : ""}`);
|
|
remainingSeconds %= unit.seconds;
|
|
}
|
|
});
|
|
|
|
return result.join(" ") || "0 seconde";
|
|
}
|
|
|
|
/**
|
|
* Format a time.
|
|
* @param number number
|
|
* @return string
|
|
*/
|
|
export function useFormatedNumber(
|
|
number: number,
|
|
options: {
|
|
decimals: number;
|
|
decimalSeparator: string;
|
|
thousandsSeparator: string;
|
|
abbreviate: boolean;
|
|
} = {
|
|
decimals: 2,
|
|
decimalSeparator: ",",
|
|
thousandsSeparator: " ",
|
|
abbreviate: false,
|
|
},
|
|
): string {
|
|
const timezone = useLocale.use.timezone();
|
|
const abbreviateNumber = (num: number) => {
|
|
const abbreviations = ["", "k", "M", "B", "T"];
|
|
const tier = Math.floor(Math.log10(Math.abs(num)) / 3);
|
|
if (tier === 0) return num.toFixed(2).replace(/\.?0+$/, "");
|
|
const suffix = abbreviations[tier];
|
|
const scale = 10 ** (tier * 3);
|
|
const scaled = num / scale;
|
|
return scaled.toFixed(2).replace(/\.?0+$/, "") + suffix;
|
|
};
|
|
|
|
let formattedNumber;
|
|
|
|
if (options.abbreviate) {
|
|
formattedNumber = abbreviateNumber(number);
|
|
} else {
|
|
formattedNumber = number
|
|
.toLocaleString(timezone, {
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: options.decimals,
|
|
})
|
|
.replace(".", options.decimalSeparator)
|
|
.replace(/\s/g, options.thousandsSeparator);
|
|
}
|
|
|
|
return String(formattedNumber);
|
|
}
|