react-intl 3.9.1 → 3.11.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 (46) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/components/createFormattedComponent.js +4 -2
  3. package/dist/components/html-message.js +1 -1
  4. package/dist/components/injectIntl.d.ts +1 -1
  5. package/dist/components/message.js +13 -4
  6. package/dist/components/plural.d.ts +1 -1
  7. package/dist/components/provider.js +2 -2
  8. package/dist/components/relative.js +3 -10
  9. package/dist/formatters/dateTime.js +2 -2
  10. package/dist/formatters/list.d.ts +1 -2
  11. package/dist/formatters/message.d.ts +1 -0
  12. package/dist/formatters/message.js +19 -18
  13. package/dist/index.d.ts +2 -2
  14. package/dist/react-intl.api.md +7 -5
  15. package/dist/react-intl.d.ts +30 -9
  16. package/dist/react-intl.js +563 -386
  17. package/dist/react-intl.js.map +1 -1
  18. package/dist/react-intl.min.js +1 -1
  19. package/dist/react-intl.min.js.map +1 -1
  20. package/dist/types.d.ts +2 -1
  21. package/dist/utils.d.ts +1 -1
  22. package/dist/utils.js +2 -7
  23. package/lib/components/createFormattedComponent.js +4 -2
  24. package/lib/components/html-message.js +1 -1
  25. package/lib/components/injectIntl.d.ts +1 -1
  26. package/lib/components/message.js +5 -3
  27. package/lib/components/plural.d.ts +1 -1
  28. package/lib/components/provider.js +2 -2
  29. package/lib/components/relative.js +3 -10
  30. package/lib/formatters/dateTime.js +2 -2
  31. package/lib/formatters/list.d.ts +1 -2
  32. package/lib/formatters/message.d.ts +1 -0
  33. package/lib/formatters/message.js +12 -18
  34. package/lib/index.d.ts +2 -2
  35. package/lib/react-intl.d.ts +6 -5
  36. package/lib/tsdoc-metadata.json +1 -1
  37. package/lib/types.d.ts +2 -1
  38. package/lib/utils.d.ts +1 -1
  39. package/lib/utils.js +1 -6
  40. package/package.json +31 -31
  41. package/src/components/provider.tsx +1 -1
  42. package/src/components/relative.tsx +6 -15
  43. package/src/formatters/list.ts +7 -1
  44. package/src/formatters/message.ts +13 -18
  45. package/src/types.ts +5 -1
  46. package/src/utils.ts +2 -7
@@ -5,12 +5,7 @@
5
5
  */
6
6
 
7
7
  import * as React from 'react';
8
- // Since rollup cannot deal with namespace being a function,
9
- // this is to interop with TypeScript since `invariant`
10
- // does not export a default
11
- // https://github.com/rollup/rollup/issues/1267
12
- import * as invariant_ from 'invariant';
13
- const invariant: typeof invariant_ = (invariant_ as any).default || invariant_;
8
+ import {invariant} from '@formatjs/intl-utils';
14
9
 
15
10
  import {
16
11
  Formatters,
@@ -77,6 +72,10 @@ function deepMergeFormatsAndSetTimeZone(
77
72
  };
78
73
  }
79
74
 
75
+ export const prepareIntlMessageFormatHtmlOutput = (
76
+ chunks: (string | object)[]
77
+ ): React.ReactElement => React.createElement(React.Fragment, null, ...chunks);
78
+
80
79
  export function formatMessage(
81
80
  {
82
81
  locale,
@@ -123,17 +122,12 @@ export function formatMessage(
123
122
  string,
124
123
  PrimitiveType | React.ReactElement | FormatXMLElementFn
125
124
  > = {}
126
- ): string | React.ReactNodeArray {
125
+ ): string | React.ReactNodeArray | React.ReactElement {
127
126
  const {id, defaultMessage} = messageDescriptor;
128
127
 
129
128
  // `id` is a required field of a Message Descriptor.
130
- invariant(id, '[React Intl] An `id` must be provided to format a message.');
131
- if (!id) {
132
- throw new Error(
133
- '[React Intl] An `id` must be provided to format a message.'
134
- );
135
- }
136
- const message = messages && messages[id];
129
+ invariant(!!id, '[React Intl] An `id` must be provided to format a message.');
130
+ const message = messages && messages[String(id)];
137
131
  formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
138
132
  defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
139
133
 
@@ -198,17 +192,18 @@ export function formatMessage(
198
192
  )
199
193
  );
200
194
  if (typeof message === 'string') {
201
- return message || defaultMessage || id;
195
+ return message || defaultMessage || String(id);
202
196
  }
203
- return defaultMessage || id;
197
+ return defaultMessage || String(id);
204
198
  }
205
199
  if (
206
200
  formattedMessageParts.length === 1 &&
207
201
  typeof formattedMessageParts[0] === 'string'
208
202
  ) {
209
- return (formattedMessageParts[0] as string) || defaultMessage || id;
203
+ return (formattedMessageParts[0] as string) || defaultMessage || String(id);
210
204
  }
211
- return formattedMessageParts;
205
+
206
+ return prepareIntlMessageFormatHtmlOutput(formattedMessageParts);
212
207
  }
213
208
 
214
209
  export function formatHTMLMessage(
package/src/types.ts CHANGED
@@ -107,6 +107,10 @@ export interface IntlFormatters {
107
107
  descriptor: MessageDescriptor,
108
108
  values?: Record<string, PrimitiveType>
109
109
  ): React.ReactNode;
110
+ formatList(
111
+ values: Array<string>,
112
+ opts?: FormatListOptions
113
+ ): string;
110
114
  formatList(
111
115
  values: Array<string | React.ReactNode>,
112
116
  opts?: FormatListOptions
@@ -148,7 +152,7 @@ export interface IntlCache {
148
152
  }
149
153
 
150
154
  export interface MessageDescriptor {
151
- id?: string;
155
+ id?: string | number;
152
156
  description?: string | object;
153
157
  defaultMessage?: string;
154
158
  }
package/src/utils.ts CHANGED
@@ -13,13 +13,8 @@ import {IntlConfig, IntlCache, CustomFormats, Formatters} from './types';
13
13
  import * as React from 'react';
14
14
  import IntlMessageFormat from 'intl-messageformat';
15
15
  import memoizeIntlConstructor from 'intl-format-cache';
16
- // Since rollup cannot deal with namespace being a function,
17
- // this is to interop with TypeScript since `invariant`
18
- // does not export a default
19
- // https://github.com/rollup/rollup/issues/1267
20
- import * as invariant_ from 'invariant';
16
+ import {invariant} from '@formatjs/intl-utils'
21
17
  import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat';
22
- const invariant: typeof invariant_ = (invariant_ as any).default || invariant_;
23
18
 
24
19
  const ESCAPED_CHARS: Record<number, string> = {
25
20
  38: '&amp;',
@@ -54,7 +49,7 @@ export function filterProps<T extends Record<string, any>, K extends string>(
54
49
  }, {} as Pick<T, K>);
55
50
  }
56
51
 
57
- export function invariantIntlContext(intl?: any): void {
52
+ export function invariantIntlContext(intl?: any): asserts intl {
58
53
  invariant(
59
54
  intl,
60
55
  '[React Intl] Could not find required `intl` object. ' +