react-intl 4.2.2 → 4.5.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 (52) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +1 -1
  3. package/dist/components/createFormattedComponent.js +3 -1
  4. package/dist/components/injectIntl.js +2 -3
  5. package/dist/components/message.d.ts +1 -1
  6. package/dist/error.d.ts +4 -2
  7. package/dist/error.js +2 -1
  8. package/dist/formatters/message.js +4 -4
  9. package/dist/formatters/relativeTime.js +1 -4
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +4 -0
  12. package/dist/react-intl.api.md +42 -35
  13. package/dist/react-intl.d.ts +72 -59
  14. package/dist/react-intl.js +74 -31
  15. package/dist/react-intl.js.map +1 -1
  16. package/dist/react-intl.min.js +1 -1
  17. package/dist/react-intl.min.js.map +1 -1
  18. package/dist/types.d.ts +2 -2
  19. package/lib/components/createFormattedComponent.js +3 -1
  20. package/lib/components/injectIntl.js +2 -3
  21. package/lib/components/message.d.ts +1 -1
  22. package/lib/error.d.ts +4 -2
  23. package/lib/error.js +2 -1
  24. package/lib/formatters/message.js +4 -4
  25. package/lib/formatters/relativeTime.js +1 -4
  26. package/lib/index.d.ts +1 -0
  27. package/lib/index.js +3 -0
  28. package/lib/react-intl.d.ts +51 -47
  29. package/lib/tsdoc-metadata.json +1 -1
  30. package/lib/types.d.ts +2 -2
  31. package/package.json +32 -39
  32. package/src/components/createFormattedComponent.tsx +50 -48
  33. package/src/components/injectIntl.tsx +33 -34
  34. package/src/components/message.tsx +33 -32
  35. package/src/components/plural.tsx +22 -22
  36. package/src/components/provider.tsx +36 -36
  37. package/src/components/relative.tsx +70 -70
  38. package/src/components/useIntl.ts +7 -7
  39. package/src/error.ts +26 -13
  40. package/src/formatters/dateTime.ts +25 -33
  41. package/src/formatters/displayName.ts +11 -11
  42. package/src/formatters/list.ts +29 -29
  43. package/src/formatters/message.ts +47 -50
  44. package/src/formatters/number.ts +15 -15
  45. package/src/formatters/plural.ts +10 -10
  46. package/src/formatters/relativeTime.ts +18 -19
  47. package/src/index.ts +49 -36
  48. package/src/tsconfig.cjs.json +8 -8
  49. package/src/tsconfig.json +7 -7
  50. package/src/types.ts +59 -59
  51. package/src/utils.ts +29 -24
  52. package/src/vendor.d.ts +1 -1
package/src/index.ts CHANGED
@@ -3,56 +3,69 @@
3
3
  * Copyrights licensed under the New BSD License.
4
4
  * See the accompanying LICENSE file for terms.
5
5
  */
6
- import * as React from 'react';
7
- export * from './types';
6
+ import * as React from 'react'
7
+ export * from './types'
8
8
  export function defineMessages<T, U extends Record<string, T>>(msgs: U): U {
9
- return msgs;
9
+ return msgs
10
+ }
11
+ export function defineMessage<T>(msg: T): T {
12
+ return msg
10
13
  }
11
14
  import {
12
15
  createFormattedComponent,
13
16
  createFormattedDateTimePartsComponent,
14
- } from './components/createFormattedComponent';
15
- import {CustomFormatConfig} from './types';
16
- import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
17
- import {IntlListFormatOptions} from '@formatjs/intl-listformat';
18
- import {DisplayNamesOptions} from '@formatjs/intl-displaynames/lib';
17
+ } from './components/createFormattedComponent'
18
+ import {CustomFormatConfig} from './types'
19
+ import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat'
20
+ import {IntlListFormatOptions} from '@formatjs/intl-listformat'
21
+ import {DisplayNamesOptions} from '@formatjs/intl-displaynames/lib'
19
22
  export {
20
23
  default as injectIntl,
21
24
  Provider as RawIntlProvider,
22
25
  Context as IntlContext,
23
26
  WithIntlProps,
24
27
  WrappedComponentProps,
25
- } from './components/injectIntl';
26
- export {default as useIntl} from './components/useIntl';
27
- export {default as IntlProvider, createIntl} from './components/provider';
28
+ } from './components/injectIntl'
29
+ export {default as useIntl} from './components/useIntl'
30
+ export {default as IntlProvider, createIntl} from './components/provider'
28
31
  // IMPORTANT: Explicit here to prevent api-extractor from outputing `import('./types').CustomFormatConfig`
29
- export const FormattedDate: React.FC<Intl.DateTimeFormatOptions &
30
- CustomFormatConfig & {
31
- value: string | number | Date | undefined;
32
- }> = createFormattedComponent('formatDate');
33
- export const FormattedTime: React.FC<Intl.DateTimeFormatOptions &
34
- CustomFormatConfig & {
35
- value: string | number | Date | undefined;
36
- }> = createFormattedComponent('formatTime');
37
- export const FormattedNumber: React.FC<UnifiedNumberFormatOptions &
38
- CustomFormatConfig & {
39
- value: number;
40
- }> = createFormattedComponent('formatNumber');
41
- export const FormattedList: React.FC<IntlListFormatOptions & {
42
- value: React.ReactNode[];
43
- }> = createFormattedComponent('formatList');
44
- export const FormattedDisplayName: React.FC<DisplayNamesOptions & {
45
- value: string | number | object;
46
- }> = createFormattedComponent('formatDisplayName');
32
+ export const FormattedDate: React.FC<
33
+ Intl.DateTimeFormatOptions &
34
+ CustomFormatConfig & {
35
+ value: string | number | Date | undefined
36
+ }
37
+ > = createFormattedComponent('formatDate')
38
+ export const FormattedTime: React.FC<
39
+ Intl.DateTimeFormatOptions &
40
+ CustomFormatConfig & {
41
+ value: string | number | Date | undefined
42
+ }
43
+ > = createFormattedComponent('formatTime')
44
+ export const FormattedNumber: React.FC<
45
+ UnifiedNumberFormatOptions &
46
+ CustomFormatConfig & {
47
+ value: number
48
+ }
49
+ > = createFormattedComponent('formatNumber')
50
+ export const FormattedList: React.FC<
51
+ IntlListFormatOptions & {
52
+ value: React.ReactNode[]
53
+ }
54
+ > = createFormattedComponent('formatList')
55
+ export const FormattedDisplayName: React.FC<
56
+ DisplayNamesOptions & {
57
+ value: string | number | object
58
+ }
59
+ > = createFormattedComponent('formatDisplayName')
47
60
  export const FormattedDateParts = createFormattedDateTimePartsComponent(
48
61
  'formatDate'
49
- );
62
+ )
50
63
  export const FormattedTimeParts = createFormattedDateTimePartsComponent(
51
64
  'formatTime'
52
- );
53
- export {FormattedNumberParts} from './components/createFormattedComponent';
54
- export {default as FormattedRelativeTime} from './components/relative';
55
- export {default as FormattedPlural} from './components/plural';
56
- export {default as FormattedMessage} from './components/message';
57
- export {createIntlCache} from './utils';
65
+ )
66
+ export {FormattedNumberParts} from './components/createFormattedComponent'
67
+ export {default as FormattedRelativeTime} from './components/relative'
68
+ export {default as FormattedPlural} from './components/plural'
69
+ export {default as FormattedMessage} from './components/message'
70
+ export {createIntlCache} from './utils'
58
71
  export {ReactIntlError, ReactIntlErrorCode} from './error'
@@ -1,8 +1,8 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../dist",
5
- "module": "commonjs",
6
- "target": "es5",
7
- }
8
- }
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../dist",
5
+ "module": "commonjs",
6
+ "target": "es5"
7
+ }
8
+ }
package/src/tsconfig.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
- "extends": "../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../lib",
5
- "baseUrl": ".",
6
- "lib": ["dom", "es2015", "esnext.intl", "es2018.intl", "es2017.intl"]
7
- }
8
- }
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../lib",
5
+ "baseUrl": ".",
6
+ "lib": ["dom", "es2015", "esnext.intl", "es2018.intl", "es2017.intl"]
7
+ }
8
+ }
package/src/types.ts CHANGED
@@ -3,168 +3,168 @@
3
3
  * Copyrights licensed under the New BSD License.
4
4
  * See the accompanying LICENSE file for terms.
5
5
  */
6
- import * as React from 'react';
6
+ import * as React from 'react'
7
7
  import IntlMessageFormat, {
8
8
  Formats,
9
9
  PrimitiveType,
10
10
  FormatXMLElementFn,
11
11
  FormatError,
12
- } from 'intl-messageformat';
12
+ } from 'intl-messageformat'
13
13
  import IntlRelativeTimeFormat, {
14
14
  IntlRelativeTimeFormatOptions,
15
- } from '@formatjs/intl-relativetimeformat';
16
- import {MessageFormatElement} from 'intl-messageformat-parser';
17
- import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
18
- import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
19
- import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames';
20
- import { ReactIntlError } from './error';
15
+ } from '@formatjs/intl-relativetimeformat'
16
+ import {MessageFormatElement} from 'intl-messageformat-parser'
17
+ import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat'
18
+ import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat'
19
+ import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames'
20
+ import {ReactIntlError} from './error'
21
21
 
22
22
  export interface IntlConfig {
23
- locale: string;
24
- timeZone?: string;
25
- formats: CustomFormats;
26
- textComponent?: React.ComponentType | keyof React.ReactHTML;
27
- messages: Record<string, string> | Record<string, MessageFormatElement[]>;
28
- defaultLocale: string;
29
- defaultFormats: CustomFormats;
30
- wrapRichTextChunksInFragment?: boolean;
31
- onError(err: ReactIntlError | FormatError): void;
23
+ locale: string
24
+ timeZone?: string
25
+ formats: CustomFormats
26
+ textComponent?: React.ComponentType | keyof React.ReactHTML
27
+ messages: Record<string, string> | Record<string, MessageFormatElement[]>
28
+ defaultLocale: string
29
+ defaultFormats: CustomFormats
30
+ wrapRichTextChunksInFragment?: boolean
31
+ onError(err: ReactIntlError | FormatError): void
32
32
  }
33
33
 
34
34
  export interface CustomFormats extends Partial<Formats> {
35
- relative?: Record<string, IntlRelativeTimeFormatOptions>;
35
+ relative?: Record<string, IntlRelativeTimeFormatOptions>
36
36
  }
37
37
 
38
38
  export interface CustomFormatConfig {
39
- format?: string;
39
+ format?: string
40
40
  }
41
41
 
42
42
  export type FormatDateOptions = Exclude<
43
43
  Intl.DateTimeFormatOptions,
44
44
  'localeMatcher'
45
45
  > &
46
- CustomFormatConfig;
46
+ CustomFormatConfig
47
47
  export type FormatNumberOptions = Exclude<
48
48
  UnifiedNumberFormatOptions,
49
49
  'localeMatcher'
50
50
  > &
51
- CustomFormatConfig;
51
+ CustomFormatConfig
52
52
  export type FormatRelativeTimeOptions = Exclude<
53
53
  IntlRelativeTimeFormatOptions,
54
54
  'localeMatcher'
55
55
  > &
56
- CustomFormatConfig;
56
+ CustomFormatConfig
57
57
  export type FormatPluralOptions = Exclude<
58
58
  Intl.PluralRulesOptions,
59
59
  'localeMatcher'
60
60
  > &
61
- CustomFormatConfig;
61
+ CustomFormatConfig
62
62
 
63
- export type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
63
+ export type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>
64
64
 
65
65
  export type FormatDisplayNameOptions = Exclude<
66
66
  DisplayNamesOptions,
67
67
  'localeMatcher'
68
- >;
68
+ >
69
69
 
70
- export interface IntlFormatters<T = React.ReactNode> {
70
+ export interface IntlFormatters<T = React.ReactNode, R = T> {
71
71
  formatDate(
72
72
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
73
73
  opts?: FormatDateOptions
74
- ): string;
74
+ ): string
75
75
  formatTime(
76
76
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
77
77
  opts?: FormatDateOptions
78
- ): string;
78
+ ): string
79
79
  formatDateToParts(
80
80
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
81
81
  opts?: FormatDateOptions
82
- ): Intl.DateTimeFormatPart[];
82
+ ): Intl.DateTimeFormatPart[]
83
83
  formatTimeToParts(
84
84
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
85
85
  opts?: FormatDateOptions
86
- ): Intl.DateTimeFormatPart[];
86
+ ): Intl.DateTimeFormatPart[]
87
87
  formatRelativeTime(
88
88
  value: Parameters<IntlRelativeTimeFormat['format']>[0],
89
89
  unit?: Parameters<IntlRelativeTimeFormat['format']>[1],
90
90
  opts?: FormatRelativeTimeOptions
91
- ): string;
91
+ ): string
92
92
  formatNumber(
93
93
  value: Parameters<Intl.NumberFormat['format']>[0],
94
94
  opts?: FormatNumberOptions
95
- ): string;
95
+ ): string
96
96
  formatNumberToParts(
97
97
  value: Parameters<Intl.NumberFormat['format']>[0],
98
98
  opts?: FormatNumberOptions
99
- ): Intl.NumberFormatPart[];
99
+ ): Intl.NumberFormatPart[]
100
100
  formatPlural(
101
101
  value: Parameters<Intl.PluralRules['select']>[0],
102
102
  opts?: FormatPluralOptions
103
- ): ReturnType<Intl.PluralRules['select']>;
103
+ ): ReturnType<Intl.PluralRules['select']>
104
104
  formatMessage(
105
105
  descriptor: MessageDescriptor,
106
106
  values?: Record<string, PrimitiveType>
107
- ): string;
107
+ ): string
108
108
  formatMessage(
109
109
  descriptor: MessageDescriptor,
110
110
  values?: Record<
111
111
  string,
112
- PrimitiveType | React.ReactElement | FormatXMLElementFn<T>
112
+ PrimitiveType | React.ReactElement | FormatXMLElementFn<T, R>
113
113
  >
114
- ): string | React.ReactNodeArray;
115
- formatList(values: Array<string>, opts?: FormatListOptions): string;
114
+ ): string | React.ReactNodeArray
115
+ formatList(values: Array<string>, opts?: FormatListOptions): string
116
116
  formatList(
117
117
  values: Array<string | React.ReactNode>,
118
118
  opts?: FormatListOptions
119
- ): React.ReactNode;
119
+ ): React.ReactNode
120
120
  formatDisplayName(
121
121
  value: Parameters<DisplayNames['of']>[0],
122
122
  opts?: FormatDisplayNameOptions
123
- ): string | undefined;
123
+ ): string | undefined
124
124
  }
125
125
 
126
126
  export interface Formatters {
127
127
  getDateTimeFormat(
128
128
  ...args: ConstructorParameters<typeof Intl.DateTimeFormat>
129
- ): Intl.DateTimeFormat;
129
+ ): Intl.DateTimeFormat
130
130
  getNumberFormat(
131
131
  ...args: ConstructorParameters<typeof Intl.NumberFormat>
132
- ): Intl.NumberFormat;
132
+ ): Intl.NumberFormat
133
133
  getMessageFormat(
134
134
  ...args: ConstructorParameters<typeof IntlMessageFormat>
135
- ): IntlMessageFormat;
135
+ ): IntlMessageFormat
136
136
  getRelativeTimeFormat(
137
137
  ...args: ConstructorParameters<typeof IntlRelativeTimeFormat>
138
- ): IntlRelativeTimeFormat;
138
+ ): IntlRelativeTimeFormat
139
139
  getPluralRules(
140
140
  ...args: ConstructorParameters<typeof Intl.PluralRules>
141
- ): Intl.PluralRules;
141
+ ): Intl.PluralRules
142
142
  getListFormat(
143
143
  ...args: ConstructorParameters<typeof IntlListFormat>
144
- ): IntlListFormat;
144
+ ): IntlListFormat
145
145
  getDisplayNames(
146
146
  ...args: ConstructorParameters<typeof DisplayNames>
147
- ): DisplayNames;
147
+ ): DisplayNames
148
148
  }
149
149
 
150
150
  export interface IntlShape extends IntlConfig, IntlFormatters {
151
- formatters: Formatters;
151
+ formatters: Formatters
152
152
  }
153
153
 
154
154
  export interface IntlCache {
155
- dateTime: Record<string, Intl.DateTimeFormat>;
156
- number: Record<string, Intl.NumberFormat>;
157
- message: Record<string, IntlMessageFormat>;
158
- relativeTime: Record<string, IntlRelativeTimeFormat>;
159
- pluralRules: Record<string, Intl.PluralRules>;
160
- list: Record<string, IntlListFormat>;
161
- displayNames: Record<string, DisplayNames>;
155
+ dateTime: Record<string, Intl.DateTimeFormat>
156
+ number: Record<string, Intl.NumberFormat>
157
+ message: Record<string, IntlMessageFormat>
158
+ relativeTime: Record<string, IntlRelativeTimeFormat>
159
+ pluralRules: Record<string, Intl.PluralRules>
160
+ list: Record<string, IntlListFormat>
161
+ displayNames: Record<string, DisplayNames>
162
162
  }
163
163
 
164
164
  export interface MessageDescriptor {
165
- id?: string | number;
166
- description?: string | object;
167
- defaultMessage?: string;
165
+ id?: string | number
166
+ description?: string | object
167
+ defaultMessage?: string
168
168
  }
169
169
 
170
- export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
170
+ export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>
package/src/utils.ts CHANGED
@@ -9,13 +9,13 @@ This source code is licensed under the BSD-style license found in the LICENSE
9
9
  file in the root directory of React's source tree.
10
10
  */
11
11
 
12
- import {IntlConfig, IntlCache, CustomFormats, Formatters} from './types';
13
- import * as React from 'react';
14
- import IntlMessageFormat from 'intl-messageformat';
15
- import memoizeIntlConstructor from 'intl-format-cache';
16
- import {invariant} from '@formatjs/intl-utils';
17
- import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat';
18
- import { ReactIntlError, ReactIntlErrorCode } from './error';
12
+ import {IntlConfig, IntlCache, CustomFormats, Formatters} from './types'
13
+ import * as React from 'react'
14
+ import IntlMessageFormat from 'intl-messageformat'
15
+ import memoizeIntlConstructor from 'intl-format-cache'
16
+ import {invariant} from '@formatjs/intl-utils'
17
+ import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat'
18
+ import {ReactIntlError, ReactIntlErrorCode} from './error'
19
19
 
20
20
  export function filterProps<T extends Record<string, any>, K extends string>(
21
21
  props: T,
@@ -24,13 +24,13 @@ export function filterProps<T extends Record<string, any>, K extends string>(
24
24
  ): Pick<T, K> {
25
25
  return whitelist.reduce((filtered, name) => {
26
26
  if (name in props) {
27
- filtered[name] = props[name];
27
+ filtered[name] = props[name]
28
28
  } else if (name in defaults) {
29
- filtered[name] = defaults[name]!;
29
+ filtered[name] = defaults[name]!
30
30
  }
31
31
 
32
- return filtered;
33
- }, {} as Pick<T, K>);
32
+ return filtered
33
+ }, {} as Pick<T, K>)
34
34
  }
35
35
 
36
36
  export function invariantIntlContext(intl?: any): asserts intl {
@@ -38,12 +38,12 @@ export function invariantIntlContext(intl?: any): asserts intl {
38
38
  intl,
39
39
  '[React Intl] Could not find required `intl` object. ' +
40
40
  '<IntlProvider> needs to exist in the component ancestry.'
41
- );
41
+ )
42
42
  }
43
43
 
44
44
  export function defaultErrorHandler(error: ReactIntlError): void {
45
45
  if (process.env.NODE_ENV !== 'production') {
46
- console.error(error);
46
+ console.error(error)
47
47
  }
48
48
  }
49
49
 
@@ -66,7 +66,7 @@ export const DEFAULT_INTL_CONFIG: Pick<
66
66
  defaultFormats: {},
67
67
 
68
68
  onError: defaultErrorHandler,
69
- };
69
+ }
70
70
 
71
71
  export function createIntlCache(): IntlCache {
72
72
  return {
@@ -77,7 +77,7 @@ export function createIntlCache(): IntlCache {
77
77
  pluralRules: {},
78
78
  list: {},
79
79
  displayNames: {},
80
- };
80
+ }
81
81
  }
82
82
 
83
83
  /**
@@ -87,9 +87,9 @@ export function createIntlCache(): IntlCache {
87
87
  export function createFormatters(
88
88
  cache: IntlCache = createIntlCache()
89
89
  ): Formatters {
90
- const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
91
- const ListFormat = (Intl as any).ListFormat;
92
- const DisplayNames = (Intl as any).DisplayNames;
90
+ const RelativeTimeFormat = (Intl as any).RelativeTimeFormat
91
+ const ListFormat = (Intl as any).ListFormat
92
+ const DisplayNames = (Intl as any).DisplayNames
93
93
  return {
94
94
  getDateTimeFormat: memoizeIntlConstructor(
95
95
  Intl.DateTimeFormat,
@@ -104,7 +104,7 @@ export function createFormatters(
104
104
  getPluralRules: memoizeIntlConstructor(Intl.PluralRules, cache.pluralRules),
105
105
  getListFormat: memoizeIntlConstructor(ListFormat, cache.list),
106
106
  getDisplayNames: memoizeIntlConstructor(DisplayNames, cache.displayNames),
107
- };
107
+ }
108
108
  }
109
109
 
110
110
  export function getNamedFormat<T extends keyof CustomFormats>(
@@ -117,14 +117,19 @@ export function getNamedFormat<T extends keyof CustomFormats>(
117
117
  | Intl.DateTimeFormatOptions
118
118
  | IntlRelativeTimeFormatOptions
119
119
  | undefined {
120
- const formatType = formats && formats[type];
121
- let format;
120
+ const formatType = formats && formats[type]
121
+ let format
122
122
  if (formatType) {
123
- format = formatType[name];
123
+ format = formatType[name]
124
124
  }
125
125
  if (format) {
126
- return format;
126
+ return format
127
127
  }
128
128
 
129
- onError(new ReactIntlError(ReactIntlErrorCode.UNSUPPORTED_FORMATTER, `No ${type} format named: ${name}`));
129
+ onError(
130
+ new ReactIntlError(
131
+ ReactIntlErrorCode.UNSUPPORTED_FORMATTER,
132
+ `No ${type} format named: ${name}`
133
+ )
134
+ )
130
135
  }
package/src/vendor.d.ts CHANGED
@@ -1 +1 @@
1
- declare module 'shallow-equal/objects';
1
+ declare module 'shallow-equal/objects'