react-intl 3.11.0 → 3.12.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [3.12.0](https://github.com/formatjs/react-intl/compare/v3.11.0...v3.12.0) (2020-02-04)
6
+
7
+
8
+ ### Features
9
+
10
+ * add formatDisplayName and FormattedDisplayName ([#1567](https://github.com/formatjs/react-intl/issues/1567)) ([10bcbe2](https://github.com/formatjs/react-intl/commit/10bcbe2e7e92902d60810a7c00568d3043b09db3)), closes [#1547](https://github.com/formatjs/react-intl/issues/1547)
11
+
5
12
  ## [3.11.0](https://github.com/formatjs/react-intl/compare/v3.10.0...v3.11.0) (2020-01-09)
6
13
 
7
14
 
@@ -1,10 +1,11 @@
1
1
  import * as React from 'react';
2
- import { IntlShape, FormatDateOptions, FormatNumberOptions, FormatListOptions } from '../types';
2
+ import { IntlShape, FormatDateOptions, FormatNumberOptions, FormatListOptions, FormatDisplayNameOptions } from '../types';
3
3
  declare type Formatter = {
4
4
  formatDate: FormatDateOptions;
5
5
  formatTime: FormatDateOptions;
6
6
  formatNumber: FormatNumberOptions;
7
7
  formatList: FormatListOptions;
8
+ formatDisplayName: FormatDisplayNameOptions;
8
9
  };
9
10
  export declare const FormattedNumberParts: React.FC<Formatter['formatNumber'] & {
10
11
  value: Parameters<IntlShape['formatNumber']>[0];
@@ -20,6 +20,9 @@ var DisplayName;
20
20
  DisplayName["formatTime"] = "FormattedTime";
21
21
  DisplayName["formatNumber"] = "FormattedNumber";
22
22
  DisplayName["formatList"] = "FormattedList";
23
+ // Note that this DisplayName is the locale display name, not to be confused with
24
+ // the name of the enum, which is for React component display name in dev tools.
25
+ DisplayName["formatDisplayName"] = "FormattedDisplayName";
23
26
  })(DisplayName || (DisplayName = {}));
24
27
  var DisplayNameParts;
25
28
  (function (DisplayNameParts) {
@@ -40,6 +40,7 @@ var plural_1 = require("../formatters/plural");
40
40
  var message_1 = require("../formatters/message");
41
41
  var shallowEquals_ = require("shallow-equal/objects");
42
42
  var list_1 = require("../formatters/list");
43
+ var displayName_1 = require("../formatters/displayName");
43
44
  var shallowEquals = shallowEquals_.default || shallowEquals_;
44
45
  function processIntlConfig(config) {
45
46
  return {
@@ -75,7 +76,7 @@ function createIntl(config, cache) {
75
76
  // each <FormattedMessage> contains a `defaultMessage` prop.
76
77
  resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
77
78
  }
78
- return __assign(__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), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatHTMLMessage: message_1.formatHTMLMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat) });
79
+ return __assign(__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), formatTimeToParts: dateTime_1.formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: plural_1.formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: message_1.formatMessage.bind(null, resolvedConfig, formatters), formatHTMLMessage: message_1.formatHTMLMessage.bind(null, resolvedConfig, formatters), formatList: list_1.formatList.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: displayName_1.formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
79
80
  }
80
81
  exports.createIntl = createIntl;
81
82
  var IntlProvider = /** @class */ (function (_super) {
@@ -0,0 +1,2 @@
1
+ import { IntlConfig, Formatters, IntlFormatters } from '../types';
2
+ export declare function formatDisplayName({ locale, onError }: Pick<IntlConfig, 'locale' | 'onError'>, getDisplayNames: Formatters['getDisplayNames'], value: Parameters<IntlFormatters['formatDisplayName']>[0], options?: Parameters<IntlFormatters['formatDisplayName']>[1]): string | undefined;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var utils_1 = require("../utils");
4
+ var DISPLAY_NAMES_OPTONS = [
5
+ 'localeMatcher',
6
+ 'style',
7
+ 'type',
8
+ 'fallback',
9
+ ];
10
+ function formatDisplayName(_a, getDisplayNames, value, options) {
11
+ var locale = _a.locale, onError = _a.onError;
12
+ if (options === void 0) { options = {}; }
13
+ var DisplayNames = Intl.DisplayNames;
14
+ if (!DisplayNames) {
15
+ onError(utils_1.createError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n"));
16
+ }
17
+ var filteredOptions = utils_1.filterProps(options, DISPLAY_NAMES_OPTONS);
18
+ try {
19
+ return getDisplayNames(locale, filteredOptions).of(value);
20
+ }
21
+ catch (e) {
22
+ onError(utils_1.createError('Error formatting display name.', e));
23
+ }
24
+ }
25
+ exports.formatDisplayName = formatDisplayName;
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { defineMessages } from '@formatjs/macro';
4
4
  import { CustomFormatConfig } from './types';
5
5
  import { UnifiedNumberFormatOptions } from '@formatjs/intl-unified-numberformat';
6
6
  import { IntlListFormatOptions } from '@formatjs/intl-listformat';
7
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames/lib';
7
8
  export { default as injectIntl, Provider as RawIntlProvider, Context as IntlContext, WithIntlProps, WrappedComponentProps, } from './components/injectIntl';
8
9
  export { default as useIntl } from './components/useIntl';
9
10
  export { default as IntlProvider, createIntl } from './components/provider';
@@ -19,6 +20,9 @@ export declare const FormattedNumber: React.FC<UnifiedNumberFormatOptions & Cust
19
20
  export declare const FormattedList: React.FC<IntlListFormatOptions & {
20
21
  value: React.ReactNode[];
21
22
  }>;
23
+ export declare const FormattedDisplayName: React.FC<DisplayNamesOptions & {
24
+ value: string | number | object;
25
+ }>;
22
26
  export declare const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & CustomFormatConfig & {
23
27
  value: string | number | Date | undefined;
24
28
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ exports.FormattedDate = createFormattedComponent_1.createFormattedComponent('for
17
17
  exports.FormattedTime = createFormattedComponent_1.createFormattedComponent('formatTime');
18
18
  exports.FormattedNumber = createFormattedComponent_1.createFormattedComponent('formatNumber');
19
19
  exports.FormattedList = createFormattedComponent_1.createFormattedComponent('formatList');
20
+ exports.FormattedDisplayName = createFormattedComponent_1.createFormattedComponent('formatDisplayName');
20
21
  exports.FormattedDateParts = createFormattedComponent_1.createFormattedDateTimePartsComponent('formatDate');
21
22
  exports.FormattedTimeParts = createFormattedComponent_1.createFormattedDateTimePartsComponent('formatTime');
22
23
  var createFormattedComponent_2 = require("./components/createFormattedComponent");
@@ -4,6 +4,9 @@
4
4
 
5
5
  ```ts
6
6
 
7
+ import { DisplayNames } from '@formatjs/intl-displaynames';
8
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames';
9
+ import { DisplayNamesOptions as DisplayNamesOptions_2 } from '@formatjs/intl-displaynames/lib';
7
10
  import * as React from 'react';
8
11
 
9
12
  // Warning: (ae-forgotten-export) The symbol "OptionalIntlConfig" needs to be exported by the entry point index.d.ts
@@ -36,6 +39,9 @@ export function defineMessages<T, U extends Record<string, T>>(msgs: U): U;
36
39
  // @public (undocumented)
37
40
  export type FormatDateOptions = Exclude<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig;
38
41
 
42
+ // @public (undocumented)
43
+ export type FormatDisplayNameOptions = Exclude<DisplayNamesOptions, 'localeMatcher'>;
44
+
39
45
  // Warning: (ae-forgotten-export) The symbol "IntlListFormatOptions" needs to be exported by the entry point index.d.ts
40
46
  //
41
47
  // @public (undocumented)
@@ -63,6 +69,11 @@ export const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & CustomFor
63
69
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
64
70
  }>;
65
71
 
72
+ // @public (undocumented)
73
+ export const FormattedDisplayName: React.FC<DisplayNamesOptions_2 & {
74
+ value: string | number | object;
75
+ }>;
76
+
66
77
  // Warning: (ae-forgotten-export) The symbol "PrimitiveType" needs to be exported by the entry point index.d.ts
67
78
  //
68
79
  // @public (undocumented)
@@ -165,6 +176,8 @@ export const FormattedTimeParts: React.FC<Intl.DateTimeFormatOptions & CustomFor
165
176
  export interface Formatters {
166
177
  // (undocumented)
167
178
  getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
179
+ // (undocumented)
180
+ getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
168
181
  // Warning: (ae-forgotten-export) The symbol "ListFormat" needs to be exported by the entry point index.d.ts
169
182
  //
170
183
  // (undocumented)
@@ -200,6 +213,8 @@ export interface IntlCache {
200
213
  // (undocumented)
201
214
  dateTime: Record<string, Intl.DateTimeFormat>;
202
215
  // (undocumented)
216
+ displayNames: Record<string, DisplayNames>;
217
+ // (undocumented)
203
218
  list: Record<string, ListFormat>;
204
219
  // (undocumented)
205
220
  message: Record<string, IntlMessageFormat>;
@@ -243,6 +258,8 @@ export interface IntlFormatters {
243
258
  // (undocumented)
244
259
  formatDateToParts(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
245
260
  // (undocumented)
261
+ formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
262
+ // (undocumented)
246
263
  formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
247
264
  // (undocumented)
248
265
  formatList(values: Array<string>, opts?: FormatListOptions): string;
@@ -1,3 +1,6 @@
1
+ import { DisplayNames } from '@formatjs/intl-displaynames';
2
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames';
3
+ import { DisplayNamesOptions as DisplayNamesOptions_2 } from '@formatjs/intl-displaynames/lib';
1
4
  import * as React from 'react';
2
5
 
3
6
  declare type ArgumentElement = BaseElement<TYPE.argument>;
@@ -61,6 +64,8 @@ declare interface FieldData {
61
64
 
62
65
  export declare type FormatDateOptions = Exclude<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig;
63
66
 
67
+ export declare type FormatDisplayNameOptions = Exclude<DisplayNamesOptions, 'localeMatcher'>;
68
+
64
69
  export declare type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
65
70
 
66
71
  export declare type FormatNumberOptions = Exclude<UnifiedNumberFormatOptions, 'localeMatcher'> & CustomFormatConfig;
@@ -86,6 +91,10 @@ export declare const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & C
86
91
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
87
92
  }>;
88
93
 
94
+ export declare const FormattedDisplayName: React.FC<DisplayNamesOptions_2 & {
95
+ value: string | number | object;
96
+ }>;
97
+
89
98
  export declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
90
99
  static displayName: string;
91
100
  static defaultProps: {
@@ -151,6 +160,7 @@ declare type Formatter = {
151
160
  formatTime: FormatDateOptions;
152
161
  formatNumber: FormatNumberOptions;
153
162
  formatList: FormatListOptions;
163
+ formatDisplayName: FormatDisplayNameOptions;
154
164
  };
155
165
 
156
166
  export declare interface Formatters {
@@ -160,6 +170,7 @@ export declare interface Formatters {
160
170
  getRelativeTimeFormat(...args: ConstructorParameters<typeof RelativeTimeFormat>): RelativeTimeFormat;
161
171
  getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
162
172
  getListFormat(...args: ConstructorParameters<typeof ListFormat>): ListFormat;
173
+ getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
163
174
  }
164
175
 
165
176
  declare interface Formatters_2 {
@@ -185,6 +196,7 @@ export declare interface IntlCache {
185
196
  relativeTime: Record<string, RelativeTimeFormat>;
186
197
  pluralRules: Record<string, Intl.PluralRules>;
187
198
  list: Record<string, ListFormat>;
199
+ displayNames: Record<string, DisplayNames>;
188
200
  }
189
201
 
190
202
  export declare interface IntlConfig {
@@ -214,6 +226,7 @@ export declare interface IntlFormatters {
214
226
  formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
215
227
  formatList(values: Array<string>, opts?: FormatListOptions): string;
216
228
  formatList(values: Array<string | React.ReactNode>, opts?: FormatListOptions): React.ReactNode;
229
+ formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
217
230
  }
218
231
 
219
232
  declare interface IntlListFormatOptions {
@@ -4335,7 +4335,8 @@
4335
4335
  message: {},
4336
4336
  relativeTime: {},
4337
4337
  pluralRules: {},
4338
- list: {}
4338
+ list: {},
4339
+ displayNames: {}
4339
4340
  };
4340
4341
  }
4341
4342
  /**
@@ -4347,13 +4348,15 @@
4347
4348
  var cache = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : createIntlCache();
4348
4349
  var RelativeTimeFormat = Intl.RelativeTimeFormat;
4349
4350
  var ListFormat = Intl.ListFormat;
4351
+ var DisplayNames = Intl.DisplayNames;
4350
4352
  return {
4351
4353
  getDateTimeFormat: memoizeFormatConstructor(Intl.DateTimeFormat, cache.dateTime),
4352
4354
  getNumberFormat: memoizeFormatConstructor(Intl.NumberFormat, cache.number),
4353
4355
  getMessageFormat: memoizeFormatConstructor(IntlMessageFormat, cache.message),
4354
4356
  getRelativeTimeFormat: memoizeFormatConstructor(RelativeTimeFormat, cache.relativeTime),
4355
4357
  getPluralRules: memoizeFormatConstructor(Intl.PluralRules, cache.pluralRules),
4356
- getListFormat: memoizeFormatConstructor(ListFormat, cache.list)
4358
+ getListFormat: memoizeFormatConstructor(ListFormat, cache.list),
4359
+ getDisplayNames: memoizeFormatConstructor(DisplayNames, cache.displayNames)
4357
4360
  };
4358
4361
  }
4359
4362
  function getNamedFormat(formats, type, name, onError) {
@@ -4815,7 +4818,10 @@
4815
4818
  DisplayName["formatDate"] = "FormattedDate";
4816
4819
  DisplayName["formatTime"] = "FormattedTime";
4817
4820
  DisplayName["formatNumber"] = "FormattedNumber";
4818
- DisplayName["formatList"] = "FormattedList";
4821
+ DisplayName["formatList"] = "FormattedList"; // Note that this DisplayName is the locale display name, not to be confused with
4822
+ // the name of the enum, which is for React component display name in dev tools.
4823
+
4824
+ DisplayName["formatDisplayName"] = "FormattedDisplayName";
4819
4825
  })(DisplayName || (DisplayName = {}));
4820
4826
 
4821
4827
  var DisplayNameParts;
@@ -5291,6 +5297,26 @@
5291
5297
  return values;
5292
5298
  }
5293
5299
 
5300
+ var DISPLAY_NAMES_OPTONS = ['localeMatcher', 'style', 'type', 'fallback'];
5301
+ function formatDisplayName(_ref, getDisplayNames, value) {
5302
+ var locale = _ref.locale,
5303
+ onError = _ref.onError;
5304
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
5305
+ var DisplayNames = Intl.DisplayNames;
5306
+
5307
+ if (!DisplayNames) {
5308
+ onError(createError("Intl.DisplayNames is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-displaynames\"\n"));
5309
+ }
5310
+
5311
+ var filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
5312
+
5313
+ try {
5314
+ return getDisplayNames(locale, filteredOptions).of(value);
5315
+ } catch (e) {
5316
+ onError(createError('Error formatting display name.', e));
5317
+ }
5318
+ }
5319
+
5294
5320
  var shallowEquals = objects || shallowEquals_;
5295
5321
 
5296
5322
  function processIntlConfig(config) {
@@ -5345,7 +5371,8 @@
5345
5371
  formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules),
5346
5372
  formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
5347
5373
  formatHTMLMessage: formatHTMLMessage$1.bind(null, resolvedConfig, formatters),
5348
- formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat)
5374
+ formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat),
5375
+ formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames)
5349
5376
  });
5350
5377
  }
5351
5378
 
@@ -5814,11 +5841,13 @@
5814
5841
  var FormattedTime = createFormattedComponent('formatTime');
5815
5842
  var FormattedNumber = createFormattedComponent('formatNumber');
5816
5843
  var FormattedList = createFormattedComponent('formatList');
5844
+ var FormattedDisplayName = createFormattedComponent('formatDisplayName');
5817
5845
  var FormattedDateParts = createFormattedDateTimePartsComponent('formatDate');
5818
5846
  var FormattedTimeParts = createFormattedDateTimePartsComponent('formatTime');
5819
5847
 
5820
5848
  exports.FormattedDate = FormattedDate;
5821
5849
  exports.FormattedDateParts = FormattedDateParts;
5850
+ exports.FormattedDisplayName = FormattedDisplayName;
5822
5851
  exports.FormattedHTMLMessage = FormattedHTMLMessage;
5823
5852
  exports.FormattedList = FormattedList;
5824
5853
  exports.FormattedMessage = FormattedMessage;