( els: MessageFormatElement[], locales: string | string[], formatters: Formatters, formats: Formats, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, currentPluralValue?: number, // For debugging originalMessage?: string )
| 114 | |
| 115 | // TODO(skeleton): add skeleton support |
| 116 | export function formatToParts<T>( |
| 117 | els: MessageFormatElement[], |
| 118 | locales: string | string[], |
| 119 | formatters: Formatters, |
| 120 | formats: Formats, |
| 121 | values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, |
| 122 | currentPluralValue?: number, |
| 123 | // For debugging |
| 124 | originalMessage?: string |
| 125 | ): MessageFormatPart<T>[] { |
| 126 | // Hot path for straight simple msg translations |
| 127 | if (els.length === 1 && isLiteralElement(els[0])) { |
| 128 | return [ |
| 129 | { |
| 130 | type: PART_TYPE.literal, |
| 131 | value: els[0].value, |
| 132 | }, |
| 133 | ] |
| 134 | } |
| 135 | const result: MessageFormatPart<T>[] = [] |
| 136 | for (const el of els) { |
| 137 | // Exit early for string parts. |
| 138 | if (isLiteralElement(el)) { |
| 139 | result.push({ |
| 140 | type: PART_TYPE.literal, |
| 141 | value: el.value, |
| 142 | }) |
| 143 | continue |
| 144 | } |
| 145 | // TODO: should this part be literal type? |
| 146 | // Replace `#` in plural rules with the actual numeric value. |
| 147 | if (isPoundElement(el)) { |
| 148 | if (typeof currentPluralValue === 'number') { |
| 149 | result.push({ |
| 150 | type: PART_TYPE.literal, |
| 151 | value: formatters.getNumberFormat(locales).format(currentPluralValue), |
| 152 | }) |
| 153 | } |
| 154 | continue |
| 155 | } |
| 156 | |
| 157 | const {value: varName} = el |
| 158 | |
| 159 | // Enforce that all required values are provided by the caller. |
| 160 | if (!(values && varName in values)) { |
| 161 | throw new MissingValueError(varName, originalMessage) |
| 162 | } |
| 163 | |
| 164 | let value = values[varName] |
| 165 | if (isArgumentElement(el)) { |
| 166 | if ( |
| 167 | !value || |
| 168 | typeof value === 'string' || |
| 169 | typeof value === 'number' || |
| 170 | typeof value === 'bigint' |
| 171 | ) { |
| 172 | value = |
| 173 | typeof value === 'string' || |
no test coverage detected