UNPKG

kea-react

Version:

Componentes comunes de react

113 lines (112 loc) 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var locale = "es-MX"; /**Formatea una fecha */ function FormatDate(value) { if (value == null) return ""; var date = typeof (value) == "string" || typeof (value) == "number" ? new Date(value) : value; return date.toLocaleString(locale, { year: "numeric", month: "2-digit", day: "2-digit" }); } exports.FormatDate = FormatDate; function FormatHours(value) { if (value == null) return ""; var hours = Math.floor(value); var minutes = Math.floor(value * 60 - (hours * 60)); return FormatInt(hours, 2) + ":" + FormatInt(minutes, 2); } exports.FormatHours = FormatHours; /**Formatea una fecha y hora*/ function FormatDateTime(value) { if (value == null) return ""; var date = typeof (value) == "string" || typeof (value) == "number" ? new Date(value) : value; return date.toLocaleString(locale, { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" }); } exports.FormatDateTime = FormatDateTime; /**Formatea una cantidad de moneda*/ function FormatCurrency(value) { if (value == null) return ""; return "$" + Number(value).toLocaleString(locale, { minimumFractionDigits: 2 }); } exports.FormatCurrency = FormatCurrency; /**Formatea un entero poniendo cierto numero de ceros */ function FormatInt(value, zeroes) { var text = "" + value; var zeroStr = "00000000000000000000" + text; return zeroStr.substr(zeroStr.length - Math.max(zeroes, text.length)); } exports.FormatInt = FormatInt; function toDate(value) { if (value == null) { return value; } if (typeof value == "string") return new Date(value); return value; } exports.toDate = toDate; /**Quita los saltos de linea y recorta una cadena*/ function formatLongText(text, len) { if (len === void 0) { len = 60; } var removeLineJumps = text.replace(/\r|\n/, ""); if (removeLineJumps.length <= len) return removeLineJumps; else return removeLineJumps.substr(0, len) + "..."; } exports.formatLongText = formatLongText; /**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) */ function formatLongList(list, maxLen) { if (maxLen === void 0) { maxLen = 5; } var items = list; var 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)"; } } exports.formatLongList = formatLongList; /**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 */ function FormatDateTimeFriendly(value) { if (value == null) return ""; var date = toDate(value); var now = new Date().valueOf(); var seconds = (now - date.valueOf()) / 1000; var minutes = Math.floor(seconds / 60); var hours = Math.floor(minutes / 60); var days = Math.floor(hours / 24); var weeks = Math.floor(days / 7); var months = Math.floor(days / 30); var 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"); } exports.FormatDateTimeFriendly = FormatDateTimeFriendly;