Repository URL to install this package:
|
Version:
3.12.2 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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); }
/**
* @param {Date} date
* @param {object} [translations] - Object with translations. Notice: it's used for Hub.
* @returns {string} - For example: "18 hours ago"
*/
var humanReadableDate = function humanReadableDate(date, translationsList) {
var translations = _objectSpread({
ago: 'ago',
second: 'second',
seconds: 'seconds',
minute: 'minute',
minutes: 'minutes',
hour: 'hour',
hours: 'hours',
day: 'day',
days: 'days',
month: 'month',
months: 'months',
year: 'year',
years: 'years'
}, translationsList || {});
var timeAgo = function timeAgo(interval, periodMultiple, periodSingle) {
return "".concat(Math.floor(interval), " ").concat(Math.floor(interval) > 1 ? periodMultiple : periodSingle, " ").concat(translations.ago);
};
var seconds = Math.floor((new Date() - new Date(date)) / 1000);
var interval = seconds / 31536000;
if (interval > 1) return timeAgo(interval, translations.years, translations.year);
interval = seconds / 2592000;
if (interval > 1) return timeAgo(interval, translations.months, translations.month);
interval = seconds / 86400;
if (interval > 1) return timeAgo(interval, translations.days, translations.day);
interval = seconds / 3600;
if (interval > 1) return timeAgo(interval, translations.hours, translations.hour);
interval = seconds / 60;
if (interval > 1) return timeAgo(interval, translations.minutes, translations.minute);
return timeAgo(interval, translations.seconds, translations.second);
};
export default humanReadableDate;