UNPKG

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