@formatjs/intl 3.1.7 → 4.0.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.
Files changed (49) hide show
  1. package/index.d.ts +14 -14
  2. package/index.js +13 -36
  3. package/package.json +11 -8
  4. package/src/create-intl.d.ts +1 -1
  5. package/src/create-intl.js +17 -20
  6. package/src/dateTime.d.ts +1 -1
  7. package/src/dateTime.js +17 -25
  8. package/src/displayName.d.ts +1 -1
  9. package/src/displayName.js +7 -10
  10. package/src/error.d.ts +1 -1
  11. package/src/error.js +17 -20
  12. package/src/list.d.ts +1 -1
  13. package/src/list.js +10 -14
  14. package/src/message.d.ts +1 -1
  15. package/src/message.js +19 -23
  16. package/src/number.d.ts +1 -1
  17. package/src/number.js +9 -14
  18. package/src/plural.d.ts +1 -1
  19. package/src/plural.js +7 -10
  20. package/src/relativeTime.d.ts +1 -1
  21. package/src/relativeTime.js +8 -11
  22. package/src/types.d.ts +4 -3
  23. package/src/types.js +1 -2
  24. package/src/utils.d.ts +1 -1
  25. package/src/utils.js +33 -41
  26. package/lib/index.d.ts +0 -16
  27. package/lib/index.js +0 -17
  28. package/lib/src/create-intl.d.ts +0 -10
  29. package/lib/src/create-intl.js +0 -51
  30. package/lib/src/dateTime.d.ts +0 -37
  31. package/lib/src/dateTime.js +0 -117
  32. package/lib/src/displayName.d.ts +0 -5
  33. package/lib/src/displayName.js +0 -23
  34. package/lib/src/error.d.ts +0 -35
  35. package/lib/src/error.js +0 -91
  36. package/lib/src/list.d.ts +0 -9
  37. package/lib/src/list.js +0 -61
  38. package/lib/src/message.d.ts +0 -15
  39. package/lib/src/message.js +0 -103
  40. package/lib/src/number.d.ts +0 -16
  41. package/lib/src/number.js +0 -58
  42. package/lib/src/plural.d.ts +0 -5
  43. package/lib/src/plural.js +0 -19
  44. package/lib/src/relativeTime.d.ts +0 -6
  45. package/lib/src/relativeTime.js +0 -29
  46. package/lib/src/types.d.ts +0 -109
  47. package/lib/src/types.js +0 -1
  48. package/lib/src/utils.d.ts +0 -12
  49. package/lib/src/utils.js +0 -168
package/src/message.js CHANGED
@@ -1,21 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatMessage = void 0;
4
- var tslib_1 = require("tslib");
5
- var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
6
- var intl_messageformat_1 = require("intl-messageformat");
7
- var error_1 = require("./error");
8
- var utils_1 = require("./utils");
1
+ import { __assign } from "tslib";
2
+ import { TYPE } from '@formatjs/icu-messageformat-parser';
3
+ import { IntlMessageFormat, } from 'intl-messageformat';
4
+ import { MessageFormatError, MissingTranslationError } from './error.js';
5
+ import { invariant } from './utils.js';
9
6
  function setTimeZoneInOptions(opts, timeZone) {
10
7
  return Object.keys(opts).reduce(function (all, k) {
11
- all[k] = tslib_1.__assign({ timeZone: timeZone }, opts[k]);
8
+ all[k] = __assign({ timeZone: timeZone }, opts[k]);
12
9
  return all;
13
10
  }, {});
14
11
  }
15
12
  function deepMergeOptions(opts1, opts2) {
16
- var keys = Object.keys(tslib_1.__assign(tslib_1.__assign({}, opts1), opts2));
13
+ var keys = Object.keys(__assign(__assign({}, opts1), opts2));
17
14
  return keys.reduce(function (all, k) {
18
- all[k] = tslib_1.__assign(tslib_1.__assign({}, (opts1[k] || {})), (opts2[k] || {}));
15
+ all[k] = __assign(__assign({}, opts1[k]), opts2[k]);
19
16
  return all;
20
17
  }, {});
21
18
  }
@@ -23,15 +20,15 @@ function deepMergeFormatsAndSetTimeZone(f1, timeZone) {
23
20
  if (!timeZone) {
24
21
  return f1;
25
22
  }
26
- var mfFormats = intl_messageformat_1.IntlMessageFormat.formats;
27
- return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, mfFormats), f1), { date: deepMergeOptions(setTimeZoneInOptions(mfFormats.date, timeZone), setTimeZoneInOptions(f1.date || {}, timeZone)), time: deepMergeOptions(setTimeZoneInOptions(mfFormats.time, timeZone), setTimeZoneInOptions(f1.time || {}, timeZone)) });
23
+ var mfFormats = IntlMessageFormat.formats;
24
+ return __assign(__assign(__assign({}, mfFormats), f1), { date: deepMergeOptions(setTimeZoneInOptions(mfFormats.date, timeZone), setTimeZoneInOptions(f1.date || {}, timeZone)), time: deepMergeOptions(setTimeZoneInOptions(mfFormats.time, timeZone), setTimeZoneInOptions(f1.time || {}, timeZone)) });
28
25
  }
29
- var formatMessage = function (_a, state, messageDescriptor, values, opts) {
26
+ export var formatMessage = function (_a, state, messageDescriptor, values, opts) {
30
27
  var locale = _a.locale, formats = _a.formats, messages = _a.messages, defaultLocale = _a.defaultLocale, defaultFormats = _a.defaultFormats, fallbackOnEmptyString = _a.fallbackOnEmptyString, onError = _a.onError, timeZone = _a.timeZone, defaultRichTextElements = _a.defaultRichTextElements;
31
28
  if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
32
29
  var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
33
30
  // `id` is a required field of a Message Descriptor.
34
- (0, utils_1.invariant)(!!msgId, "[@formatjs/intl] An `id` must be provided to format a message. You can either:\n1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.github.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
31
+ invariant(!!msgId, "[@formatjs/intl] An `id` must be provided to format a message. You can either:\n1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.github.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
35
32
  var id = String(msgId);
36
33
  var message =
37
34
  // In case messages is Object.create(null)
@@ -43,7 +40,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
43
40
  // IMPORTANT: Hot path if `message` is AST with a single literal node
44
41
  if (Array.isArray(message) &&
45
42
  message.length === 1 &&
46
- message[0].type === icu_messageformat_parser_1.TYPE.literal) {
43
+ message[0].type === TYPE.literal) {
47
44
  return message[0].value;
48
45
  }
49
46
  // IMPORTANT: Hot path straight lookup for performance
@@ -53,7 +50,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
53
50
  !defaultRichTextElements) {
54
51
  return message.replace(/'\{(.*?)\}'/gi, "{$1}");
55
52
  }
56
- values = tslib_1.__assign(tslib_1.__assign({}, defaultRichTextElements), (values || {}));
53
+ values = __assign(__assign({}, defaultRichTextElements), values);
57
54
  formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
58
55
  defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
59
56
  if (!message) {
@@ -65,7 +62,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
65
62
  // This prevents warnings from littering the console in development
66
63
  // when no `messages` are passed into the <IntlProvider> for the
67
64
  // default locale.
68
- onError(new error_1.MissingTranslationError(messageDescriptor, locale));
65
+ onError(new MissingTranslationError(messageDescriptor, locale));
69
66
  }
70
67
  if (defaultMessage) {
71
68
  try {
@@ -73,7 +70,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
73
70
  return formatter.format(values);
74
71
  }
75
72
  catch (e) {
76
- onError(new error_1.MessageFormatError("Error formatting default message for: \"".concat(id, "\", rendering default message verbatim"), locale, messageDescriptor, e));
73
+ onError(new MessageFormatError("Error formatting default message for: \"".concat(id, "\", rendering default message verbatim"), locale, messageDescriptor, e));
77
74
  return typeof defaultMessage === 'string' ? defaultMessage : id;
78
75
  }
79
76
  }
@@ -81,11 +78,11 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
81
78
  }
82
79
  // We have the translated message
83
80
  try {
84
- var formatter = state.getMessageFormat(message, locale, formats, tslib_1.__assign({ formatters: state }, (opts || {})));
81
+ var formatter = state.getMessageFormat(message, locale, formats, __assign({ formatters: state }, opts));
85
82
  return formatter.format(values);
86
83
  }
87
84
  catch (e) {
88
- onError(new error_1.MessageFormatError("Error formatting message: \"".concat(id, "\", using ").concat(defaultMessage ? 'default message' : 'id', " as fallback."), locale, messageDescriptor, e));
85
+ onError(new MessageFormatError("Error formatting message: \"".concat(id, "\", using ").concat(defaultMessage ? 'default message' : 'id', " as fallback."), locale, messageDescriptor, e));
89
86
  }
90
87
  if (defaultMessage) {
91
88
  try {
@@ -93,7 +90,7 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
93
90
  return formatter.format(values);
94
91
  }
95
92
  catch (e) {
96
- onError(new error_1.MessageFormatError("Error formatting the default message for: \"".concat(id, "\", rendering message verbatim"), locale, messageDescriptor, e));
93
+ onError(new MessageFormatError("Error formatting the default message for: \"".concat(id, "\", rendering message verbatim"), locale, messageDescriptor, e));
97
94
  }
98
95
  }
99
96
  if (typeof message === 'string') {
@@ -104,4 +101,3 @@ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
104
101
  }
105
102
  return id;
106
103
  };
107
- exports.formatMessage = formatMessage;
package/src/number.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
1
+ import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types.js';
2
2
  export declare function getFormatter({ locale, formats, onError, }: {
3
3
  locale: string;
4
4
  formats: CustomFormats;
package/src/number.js CHANGED
@@ -1,10 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormatter = getFormatter;
4
- exports.formatNumber = formatNumber;
5
- exports.formatNumberToParts = formatNumberToParts;
6
- var error_1 = require("./error");
7
- var utils_1 = require("./utils");
1
+ import { IntlFormatError } from './error.js';
2
+ import { filterProps, getNamedFormat } from './utils.js';
8
3
  var NUMBER_FORMAT_OPTIONS = [
9
4
  'style',
10
5
  'currency',
@@ -31,33 +26,33 @@ var NUMBER_FORMAT_OPTIONS = [
31
26
  'roundingIncrement',
32
27
  'roundingMode',
33
28
  ];
34
- function getFormatter(_a, getNumberFormat, options) {
29
+ export function getFormatter(_a, getNumberFormat, options) {
35
30
  var locale = _a.locale, formats = _a.formats, onError = _a.onError;
36
31
  if (options === void 0) { options = {}; }
37
32
  var format = options.format;
38
33
  var defaults = ((format &&
39
- (0, utils_1.getNamedFormat)(formats, 'number', format, onError)) ||
34
+ getNamedFormat(formats, 'number', format, onError)) ||
40
35
  {});
41
- var filteredOptions = (0, utils_1.filterProps)(options, NUMBER_FORMAT_OPTIONS, defaults);
36
+ var filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
42
37
  return getNumberFormat(locale, filteredOptions);
43
38
  }
44
- function formatNumber(config, getNumberFormat, value, options) {
39
+ export function formatNumber(config, getNumberFormat, value, options) {
45
40
  if (options === void 0) { options = {}; }
46
41
  try {
47
42
  return getFormatter(config, getNumberFormat, options).format(value);
48
43
  }
49
44
  catch (e) {
50
- config.onError(new error_1.IntlFormatError('Error formatting number.', config.locale, e));
45
+ config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
51
46
  }
52
47
  return String(value);
53
48
  }
54
- function formatNumberToParts(config, getNumberFormat, value, options) {
49
+ export function formatNumberToParts(config, getNumberFormat, value, options) {
55
50
  if (options === void 0) { options = {}; }
56
51
  try {
57
52
  return getFormatter(config, getNumberFormat, options).formatToParts(value);
58
53
  }
59
54
  catch (e) {
60
- config.onError(new error_1.IntlFormatError('Error formatting number.', config.locale, e));
55
+ config.onError(new IntlFormatError('Error formatting number.', config.locale, e));
61
56
  }
62
57
  return [];
63
58
  }
package/src/plural.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Formatters, IntlFormatters, OnErrorFn } from './types';
1
+ import { Formatters, IntlFormatters, OnErrorFn } from './types.js';
2
2
  export declare function formatPlural({ locale, onError, }: {
3
3
  locale: string;
4
4
  onError: OnErrorFn;
package/src/plural.js CHANGED
@@ -1,22 +1,19 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatPlural = formatPlural;
4
- var intl_messageformat_1 = require("intl-messageformat");
5
- var error_1 = require("./error");
6
- var utils_1 = require("./utils");
1
+ import { ErrorCode, FormatError } from 'intl-messageformat';
2
+ import { IntlFormatError } from './error.js';
3
+ import { filterProps } from './utils.js';
7
4
  var PLURAL_FORMAT_OPTIONS = ['type'];
8
- function formatPlural(_a, getPluralRules, value, options) {
5
+ export function formatPlural(_a, getPluralRules, value, options) {
9
6
  var locale = _a.locale, onError = _a.onError;
10
7
  if (options === void 0) { options = {}; }
11
8
  if (!Intl.PluralRules) {
12
- onError(new intl_messageformat_1.FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", intl_messageformat_1.ErrorCode.MISSING_INTL_API));
9
+ onError(new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API));
13
10
  }
14
- var filteredOptions = (0, utils_1.filterProps)(options, PLURAL_FORMAT_OPTIONS);
11
+ var filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
15
12
  try {
16
13
  return getPluralRules(locale, filteredOptions).select(value);
17
14
  }
18
15
  catch (e) {
19
- onError(new error_1.IntlFormatError('Error formatting plural.', locale, e));
16
+ onError(new IntlFormatError('Error formatting plural.', locale, e));
20
17
  }
21
18
  return 'other';
22
19
  }
@@ -1,4 +1,4 @@
1
- import { IntlFormatters, Formatters, CustomFormats, OnErrorFn } from './types';
1
+ import { IntlFormatters, Formatters, CustomFormats, OnErrorFn } from './types.js';
2
2
  export declare function formatRelativeTime(config: {
3
3
  locale: string;
4
4
  formats: CustomFormats;
@@ -1,32 +1,29 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatRelativeTime = formatRelativeTime;
4
- var utils_1 = require("./utils");
5
- var intl_messageformat_1 = require("intl-messageformat");
6
- var error_1 = require("./error");
1
+ import { getNamedFormat, filterProps } from './utils.js';
2
+ import { FormatError, ErrorCode } from 'intl-messageformat';
3
+ import { IntlFormatError } from './error.js';
7
4
  var RELATIVE_TIME_FORMAT_OPTIONS = ['numeric', 'style'];
8
5
  function getFormatter(_a, getRelativeTimeFormat, options) {
9
6
  var locale = _a.locale, formats = _a.formats, onError = _a.onError;
10
7
  if (options === void 0) { options = {}; }
11
8
  var format = options.format;
12
- var defaults = (!!format && (0, utils_1.getNamedFormat)(formats, 'relative', format, onError)) || {};
13
- var filteredOptions = (0, utils_1.filterProps)(options, RELATIVE_TIME_FORMAT_OPTIONS, defaults);
9
+ var defaults = (!!format && getNamedFormat(formats, 'relative', format, onError)) || {};
10
+ var filteredOptions = filterProps(options, RELATIVE_TIME_FORMAT_OPTIONS, defaults);
14
11
  return getRelativeTimeFormat(locale, filteredOptions);
15
12
  }
16
- function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options) {
13
+ export function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options) {
17
14
  if (options === void 0) { options = {}; }
18
15
  if (!unit) {
19
16
  unit = 'second';
20
17
  }
21
18
  var RelativeTimeFormat = Intl.RelativeTimeFormat;
22
19
  if (!RelativeTimeFormat) {
23
- config.onError(new intl_messageformat_1.FormatError("Intl.RelativeTimeFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-relativetimeformat\"\n", intl_messageformat_1.ErrorCode.MISSING_INTL_API));
20
+ config.onError(new FormatError("Intl.RelativeTimeFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-relativetimeformat\"\n", ErrorCode.MISSING_INTL_API));
24
21
  }
25
22
  try {
26
23
  return getFormatter(config, getRelativeTimeFormat, options).format(value, unit);
27
24
  }
28
25
  catch (e) {
29
- config.onError(new error_1.IntlFormatError('Error formatting relative time.', config.locale, e));
26
+ config.onError(new IntlFormatError('Error formatting relative time.', config.locale, e));
30
27
  }
31
28
  return String(value);
32
29
  }
package/src/types.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
2
2
  import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
3
3
  import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
4
- import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error';
5
- import { DEFAULT_INTL_CONFIG } from './utils';
4
+ import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error.js';
5
+ import { DEFAULT_INTL_CONFIG } from './utils.js';
6
6
  export interface Part<T = string> {
7
7
  type: 'element' | 'literal';
8
8
  value: T;
@@ -51,6 +51,7 @@ export interface CustomFormatConfig<Source = string> {
51
51
  }
52
52
  export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'dateTimeRange'>;
53
53
  export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
54
+ export type FormatTimeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
54
55
  export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
55
56
  export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
56
57
  export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
@@ -63,7 +64,7 @@ export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMat
63
64
  export interface IntlFormatters<TBase = unknown> {
64
65
  formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0] | string, to: Parameters<Intl.DateTimeFormat['formatRange']>[1] | string, opts?: FormatDateTimeRangeOptions): string;
65
66
  formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
66
- formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
67
+ formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatTimeOptions): string;
67
68
  formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
68
69
  formatTimeToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
69
70
  formatRelativeTime(this: void, value: Parameters<Intl.RelativeTimeFormat['format']>[0], unit?: Parameters<Intl.RelativeTimeFormat['format']>[1], opts?: FormatRelativeTimeOptions): string;
package/src/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/src/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
2
- import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types';
2
+ import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types.js';
3
3
  export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
4
4
  export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
5
5
  export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
package/src/utils.js CHANGED
@@ -1,22 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_INTL_CONFIG = void 0;
4
- exports.invariant = invariant;
5
- exports.filterProps = filterProps;
6
- exports.createIntlCache = createIntlCache;
7
- exports.createFormatters = createFormatters;
8
- exports.getNamedFormat = getNamedFormat;
9
- var tslib_1 = require("tslib");
10
- var fast_memoize_1 = require("@formatjs/fast-memoize");
11
- var intl_messageformat_1 = require("intl-messageformat");
12
- var error_1 = require("./error");
13
- function invariant(condition, message, Err) {
1
+ import { __assign, __spreadArray } from "tslib";
2
+ import { memoize, strategies } from '@formatjs/fast-memoize';
3
+ import { IntlMessageFormat } from 'intl-messageformat';
4
+ import { UnsupportedFormatterError } from './error.js';
5
+ export function invariant(condition, message, Err) {
14
6
  if (Err === void 0) { Err = Error; }
15
7
  if (!condition) {
16
8
  throw new Err(message);
17
9
  }
18
10
  }
19
- function filterProps(props, allowlist, defaults) {
11
+ export function filterProps(props, allowlist, defaults) {
20
12
  if (defaults === void 0) { defaults = {}; }
21
13
  return allowlist.reduce(function (filtered, name) {
22
14
  if (name in props) {
@@ -40,7 +32,7 @@ var defaultWarnHandler = function (warning) {
40
32
  console.warn(warning);
41
33
  }
42
34
  };
43
- exports.DEFAULT_INTL_CONFIG = {
35
+ export var DEFAULT_INTL_CONFIG = {
44
36
  formats: {},
45
37
  messages: {},
46
38
  timeZone: undefined,
@@ -50,7 +42,7 @@ exports.DEFAULT_INTL_CONFIG = {
50
42
  onError: defaultErrorHandler,
51
43
  onWarn: defaultWarnHandler,
52
44
  };
53
- function createIntlCache() {
45
+ export function createIntlCache() {
54
46
  return {
55
47
  dateTime: {},
56
48
  number: {},
@@ -79,91 +71,91 @@ function createFastMemoizeCache(store) {
79
71
  * Create intl formatters and populate cache
80
72
  * @param cache explicit cache to prevent leaking memory
81
73
  */
82
- function createFormatters(cache) {
74
+ export function createFormatters(cache) {
83
75
  if (cache === void 0) { cache = createIntlCache(); }
84
76
  var RelativeTimeFormat = Intl.RelativeTimeFormat;
85
77
  var ListFormat = Intl.ListFormat;
86
78
  var DisplayNames = Intl.DisplayNames;
87
- var getDateTimeFormat = (0, fast_memoize_1.memoize)(function () {
79
+ var getDateTimeFormat = memoize(function () {
88
80
  var _a;
89
81
  var args = [];
90
82
  for (var _i = 0; _i < arguments.length; _i++) {
91
83
  args[_i] = arguments[_i];
92
84
  }
93
- return new ((_a = Intl.DateTimeFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
85
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
94
86
  }, {
95
87
  cache: createFastMemoizeCache(cache.dateTime),
96
- strategy: fast_memoize_1.strategies.variadic,
88
+ strategy: strategies.variadic,
97
89
  });
98
- var getNumberFormat = (0, fast_memoize_1.memoize)(function () {
90
+ var getNumberFormat = memoize(function () {
99
91
  var _a;
100
92
  var args = [];
101
93
  for (var _i = 0; _i < arguments.length; _i++) {
102
94
  args[_i] = arguments[_i];
103
95
  }
104
- return new ((_a = Intl.NumberFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
96
+ return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
105
97
  }, {
106
98
  cache: createFastMemoizeCache(cache.number),
107
- strategy: fast_memoize_1.strategies.variadic,
99
+ strategy: strategies.variadic,
108
100
  });
109
- var getPluralRules = (0, fast_memoize_1.memoize)(function () {
101
+ var getPluralRules = memoize(function () {
110
102
  var _a;
111
103
  var args = [];
112
104
  for (var _i = 0; _i < arguments.length; _i++) {
113
105
  args[_i] = arguments[_i];
114
106
  }
115
- return new ((_a = Intl.PluralRules).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
107
+ return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
116
108
  }, {
117
109
  cache: createFastMemoizeCache(cache.pluralRules),
118
- strategy: fast_memoize_1.strategies.variadic,
110
+ strategy: strategies.variadic,
119
111
  });
120
112
  return {
121
113
  getDateTimeFormat: getDateTimeFormat,
122
114
  getNumberFormat: getNumberFormat,
123
- getMessageFormat: (0, fast_memoize_1.memoize)(function (message, locales, overrideFormats, opts) {
124
- return new intl_messageformat_1.IntlMessageFormat(message, locales, overrideFormats, tslib_1.__assign({ formatters: {
115
+ getMessageFormat: memoize(function (message, locales, overrideFormats, opts) {
116
+ return new IntlMessageFormat(message, locales, overrideFormats, __assign({ formatters: {
125
117
  getNumberFormat: getNumberFormat,
126
118
  getDateTimeFormat: getDateTimeFormat,
127
119
  getPluralRules: getPluralRules,
128
- } }, (opts || {})));
120
+ } }, opts));
129
121
  }, {
130
122
  cache: createFastMemoizeCache(cache.message),
131
- strategy: fast_memoize_1.strategies.variadic,
123
+ strategy: strategies.variadic,
132
124
  }),
133
- getRelativeTimeFormat: (0, fast_memoize_1.memoize)(function () {
125
+ getRelativeTimeFormat: memoize(function () {
134
126
  var args = [];
135
127
  for (var _i = 0; _i < arguments.length; _i++) {
136
128
  args[_i] = arguments[_i];
137
129
  }
138
- return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, tslib_1.__spreadArray([void 0], args, false)))();
130
+ return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, __spreadArray([void 0], args, false)))();
139
131
  }, {
140
132
  cache: createFastMemoizeCache(cache.relativeTime),
141
- strategy: fast_memoize_1.strategies.variadic,
133
+ strategy: strategies.variadic,
142
134
  }),
143
135
  getPluralRules: getPluralRules,
144
- getListFormat: (0, fast_memoize_1.memoize)(function () {
136
+ getListFormat: memoize(function () {
145
137
  var args = [];
146
138
  for (var _i = 0; _i < arguments.length; _i++) {
147
139
  args[_i] = arguments[_i];
148
140
  }
149
- return new (ListFormat.bind.apply(ListFormat, tslib_1.__spreadArray([void 0], args, false)))();
141
+ return new (ListFormat.bind.apply(ListFormat, __spreadArray([void 0], args, false)))();
150
142
  }, {
151
143
  cache: createFastMemoizeCache(cache.list),
152
- strategy: fast_memoize_1.strategies.variadic,
144
+ strategy: strategies.variadic,
153
145
  }),
154
- getDisplayNames: (0, fast_memoize_1.memoize)(function () {
146
+ getDisplayNames: memoize(function () {
155
147
  var args = [];
156
148
  for (var _i = 0; _i < arguments.length; _i++) {
157
149
  args[_i] = arguments[_i];
158
150
  }
159
- return new (DisplayNames.bind.apply(DisplayNames, tslib_1.__spreadArray([void 0], args, false)))();
151
+ return new (DisplayNames.bind.apply(DisplayNames, __spreadArray([void 0], args, false)))();
160
152
  }, {
161
153
  cache: createFastMemoizeCache(cache.displayNames),
162
- strategy: fast_memoize_1.strategies.variadic,
154
+ strategy: strategies.variadic,
163
155
  }),
164
156
  };
165
157
  }
166
- function getNamedFormat(formats, type, name, onError) {
158
+ export function getNamedFormat(formats, type, name, onError) {
167
159
  var formatType = formats && formats[type];
168
160
  var format;
169
161
  if (formatType) {
@@ -172,5 +164,5 @@ function getNamedFormat(formats, type, name, onError) {
172
164
  if (format) {
173
165
  return format;
174
166
  }
175
- onError(new error_1.UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
167
+ onError(new UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
176
168
  }
package/lib/index.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { MessageDescriptor } from './src/types';
2
- export * from './src/types';
3
- export declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
4
- export declare function defineMessage<T>(msg: T): T;
5
- export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
6
- export * from './src/error';
7
- export { formatMessage } from './src/message';
8
- export type { FormatMessageFn } from './src/message';
9
- export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
10
- export { formatDisplayName } from './src/displayName';
11
- export { formatList } from './src/list';
12
- export { formatPlural } from './src/plural';
13
- export { formatRelativeTime } from './src/relativeTime';
14
- export { formatNumber, formatNumberToParts } from './src/number';
15
- export { createIntl } from './src/create-intl';
16
- export type { CreateIntlFn } from './src/create-intl';
package/lib/index.js DELETED
@@ -1,17 +0,0 @@
1
- export * from './src/types';
2
- export function defineMessages(msgs) {
3
- return msgs;
4
- }
5
- export function defineMessage(msg) {
6
- return msg;
7
- }
8
- export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat, } from './src/utils';
9
- export * from './src/error';
10
- export { formatMessage } from './src/message';
11
- export { formatDate, formatDateToParts, formatTime, formatTimeToParts, } from './src/dateTime';
12
- export { formatDisplayName } from './src/displayName';
13
- export { formatList } from './src/list';
14
- export { formatPlural } from './src/plural';
15
- export { formatRelativeTime } from './src/relativeTime';
16
- export { formatNumber, formatNumberToParts } from './src/number';
17
- export { createIntl } from './src/create-intl';
@@ -1,10 +0,0 @@
1
- import { IntlCache, IntlConfig, IntlShape } from './types';
2
- export interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
3
- (config: C, cache?: IntlCache): S;
4
- }
5
- /**
6
- * Create intl object
7
- * @param config intl config
8
- * @param cache cache for formatter instances to prevent memory leak
9
- */
10
- export declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
@@ -1,51 +0,0 @@
1
- import { __assign } from "tslib";
2
- import { formatDate, formatDateTimeRange, formatDateToParts, formatTime, formatTimeToParts, } from './dateTime';
3
- import { formatDisplayName } from './displayName';
4
- import { InvalidConfigError, MissingDataError } from './error';
5
- import { formatList, formatListToParts } from './list';
6
- import { formatMessage } from './message';
7
- import { formatNumber, formatNumberToParts } from './number';
8
- import { formatPlural } from './plural';
9
- import { formatRelativeTime } from './relativeTime';
10
- import { createFormatters, DEFAULT_INTL_CONFIG } from './utils';
11
- function messagesContainString(messages) {
12
- var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
13
- return typeof firstMessage === 'string';
14
- }
15
- 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
- }
21
- }
22
- /**
23
- * Create intl object
24
- * @param config intl config
25
- * @param cache cache for formatter instances to prevent memory leak
26
- */
27
- 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) });
51
- }
@@ -1,37 +0,0 @@
1
- import { CustomFormats, Formatters, IntlFormatters, OnErrorFn } from './types';
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
- 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;
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;
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;
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[];
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[];