@formatjs/intl 2.3.2 → 2.3.4

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 (104) hide show
  1. package/index.d.ts +17 -0
  2. package/index.d.ts.map +1 -0
  3. package/index.js +40 -0
  4. package/lib/index.d.ts +17 -0
  5. package/lib/index.d.ts.map +1 -0
  6. package/lib/index.js +17 -0
  7. package/lib/src/create-intl.d.ts +11 -0
  8. package/lib/src/create-intl.d.ts.map +1 -0
  9. package/lib/src/create-intl.js +55 -0
  10. package/lib/src/dateTime.d.ts +38 -0
  11. package/lib/src/dateTime.d.ts.map +1 -0
  12. package/lib/src/dateTime.js +117 -0
  13. package/lib/src/displayName.d.ts +6 -0
  14. package/lib/src/displayName.d.ts.map +1 -0
  15. package/lib/src/displayName.js +22 -0
  16. package/lib/src/error.d.ts +34 -0
  17. package/lib/src/error.d.ts.map +1 -0
  18. package/lib/src/error.js +88 -0
  19. package/lib/src/list.d.ts +11 -0
  20. package/lib/src/list.d.ts.map +1 -0
  21. package/lib/src/list.js +61 -0
  22. package/lib/src/message.d.ts +16 -0
  23. package/lib/src/message.d.ts.map +1 -0
  24. package/lib/src/message.js +103 -0
  25. package/lib/src/number.d.ts +17 -0
  26. package/lib/src/number.d.ts.map +1 -0
  27. package/lib/src/number.js +54 -0
  28. package/lib/src/plural.d.ts +7 -0
  29. package/lib/src/plural.d.ts.map +1 -0
  30. package/lib/src/plural.js +19 -0
  31. package/lib/src/relativeTime.d.ts +7 -0
  32. package/lib/src/relativeTime.d.ts.map +1 -0
  33. package/lib/src/relativeTime.js +29 -0
  34. package/lib/src/types.d.ts +103 -0
  35. package/lib/src/types.d.ts.map +1 -0
  36. package/lib/src/types.js +1 -0
  37. package/lib/src/utils.d.ts +12 -0
  38. package/lib/src/utils.d.ts.map +1 -0
  39. package/lib/src/utils.js +162 -0
  40. package/package.json +9 -9
  41. package/src/create-intl.d.ts +11 -0
  42. package/src/create-intl.d.ts.map +1 -0
  43. package/src/create-intl.js +59 -0
  44. package/src/dateTime.d.ts +38 -0
  45. package/src/dateTime.d.ts.map +1 -0
  46. package/src/dateTime.js +126 -0
  47. package/src/displayName.d.ts +6 -0
  48. package/src/displayName.d.ts.map +1 -0
  49. package/src/displayName.js +26 -0
  50. package/src/error.d.ts +34 -0
  51. package/src/error.d.ts.map +1 -0
  52. package/src/error.js +91 -0
  53. package/src/list.d.ts +11 -0
  54. package/src/list.d.ts.map +1 -0
  55. package/src/list.js +66 -0
  56. package/src/message.d.ts +16 -0
  57. package/src/message.d.ts.map +1 -0
  58. package/src/message.js +107 -0
  59. package/src/number.d.ts +17 -0
  60. package/src/number.d.ts.map +1 -0
  61. package/src/number.js +60 -0
  62. package/src/plural.d.ts +7 -0
  63. package/src/plural.d.ts.map +1 -0
  64. package/src/plural.js +23 -0
  65. package/src/relativeTime.d.ts +7 -0
  66. package/src/relativeTime.d.ts.map +1 -0
  67. package/src/relativeTime.js +33 -0
  68. package/src/types.d.ts +103 -0
  69. package/src/types.d.ts.map +1 -0
  70. package/src/types.js +2 -0
  71. package/src/utils.d.ts +12 -0
  72. package/src/utils.d.ts.map +1 -0
  73. package/src/utils.js +169 -0
  74. package/BUILD +0 -79
  75. package/CHANGELOG.md +0 -537
  76. package/index.ts +0 -38
  77. package/src/create-intl.ts +0 -159
  78. package/src/dateTime.ts +0 -204
  79. package/src/displayName.ts +0 -54
  80. package/src/error.ts +0 -106
  81. package/src/list.ts +0 -119
  82. package/src/message.ts +0 -246
  83. package/src/number.ts +0 -101
  84. package/src/plural.ts +0 -45
  85. package/src/relativeTime.ts +0 -74
  86. package/src/types.ts +0 -234
  87. package/src/utils.ts +0 -177
  88. package/tests/__snapshots__/formatDate.test.ts.snap +0 -13
  89. package/tests/__snapshots__/formatMessage.test.ts.snap +0 -45
  90. package/tests/__snapshots__/formatNumber.test.ts.snap +0 -13
  91. package/tests/__snapshots__/formatRelativeTime.test.ts.snap +0 -19
  92. package/tests/__snapshots__/formatTime.test.ts.snap +0 -13
  93. package/tests/create-intl.test.ts +0 -60
  94. package/tests/error.test.ts +0 -25
  95. package/tests/formatDate.test.ts +0 -167
  96. package/tests/formatDisplayNames.test.ts +0 -63
  97. package/tests/formatList.test.ts +0 -51
  98. package/tests/formatMessage.test.ts +0 -495
  99. package/tests/formatNumber.test.ts +0 -172
  100. package/tests/formatPlural.test.ts +0 -119
  101. package/tests/formatRelativeTime.test.ts +0 -145
  102. package/tests/formatTime.test.ts +0 -234
  103. package/tests/global.d.ts +0 -7
  104. package/tsconfig.json +0 -5
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createIntl = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var utils_1 = require("./utils");
6
+ var error_1 = require("./error");
7
+ var number_1 = require("./number");
8
+ var relativeTime_1 = require("./relativeTime");
9
+ var dateTime_1 = require("./dateTime");
10
+ var plural_1 = require("./plural");
11
+ var message_1 = require("./message");
12
+ var list_1 = require("./list");
13
+ var displayName_1 = require("./displayName");
14
+ function messagesContainString(messages) {
15
+ var firstMessage = messages ? messages[Object.keys(messages)[0]] : undefined;
16
+ return typeof firstMessage === 'string';
17
+ }
18
+ function verifyConfigMessages(config) {
19
+ if (config.onWarn &&
20
+ config.defaultRichTextElements &&
21
+ messagesContainString(config.messages || {})) {
22
+ 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.io/docs/getting-started/message-distribution");
23
+ }
24
+ }
25
+ /**
26
+ * Create intl object
27
+ * @param config intl config
28
+ * @param cache cache for formatter instances to prevent memory leak
29
+ */
30
+ function createIntl(config, cache) {
31
+ var formatters = (0, utils_1.createFormatters)(cache);
32
+ var resolvedConfig = tslib_1.__assign(tslib_1.__assign({}, utils_1.DEFAULT_INTL_CONFIG), config);
33
+ var locale = resolvedConfig.locale, defaultLocale = resolvedConfig.defaultLocale, onError = resolvedConfig.onError;
34
+ if (!locale) {
35
+ if (onError) {
36
+ onError(new error_1.InvalidConfigError("\"locale\" was not configured, using \"".concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl/api#intlshape for more details")));
37
+ }
38
+ // Since there's no registered locale data for `locale`, this will
39
+ // fallback to the `defaultLocale` to make sure things can render.
40
+ // The `messages` are overridden to the `defaultProps` empty object
41
+ // to maintain referential equality across re-renders. It's assumed
42
+ // each <FormattedMessage> contains a `defaultMessage` prop.
43
+ resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
44
+ }
45
+ else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
46
+ onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.NumberFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
47
+ }
48
+ else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
49
+ onError) {
50
+ onError(new error_1.MissingDataError("Missing locale data for locale: \"".concat(locale, "\" in Intl.DateTimeFormat. Using default locale: \"").concat(defaultLocale, "\" as fallback. See https://formatjs.io/docs/react-intl#runtime-requirements for more details")));
51
+ }
52
+ verifyConfigMessages(resolvedConfig);
53
+ return tslib_1.__assign(tslib_1.__assign({}, resolvedConfig), { formatters: formatters, formatNumber: number_1.formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: number_1.formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: relativeTime_1.formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: dateTime_1.formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: dateTime_1.formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: dateTime_1.formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateTimeRange: dateTime_1.formatDateTimeRange.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
54
+ // @ts-expect-error TODO: will get to this later
55
+ formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters),
56
+ // @ts-expect-error TODO: will get to this later
57
+ $t: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatListToParts: list_1.formatListToParts.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
58
+ }
59
+ exports.createIntl = createIntl;
@@ -0,0 +1,38 @@
1
+ import { Formatters, IntlFormatters, CustomFormats, OnErrorFn } from './types';
2
+ import { DateTimeFormat } from '@formatjs/ecma402-abstract';
3
+ export declare function getFormatter({ locale, formats, onError, timeZone, }: {
4
+ locale: string;
5
+ timeZone?: string;
6
+ formats: CustomFormats;
7
+ onError: OnErrorFn;
8
+ }, type: 'date' | 'time', getDateTimeFormat: Formatters['getDateTimeFormat'], options?: Parameters<IntlFormatters['formatDate']>[1]): DateTimeFormat;
9
+ export declare function formatDate(config: {
10
+ locale: string;
11
+ timeZone?: string;
12
+ formats: CustomFormats;
13
+ onError: OnErrorFn;
14
+ }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatDate']>): string;
15
+ export declare function formatTime(config: {
16
+ locale: string;
17
+ timeZone?: string;
18
+ formats: CustomFormats;
19
+ onError: OnErrorFn;
20
+ }, getDateTimeFormat: Formatters['getDateTimeFormat'], ...[value, options]: Parameters<IntlFormatters['formatTime']>): string;
21
+ export declare function formatDateTimeRange(config: {
22
+ locale: string;
23
+ timeZone?: string;
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[];
38
+ //# sourceMappingURL=dateTime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dateTime.d.ts","sourceRoot":"","sources":["dateTime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAI5E,OAAO,EAAC,cAAc,EAAC,MAAM,4BAA4B,CAAA;AA0BzD,wBAAgB,YAAY,CAC1B,EACE,MAAM,EACN,OAAO,EACP,OAAO,EACP,QAAQ,GACT,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,iBAAiB,EAAE,UAAU,CAAC,mBAAmB,CAAC,EAClD,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAM,GACxD,cAAc,CA0BhB;AAED,wBAAgB,UAAU,CACxB,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,iBAAiB,EAAE,UAAU,CAAC,mBAAmB,C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IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,iBAAiB,EAAE,UAAU,CAAC,mBAAmB,CAAC,EAClD,GAAG,CAAC,KAAK,EAAE,OAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,GACxE,IAAI,CAAC,kBAAkB,EAAE,CAiB3B"}
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatTimeToParts = exports.formatDateToParts = exports.formatDateTimeRange = exports.formatTime = exports.formatDate = exports.getFormatter = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var utils_1 = require("./utils");
6
+ var error_1 = require("./error");
7
+ var DATE_TIME_FORMAT_OPTIONS = [
8
+ 'formatMatcher',
9
+ 'timeZone',
10
+ 'hour12',
11
+ 'weekday',
12
+ 'era',
13
+ 'year',
14
+ 'month',
15
+ 'day',
16
+ 'hour',
17
+ 'minute',
18
+ 'second',
19
+ 'timeZoneName',
20
+ 'hourCycle',
21
+ 'dateStyle',
22
+ 'timeStyle',
23
+ 'calendar',
24
+ // 'dayPeriod',
25
+ 'numberingSystem',
26
+ 'fractionalSecondDigits',
27
+ ];
28
+ function getFormatter(_a, type, getDateTimeFormat, options) {
29
+ var locale = _a.locale, formats = _a.formats, onError = _a.onError, timeZone = _a.timeZone;
30
+ if (options === void 0) { options = {}; }
31
+ var format = options.format;
32
+ var defaults = tslib_1.__assign(tslib_1.__assign({}, (timeZone && { timeZone: timeZone })), (format && (0, utils_1.getNamedFormat)(formats, type, format, onError)));
33
+ var filteredOptions = (0, utils_1.filterProps)(options, DATE_TIME_FORMAT_OPTIONS, defaults);
34
+ if (type === 'time' &&
35
+ !filteredOptions.hour &&
36
+ !filteredOptions.minute &&
37
+ !filteredOptions.second &&
38
+ !filteredOptions.timeStyle &&
39
+ !filteredOptions.dateStyle) {
40
+ // Add default formatting options if hour, minute, or second isn't defined.
41
+ filteredOptions = tslib_1.__assign(tslib_1.__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
42
+ }
43
+ return getDateTimeFormat(locale, filteredOptions);
44
+ }
45
+ exports.getFormatter = getFormatter;
46
+ function formatDate(config, getDateTimeFormat) {
47
+ var _a = [];
48
+ for (var _i = 2; _i < arguments.length; _i++) {
49
+ _a[_i - 2] = arguments[_i];
50
+ }
51
+ var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
52
+ var date = typeof value === 'string' ? new Date(value || 0) : value;
53
+ try {
54
+ return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
55
+ }
56
+ catch (e) {
57
+ config.onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting date.', e));
58
+ }
59
+ return String(date);
60
+ }
61
+ exports.formatDate = formatDate;
62
+ function formatTime(config, getDateTimeFormat) {
63
+ var _a = [];
64
+ for (var _i = 2; _i < arguments.length; _i++) {
65
+ _a[_i - 2] = arguments[_i];
66
+ }
67
+ var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
68
+ var date = typeof value === 'string' ? new Date(value || 0) : value;
69
+ try {
70
+ return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
71
+ }
72
+ catch (e) {
73
+ config.onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting time.', e));
74
+ }
75
+ return String(date);
76
+ }
77
+ exports.formatTime = formatTime;
78
+ function formatDateTimeRange(config, getDateTimeFormat) {
79
+ var _a = [];
80
+ for (var _i = 2; _i < arguments.length; _i++) {
81
+ _a[_i - 2] = arguments[_i];
82
+ }
83
+ var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b;
84
+ var timeZone = config.timeZone, locale = config.locale, onError = config.onError;
85
+ var filteredOptions = (0, utils_1.filterProps)(options, DATE_TIME_FORMAT_OPTIONS, timeZone ? { timeZone: timeZone } : {});
86
+ try {
87
+ return getDateTimeFormat(locale, filteredOptions).formatRange(from, to);
88
+ }
89
+ catch (e) {
90
+ onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting date time range.', e));
91
+ }
92
+ return String(from);
93
+ }
94
+ exports.formatDateTimeRange = formatDateTimeRange;
95
+ function formatDateToParts(config, getDateTimeFormat) {
96
+ var _a = [];
97
+ for (var _i = 2; _i < arguments.length; _i++) {
98
+ _a[_i - 2] = arguments[_i];
99
+ }
100
+ var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
101
+ var date = typeof value === 'string' ? new Date(value || 0) : value;
102
+ try {
103
+ return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date);
104
+ }
105
+ catch (e) {
106
+ config.onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting date.', e));
107
+ }
108
+ return [];
109
+ }
110
+ exports.formatDateToParts = formatDateToParts;
111
+ function formatTimeToParts(config, getDateTimeFormat) {
112
+ var _a = [];
113
+ for (var _i = 2; _i < arguments.length; _i++) {
114
+ _a[_i - 2] = arguments[_i];
115
+ }
116
+ var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
117
+ var date = typeof value === 'string' ? new Date(value || 0) : value;
118
+ try {
119
+ return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date);
120
+ }
121
+ catch (e) {
122
+ config.onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting time.', e));
123
+ }
124
+ return [];
125
+ }
126
+ exports.formatTimeToParts = formatTimeToParts;
@@ -0,0 +1,6 @@
1
+ import { Formatters, IntlFormatters, OnErrorFn } from './types';
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;
6
+ //# sourceMappingURL=displayName.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"displayName.d.ts","sourceRoot":"","sources":["displayName.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAe7D,wBAAgB,iBAAiB,CAC/B,EACE,MAAM,EACN,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,EACzD,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,MAAM,GAAG,SAAS,CA2BpB"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatDisplayName = void 0;
4
+ var utils_1 = require("./utils");
5
+ var intl_messageformat_1 = require("intl-messageformat");
6
+ var error_1 = require("./error");
7
+ var DISPLAY_NAMES_OPTONS = [
8
+ 'style',
9
+ 'type',
10
+ 'fallback',
11
+ ];
12
+ function formatDisplayName(_a, getDisplayNames, value, options) {
13
+ var locale = _a.locale, onError = _a.onError;
14
+ var DisplayNames = Intl.DisplayNames;
15
+ if (!DisplayNames) {
16
+ onError(new intl_messageformat_1.FormatError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n", intl_messageformat_1.ErrorCode.MISSING_INTL_API));
17
+ }
18
+ var filteredOptions = (0, utils_1.filterProps)(options, DISPLAY_NAMES_OPTONS);
19
+ try {
20
+ return getDisplayNames(locale, filteredOptions).of(value);
21
+ }
22
+ catch (e) {
23
+ onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting display name.', e));
24
+ }
25
+ }
26
+ exports.formatDisplayName = formatDisplayName;
package/src/error.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { MessageDescriptor } from './types';
2
+ export declare enum IntlErrorCode {
3
+ FORMAT_ERROR = "FORMAT_ERROR",
4
+ UNSUPPORTED_FORMATTER = "UNSUPPORTED_FORMATTER",
5
+ INVALID_CONFIG = "INVALID_CONFIG",
6
+ MISSING_DATA = "MISSING_DATA",
7
+ MISSING_TRANSLATION = "MISSING_TRANSLATION"
8
+ }
9
+ export declare class IntlError<T extends IntlErrorCode = IntlErrorCode.FORMAT_ERROR> extends Error {
10
+ readonly code: T;
11
+ constructor(code: T, message: string, exception?: Error | unknown);
12
+ }
13
+ export declare class UnsupportedFormatterError extends IntlError<IntlErrorCode.UNSUPPORTED_FORMATTER> {
14
+ constructor(message: string, exception?: Error | unknown);
15
+ }
16
+ export declare class InvalidConfigError extends IntlError<IntlErrorCode.INVALID_CONFIG> {
17
+ constructor(message: string, exception?: Error | unknown);
18
+ }
19
+ export declare class MissingDataError extends IntlError<IntlErrorCode.MISSING_DATA> {
20
+ constructor(message: string, exception?: Error | unknown);
21
+ }
22
+ export declare class IntlFormatError extends IntlError<IntlErrorCode.FORMAT_ERROR> {
23
+ readonly descriptor?: MessageDescriptor;
24
+ constructor(message: string, locale: string, exception?: Error | unknown);
25
+ }
26
+ export declare class MessageFormatError extends IntlFormatError {
27
+ readonly descriptor?: MessageDescriptor;
28
+ constructor(message: string, locale: string, descriptor?: MessageDescriptor, exception?: Error | unknown);
29
+ }
30
+ export declare class MissingTranslationError extends IntlError<IntlErrorCode.MISSING_TRANSLATION> {
31
+ readonly descriptor?: MessageDescriptor;
32
+ constructor(descriptor: MessageDescriptor, locale: string);
33
+ }
34
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,SAAS,CAAA;AAEzC,oBAAY,aAAa;IACvB,YAAY,iBAAiB;IAC7B,qBAAqB,0BAA0B;IAC/C,cAAc,mBAAmB;IACjC,YAAY,iBAA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EAAE,MAAM,EAAE,MAAM;CAiB1D"}
package/src/error.js ADDED
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MissingTranslationError = exports.MessageFormatError = exports.IntlFormatError = exports.MissingDataError = exports.InvalidConfigError = exports.UnsupportedFormatterError = exports.IntlError = exports.IntlErrorCode = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var IntlErrorCode;
6
+ (function (IntlErrorCode) {
7
+ IntlErrorCode["FORMAT_ERROR"] = "FORMAT_ERROR";
8
+ IntlErrorCode["UNSUPPORTED_FORMATTER"] = "UNSUPPORTED_FORMATTER";
9
+ IntlErrorCode["INVALID_CONFIG"] = "INVALID_CONFIG";
10
+ IntlErrorCode["MISSING_DATA"] = "MISSING_DATA";
11
+ IntlErrorCode["MISSING_TRANSLATION"] = "MISSING_TRANSLATION";
12
+ })(IntlErrorCode = exports.IntlErrorCode || (exports.IntlErrorCode = {}));
13
+ var IntlError = /** @class */ (function (_super) {
14
+ tslib_1.__extends(IntlError, _super);
15
+ function IntlError(code, message, exception) {
16
+ var _this = this;
17
+ var err = exception
18
+ ? exception instanceof Error
19
+ ? exception
20
+ : new Error(String(exception))
21
+ : undefined;
22
+ _this = _super.call(this, "[@formatjs/intl Error ".concat(code, "] ").concat(message, "\n").concat(err ? "\n".concat(err.message, "\n").concat(err.stack) : '')) || this;
23
+ _this.code = code;
24
+ // @ts-ignore just so we don't need to declare dep on @types/node
25
+ if (typeof Error.captureStackTrace === 'function') {
26
+ // @ts-ignore just so we don't need to declare dep on @types/node
27
+ Error.captureStackTrace(_this, IntlError);
28
+ }
29
+ return _this;
30
+ }
31
+ return IntlError;
32
+ }(Error));
33
+ exports.IntlError = IntlError;
34
+ var UnsupportedFormatterError = /** @class */ (function (_super) {
35
+ tslib_1.__extends(UnsupportedFormatterError, _super);
36
+ function UnsupportedFormatterError(message, exception) {
37
+ return _super.call(this, IntlErrorCode.UNSUPPORTED_FORMATTER, message, exception) || this;
38
+ }
39
+ return UnsupportedFormatterError;
40
+ }(IntlError));
41
+ exports.UnsupportedFormatterError = UnsupportedFormatterError;
42
+ var InvalidConfigError = /** @class */ (function (_super) {
43
+ tslib_1.__extends(InvalidConfigError, _super);
44
+ function InvalidConfigError(message, exception) {
45
+ return _super.call(this, IntlErrorCode.INVALID_CONFIG, message, exception) || this;
46
+ }
47
+ return InvalidConfigError;
48
+ }(IntlError));
49
+ exports.InvalidConfigError = InvalidConfigError;
50
+ var MissingDataError = /** @class */ (function (_super) {
51
+ tslib_1.__extends(MissingDataError, _super);
52
+ function MissingDataError(message, exception) {
53
+ return _super.call(this, IntlErrorCode.MISSING_DATA, message, exception) || this;
54
+ }
55
+ return MissingDataError;
56
+ }(IntlError));
57
+ exports.MissingDataError = MissingDataError;
58
+ var IntlFormatError = /** @class */ (function (_super) {
59
+ tslib_1.__extends(IntlFormatError, _super);
60
+ function IntlFormatError(message, locale, exception) {
61
+ return _super.call(this, IntlErrorCode.FORMAT_ERROR, "".concat(message, "\nLocale: ").concat(locale, "\n"), exception) || this;
62
+ }
63
+ return IntlFormatError;
64
+ }(IntlError));
65
+ exports.IntlFormatError = IntlFormatError;
66
+ var MessageFormatError = /** @class */ (function (_super) {
67
+ tslib_1.__extends(MessageFormatError, _super);
68
+ function MessageFormatError(message, locale, descriptor, exception) {
69
+ var _this = _super.call(this, "".concat(message, "\nMessageID: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.id, "\nDefault Message: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.defaultMessage, "\nDescription: ").concat(descriptor === null || descriptor === void 0 ? void 0 : descriptor.description, "\n"), locale, exception) || this;
70
+ _this.descriptor = descriptor;
71
+ return _this;
72
+ }
73
+ return MessageFormatError;
74
+ }(IntlFormatError));
75
+ exports.MessageFormatError = MessageFormatError;
76
+ var MissingTranslationError = /** @class */ (function (_super) {
77
+ tslib_1.__extends(MissingTranslationError, _super);
78
+ function MissingTranslationError(descriptor, locale) {
79
+ var _this = _super.call(this, IntlErrorCode.MISSING_TRANSLATION, "Missing message: \"".concat(descriptor.id, "\" for locale \"").concat(locale, "\", using ").concat(descriptor.defaultMessage
80
+ ? "default message (".concat(typeof descriptor.defaultMessage === 'string'
81
+ ? descriptor.defaultMessage
82
+ : descriptor.defaultMessage
83
+ .map(function (e) { var _a; return (_a = e.value) !== null && _a !== void 0 ? _a : JSON.stringify(e); })
84
+ .join(), ")")
85
+ : 'id', " as fallback.")) || this;
86
+ _this.descriptor = descriptor;
87
+ return _this;
88
+ }
89
+ return MissingTranslationError;
90
+ }(IntlError));
91
+ exports.MissingTranslationError = MissingTranslationError;
package/src/list.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { Formatters, IntlFormatters, OnErrorFn } from './types';
2
+ import type { Part } from '@formatjs/intl-listformat';
3
+ export declare function formatList(opts: {
4
+ locale: string;
5
+ onError: OnErrorFn;
6
+ }, getListFormat: Formatters['getListFormat'], values: ReadonlyArray<string>, options: Parameters<IntlFormatters['formatList']>[1]): string;
7
+ export declare function formatListToParts<T>(opts: {
8
+ locale: string;
9
+ onError: OnErrorFn;
10
+ }, getListFormat: Formatters['getListFormat'], values: ReadonlyArray<string>, options: Parameters<IntlFormatters['formatList']>[1]): Part[];
11
+ //# sourceMappingURL=list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list.d.ts","sourceRoot":"","sources":["list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AAG7D,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,2BAA2B,CAAA;AAgBnD,wBAAgB,UAAU,CACxB,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,aAAa,EAAE,UAAU,CAAC,eAAe,CAAC,EAC1C,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,MAAM,CAAA;AA6BT,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,aAAa,EAAE,UAAU,CAAC,eAAe,CAAC,EAC1C,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,IAAI,EAAE,CAAA"}
package/src/list.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatListToParts = exports.formatList = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var utils_1 = require("./utils");
6
+ var intl_messageformat_1 = require("intl-messageformat");
7
+ var error_1 = require("./error");
8
+ var LIST_FORMAT_OPTIONS = [
9
+ 'type',
10
+ 'style',
11
+ ];
12
+ var now = Date.now();
13
+ function generateToken(i) {
14
+ return "".concat(now, "_").concat(i, "_").concat(now);
15
+ }
16
+ function formatList(opts, getListFormat, values, options) {
17
+ if (options === void 0) { options = {}; }
18
+ var results = formatListToParts(opts, getListFormat, values, options).reduce(function (all, el) {
19
+ var val = el.value;
20
+ if (typeof val !== 'string') {
21
+ all.push(val);
22
+ }
23
+ else if (typeof all[all.length - 1] === 'string') {
24
+ all[all.length - 1] += val;
25
+ }
26
+ else {
27
+ all.push(val);
28
+ }
29
+ return all;
30
+ }, []);
31
+ return results.length === 1 ? results[0] : results;
32
+ }
33
+ exports.formatList = formatList;
34
+ function formatListToParts(_a, getListFormat, values, options) {
35
+ var locale = _a.locale, onError = _a.onError;
36
+ if (options === void 0) { options = {}; }
37
+ var ListFormat = Intl.ListFormat;
38
+ if (!ListFormat) {
39
+ onError(new intl_messageformat_1.FormatError("Intl.ListFormat is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-listformat\"\n", intl_messageformat_1.ErrorCode.MISSING_INTL_API));
40
+ }
41
+ var filteredOptions = (0, utils_1.filterProps)(options, LIST_FORMAT_OPTIONS);
42
+ try {
43
+ var richValues_1 = {};
44
+ var serializedValues = values.map(function (v, i) {
45
+ if (typeof v === 'object') {
46
+ var id = generateToken(i);
47
+ richValues_1[id] = v;
48
+ return id;
49
+ }
50
+ return String(v);
51
+ });
52
+ return getListFormat(locale, filteredOptions)
53
+ .formatToParts(serializedValues)
54
+ .map(function (part) {
55
+ return part.type === 'literal'
56
+ ? part
57
+ : tslib_1.__assign(tslib_1.__assign({}, part), { value: richValues_1[part.value] || part.value });
58
+ });
59
+ }
60
+ catch (e) {
61
+ onError(new error_1.IntlError(error_1.IntlErrorCode.FORMAT_ERROR, 'Error formatting list.', e));
62
+ }
63
+ // @ts-ignore
64
+ return values;
65
+ }
66
+ exports.formatListToParts = formatListToParts;
@@ -0,0 +1,16 @@
1
+ import { Formatters, MessageDescriptor, CustomFormats, OnErrorFn } from './types';
2
+ import { FormatXMLElementFn, PrimitiveType, Formatters as IntlMessageFormatFormatters, Options } from 'intl-messageformat';
3
+ import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
4
+ export declare type FormatMessageFn<T> = ({ locale, formats, messages, defaultLocale, defaultFormats, fallbackOnEmptyString, onError, timeZone, defaultRichTextElements, }: {
5
+ locale: string;
6
+ timeZone?: string;
7
+ formats: CustomFormats;
8
+ messages: Record<string, string> | Record<string, MessageFormatElement[]>;
9
+ defaultLocale: string;
10
+ defaultFormats: CustomFormats;
11
+ defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
12
+ fallbackOnEmptyString?: boolean;
13
+ onError: OnErrorFn;
14
+ }, state: IntlMessageFormatFormatters & Pick<Formatters, 'getMessageFormat'>, messageDescriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: Options) => T extends string ? string : Array<T | string> | string | T;
15
+ export declare const formatMessage: F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GAAG,KAAK,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,CAAA;AAE/D,eAAO,MAAM,aAAa,EAAE,eAAe,CAAC,GAAG,CAuJ9C,CAAA"}
package/src/message.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatMessage = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var ecma402_abstract_1 = require("@formatjs/ecma402-abstract");
6
+ var intl_messageformat_1 = require("intl-messageformat");
7
+ var error_1 = require("./error");
8
+ var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
9
+ function setTimeZoneInOptions(opts, timeZone) {
10
+ return Object.keys(opts).reduce(function (all, k) {
11
+ all[k] = tslib_1.__assign({ timeZone: timeZone }, opts[k]);
12
+ return all;
13
+ }, {});
14
+ }
15
+ function deepMergeOptions(opts1, opts2) {
16
+ var keys = Object.keys(tslib_1.__assign(tslib_1.__assign({}, opts1), opts2));
17
+ return keys.reduce(function (all, k) {
18
+ all[k] = tslib_1.__assign(tslib_1.__assign({}, (opts1[k] || {})), (opts2[k] || {}));
19
+ return all;
20
+ }, {});
21
+ }
22
+ function deepMergeFormatsAndSetTimeZone(f1, timeZone) {
23
+ if (!timeZone) {
24
+ return f1;
25
+ }
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)) });
28
+ }
29
+ var formatMessage = function (_a, state, messageDescriptor, values, opts) {
30
+ 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
+ if (messageDescriptor === void 0) { messageDescriptor = { id: '' }; }
32
+ var msgId = messageDescriptor.id, defaultMessage = messageDescriptor.defaultMessage;
33
+ // `id` is a required field of a Message Descriptor.
34
+ (0, ecma402_abstract_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.io/docs/tooling/babel-plugin)\nor [@formatjs/ts-transformer](https://formatjs.io/docs/tooling/ts-transformer) OR\n2. Configure your `eslint` config to include [eslint-plugin-formatjs](https://formatjs.io/docs/tooling/linter#enforce-id)\nto autofix this issue");
35
+ var id = String(msgId);
36
+ var message =
37
+ // In case messages is Object.create(null)
38
+ // e.g import('foo.json') from webpack)
39
+ // See https://github.com/formatjs/formatjs/issues/1914
40
+ messages &&
41
+ Object.prototype.hasOwnProperty.call(messages, id) &&
42
+ messages[id];
43
+ // IMPORTANT: Hot path if `message` is AST with a single literal node
44
+ if (Array.isArray(message) &&
45
+ message.length === 1 &&
46
+ message[0].type === icu_messageformat_parser_1.TYPE.literal) {
47
+ return message[0].value;
48
+ }
49
+ // IMPORTANT: Hot path straight lookup for performance
50
+ if (!values &&
51
+ message &&
52
+ typeof message === 'string' &&
53
+ !defaultRichTextElements) {
54
+ return message.replace(/'\{(.*?)\}'/gi, "{$1}");
55
+ }
56
+ values = tslib_1.__assign(tslib_1.__assign({}, defaultRichTextElements), (values || {}));
57
+ formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
58
+ defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
59
+ if (!message) {
60
+ if (fallbackOnEmptyString === false && message === '') {
61
+ return message;
62
+ }
63
+ if (!defaultMessage ||
64
+ (locale && locale.toLowerCase() !== defaultLocale.toLowerCase())) {
65
+ // This prevents warnings from littering the console in development
66
+ // when no `messages` are passed into the <IntlProvider> for the
67
+ // default locale.
68
+ onError(new error_1.MissingTranslationError(messageDescriptor, locale));
69
+ }
70
+ if (defaultMessage) {
71
+ try {
72
+ var formatter = state.getMessageFormat(defaultMessage, defaultLocale, defaultFormats, opts);
73
+ return formatter.format(values);
74
+ }
75
+ catch (e) {
76
+ onError(new error_1.MessageFormatError("Error formatting default message for: \"".concat(id, "\", rendering default message verbatim"), locale, messageDescriptor, e));
77
+ return typeof defaultMessage === 'string' ? defaultMessage : id;
78
+ }
79
+ }
80
+ return id;
81
+ }
82
+ // We have the translated message
83
+ try {
84
+ var formatter = state.getMessageFormat(message, locale, formats, tslib_1.__assign({ formatters: state }, (opts || {})));
85
+ return formatter.format(values);
86
+ }
87
+ catch (e) {
88
+ onError(new error_1.MessageFormatError("Error formatting message: \"".concat(id, "\", using ").concat(defaultMessage ? 'default message' : 'id', " as fallback."), locale, messageDescriptor, e));
89
+ }
90
+ if (defaultMessage) {
91
+ try {
92
+ var formatter = state.getMessageFormat(defaultMessage, defaultLocale, defaultFormats, opts);
93
+ return formatter.format(values);
94
+ }
95
+ catch (e) {
96
+ onError(new error_1.MessageFormatError("Error formatting the default message for: \"".concat(id, "\", rendering message verbatim"), locale, messageDescriptor, e));
97
+ }
98
+ }
99
+ if (typeof message === 'string') {
100
+ return message;
101
+ }
102
+ if (typeof defaultMessage === 'string') {
103
+ return defaultMessage;
104
+ }
105
+ return id;
106
+ };
107
+ exports.formatMessage = formatMessage;
@@ -0,0 +1,17 @@
1
+ import { Formatters, IntlFormatters, CustomFormats, OnErrorFn } from './types';
2
+ export declare function getFormatter({ locale, formats, onError, }: {
3
+ locale: string;
4
+ formats: CustomFormats;
5
+ onError: OnErrorFn;
6
+ }, getNumberFormat: Formatters['getNumberFormat'], options?: Parameters<IntlFormatters['formatNumber']>[1]): Intl.NumberFormat;
7
+ export declare function formatNumber(config: {
8
+ locale: string;
9
+ formats: CustomFormats;
10
+ onError: OnErrorFn;
11
+ }, getNumberFormat: Formatters['getNumberFormat'], value: Parameters<IntlFormatters['formatNumber']>[0], options?: Parameters<IntlFormatters['formatNumber']>[1]): string;
12
+ export declare function formatNumberToParts(config: {
13
+ locale: string;
14
+ formats: CustomFormats;
15
+ onError: OnErrorFn;
16
+ }, getNumberFormat: Formatters['getNumberFormat'], value: Parameters<IntlFormatters['formatNumber']>[0], options?: Parameters<IntlFormatters['formatNumber']>[1]): Intl.NumberFormatPart[];
17
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,SAAS,CAAA;AA8B5E,wBAAgB,YAAY,CAC1B,EACE,MAAM,EACN,OAAO,EACP,OAAO,GACR,EAAE;IACD,MAAM,EAAE,MAAM,CAAA;IAEd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,IAAI,CAAC,YAAY,CAYnB;AAED,wBAAgB,YAAY,CAC1B,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IAEd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,EAAE,UAAU,CAAC,iBAAiB,CAAC,EAC9C,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,OAAO,GAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAM,GAC1D,MAAM,CAUR;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,aAAa,CAAA;IACtB,OAAO,EAAE,SAAS,CAAA;CACnB,EACD,eAAe,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