@formatjs/intl 3.0.4 → 3.1.1
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/lib/src/dateTime.d.ts +2 -1
- package/lib/src/dateTime.js +5 -5
- package/lib/src/types.d.ts +3 -1
- package/package.json +5 -5
- package/src/dateTime.d.ts +2 -1
- package/src/dateTime.js +5 -5
- package/src/types.d.ts +3 -1
package/lib/src/dateTime.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
|
4
4
|
timeZone?: string;
|
|
5
5
|
formats: CustomFormats;
|
|
6
6
|
onError: OnErrorFn;
|
|
7
|
-
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
7
|
+
}, type: 'date' | 'time' | 'dateTimeRange', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
8
8
|
export declare function formatDate(config: {
|
|
9
9
|
locale: string;
|
|
10
10
|
timeZone?: string;
|
|
@@ -20,6 +20,7 @@ export declare function formatTime(config: {
|
|
|
20
20
|
export declare function formatDateTimeRange(config: {
|
|
21
21
|
locale: string;
|
|
22
22
|
timeZone?: string;
|
|
23
|
+
formats: CustomFormats;
|
|
23
24
|
onError: OnErrorFn;
|
|
24
25
|
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[from, to, options]: Parameters<IntlFormatters['formatDateTimeRange']>): string;
|
|
25
26
|
export declare function formatDateToParts(config: {
|
package/lib/src/dateTime.js
CHANGED
|
@@ -75,15 +75,15 @@ export function formatDateTimeRange(config, getDateTimeFormat) {
|
|
|
75
75
|
_a[_i - 2] = arguments[_i];
|
|
76
76
|
}
|
|
77
77
|
var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b;
|
|
78
|
-
var
|
|
79
|
-
var
|
|
78
|
+
var fromDate = typeof from === 'string' ? new Date(from || 0) : from;
|
|
79
|
+
var toDate = typeof to === 'string' ? new Date(to || 0) : to;
|
|
80
80
|
try {
|
|
81
|
-
return
|
|
81
|
+
return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
82
82
|
}
|
|
83
83
|
catch (e) {
|
|
84
|
-
onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
84
|
+
config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
85
85
|
}
|
|
86
|
-
return String(
|
|
86
|
+
return String(fromDate);
|
|
87
87
|
}
|
|
88
88
|
export function formatDateToParts(config, getDateTimeFormat) {
|
|
89
89
|
var _a = [];
|
package/lib/src/types.d.ts
CHANGED
|
@@ -44,10 +44,12 @@ export interface ResolvedIntlConfig<T = string> {
|
|
|
44
44
|
}
|
|
45
45
|
export interface CustomFormats extends Partial<Formats> {
|
|
46
46
|
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
|
|
47
|
+
dateTimeRange?: Record<string, Intl.DateTimeFormatOptions>;
|
|
47
48
|
}
|
|
48
49
|
export interface CustomFormatConfig<Source = string> {
|
|
49
50
|
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
50
51
|
}
|
|
52
|
+
export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'dateTimeRange'>;
|
|
51
53
|
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
52
54
|
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
53
55
|
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
@@ -59,7 +61,7 @@ export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMat
|
|
|
59
61
|
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
60
62
|
*/
|
|
61
63
|
export interface IntlFormatters<TBase = unknown> {
|
|
62
|
-
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0], to: Parameters<Intl.DateTimeFormat['formatRange']>[1], opts?:
|
|
64
|
+
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0] | string, to: Parameters<Intl.DateTimeFormat['formatRange']>[1] | string, opts?: FormatDateTimeRangeOptions): string;
|
|
63
65
|
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
64
66
|
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
65
67
|
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"intl",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"tslib": "2",
|
|
33
|
-
"@formatjs/
|
|
34
|
-
"@formatjs/
|
|
35
|
-
"intl-messageformat": "10.7.
|
|
36
|
-
"@formatjs/ecma402-abstract": "2.3.
|
|
33
|
+
"@formatjs/fast-memoize": "2.2.6",
|
|
34
|
+
"@formatjs/icu-messageformat-parser": "2.10.0",
|
|
35
|
+
"intl-messageformat": "10.7.12",
|
|
36
|
+
"@formatjs/ecma402-abstract": "2.3.2"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"typescript": "5"
|
package/src/dateTime.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare function getFormatter({ locale, formats, onError, timeZone, }: {
|
|
|
4
4
|
timeZone?: string;
|
|
5
5
|
formats: CustomFormats;
|
|
6
6
|
onError: OnErrorFn;
|
|
7
|
-
}, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
7
|
+
}, type: 'date' | 'time' | 'dateTimeRange', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
|
|
8
8
|
export declare function formatDate(config: {
|
|
9
9
|
locale: string;
|
|
10
10
|
timeZone?: string;
|
|
@@ -20,6 +20,7 @@ export declare function formatTime(config: {
|
|
|
20
20
|
export declare function formatDateTimeRange(config: {
|
|
21
21
|
locale: string;
|
|
22
22
|
timeZone?: string;
|
|
23
|
+
formats: CustomFormats;
|
|
23
24
|
onError: OnErrorFn;
|
|
24
25
|
}, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[from, to, options]: Parameters<IntlFormatters['formatDateTimeRange']>): string;
|
|
25
26
|
export declare function formatDateToParts(config: {
|
package/src/dateTime.js
CHANGED
|
@@ -83,15 +83,15 @@ function formatDateTimeRange(config, getDateTimeFormat) {
|
|
|
83
83
|
_a[_i - 2] = arguments[_i];
|
|
84
84
|
}
|
|
85
85
|
var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b;
|
|
86
|
-
var
|
|
87
|
-
var
|
|
86
|
+
var fromDate = typeof from === 'string' ? new Date(from || 0) : from;
|
|
87
|
+
var toDate = typeof to === 'string' ? new Date(to || 0) : to;
|
|
88
88
|
try {
|
|
89
|
-
return
|
|
89
|
+
return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
|
|
90
90
|
}
|
|
91
91
|
catch (e) {
|
|
92
|
-
onError(new error_1.IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
92
|
+
config.onError(new error_1.IntlFormatError('Error formatting date time range.', config.locale, e));
|
|
93
93
|
}
|
|
94
|
-
return String(
|
|
94
|
+
return String(fromDate);
|
|
95
95
|
}
|
|
96
96
|
function formatDateToParts(config, getDateTimeFormat) {
|
|
97
97
|
var _a = [];
|
package/src/types.d.ts
CHANGED
|
@@ -44,10 +44,12 @@ export interface ResolvedIntlConfig<T = string> {
|
|
|
44
44
|
}
|
|
45
45
|
export interface CustomFormats extends Partial<Formats> {
|
|
46
46
|
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
|
|
47
|
+
dateTimeRange?: Record<string, Intl.DateTimeFormatOptions>;
|
|
47
48
|
}
|
|
48
49
|
export interface CustomFormatConfig<Source = string> {
|
|
49
50
|
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
50
51
|
}
|
|
52
|
+
export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'dateTimeRange'>;
|
|
51
53
|
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
52
54
|
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
53
55
|
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
@@ -59,7 +61,7 @@ export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMat
|
|
|
59
61
|
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
60
62
|
*/
|
|
61
63
|
export interface IntlFormatters<TBase = unknown> {
|
|
62
|
-
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0], to: Parameters<Intl.DateTimeFormat['formatRange']>[1], opts?:
|
|
64
|
+
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0] | string, to: Parameters<Intl.DateTimeFormat['formatRange']>[1] | string, opts?: FormatDateTimeRangeOptions): string;
|
|
63
65
|
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
64
66
|
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
65
67
|
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|