javascript-time-ago
Version:
Localized relative date/time formatting
72 lines (69 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = chooseLocale;
exports.intlDateTimeFormatSupported = intlDateTimeFormatSupported;
exports.intlDateTimeFormatSupportedLocale = intlDateTimeFormatSupportedLocale;
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 _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) { n[e] = r[e]; } return n; }
/**
* Chooses the most appropriate locale
* (one of the registered ones)
* based on the list of preferred `locales` supplied by the user.
*
* @param {string[]} locales - the list of preferable locales (in [IETF format](https://en.wikipedia.org/wiki/IETF_language_tag)).
* @param {Function} isLocaleDataAvailable - tests if a locale is available.
*
* @returns {string} The most suitable locale.
*
* @example
* // Returns 'en'
* chooseLocale(['en-US'], undefined, (locale) => locale === 'ru' || locale === 'en')
*/
function chooseLocale(locales, isLocaleDataAvailable) {
// This is not an intelligent algorithm,
// but it will do for this library's case.
// `sr-Cyrl-BA` -> `sr-Cyrl` -> `sr`.
for (var _iterator = _createForOfIteratorHelperLoose(locales), _step; !(_step = _iterator()).done;) {
var locale = _step.value;
if (isLocaleDataAvailable(locale)) {
return locale;
}
var parts = locale.split('-');
while (parts.length > 1) {
parts.pop();
locale = parts.join('-');
if (isLocaleDataAvailable(locale)) {
return locale;
}
}
}
throw new Error("No locale data has been registered for any of the locales: ".concat(locales.join(', ')));
}
/**
* Whether can use `Intl.DateTimeFormat` for these `locales`.
* Returns the first suitable one.
* @param {(string|string[])} locales
* @return {?string} The first locale that can be used.
*/
function intlDateTimeFormatSupportedLocale(locales) {
/* istanbul ignore else */
if (intlDateTimeFormatSupported()) {
return Intl.DateTimeFormat.supportedLocalesOf(locales)[0];
}
}
/**
* Whether can use `Intl.DateTimeFormat`.
* @return {boolean}
*/
function intlDateTimeFormatSupported() {
// 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';
return isIntlAvailable && typeof Intl.DateTimeFormat === 'function';
}
//# sourceMappingURL=locale.js.map