@formatjs/intl-relativetimeformat 12.1.1 → 12.2.0
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.
- package/abstract/FormatRelativeTime.d.ts +2 -2
- package/abstract/FormatRelativeTime.js +8 -8
- package/abstract/FormatRelativeTimeToParts.d.ts +2 -2
- package/abstract/FormatRelativeTimeToParts.js +3 -2
- package/abstract/InitializeRelativeTimeFormat.d.ts +7 -7
- package/abstract/InitializeRelativeTimeFormat.js +41 -33
- package/abstract/MakePartsList.js +21 -24
- package/abstract/PartitionRelativeTimePattern.d.ts +3 -3
- package/abstract/PartitionRelativeTimePattern.js +42 -52
- package/abstract/SingularRelativeTimeUnit.d.ts +4 -4
- package/abstract/SingularRelativeTimeUnit.js +17 -32
- package/get_internal_slots.d.ts +1 -1
- package/get_internal_slots.js +8 -7
- package/index.d.ts +13 -13
- package/index.js +103 -119
- package/package.json +7 -7
- package/polyfill-force.js +6 -6
- package/polyfill.iife.js +2302 -3062
- package/polyfill.js +8 -8
- package/should-polyfill.js +16 -24
- package/supported-locales.generated.js +572 -572
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RelativeTimeFormatInternal } from
|
|
1
|
+
import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export declare function FormatRelativeTime(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, implDetails: {
|
|
3
|
-
|
|
3
|
+
getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
|
|
4
4
|
}): string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "@formatjs/ecma402-abstract";
|
|
2
|
+
import { PartitionRelativeTimePattern } from "./PartitionRelativeTimePattern.js";
|
|
2
3
|
export function FormatRelativeTime(rtf, value, unit, implDetails) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
1
|
+
import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export declare function FormatRelativeTimeToParts(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, implDetails: {
|
|
3
|
-
|
|
3
|
+
getInternalSlots(rtf: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
|
|
4
4
|
}): Intl.RelativeTimeFormatPart[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "@formatjs/ecma402-abstract";
|
|
2
|
+
import { PartitionRelativeTimePattern } from "./PartitionRelativeTimePattern.js";
|
|
2
3
|
export function FormatRelativeTimeToParts(rtf, value, unit, implDetails) {
|
|
3
|
-
|
|
4
|
+
return PartitionRelativeTimePattern(rtf, value, unit, implDetails);
|
|
4
5
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { LocaleFieldsData, RelativeTimeFormatInternal } from
|
|
2
|
-
export declare function InitializeRelativeTimeFormat(rtf: Intl.RelativeTimeFormat, locales: string | string[] | undefined, options: Intl.RelativeTimeFormatOptions | undefined, { getInternalSlots, availableLocales, relevantExtensionKeys, localeData, getDefaultLocale
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
2
|
-
import { ResolveLocale } from
|
|
3
|
-
|
|
4
|
-
export function InitializeRelativeTimeFormat(rtf, locales, options,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
1
|
+
import { invariant, PartitionPattern } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export function MakePartsList(pattern, unit, parts) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
2
|
-
export declare function PartitionRelativeTimePattern(rtf: Intl.RelativeTimeFormat, value: number, unit: Intl.RelativeTimeFormatUnit, { getInternalSlots
|
|
3
|
-
|
|
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
|
|
2
|
-
import { SingularRelativeTimeUnit } from
|
|
3
|
-
import { MakePartsList } from
|
|
4
|
-
export function PartitionRelativeTimePattern(rtf, value, unit,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
1
|
+
import { type RelativeTimeFormatSingularUnit } from "@formatjs/ecma402-abstract";
|
|
2
2
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
1
|
+
import { invariant, Type } from "@formatjs/ecma402-abstract";
|
|
2
2
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
* https://tc39.es/proposal-intl-relative-time/#sec-singularrelativetimeunit
|
|
4
|
+
* @param unit
|
|
5
|
+
*/
|
|
6
6
|
export function SingularRelativeTimeUnit(unit) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
}
|
package/get_internal_slots.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { RelativeTimeFormatInternal } from
|
|
1
|
+
import { type RelativeTimeFormatInternal } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export default function getInternalSlots(x: Intl.RelativeTimeFormat): RelativeTimeFormatInternal;
|
package/get_internal_slots.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// Type-only circular import
|
|
2
2
|
// eslint-disable-next-line import/no-cycle
|
|
3
|
-
|
|
3
|
+
import "@formatjs/ecma402-abstract";
|
|
4
|
+
const internalSlotMap = new WeakMap();
|
|
4
5
|
export default function getInternalSlots(x) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
1
|
+
import { type LocaleFieldsData, type RelativeTimeLocaleData } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export default class RelativeTimeFormat {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
}
|