@formatjs/intl 4.0.8 → 4.0.9

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/index.d.ts CHANGED
@@ -1,16 +1,20 @@
1
- import { MessageDescriptor } from './src/types.js';
2
- export * from './src/types.js';
3
- export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
1
+ import { type MessageDescriptor } from "./src/types.js";
2
+ export * from "./src/types.js";
3
+ export declare function defineMessages<
4
+ K extends keyof any,
5
+ T = MessageDescriptor,
6
+ U extends Record<K, T> = Record<K, T>
7
+ >(msgs: U): U;
4
8
  export declare function defineMessage<T>(msg: T): T;
5
- export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils.js';
6
- export * from './src/error.js';
7
- export { formatMessage } from './src/message.js';
8
- export type { FormatMessageFn } from './src/message.js';
9
- export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime.js';
10
- export { formatDisplayName } from './src/displayName.js';
11
- export { formatList } from './src/list.js';
12
- export { formatPlural } from './src/plural.js';
13
- export { formatRelativeTime } from './src/relativeTime.js';
14
- export { formatNumber, formatNumberToParts } from './src/number.js';
15
- export { createIntl } from './src/create-intl.js';
16
- export type { CreateIntlFn } from './src/create-intl.js';
9
+ export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat } from "./src/utils.js";
10
+ export * from "./src/error.js";
11
+ export { formatMessage } from "./src/message.js";
12
+ export type { FormatMessageFn } from "./src/message.js";
13
+ export { formatDate, formatDateToParts, formatTime, formatTimeToParts } from "./src/dateTime.js";
14
+ export { formatDisplayName } from "./src/displayName.js";
15
+ export { formatList } from "./src/list.js";
16
+ export { formatPlural } from "./src/plural.js";
17
+ export { formatRelativeTime } from "./src/relativeTime.js";
18
+ export { formatNumber, formatNumberToParts } from "./src/number.js";
19
+ export { createIntl } from "./src/create-intl.js";
20
+ export type { CreateIntlFn } from "./src/create-intl.js";
package/index.js CHANGED
@@ -1,17 +1,18 @@
1
- export * from './src/types.js';
1
+ import "./src/types.js";
2
+ export * from "./src/types.js";
2
3
  export function defineMessages(msgs) {
3
- return msgs;
4
+ return msgs;
4
5
  }
5
6
  export function defineMessage(msg) {
6
- return msg;
7
+ return msg;
7
8
  }
8
- export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils.js';
9
- export * from './src/error.js';
10
- export { formatMessage } from './src/message.js';
11
- export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime.js';
12
- export { formatDisplayName } from './src/displayName.js';
13
- export { formatList } from './src/list.js';
14
- export { formatPlural } from './src/plural.js';
15
- export { formatRelativeTime } from './src/relativeTime.js';
16
- export { formatNumber, formatNumberToParts } from './src/number.js';
17
- export { createIntl } from './src/create-intl.js';
9
+ export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat } from "./src/utils.js";
10
+ export * from "./src/error.js";
11
+ export { formatMessage } from "./src/message.js";
12
+ export { formatDate, formatDateToParts, formatTime, formatTimeToParts } from "./src/dateTime.js";
13
+ export { formatDisplayName } from "./src/displayName.js";
14
+ export { formatList } from "./src/list.js";
15
+ export { formatPlural } from "./src/plural.js";
16
+ export { formatRelativeTime } from "./src/relativeTime.js";
17
+ export { formatNumber, formatNumberToParts } from "./src/number.js";
18
+ export { createIntl } from "./src/create-intl.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl",
3
3
  "description": "Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.",
4
- "version": "4.0.8",
4
+ "version": "4.0.9",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -12,10 +12,10 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "tslib": "^2.8.0",
15
- "@formatjs/fast-memoize": "3.0.2",
16
- "intl-messageformat": "11.0.8",
17
- "@formatjs/icu-messageformat-parser": "3.2.1",
18
- "@formatjs/ecma402-abstract": "3.0.7"
15
+ "@formatjs/ecma402-abstract": "3.0.8",
16
+ "@formatjs/fast-memoize": "3.0.3",
17
+ "@formatjs/icu-messageformat-parser": "3.3.0",
18
+ "intl-messageformat": "11.0.9"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "typescript": "^5.6.0"
@@ -1,10 +1,14 @@
1
- import { IntlCache, IntlConfig, IntlShape } from './types.js';
2
- export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
3
- (config: C, cache?: IntlCache): S;
1
+ import { type IntlCache, type IntlConfig, type IntlShape } from "./types.js";
2
+ export interface CreateIntlFn<
3
+ T = string,
4
+ C extends IntlConfig<T> = IntlConfig<T>,
5
+ S extends IntlShape<T> = IntlShape<T>
6
+ > {
7
+ (config: C, cache?: IntlCache): S;
4
8
  }
5
9
  /**
6
- * Create intl object
7
- * @param config intl config
8
- * @param cache cache for formatter instances to prevent memory leak
9
- */
10
+ * Create intl object
11
+ * @param config intl config
12
+ * @param cache cache for formatter instances to prevent memory leak
13
+ */
10
14
  export declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
@@ -1,51 +1,69 @@
1
- import { __assign } from "tslib";
2
- import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts, } from './dateTime.js';
3
- import { formatDisplayName } from './displayName.js';
4
- import { InvalidConfigError, MissingDataError } from './error.js';
5
- import { formatList, formatListToParts } from './list.js';
6
- import { formatMessage } from './message.js';
7
- import { formatNumber, formatNumberToParts } from './number.js';
8
- import { formatPlural } from './plural.js';
9
- import { formatRelativeTime } from './relativeTime.js';
10
- import { createFormatters, DEFAULT_INTL_CONFIG } from './utils.js';
1
+ import "@formatjs/icu-messageformat-parser";
2
+ import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts } from "./dateTime.js";
3
+ import { formatDisplayName } from "./displayName.js";
4
+ import { InvalidConfigError, MissingDataError } from "./error.js";
5
+ import { formatList, formatListToParts } from "./list.js";
6
+ import { formatMessage } from "./message.js";
7
+ import { formatNumber, formatNumberToParts } from "./number.js";
8
+ import { formatPlural } from "./plural.js";
9
+ import { formatRelativeTime } from "./relativeTime.js";
10
+ import "./types.js";
11
+ import { createFormatters, DEFAULT_INTL_CONFIG } from "./utils.js";
11
12
  function messagesContainString(messages) {
12
- var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
13
- return typeof firstMessage === 'string';
13
+ const firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
14
+ return typeof firstMessage === "string";
14
15
  }
15
16
  function verifyConfigMessages(config) {
16
- if (config.onWarn &&
17
- config.defaultRichTextElements &&
18
- messagesContainString(config.messages || {})) {
19
- config.onWarn("[@formatjs/intl] \"defaultRichTextElements\" was specified but \"message\" was not pre-compiled. \nPlease consider using \"@formatjs/cli\" to pre-compile your messages for performance.\nFor more details see https://formatjs.github.io/docs/getting-started/message-distribution");
20
- }
17
+ if (config.onWarn && config.defaultRichTextElements && messagesContainString(config.messages || {})) {
18
+ config.onWarn(`[@formatjs/intl] "defaultRichTextElements" was specified but "message" was not pre-compiled.
19
+ Please consider using "@formatjs/cli" to pre-compile your messages for performance.
20
+ For more details see https://formatjs.github.io/docs/getting-started/message-distribution`);
21
+ }
21
22
  }
22
23
  /**
23
- * Create intl object
24
- * @param config intl config
25
- * @param cache cache for formatter instances to prevent memory leak
26
- */
24
+ * Create intl object
25
+ * @param config intl config
26
+ * @param cache cache for formatter instances to prevent memory leak
27
+ */
27
28
  export function createIntl(config, cache) {
28
- var formatters = createFormatters(cache);
29
- var resolvedConfig = __assign(__assign({}, DEFAULT_INTL_CONFIG), config);
30
- var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
31
- if (!locale) {
32
- if (onError) {
33
- onError(new InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details")));
34
- }
35
- // Since there's no registered locale data for `locale`, this will
36
- // fallback to the `defaultLocale` to make sure things can render.
37
- // The `messages` are overridden to the `defaultProps` empty object
38
- // to maintain referential equality across re-renders. It's assumed
39
- // each <FormattedMessage> contains a `defaultMessage` prop.
40
- resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
41
- }
42
- else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
43
- onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
44
- }
45
- else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
46
- onError) {
47
- onError(new MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details")));
48
- }
49
- verifyConfigMessages(resolvedConfig);
50
- return __assign(__assign({}, resolvedConfig), { formatters: formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: formatMessage.bind(null, resolvedConfig, formatters), $t: formatMessage.bind(null, resolvedConfig, formatters), formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
29
+ const formatters = createFormatters(cache);
30
+ const resolvedConfig = {
31
+ ...DEFAULT_INTL_CONFIG,
32
+ ...config
33
+ };
34
+ const { locale, defaultLocale, onError } = resolvedConfig;
35
+ if (!locale) {
36
+ if (onError) {
37
+ onError(new InvalidConfigError(`"locale" was not configured, using "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details`));
38
+ }
39
+ // Since there's no registered locale data for `locale`, this will
40
+ // fallback to the `defaultLocale` to make sure things can render.
41
+ // The `messages` are overridden to the `defaultProps` empty object
42
+ // to maintain referential equality across re-renders. It's assumed
43
+ // each <FormattedMessage> contains a `defaultMessage` prop.
44
+ resolvedConfig.locale = resolvedConfig.defaultLocale || "en";
45
+ } else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
46
+ onError(new MissingDataError(`Missing locale data for locale: "${locale}" in Intl.NumberFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`));
47
+ } else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length && onError) {
48
+ onError(new MissingDataError(`Missing locale data for locale: "${locale}" in Intl.DateTimeFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`));
49
+ }
50
+ verifyConfigMessages(resolvedConfig);
51
+ return {
52
+ ...resolvedConfig,
53
+ formatters,
54
+ formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat),
55
+ formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat),
56
+ formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat),
57
+ formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat),
58
+ formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat),
59
+ formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat),
60
+ formatDateTimeRange: formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat),
61
+ formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat),
62
+ formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
63
+ formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
64
+ $t: formatMessage.bind(null, resolvedConfig, formatters),
65
+ formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat),
66
+ formatListToParts: formatListToParts.bind(null, resolvedConfig, formatters.getListFormat),
67
+ formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames)
68
+ };
51
69
  }
package/src/dateTime.d.ts CHANGED
@@ -1,37 +1,37 @@
1
- import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types.js';
2
- export declare function getFormatter({ locale, formats, onError, timeZone, }: {
3
- locale: string;
4
- timeZone?: string;
5
- formats: CustomFormats;
6
- onError: OnErrorFn;
7
- }, type: 'date' | 'time' | 'dateTimeRange', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): Intl.DateTimeFormat;
1
+ import { type CustomFormats, type Formatters, type IntlFormatters, type OnErrorFn } from "./types.js";
2
+ export declare function getFormatter({ locale, formats, onError, timeZone }: {
3
+ locale: string;
4
+ timeZone?: string;
5
+ formats: CustomFormats;
6
+ onError: OnErrorFn;
7
+ }, type: "date" | "time" | "dateTimeRange", getDateTimeFormat: Formatters["getDateTimeFormat"], options?: Parameters<IntlFormatters["formatDate"]>[1]): Intl.DateTimeFormat;
8
8
  export declare function formatDate(config: {
9
- locale: string;
10
- timeZone?: string;
11
- formats: CustomFormats;
12
- onError: OnErrorFn;
13
- }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): string;
9
+ locale: string;
10
+ timeZone?: string;
11
+ formats: CustomFormats;
12
+ onError: OnErrorFn;
13
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): string;
14
14
  export declare function formatTime(config: {
15
- locale: string;
16
- timeZone?: string;
17
- formats: CustomFormats;
18
- onError: OnErrorFn;
19
- }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTime']>): string;
15
+ locale: string;
16
+ timeZone?: string;
17
+ formats: CustomFormats;
18
+ onError: OnErrorFn;
19
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTime"]>[0], options?: Parameters<IntlFormatters["formatTime"]>[1]): string;
20
20
  export declare function formatDateTimeRange(config: {
21
- locale: string;
22
- timeZone?: string;
23
- formats: CustomFormats;
24
- onError: OnErrorFn;
25
- }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[from, to, options]: Parameters<IntlFormatters['formatDateTimeRange']>): string;
21
+ locale: string;
22
+ timeZone?: string;
23
+ formats: CustomFormats;
24
+ onError: OnErrorFn;
25
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], from: Parameters<IntlFormatters["formatDateTimeRange"]>[0], to: Parameters<IntlFormatters["formatDateTimeRange"]>[1], options?: Parameters<IntlFormatters["formatDateTimeRange"]>[2]): string;
26
26
  export declare function formatDateToParts(config: {
27
- locale: string;
28
- timeZone?: string;
29
- formats: CustomFormats;
30
- onError: OnErrorFn;
31
- }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): Intl.DateTimeFormatPart[];
27
+ locale: string;
28
+ timeZone?: string;
29
+ formats: CustomFormats;
30
+ onError: OnErrorFn;
31
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): Intl.DateTimeFormatPart[];
32
32
  export declare function formatTimeToParts(config: {
33
- locale: string;
34
- timeZone?: string;
35
- formats: CustomFormats;
36
- onError: OnErrorFn;
37
- }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTimeToParts']>): Intl.DateTimeFormatPart[];
33
+ locale: string;
34
+ timeZone?: string;
35
+ formats: CustomFormats;
36
+ onError: OnErrorFn;
37
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTimeToParts"]>[0], options?: Parameters<IntlFormatters["formatTimeToParts"]>[1]): Intl.DateTimeFormatPart[];
package/src/dateTime.js CHANGED
@@ -1,117 +1,86 @@
1
- import { __assign } from "tslib";
2
- import { IntlFormatError } from './error.js';
3
- import { filterProps, getNamedFormat } from './utils.js';
4
- var DATE_TIME_FORMAT_OPTIONS = [
5
- 'formatMatcher',
6
- 'timeZone',
7
- 'hour12',
8
- 'weekday',
9
- 'era',
10
- 'year',
11
- 'month',
12
- 'day',
13
- 'hour',
14
- 'minute',
15
- 'second',
16
- 'timeZoneName',
17
- 'hourCycle',
18
- 'dateStyle',
19
- 'timeStyle',
20
- 'calendar',
21
- // 'dayPeriod',
22
- 'numberingSystem',
23
- 'fractionalSecondDigits',
1
+ import "./types.js";
2
+ import { IntlFormatError } from "./error.js";
3
+ import { filterProps, getNamedFormat } from "./utils.js";
4
+ const DATE_TIME_FORMAT_OPTIONS = [
5
+ "formatMatcher",
6
+ "timeZone",
7
+ "hour12",
8
+ "weekday",
9
+ "era",
10
+ "year",
11
+ "month",
12
+ "day",
13
+ "hour",
14
+ "minute",
15
+ "second",
16
+ "timeZoneName",
17
+ "hourCycle",
18
+ "dateStyle",
19
+ "timeStyle",
20
+ "calendar",
21
+ "numberingSystem",
22
+ "fractionalSecondDigits"
24
23
  ];
25
- export function getFormatter(_a, type, getDateTimeFormat, options) {
26
- var locale = _a.locale, formats = _a.formats, onError = _a.onError, timeZone = _a.timeZone;
27
- if (options === void 0) { options = {}; }
28
- var format = options.format;
29
- var defaults = __assign(__assign({}, (timeZone && { timeZone: timeZone })), (format && getNamedFormat(formats, type, format, onError)));
30
- var filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
31
- if (type === 'time' &&
32
- !filteredOptions.hour &&
33
- !filteredOptions.minute &&
34
- !filteredOptions.second &&
35
- !filteredOptions.timeStyle &&
36
- !filteredOptions.dateStyle) {
37
- // Add default formatting options if hour, minute, or second isn't defined.
38
- filteredOptions = __assign(__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
39
- }
40
- return getDateTimeFormat(locale, filteredOptions);
24
+ export function getFormatter({ locale, formats, onError, timeZone }, type, getDateTimeFormat, options = {}) {
25
+ const { format } = options;
26
+ const defaults = {
27
+ ...timeZone && { timeZone },
28
+ ...format && getNamedFormat(formats, type, format, onError)
29
+ };
30
+ let filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
31
+ if (type === "time" && !filteredOptions.hour && !filteredOptions.minute && !filteredOptions.second && !filteredOptions.timeStyle && !filteredOptions.dateStyle) {
32
+ // Add default formatting options if hour, minute, or second isn't defined.
33
+ filteredOptions = {
34
+ ...filteredOptions,
35
+ hour: "numeric",
36
+ minute: "numeric"
37
+ };
38
+ }
39
+ return getDateTimeFormat(locale, filteredOptions);
41
40
  }
42
- export function formatDate(config, getDateTimeFormat) {
43
- var _a = [];
44
- for (var _i = 2; _i < arguments.length; _i++) {
45
- _a[_i - 2] = arguments[_i];
46
- }
47
- var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
48
- var date = typeof value === 'string' ? new Date(value || 0) : value;
49
- try {
50
- return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
51
- }
52
- catch (e) {
53
- config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
54
- }
55
- return String(date);
41
+ export function formatDate(config, getDateTimeFormat, value, options = {}) {
42
+ const date = typeof value === "string" ? new Date(value || 0) : value;
43
+ try {
44
+ return getFormatter(config, "date", getDateTimeFormat, options).format(date);
45
+ } catch (e) {
46
+ config.onError(new IntlFormatError("Error formatting date.", config.locale, e));
47
+ }
48
+ return String(date);
56
49
  }
57
- export function formatTime(config, getDateTimeFormat) {
58
- var _a = [];
59
- for (var _i = 2; _i < arguments.length; _i++) {
60
- _a[_i - 2] = arguments[_i];
61
- }
62
- var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
63
- var date = typeof value === 'string' ? new Date(value || 0) : value;
64
- try {
65
- return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
66
- }
67
- catch (e) {
68
- config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
69
- }
70
- return String(date);
50
+ export function formatTime(config, getDateTimeFormat, value, options = {}) {
51
+ const date = typeof value === "string" ? new Date(value || 0) : value;
52
+ try {
53
+ return getFormatter(config, "time", getDateTimeFormat, options).format(date);
54
+ } catch (e) {
55
+ config.onError(new IntlFormatError("Error formatting time.", config.locale, e));
56
+ }
57
+ return String(date);
71
58
  }
72
- export function formatDateTimeRange(config, getDateTimeFormat) {
73
- var _a = [];
74
- for (var _i = 2; _i < arguments.length; _i++) {
75
- _a[_i - 2] = arguments[_i];
76
- }
77
- var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b;
78
- var fromDate = typeof from === 'string' ? new Date(from || 0) : from;
79
- var toDate = typeof to === 'string' ? new Date(to || 0) : to;
80
- try {
81
- return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate);
82
- }
83
- catch (e) {
84
- config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e));
85
- }
86
- return String(fromDate);
59
+ export function formatDateTimeRange(config, getDateTimeFormat, from, to, options = {}) {
60
+ const fromDate = typeof from === "string" ? new Date(from || 0) : from;
61
+ const toDate = typeof to === "string" ? new Date(to || 0) : to;
62
+ try {
63
+ return getFormatter(config, "dateTimeRange", getDateTimeFormat, options).formatRange(fromDate, toDate);
64
+ } catch (e) {
65
+ config.onError(new IntlFormatError("Error formatting date time range.", config.locale, e));
66
+ }
67
+ return String(fromDate);
87
68
  }
88
- export function formatDateToParts(config, getDateTimeFormat) {
89
- var _a = [];
90
- for (var _i = 2; _i < arguments.length; _i++) {
91
- _a[_i - 2] = arguments[_i];
92
- }
93
- var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
94
- var date = typeof value === 'string' ? new Date(value || 0) : value;
95
- try {
96
- return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
97
- }
98
- catch (e) {
99
- config.onError(new IntlFormatError('Error formatting date.', config.locale, e));
100
- }
101
- return [];
69
+ export function formatDateToParts(config, getDateTimeFormat, value, options = {}) {
70
+ const date = typeof value === "string" ? new Date(value || 0) : value;
71
+ try {
72
+ return getFormatter(config, "date", getDateTimeFormat, options).formatToParts(date);
73
+ } catch (e) {
74
+ config.onError(new IntlFormatError("Error formatting date.", config.locale, e));
75
+ }
76
+ return [];
102
77
  }
103
- export function formatTimeToParts(config, getDateTimeFormat) {
104
- var _a = [];
105
- for (var _i = 2; _i < arguments.length; _i++) {
106
- _a[_i - 2] = arguments[_i];
107
- }
108
- var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
109
- var date = typeof value === 'string' ? new Date(value || 0) : value;
110
- try {
111
- return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged
112
- }
113
- catch (e) {
114
- config.onError(new IntlFormatError('Error formatting time.', config.locale, e));
115
- }
116
- return [];
78
+ export function formatTimeToParts(config, getDateTimeFormat, value, options = {}) {
79
+ const date = typeof value === "string" ? new Date(value || 0) : value;
80
+ try {
81
+ return getFormatter(config, "time", getDateTimeFormat, options).formatToParts(date);
82
+ } catch (e) {
83
+ config.onError(new IntlFormatError("Error formatting time.", config.locale, e));
84
+ }
85
+ return [];
117
86
  }
@@ -1,5 +1,5 @@
1
- import { Formatters, IntlFormatters, OnErrorFn } from './types.js';
2
- export declare function formatDisplayName({ locale, onError, }: {
3
- locale: string;
4
- onError: OnErrorFn;
5
- }, getDisplayNames: Formatters['getDisplayNames'], value: Parameters<IntlFormatters['formatDisplayName']>[0], options: Parameters<IntlFormatters['formatDisplayName']>[1]): string | undefined;
1
+ import { type Formatters, type IntlFormatters, type OnErrorFn } from "./types.js";
2
+ export declare function formatDisplayName({ locale, onError }: {
3
+ locale: string;
4
+ onError: OnErrorFn;
5
+ }, getDisplayNames: Formatters["getDisplayNames"], value: Parameters<IntlFormatters["formatDisplayName"]>[0], options: Parameters<IntlFormatters["formatDisplayName"]>[1]): string | undefined;
@@ -1,23 +1,24 @@
1
- import { filterProps } from './utils.js';
2
- import { ErrorCode, FormatError } from 'intl-messageformat';
3
- import { IntlFormatError } from './error.js';
4
- var DISPLAY_NAMES_OPTONS = [
5
- 'style',
6
- 'type',
7
- 'fallback',
8
- 'languageDisplay',
1
+ import "./types.js";
2
+ import { filterProps } from "./utils.js";
3
+ import { ErrorCode, FormatError } from "intl-messageformat";
4
+ import { IntlFormatError } from "./error.js";
5
+ const DISPLAY_NAMES_OPTONS = [
6
+ "style",
7
+ "type",
8
+ "fallback",
9
+ "languageDisplay"
9
10
  ];
10
- export function formatDisplayName(_a, getDisplayNames, value, options) {
11
- var locale = _a.locale, onError = _a.onError;
12
- var DisplayNames = Intl.DisplayNames;
13
- if (!DisplayNames) {
14
- onError(new FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", ErrorCode.MISSING_INTL_API));
15
- }
16
- var filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
17
- try {
18
- return getDisplayNames(locale, filteredOptions).of(value);
19
- }
20
- catch (e) {
21
- onError(new IntlFormatError('Error formatting display name.', locale, e));
22
- }
11
+ export function formatDisplayName({ locale, onError }, getDisplayNames, value, options) {
12
+ const DisplayNames = Intl.DisplayNames;
13
+ if (!DisplayNames) {
14
+ onError(new FormatError(`Intl.DisplayNames is not available in this environment.
15
+ Try polyfilling it using "@formatjs/intl-displaynames"
16
+ `, ErrorCode.MISSING_INTL_API));
17
+ }
18
+ const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
19
+ try {
20
+ return getDisplayNames(locale, filteredOptions).of(value);
21
+ } catch (e) {
22
+ onError(new IntlFormatError("Error formatting display name.", locale, e));
23
+ }
23
24
  }