UNPKG

@formatjs/intl-relativetimeformat

Version:

Formats JavaScript dates to relative time strings.

121 lines (120 loc) 5.51 kB
import { CanonicalizeLocaleList, SupportedLocales, ToString, } from '@formatjs/ecma402-abstract'; import { InitializeRelativeTimeFormat } from './abstract/InitializeRelativeTimeFormat'; import { PartitionRelativeTimePattern } from './abstract/PartitionRelativeTimePattern'; import getInternalSlots from './get_internal_slots'; var RelativeTimeFormat = /** @class */ (function () { function RelativeTimeFormat(locales, options) { // test262/test/intl402/RelativeTimeFormat/constructor/constructor/newtarget-undefined.js // Cannot use `new.target` bc of IE11 & TS transpiles it to something else var newTarget = this && this instanceof RelativeTimeFormat ? this.constructor : void 0; if (!newTarget) { throw new TypeError("Intl.RelativeTimeFormat must be called with 'new'"); } return InitializeRelativeTimeFormat(this, locales, options, { getInternalSlots: getInternalSlots, availableLocales: RelativeTimeFormat.availableLocales, relevantExtensionKeys: RelativeTimeFormat.relevantExtensionKeys, localeData: RelativeTimeFormat.localeData, getDefaultLocale: RelativeTimeFormat.getDefaultLocale, }); } RelativeTimeFormat.prototype.format = function (value, unit) { if (typeof this !== 'object') { throw new TypeError('format was called on a non-object'); } var internalSlots = getInternalSlots(this); if (!internalSlots.initializedRelativeTimeFormat) { throw new TypeError('format was called on a invalid context'); } return PartitionRelativeTimePattern(this, Number(value), ToString(unit), { getInternalSlots: getInternalSlots, }) .map(function (el) { return el.value; }) .join(''); }; RelativeTimeFormat.prototype.formatToParts = function (value, unit) { if (typeof this !== 'object') { throw new TypeError('formatToParts was called on a non-object'); } var internalSlots = getInternalSlots(this); if (!internalSlots.initializedRelativeTimeFormat) { throw new TypeError('formatToParts was called on a invalid context'); } return PartitionRelativeTimePattern(this, Number(value), ToString(unit), { getInternalSlots: getInternalSlots }); }; RelativeTimeFormat.prototype.resolvedOptions = function () { if (typeof this !== 'object') { throw new TypeError('resolvedOptions was called on a non-object'); } var internalSlots = getInternalSlots(this); if (!internalSlots.initializedRelativeTimeFormat) { throw new TypeError('resolvedOptions was called on a invalid context'); } // test262/test/intl402/RelativeTimeFormat/prototype/resolvedOptions/type.js return { locale: internalSlots.locale, style: internalSlots.style, numeric: internalSlots.numeric, numberingSystem: internalSlots.numberingSystem, }; }; RelativeTimeFormat.supportedLocalesOf = function (locales, options) { return SupportedLocales(RelativeTimeFormat.availableLocales, CanonicalizeLocaleList(locales), options); }; RelativeTimeFormat.__addLocaleData = function () { var data = []; for (var _i = 0; _i < arguments.length; _i++) { data[_i] = arguments[_i]; } for (var _a = 0, data_1 = data; _a < data_1.length; _a++) { var _b = data_1[_a], d = _b.data, locale = _b.locale; var minimizedLocale = new Intl.Locale(locale) .minimize() .toString(); RelativeTimeFormat.localeData[locale] = RelativeTimeFormat.localeData[minimizedLocale] = d; RelativeTimeFormat.availableLocales.add(minimizedLocale); RelativeTimeFormat.availableLocales.add(locale); if (!RelativeTimeFormat.__defaultLocale) { RelativeTimeFormat.__defaultLocale = minimizedLocale; } } }; RelativeTimeFormat.getDefaultLocale = function () { return RelativeTimeFormat.__defaultLocale; }; RelativeTimeFormat.localeData = {}; RelativeTimeFormat.availableLocales = new Set(); RelativeTimeFormat.__defaultLocale = ''; RelativeTimeFormat.relevantExtensionKeys = ['nu']; RelativeTimeFormat.polyfilled = true; return RelativeTimeFormat; }()); export default RelativeTimeFormat; try { // IE11 does not have Symbol if (typeof Symbol !== 'undefined') { Object.defineProperty(RelativeTimeFormat.prototype, Symbol.toStringTag, { value: 'Intl.RelativeTimeFormat', writable: false, enumerable: false, configurable: true, }); } // https://github.com/tc39/test262/blob/master/test/intl402/RelativeTimeFormat/constructor/length.js Object.defineProperty(RelativeTimeFormat.prototype.constructor, 'length', { value: 0, writable: false, enumerable: false, configurable: true, }); // https://github.com/tc39/test262/blob/master/test/intl402/RelativeTimeFormat/constructor/supportedLocalesOf/length.js Object.defineProperty(RelativeTimeFormat.supportedLocalesOf, 'length', { value: 1, writable: false, enumerable: false, configurable: true, }); } catch (e) { // Meta fix so we're test262-compliant, not important }