@formatjs/intl-relativetimeformat
Version:
Formats JavaScript dates to relative time strings.
43 lines (42 loc) • 1.87 kB
JavaScript
import { CanonicalizeLocaleList, CoerceOptionsToObject, createMemoizedNumberFormat, createMemoizedPluralRules, GetOption, invariant } from "@formatjs/ecma402-abstract";
import { ResolveLocale } from "@formatjs/intl-localematcher";
const NUMBERING_SYSTEM_REGEX = /^[a-z0-9]{3,8}(-[a-z0-9]{3,8})*$/i;
export function InitializeRelativeTimeFormat(rtf, locales, options, { getInternalSlots, availableLocales, relevantExtensionKeys, localeData, getDefaultLocale }) {
const internalSlots = getInternalSlots(rtf);
internalSlots.initializedRelativeTimeFormat = true;
const requestedLocales = CanonicalizeLocaleList(locales);
const opt = Object.create(null);
const opts = CoerceOptionsToObject(options);
const matcher = GetOption(opts, "localeMatcher", "string", ["best fit", "lookup"], "best fit");
opt.localeMatcher = matcher;
const numberingSystem = GetOption(
opts,
// @ts-expect-error TS option is wack
"numberingSystem",
"string",
undefined,
undefined
);
if (numberingSystem !== undefined) {
if (!NUMBERING_SYSTEM_REGEX.test(numberingSystem)) {
throw new RangeError(`Invalid numbering system ${numberingSystem}`);
}
}
opt.nu = numberingSystem;
const r = ResolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, localeData, getDefaultLocale);
const { locale, nu } = r;
internalSlots.locale = locale;
internalSlots.style = GetOption(opts, "style", "string", [
"long",
"narrow",
"short"
], "long");
internalSlots.numeric = GetOption(opts, "numeric", "string", ["always", "auto"], "always");
const fields = localeData[r.dataLocale];
invariant(!!fields, `Missing locale data for ${r.dataLocale}`);
internalSlots.fields = fields;
internalSlots.numberFormat = createMemoizedNumberFormat(locales);
internalSlots.pluralRules = createMemoizedPluralRules(locales);
internalSlots.numberingSystem = nu;
return rtf;
}