kea-react
Version:
Componentes comunes de react
114 lines (100 loc) • 4.61 kB
text/typescript
const locale = "es-MX";
/**Formatea una fecha */
export function FormatDate(value: Date | string | number | null | undefined): string {
if (value == null)
return "";
const date = typeof (value) == "string" || typeof (value) == "number" ? new Date(value as any) : value;
return date.toLocaleString(locale, {
year: "numeric",
month: "2-digit",
day: "2-digit"
})
}
export function FormatHours(value: number | null | undefined): string {
if (value == null) return "";
const hours = Math.floor(value);
const minutes = Math.floor(value * 60 - (hours * 60));
return FormatInt(hours, 2) + ":" + FormatInt(minutes, 2);
}
/**Formatea una fecha y hora*/
export function FormatDateTime(value: Date | string | number | null | undefined): string {
if (value == null)
return "";
const date = typeof (value) == "string" || typeof (value) == "number" ? new Date(value as any) : value;
return date.toLocaleString(locale, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit"
})
}
/**Formatea una cantidad de moneda*/
export function FormatCurrency(value: number | string | null | undefined): string {
if (value == null)
return "";
return "$" + Number(value).toLocaleString(locale, { minimumFractionDigits: 2 });
}
/**Formatea un entero poniendo cierto numero de ceros */
export function FormatInt(value: number | string | null | undefined, zeroes: number): string {
const text = "" + value;
const zeroStr = "00000000000000000000" + text;
return zeroStr.substr(zeroStr.length - Math.max(zeroes, text.length));
}
export function toDate(value: Date | string): Date
export function toDate<TNull extends null | undefined>(value: Date | string | TNull): Date | TNull
export function toDate<TNull extends null | undefined>(value: Date | string | TNull): Date | TNull {
if (value == null) {
return value as TNull;
}
if (typeof value == "string") return new Date(value);
return value;
}
/**Quita los saltos de linea y recorta una cadena*/
export function formatLongText(text: string, len: number = 60) {
const removeLineJumps = text.replace(/\r|\n/, "");
if (removeLineJumps.length <= len)
return removeLineJumps;
else
return removeLineJumps.substr(0, len) + "...";
}
/**Pega todos los elemetnos de la lista con saltos de linea, si la lista es de mas de maxLen elementos nos muestra al final (n elementos más) */
export function formatLongList(list: string[], maxLen: number = 5) {
const items = list;
const max = maxLen;
if(items.length <= max ) {
return items.join("\r\n");
} else {
return items.slice(0, max).join("\r\n") + "\r\n" + "(" + (items.length - max) + " elementos mas)";
}
}
/**Formatea una fecha considerando la fecha de hoy y escribiendo el tiempo transcurrido. Ej. Justo ahora, hace 3 minutos, hace 2 semanas, hace 1 año */
export function FormatDateTimeFriendly(value: Date | string | null | undefined) {
if (value == null)
return "";
const date = toDate(value);
const now = new Date().valueOf();
const seconds = (now - date.valueOf()) / 1000;
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const weeks = Math.floor(days / 7);
const months = Math.floor(days / 30);
const years = Math.floor(days / 365.25);
return (
seconds < 30 ? "Justo ahora" :
seconds < 120 ? "Hace un minuto" :
minutes < 30 ? "Hace " + minutes + " minutos" :
minutes < 45 ? "Hace media hora" :
hours == 1 ? "Hace una hora" :
hours <= 24 ? "Hace " + hours + " horas" :
days < 2 ? "Ayer" :
days < 7 ? "Hace " + days + " dias" :
weeks == 1 ? "Hace una semana" :
weeks < 4 ? "Hace " + weeks + " semanas" :
months == 1 ? "Hace un mes" :
months < 12 ? "Hace " + months + " meses" :
years == 1 ? "Hace un año" :
"Hace " + years + " años"
);
}