UNPKG

2 kBJavaScriptView Raw
1import { CanonicalizeLocaleList, CoerceOptionsToObject, GetOption, invariant, } from '@formatjs/ecma402-abstract';
2import { ResolveLocale } from '@formatjs/intl-localematcher';
3var NUMBERING_SYSTEM_REGEX = /^[a-z0-9]{3,8}(-[a-z0-9]{3,8})*$/i;
4export function InitializeRelativeTimeFormat(rtf, locales, options, _a) {
5 var getInternalSlots = _a.getInternalSlots, availableLocales = _a.availableLocales, relevantExtensionKeys = _a.relevantExtensionKeys, localeData = _a.localeData, getDefaultLocale = _a.getDefaultLocale;
6 var internalSlots = getInternalSlots(rtf);
7 internalSlots.initializedRelativeTimeFormat = true;
8 var requestedLocales = CanonicalizeLocaleList(locales);
9 var opt = Object.create(null);
10 var opts = CoerceOptionsToObject(options);
11 var matcher = GetOption(opts, 'localeMatcher', 'string', ['best fit', 'lookup'], 'best fit');
12 opt.localeMatcher = matcher;
13 var numberingSystem = GetOption(opts,
14 // @ts-expect-error TS option is wack
15 'numberingSystem', 'string', undefined, undefined);
16 if (numberingSystem !== undefined) {
17 if (!NUMBERING_SYSTEM_REGEX.test(numberingSystem)) {
18 throw new RangeError("Invalid numbering system ".concat(numberingSystem));
19 }
20 }
21 opt.nu = numberingSystem;
22 var r = ResolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, localeData, getDefaultLocale);
23 var locale = r.locale, nu = r.nu;
24 internalSlots.locale = locale;
25 internalSlots.style = GetOption(opts, 'style', 'string', ['long', 'narrow', 'short'], 'long');
26 internalSlots.numeric = GetOption(opts, 'numeric', 'string', ['always', 'auto'], 'always');
27 var fields = localeData[r.dataLocale];
28 invariant(!!fields, "Missing locale data for ".concat(r.dataLocale));
29 internalSlots.fields = fields;
30 internalSlots.numberFormat = new Intl.NumberFormat(locales);
31 internalSlots.pluralRules = new Intl.PluralRules(locales);
32 internalSlots.numberingSystem = nu;
33 return rtf;
34}