javascript-time-ago
Version:
Localized relative date/time formatting
67 lines (65 loc) • 3.3 kB
JavaScript
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var FullDateFormatter = /*#__PURE__*/function () {
function FullDateFormatter(localeOrLocales) {
_classCallCheck(this, FullDateFormatter);
// See if `Intl.DateTimeFormat` is supported in the current environment.
//
// `Intl` is present in all modern web browsers and is only absent from some of the ancient ones.
//
// Babel transforms `typeof` into some "branches"
// so istanbul will show this as "branch not covered".
/* istanbul ignore next */
var isIntlAvailable = (typeof Intl === "undefined" ? "undefined" : _typeof(Intl)) === 'object';
var isIntlDateTimeFormatAvailable = isIntlAvailable && typeof Intl.DateTimeFormat === 'function';
/* istanbul ignore else */
if (isIntlDateTimeFormatAvailable) {
this.formatter = new Intl.DateTimeFormat(localeOrLocales, VERBOSE_DATE_FORMAT_OPTIONS);
} else {
this.formatter = new FallbackDateFormatter();
}
}
_createClass(FullDateFormatter, [{
key: "format",
value: function format(dateOrTimestamp) {
return this.formatter.format(getDate(dateOrTimestamp));
}
}]);
return FullDateFormatter;
}();
export { FullDateFormatter as default };
export var FallbackDateFormatter = /*#__PURE__*/function () {
function FallbackDateFormatter() {
_classCallCheck(this, FallbackDateFormatter);
}
_createClass(FallbackDateFormatter, [{
key: "format",
value: function format(date) {
return String(date);
}
}]);
return FallbackDateFormatter;
}();
function getDate(dateOrTimestamp) {
if (typeof dateOrTimestamp === 'number') {
return new Date(dateOrTimestamp);
}
return dateOrTimestamp;
}
// `Intl.DateTimeFormat` options for outputting a verbose date.
// Formatted date example: "Thursday, December 20, 2012, 7:00:00 AM GMT+4"
var VERBOSE_DATE_FORMAT_OPTIONS = {
weekday: 'long',
day: 'numeric',
month: 'long',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
// timeZoneName: 'short'
};
//# sourceMappingURL=FullDateFormatter.js.map