(
{
locale,
formats,
messages,
defaultLocale,
defaultFormats,
fallbackOnEmptyString,
onError,
timeZone,
defaultRichTextElements,
},
state,
messageDescriptor = {id: ''},
values,
opts
)
| 103 | ) => T extends string ? string : Array<T | string> | string | T |
| 104 | |
| 105 | export const formatMessage: FormatMessageFn<any> = ( |
| 106 | { |
| 107 | locale, |
| 108 | formats, |
| 109 | messages, |
| 110 | defaultLocale, |
| 111 | defaultFormats, |
| 112 | fallbackOnEmptyString, |
| 113 | onError, |
| 114 | timeZone, |
| 115 | defaultRichTextElements, |
| 116 | }, |
| 117 | state, |
| 118 | messageDescriptor = {id: ''}, |
| 119 | values, |
| 120 | opts |
| 121 | ) => { |
| 122 | const {id: msgId, defaultMessage} = messageDescriptor |
| 123 | |
| 124 | // `id` is a required field of a Message Descriptor. |
| 125 | invariant( |
| 126 | !!msgId, |
| 127 | `[@formatjs/intl] An \`id\` must be provided to format a message. You can either: |
| 128 | 1. Configure your build toolchain with [babel-plugin-formatjs](https://formatjs.github.io/docs/tooling/babel-plugin) |
| 129 | or [@formatjs/ts-transformer](https://formatjs.github.io/docs/tooling/ts-transformer) OR |
| 130 | 2. Configure your \`eslint\` config to include [eslint-plugin-formatjs](https://formatjs.github.io/docs/tooling/linter#enforce-id) |
| 131 | to autofix this issue` |
| 132 | ) |
| 133 | const id = String(msgId) |
| 134 | const message = |
| 135 | // In case messages is Object.create(null) |
| 136 | // e.g import('foo.json') from webpack) |
| 137 | // See https://github.com/formatjs/formatjs/issues/1914 |
| 138 | messages && |
| 139 | Object.prototype.hasOwnProperty.call(messages, id) && |
| 140 | messages[id] |
| 141 | |
| 142 | // IMPORTANT: Hot path if `message` is AST with a single literal node |
| 143 | if ( |
| 144 | Array.isArray(message) && |
| 145 | message.length === 1 && |
| 146 | message[0].type === TYPE.literal |
| 147 | ) { |
| 148 | return message[0].value |
| 149 | } |
| 150 | |
| 151 | values = { |
| 152 | ...defaultRichTextElements, |
| 153 | ...values, |
| 154 | } |
| 155 | formats = deepMergeFormatsAndSetTimeZone(formats, timeZone) |
| 156 | defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone) |
| 157 | |
| 158 | if (!message) { |
| 159 | if (fallbackOnEmptyString === false && message === '') { |
| 160 | return message |
| 161 | } |
| 162 |
no test coverage detected