@formatjs/intl-relativetimeformat 12.1.0 → 12.1.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,4 +1,4 @@
1
- import { RelativeTimeFormatInternal } from '@formatjs/ecma402-abstract';
1
+ import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
2
  export declare function FormatRelativeTime(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, implDetails: {
3
- getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
3
+ getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
4
4
  }): string;
@@ -1,10 +1,10 @@
1
- import { PartitionRelativeTimePattern } from './PartitionRelativeTimePattern.js';
1
+ import "@formatjs/ecma402-abstract";
2
+ import { PartitionRelativeTimePattern } from "./PartitionRelativeTimePattern.js";
2
3
  export function FormatRelativeTime(rtf, value, unit, implDetails) {
3
- var parts = PartitionRelativeTimePattern(rtf, value, unit, implDetails);
4
- var result = '';
5
- for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
6
- var part = parts_1[_i];
7
- result += part.value;
8
- }
9
- return result;
4
+ const parts = PartitionRelativeTimePattern(rtf, value, unit, implDetails);
5
+ let result = "";
6
+ for (const part of parts) {
7
+ result += part.value;
8
+ }
9
+ return result;
10
10
  }
@@ -1,4 +1,4 @@
1
- import { RelativeTimeFormatInternal } from '@formatjs/ecma402-abstract';
1
+ import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
2
  export declare function FormatRelativeTimeToParts(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, implDetails: {
3
- getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
3
+ getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
4
4
  }): Intl.RelativeTimeFormatPart[];
@@ -1,4 +1,5 @@
1
- import { PartitionRelativeTimePattern } from './PartitionRelativeTimePattern.js';
1
+ import "@formatjs/ecma402-abstract";
2
+ import { PartitionRelativeTimePattern } from "./PartitionRelativeTimePattern.js";
2
3
  export function FormatRelativeTimeToParts(rtf, value, unit, implDetails) {
3
- return PartitionRelativeTimePattern(rtf, value, unit, implDetails);
4
+ return PartitionRelativeTimePattern(rtf, value, unit, implDetails);
4
5
  }
@@ -1,8 +1,8 @@
1
- import { LocaleFieldsData, RelativeTimeFormatInternal } from '@formatjs/ecma402-abstract';
2
- export declare function InitializeRelativeTimeFormat(rtf: Intl.RelativeTimeFormat, locales: string | string[] | undefined, options: Intl.RelativeTimeFormatOptions | undefined, { getInternalSlots, availableLocales, relevantExtensionKeys, localeData, getDefaultLocale, }: {
3
- getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
4
- availableLocales: Set<string>;
5
- relevantExtensionKeys: string[];
6
- localeData: Record<string, LocaleFieldsData | undefined>;
7
- getDefaultLocale(): string;
1
+ import { type LocaleFieldsData, type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
+ export declare function InitializeRelativeTimeFormat(rtf: Intl.RelativeTimeFormat, locales: string | string[] | undefined, options: Intl.RelativeTimeFormatOptions | undefined, { getInternalSlots, availableLocales, relevantExtensionKeys, localeData, getDefaultLocale }: {
3
+ getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
4
+ availableLocales: Set<string>;
5
+ relevantExtensionKeys: string[];
6
+ localeData: Record<string, LocaleFieldsData | undefined>;
7
+ getDefaultLocale(): string;
8
8
  }): Intl.RelativeTimeFormat;
@@ -1,34 +1,42 @@
1
- import { CanonicalizeLocaleList, CoerceOptionsToObject, createMemoizedNumberFormat, createMemoizedPluralRules, GetOption, invariant, } from '@formatjs/ecma402-abstract';
2
- import { ResolveLocale } from '@formatjs/intl-localematcher';
3
- var NUMBERING_SYSTEM_REGEX = /^[a-z0-9]{3,8}(-[a-z0-9]{3,8})*$/i;
4
- export 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 = createMemoizedNumberFormat(locales);
31
- internalSlots.pluralRules = createMemoizedPluralRules(locales);
32
- internalSlots.numberingSystem = nu;
33
- return rtf;
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;
34
42
  }
@@ -1,26 +1,23 @@
1
- import { invariant, PartitionPattern } from '@formatjs/ecma402-abstract';
1
+ import { invariant, PartitionPattern } from "@formatjs/ecma402-abstract";
2
2
  export function MakePartsList(pattern, unit, parts) {
3
- var patternParts = PartitionPattern(pattern);
4
- var result = [];
5
- for (var _i = 0, patternParts_1 = patternParts; _i < patternParts_1.length; _i++) {
6
- var patternPart = patternParts_1[_i];
7
- if (patternPart.type === 'literal') {
8
- result.push({
9
- type: 'literal',
10
- value: patternPart.value,
11
- });
12
- }
13
- else {
14
- invariant(patternPart.type === '0', "Malformed pattern ".concat(pattern));
15
- for (var _a = 0, parts_1 = parts; _a < parts_1.length; _a++) {
16
- var part = parts_1[_a];
17
- result.push({
18
- type: part.type,
19
- value: part.value,
20
- unit: unit,
21
- });
22
- }
23
- }
24
- }
25
- return result;
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;
26
23
  }
@@ -1,4 +1,4 @@
1
- import { 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;
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
4
  }): Intl.RelativeTimeFormatPart[];
@@ -1,53 +1,43 @@
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, _a) {
5
- var getInternalSlots = _a.getInternalSlots;
6
- invariant(Type(value) === 'Number', "value must be number, instead got ".concat(typeof value), TypeError);
7
- invariant(Type(unit) === 'String', "unit must be number, instead got ".concat(typeof value), TypeError);
8
- if (isNaN(value) || !isFinite(value)) {
9
- throw new RangeError("Invalid value ".concat(value));
10
- }
11
- var resolvedUnit = SingularRelativeTimeUnit(unit);
12
- var _b = getInternalSlots(rtf), fields = _b.fields, style = _b.style, numeric = _b.numeric, pluralRules = _b.pluralRules, numberFormat = _b.numberFormat;
13
- var entry = resolvedUnit;
14
- if (style === 'short') {
15
- entry = "".concat(resolvedUnit, "-short");
16
- }
17
- else if (style === 'narrow') {
18
- entry = "".concat(resolvedUnit, "-narrow");
19
- }
20
- if (!(entry in fields)) {
21
- entry = resolvedUnit;
22
- }
23
- var patterns = fields[entry];
24
- if (numeric === 'auto') {
25
- if (ToString(value) in patterns) {
26
- return [
27
- {
28
- type: 'literal',
29
- value: patterns[ToString(value)],
30
- },
31
- ];
32
- }
33
- }
34
- var tl = 'future';
35
- if (SameValue(value, -0) || value < 0) {
36
- tl = 'past';
37
- }
38
- var po = patterns[tl];
39
- var fv = typeof numberFormat.formatToParts === 'function'
40
- ? numberFormat.formatToParts(Math.abs(value))
41
- : // TODO: If formatToParts is not supported, we assume the whole formatted
42
- // number is a part
43
- [
44
- {
45
- type: 'literal',
46
- value: numberFormat.format(Math.abs(value)),
47
- unit: unit,
48
- },
49
- ];
50
- var pr = pluralRules.select(value);
51
- var pattern = po[pr];
52
- return MakePartsList(pattern, resolvedUnit, fv);
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);
53
43
  }
@@ -1,6 +1,6 @@
1
- import { RelativeTimeFormatSingularUnit } from '@formatjs/ecma402-abstract';
1
+ import { type RelativeTimeFormatSingularUnit } from "@formatjs/ecma402-abstract";
2
2
  /**
3
- * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
- * @param unit
5
- */
3
+ * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
+ * @param unit
5
+ */
6
6
  export declare function SingularRelativeTimeUnit(unit: Intl.RelativeTimeFormatUnit): RelativeTimeFormatSingularUnit;
@@ -1,35 +1,20 @@
1
- import { invariant, Type, } from '@formatjs/ecma402-abstract';
1
+ import { invariant, Type } from "@formatjs/ecma402-abstract";
2
2
  /**
3
- * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
- * @param unit
5
- */
3
+ * https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
4
+ * @param unit
5
+ */
6
6
  export function SingularRelativeTimeUnit(unit) {
7
- invariant(Type(unit) === 'String', 'unit must be a string');
8
- if (unit === 'seconds')
9
- return 'second';
10
- if (unit === 'minutes')
11
- return 'minute';
12
- if (unit === 'hours')
13
- return 'hour';
14
- if (unit === 'days')
15
- return 'day';
16
- if (unit === 'weeks')
17
- return 'week';
18
- if (unit === 'months')
19
- return 'month';
20
- if (unit === 'quarters')
21
- return 'quarter';
22
- if (unit === 'years')
23
- return 'year';
24
- if (unit !== 'second' &&
25
- unit !== 'minute' &&
26
- unit !== 'hour' &&
27
- unit !== 'day' &&
28
- unit !== 'week' &&
29
- unit !== 'month' &&
30
- unit !== 'quarter' &&
31
- unit !== 'year') {
32
- throw new RangeError('invalid unit');
33
- }
34
- return 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;
35
20
  }
@@ -1,2 +1,4 @@
1
- import { RelativeTimeFormatInternal } from '@formatjs/ecma402-abstract';
1
+ // Type-only circular import
2
+ // eslint-disable-next-line import/no-cycle
3
+ import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
2
4
  export default function getInternalSlots(x: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
@@ -1,11 +1,12 @@
1
1
  // Type-only circular import
2
2
  // eslint-disable-next-line import/no-cycle
3
- var internalSlotMap = new WeakMap();
3
+ import "@formatjs/ecma402-abstract";
4
+ const internalSlotMap = new WeakMap();
4
5
  export default function getInternalSlots(x) {
5
- var internalSlots = internalSlotMap.get(x);
6
- if (!internalSlots) {
7
- internalSlots = Object.create(null);
8
- internalSlotMap.set(x, internalSlots);
9
- }
10
- return internalSlots;
6
+ let internalSlots = internalSlotMap.get(x);
7
+ if (!internalSlots) {
8
+ internalSlots = Object.create(null);
9
+ internalSlotMap.set(x, internalSlots);
10
+ }
11
+ return internalSlots;
11
12
  }
package/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- import { LocaleFieldsData, RelativeTimeLocaleData } from '@formatjs/ecma402-abstract';
1
+ import { type LocaleFieldsData, type RelativeTimeLocaleData } from "@formatjs/ecma402-abstract";
2
2
  export default class RelativeTimeFormat {
3
- constructor(locales?: string | string[], options?: Intl.RelativeTimeFormatOptions);
4
- format(value: number, unit: Intl.RelativeTimeFormatUnit): string;
5
- formatToParts(value: number, unit: Intl.RelativeTimeFormatUnit): Intl.RelativeTimeFormatPart[];
6
- resolvedOptions(): Intl.ResolvedRelativeTimeFormatOptions;
7
- static supportedLocalesOf(locales: string | string[], options?: Pick<Intl.RelativeTimeFormatOptions, 'localeMatcher'>): string[];
8
- static __addLocaleData(...data: RelativeTimeLocaleData[]): void;
9
- static localeData: Record<string, LocaleFieldsData>;
10
- private static availableLocales;
11
- private static __defaultLocale;
12
- private static getDefaultLocale;
13
- private static relevantExtensionKeys;
14
- static polyfilled: boolean;
3
+ constructor(locales?: string | string[], options?: Intl.RelativeTimeFormatOptions);
4
+ format(value: number, unit: Intl.RelativeTimeFormatUnit): string;
5
+ formatToParts(value: number, unit: Intl.RelativeTimeFormatUnit): Intl.RelativeTimeFormatPart[];
6
+ resolvedOptions(): Intl.ResolvedRelativeTimeFormatOptions;
7
+ static supportedLocalesOf(locales: string | string[], options?: Pick<Intl.RelativeTimeFormatOptions, "localeMatcher">): string[];
8
+ static __addLocaleData(...data: RelativeTimeLocaleData[]): void;
9
+ static localeData: Record<string, LocaleFieldsData>;
10
+ private static availableLocales;
11
+ private static __defaultLocale;
12
+ private static getDefaultLocale;
13
+ private static relevantExtensionKeys;
14
+ static polyfilled: boolean;
15
15
  }