import HTML from "@supersoniks/concorde/core/utils/HTML";
declare namespace Intl {
class ListFormat {
constructor(locale: string, options: any);
public format(list: string[]): string;
}
class NumberFormat {
constructor(locale: string, options: any);
public format(value: number): string;
}
}
class Format {
/**
* Passe le premier caractère de la chaine en majuscule
*/
static ucFirst(str: string): string {
if (typeof str != "string") return str;
return str.charAt(0).toUpperCase() + str.substring(1);
}
static minutesDuration(
minutes: number,
locale: string = "",
unitDisplay: "long" | "short" | "narrow" | undefined = "long"
) {
if (!locale) locale = HTML.getLanguage();
const divMod = (n: number, m: number) => [Math.floor(n / m), n % m];
function timeUnitFormatter(
locale: string,
unit: string,
unitDisplay: "long" | "short" | "narrow" | undefined
) {
return new Intl.NumberFormat(locale, { style: "unit", unit, unitDisplay })
.format;
}
const [hrs, mins] = divMod(minutes, 60);
const list = [];
if (hrs) list.push(timeUnitFormatter(locale, "hour", unitDisplay)(hrs));
if (mins) list.push(timeUnitFormatter(locale, "minute", unitDisplay)(mins));
return new Intl.ListFormat(locale, {
style: "long",
type: "conjunction",
}).format(list);
}
/**
* Retourne le résultat de l'évaluation de la chaine fournie
*/
static js(value: string) {
try {
return Function("return " + value)();
} catch (e) {
return "";
}
}
}
export default Format;