@app-masters/js-lib
Version:
251 lines (242 loc) • 9.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
_moment.default.updateLocale('pt-br', require('moment/locale/pt-br'));
var DateTime = /*#__PURE__*/function () {
function DateTime() {
_classCallCheck(this, DateTime);
}
_createClass(DateTime, null, [{
key: "humanize",
value:
/**
* DONT USE IT!!! NEED MORE TESTS!!!
* Show a pretty date format
* @param date
* @param unique
* @returns {*}
*/
function humanize(date, unique) {
if (!date) return null;
var now = new Date();
var timeDiff = now - new Date(date);
timeDiff /= 1000;
var days = Math.floor(timeDiff / 86400);
var hours = Math.floor((timeDiff - days * 86400) / 3600);
var minutes = Math.floor((timeDiff - days * 86400 - hours * 3600) / 60);
var secs = Math.floor(timeDiff - days * 86400 - hours * 3600 - minutes * 60);
var result = '';
if (unique === false) {
result = [];
if (days === 1) result.push("1 dia");
if (days > 1) result.push(days + (days > 1 ? " dias" : " dia"));
if (hours > 0) result.push(hours + (hours > 1 ? " horas" : " hora"));
if (minutes > 0) result.push(minutes + (minutes > 1 ? " minutos" : " minuto"));
if (secs > 0) result.push(secs + (secs > 1 ? " segundos" : " segundo"));
result = result.join(", ");
} else {
if (days === 1) result = "1 dia";else if (days > 1) result = days + " dias";else if (hours > 0) result += hours + (hours > 1 ? " horas" : " hora");else if (minutes > 0) result += minutes + (minutes > 1 ? " minutos" : " minuto");else if (secs > 0) result += secs + (secs > 1 ? " segundos" : " segundo");else result = "agora";
}
// let x = "(" + days + " Days " + hours + " Hours " + minutes + " Minutes and " + secs + " Secondes " + ")";
return result;
}
}, {
key: "relative",
value: function relative(date, showHour, dateFormat) {
if (!date) return null;
var now = (0, _moment.default)();
var today = now.format('DD-MM-YYYY');
var tomorrow = (0, _moment.default)().add(1, 'days').format('DD-MM-YYYY');
var yesterday = (0, _moment.default)().subtract(1, 'days').format('DD-MM-YYYY');
date = (0, _moment.default)(date, dateFormat);
var dateS = date.format('DD-MM-YYYY');
today = today === dateS;
tomorrow = tomorrow === dateS;
yesterday = yesterday === dateS;
var sameMonth = now.format('MM/YYYY') === date.format('MM/YYYY');
var sameYear = now.format('YYYY') === date.format('YYYY');
var day = date.format('DD/MM');
var hour = showHour ? ' às ' + date.format('HH:mm') : '';
if (today) {
return 'Hoje' + hour;
} else if (tomorrow) {
return 'Amanhã' + hour;
} else if (yesterday) {
return 'Ontem' + hour;
} else if (sameMonth) {
return "Dia " + day.split('/')[0] + hour;
} else if (sameYear) {
return day + hour;
} else if (!sameYear) {
return date.format('DD/MM/YYYY') + hour;
} else {
console.log("days", days);
console.log("hours", hours);
console.log("minutes", minutes);
console.log("secs", secs);
}
}
}, {
key: "_sameDay",
value: function _sameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
}
/**
* Return a formated hour "hh:mm:ss" from a seconds number
* @param seconds
*/
}, {
key: "secondsToHour",
value: function secondsToHour(seconds, round) {
if (seconds === undefined || seconds === null) return null;
// console.log("seconds", seconds);
var duration = _moment.default.duration(seconds, 'seconds');
round = false;
if (round) return Math.round(duration.asHours()) + " hs";else return Math.floor(duration.asHours()) + ":" + ("0" + duration.minutes()).slice(-2);
}
/**
* Return a humanize hour like "1 dia" from a seconds number
* @param seconds
*/
}, {
key: "secondsToHumanize",
value: function secondsToHumanize(seconds) {
if (seconds === undefined || seconds === null) return null;
// console.log("seconds", seconds);
var duration = _moment.default.duration(seconds, 'seconds');
// console.log("duration", duration);
var formatted = duration.humanize();
return formatted;
}
}, {
key: "formatHour",
value: function formatHour(value) {
return (0, _moment.default)(value).format('hh:mm');
// return hora;
}
}, {
key: "_addZero",
value: function _addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
}, {
key: "formatDateTime",
value: function formatDateTime(date, space) {
if (!date) return null;
if (!space) space = ' ';
var result = '';
result = DateTime.TSToDate(date) + space + DateTime.formatHour(date);
// console.log(result);
return result;
}
// Função que recebe data DD-MM-YYYY e devolve YYYY-MM-DD
}, {
key: "DateToISO",
value: function DateToISO(dateStr) {
if (dateStr !== '') {
return dateStr.split('/').reverse().join('/');
} else {
return '';
}
}
// Função que recebe data DD-MM-YYYY e devolve UNIX TimeStamp
}, {
key: "DateToTS",
value: function DateToTS(dateStr) {
if (dateStr !== '') {
var ISODate = DateTime.DateToISO(dateStr);
var TSDate = new Date(ISODate);
return TSDate;
} else {
return '';
}
}
// Função que recebe UNIX TimeStamp e devolve data DD-MM-YYYY
}, {
key: "TSToDate",
value: function TSToDate(TSValue) {
if (TSValue !== '') {
var fullDate = new Date(TSValue);
return fullDate.getUTCDate() + '/' + (fullDate.getUTCMonth() + 1) + '/' + fullDate.getUTCFullYear();
} else {
return '';
}
}
// Função que recebe frequencia e converte para intervalo de TimeStamp
}, {
key: "freqToTS",
value: function freqToTS(freqValue, daily) {
if (daily) {
return freqValue * 3600; // 60 minutos * 60 segundos
} else {
return freqValue * 86400; // 24 horas * 60 minutos * 60 segundos
}
}
// Função que recebe data HH:MM e devolve UNIX TimeStamp
}, {
key: "TimeToTS",
value: function TimeToTS(TimeStr) {
if (TimeStr !== '') {
var timeVec = TimeStr.split(':');
return timeVec[0] * 3600 + timeVec[1] * 60;
} else {
return '';
}
}
}, {
key: "TSToTime",
value: function TSToTime(TSValue) {
if (TSValue !== '') {
var _minutes = Math.floor(TSValue % 3600 / 60);
var _hours = Math.floor(TSValue / 3600);
_hours = _hours < 10 ? '0' + _hours : _hours;
_minutes = _minutes < 10 ? '0' + _minutes : _minutes;
return _hours + ':' + _minutes;
} else {
return '';
}
}
}, {
key: "RelativeDate",
value: function RelativeDate(TimeStr) {
var dueDate = (0, _moment.default)(TimeStr, 'DD-MM-YYYY');
if (dueDate.diff((0, _moment.default)(), 'days') <= 6) {
var date = dueDate.calendar().split(' às')[0];
return date + ', ' + (0, _moment.default)(TimeStr, 'DD-MM-YYYY').format('LL');
} else {
return (0, _moment.default)(TimeStr, 'DD-MM-YYYY').format('LL');
}
}
}, {
key: "TSToFreqString",
value: function TSToFreqString(TimeStr) {
if (TimeStr <= 86400) {
return 'Diário';
} else if (TimeStr === 604800) {
return 'Semanal';
} else if (TimeStr === 1296000) {
return 'Quinzenal';
} else if (TimeStr === 2592000) {
return 'Mensal';
} else {
return null;
}
}
}]);
return DateTime;
}();
var _default = DateTime;
exports.default = _default;