/
displayName.ts
52 lines (50 loc) · 1.34 KB
/
displayName.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {Formatters, IntlFormatters, OnErrorFn} from './types';
import {filterProps} from './utils';
import {
DisplayNamesOptions,
DisplayNames as IntlDisplayNames,
} from '@formatjs/intl-displaynames';
import {FormatError, ErrorCode} from 'intl-messageformat';
import {IntlErrorCode, IntlError} from './error';
const DISPLAY_NAMES_OPTONS: Array<keyof DisplayNamesOptions> = [
'localeMatcher',
'style',
'type',
'fallback',
];
export function formatDisplayName(
{
locale,
onError,
}: {
locale: string;
onError: OnErrorFn;
},
getDisplayNames: Formatters['getDisplayNames'],
value: Parameters<IntlFormatters['formatDisplayName']>[0],
options: Parameters<IntlFormatters['formatDisplayName']>[1]
): string | undefined {
const DisplayNames: typeof IntlDisplayNames = (Intl as any).DisplayNames;
if (!DisplayNames) {
onError(
new FormatError(
`Intl.DisplayNames is not available in this environment.
Try polyfilling it using "@formatjs/intl-displaynames"
`,
ErrorCode.MISSING_INTL_API
)
);
}
const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
try {
return getDisplayNames(locale, filteredOptions).of(value);
} catch (e) {
onError(
new IntlError(
IntlErrorCode.FORMAT_ERROR,
'Error formatting display name.',
e
)
);
}
}