UNPKG

5.51 kBJavaScriptView Raw
1import { CanonicalizeLocaleList, SupportedLocales, ToString, } from '@formatjs/ecma402-abstract';
2import { InitializeRelativeTimeFormat } from './abstract/InitializeRelativeTimeFormat';
3import { PartitionRelativeTimePattern } from './abstract/PartitionRelativeTimePattern';
4import getInternalSlots from './get_internal_slots';
5var RelativeTimeFormat = /** @class */ (function () {
6 function RelativeTimeFormat(locales, options) {
7 // test262/test/intl402/RelativeTimeFormat/constructor/constructor/newtarget-undefined.js
8 // Cannot use `new.target` bc of IE11 & TS transpiles it to something else
9 var newTarget = this && this instanceof RelativeTimeFormat ? this.constructor : void 0;
10 if (!newTarget) {
11 throw new TypeError("Intl.RelativeTimeFormat must be called with 'new'");
12 }
13 return InitializeRelativeTimeFormat(this, locales, options, {
14 getInternalSlots: getInternalSlots,
15 availableLocales: RelativeTimeFormat.availableLocales,
16 relevantExtensionKeys: RelativeTimeFormat.relevantExtensionKeys,
17 localeData: RelativeTimeFormat.localeData,
18 getDefaultLocale: RelativeTimeFormat.getDefaultLocale,
19 });
20 }
21 RelativeTimeFormat.prototype.format = function (value, unit) {
22 if (typeof this !== 'object') {
23 throw new TypeError('format was called on a non-object');
24 }
25 var internalSlots = getInternalSlots(this);
26 if (!internalSlots.initializedRelativeTimeFormat) {
27 throw new TypeError('format was called on a invalid context');
28 }
29 return PartitionRelativeTimePattern(this, Number(value), ToString(unit), {
30 getInternalSlots: getInternalSlots,
31 })
32 .map(function (el) { return el.value; })
33 .join('');
34 };
35 RelativeTimeFormat.prototype.formatToParts = function (value, unit) {
36 if (typeof this !== 'object') {
37 throw new TypeError('formatToParts was called on a non-object');
38 }
39 var internalSlots = getInternalSlots(this);
40 if (!internalSlots.initializedRelativeTimeFormat) {
41 throw new TypeError('formatToParts was called on a invalid context');
42 }
43 return PartitionRelativeTimePattern(this, Number(value), ToString(unit), { getInternalSlots: getInternalSlots });
44 };
45 RelativeTimeFormat.prototype.resolvedOptions = function () {
46 if (typeof this !== 'object') {
47 throw new TypeError('resolvedOptions was called on a non-object');
48 }
49 var internalSlots = getInternalSlots(this);
50 if (!internalSlots.initializedRelativeTimeFormat) {
51 throw new TypeError('resolvedOptions was called on a invalid context');
52 }
53 // test262/test/intl402/RelativeTimeFormat/prototype/resolvedOptions/type.js
54 return {
55 locale: internalSlots.locale,
56 style: internalSlots.style,
57 numeric: internalSlots.numeric,
58 numberingSystem: internalSlots.numberingSystem,
59 };
60 };
61 RelativeTimeFormat.supportedLocalesOf = function (locales, options) {
62 return SupportedLocales(RelativeTimeFormat.availableLocales, CanonicalizeLocaleList(locales), options);
63 };
64 RelativeTimeFormat.__addLocaleData = function () {
65 var data = [];
66 for (var _i = 0; _i < arguments.length; _i++) {
67 data[_i] = arguments[_i];
68 }
69 for (var _a = 0, data_1 = data; _a < data_1.length; _a++) {
70 var _b = data_1[_a], d = _b.data, locale = _b.locale;
71 var minimizedLocale = new Intl.Locale(locale)
72 .minimize()
73 .toString();
74 RelativeTimeFormat.localeData[locale] = RelativeTimeFormat.localeData[minimizedLocale] = d;
75 RelativeTimeFormat.availableLocales.add(minimizedLocale);
76 RelativeTimeFormat.availableLocales.add(locale);
77 if (!RelativeTimeFormat.__defaultLocale) {
78 RelativeTimeFormat.__defaultLocale = minimizedLocale;
79 }
80 }
81 };
82 RelativeTimeFormat.getDefaultLocale = function () {
83 return RelativeTimeFormat.__defaultLocale;
84 };
85 RelativeTimeFormat.localeData = {};
86 RelativeTimeFormat.availableLocales = new Set();
87 RelativeTimeFormat.__defaultLocale = '';
88 RelativeTimeFormat.relevantExtensionKeys = ['nu'];
89 RelativeTimeFormat.polyfilled = true;
90 return RelativeTimeFormat;
91}());
92export default RelativeTimeFormat;
93try {
94 // IE11 does not have Symbol
95 if (typeof Symbol !== 'undefined') {
96 Object.defineProperty(RelativeTimeFormat.prototype, Symbol.toStringTag, {
97 value: 'Intl.RelativeTimeFormat',
98 writable: false,
99 enumerable: false,
100 configurable: true,
101 });
102 }
103 // https://github.com/tc39/test262/blob/master/test/intl402/RelativeTimeFormat/constructor/length.js
104 Object.defineProperty(RelativeTimeFormat.prototype.constructor, 'length', {
105 value: 0,
106 writable: false,
107 enumerable: false,
108 configurable: true,
109 });
110 // https://github.com/tc39/test262/blob/master/test/intl402/RelativeTimeFormat/constructor/supportedLocalesOf/length.js
111 Object.defineProperty(RelativeTimeFormat.supportedLocalesOf, 'length', {
112 value: 1,
113 writable: false,
114 enumerable: false,
115 configurable: true,
116 });
117}
118catch (e) {
119 // Meta fix so we're test262-compliant, not important
120}