@formatjs/intl-relativetimeformat 12.3.1 → 12.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,42 +0,0 @@
1
- import { CanonicalizeLocaleList, CoerceOptionsToObject, createMemoizedNumberFormat, createMemoizedPluralRules, GetOption, invariant } from "@formatjs/ecma402-abstract";
2
- import { ResolveLocale } from "@formatjs/intl-localematcher";
3
- const NUMBERING_SYSTEM_REGEX = /^[a-z0-9]{3,8}(-[a-z0-9]{3,8})*$/i;
4
- export function InitializeRelativeTimeFormat(rtf, locales, options, { getInternalSlots, availableLocales, relevantExtensionKeys, localeData, getDefaultLocale }) {
5
- const internalSlots = getInternalSlots(rtf);
6
- internalSlots.initializedRelativeTimeFormat = true;
7
- const requestedLocales = CanonicalizeLocaleList(locales);
8
- const opt = Object.create(null);
9
- const opts = CoerceOptionsToObject(options);
10
- const matcher = GetOption(opts, "localeMatcher", "string", ["best fit", "lookup"], "best fit");
11
- opt.localeMatcher = matcher;
12
- const numberingSystem = GetOption(
13
- opts,
14
- // @ts-expect-error TS option is wack
15
- "numberingSystem",
16
- "string",
17
- undefined,
18
- undefined
19
- );
20
- if (numberingSystem !== undefined) {
21
- if (!NUMBERING_SYSTEM_REGEX.test(numberingSystem)) {
22
- throw new RangeError(`Invalid numbering system ${numberingSystem}`);
23
- }
24
- }
25
- opt.nu = numberingSystem;
26
- const r = ResolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, localeData, getDefaultLocale);
27
- const { locale, nu } = r;
28
- internalSlots.locale = locale;
29
- internalSlots.style = GetOption(opts, "style", "string", [
30
- "long",
31
- "narrow",
32
- "short"
33
- ], "long");
34
- internalSlots.numeric = GetOption(opts, "numeric", "string", ["always", "auto"], "always");
35
- const fields = localeData[r.dataLocale];
36
- invariant(!!fields, `Missing locale data for ${r.dataLocale}`);
37
- internalSlots.fields = fields;
38
- internalSlots.numberFormat = createMemoizedNumberFormat(locales);
39
- internalSlots.pluralRules = createMemoizedPluralRules(locales);
40
- internalSlots.numberingSystem = nu;
41
- return rtf;
42
- }
@@ -1 +0,0 @@
1
- export declare function MakePartsList(pattern: string, unit: Intl.RelativeTimeFormatUnitSingular, parts: Intl.NumberFormatPart[] | Intl.RelativeTimeFormatPart[]): Intl.RelativeTimeFormatPart[];
@@ -1,23 +0,0 @@
1
- import { invariant, PartitionPattern } from "@formatjs/ecma402-abstract";
2
- export function MakePartsList(pattern, unit, parts) {
3
- const patternParts = PartitionPattern(pattern);
4
- const result = [];
5
- for (const patternPart of patternParts) {
6
- if (patternPart.type === "literal") {
7
- result.push({
8
- type: "literal",
9
- value: patternPart.value
10
- });
11
- } else {
12
- invariant(patternPart.type === "0", `Malformed pattern ${pattern}`);
13
- for (const part of parts) {
14
- result.push({
15
- type: part.type,
16
- value: part.value,
17
- unit
18
- });
19
- }
20
- }
21
- }
22
- return result;
23
- }
@@ -1,4 +0,0 @@
1
- import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
- export declare function PartitionRelativeTimePattern(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, { getInternalSlots }: {
3
- getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
4
- }): Intl.RelativeTimeFormatPart[];
@@ -1,43 +0,0 @@
1
- import { invariant, SameValue, ToString, Type } from "@formatjs/ecma402-abstract";
2
- import { SingularRelativeTimeUnit } from "./SingularRelativeTimeUnit.js";
3
- import { MakePartsList } from "./MakePartsList.js";
4
- export function PartitionRelativeTimePattern(rtf, value, unit, { getInternalSlots }) {
5
- invariant(Type(value) === "Number", `value must be number, instead got ${typeof value}`, TypeError);
6
- invariant(Type(unit) === "String", `unit must be number, instead got ${typeof value}`, TypeError);
7
- if (isNaN(value) || !isFinite(value)) {
8
- throw new RangeError(`Invalid value ${value}`);
9
- }
10
- const resolvedUnit = SingularRelativeTimeUnit(unit);
11
- const { fields, style, numeric, pluralRules, numberFormat } = getInternalSlots(rtf);
12
- let entry = resolvedUnit;
13
- if (style === "short") {
14
- entry = `${resolvedUnit}-short`;
15
- } else if (style === "narrow") {
16
- entry = `${resolvedUnit}-narrow`;
17
- }
18
- if (!(entry in fields)) {
19
- entry = resolvedUnit;
20
- }
21
- const patterns = fields[entry];
22
- if (numeric === "auto") {
23
- if (ToString(value) in patterns) {
24
- return [{
25
- type: "literal",
26
- value: patterns[ToString(value)]
27
- }];
28
- }
29
- }
30
- let tl = "future";
31
- if (SameValue(value, -0) || value < 0) {
32
- tl = "past";
33
- }
34
- const po = patterns[tl];
35
- const fv = typeof numberFormat.formatToParts === "function" ? numberFormat.formatToParts(Math.abs(value)) : [{
36
- type: "literal",
37
- value: numberFormat.format(Math.abs(value)),
38
- unit
39
- }];
40
- const pr = pluralRules.select(value);
41
- const pattern = po[pr];
42
- return MakePartsList(pattern, resolvedUnit, fv);
43
- }
@@ -1,6 +0,0 @@
1
- import { type RelativeTimeFormatSingularUnit } from "@formatjs/ecma402-abstract";
2
- /**
3
- * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
- * @param unit
5
- */
6
- export declare function SingularRelativeTimeUnit(unit: Intl.RelativeTimeFormatUnit): RelativeTimeFormatSingularUnit;
@@ -1,20 +0,0 @@
1
- import { invariant, Type } from "@formatjs/ecma402-abstract";
2
- /**
3
- * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
- * @param unit
5
- */
6
- export function SingularRelativeTimeUnit(unit) {
7
- invariant(Type(unit) === "String", "unit must be a string");
8
- if (unit === "seconds") return "second";
9
- if (unit === "minutes") return "minute";
10
- if (unit === "hours") return "hour";
11
- if (unit === "days") return "day";
12
- if (unit === "weeks") return "week";
13
- if (unit === "months") return "month";
14
- if (unit === "quarters") return "quarter";
15
- if (unit === "years") return "year";
16
- if (unit !== "second" && unit !== "minute" && unit !== "hour" && unit !== "day" && unit !== "week" && unit !== "month" && unit !== "quarter" && unit !== "year") {
17
- throw new RangeError("invalid unit");
18
- }
19
- return unit;
20
- }
@@ -1,2 +0,0 @@
1
- import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
- export default function getInternalSlots(x: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
@@ -1,12 +0,0 @@
1
- // Type-only circular import
2
- // eslint-disable-next-line import/no-cycle
3
- import "@formatjs/ecma402-abstract";
4
- const internalSlotMap = new WeakMap();
5
- export default function getInternalSlots(x) {
6
- let internalSlots = internalSlotMap.get(x);
7
- if (!internalSlots) {
8
- internalSlots = Object.create(null);
9
- internalSlotMap.set(x, internalSlots);
10
- }
11
- return internalSlots;
12
- }
@@ -1 +0,0 @@
1
- export declare const supportedLocales: string[];