|
| 1 | +import { MessageError, MessageResolutionError } from '../errors.js'; |
| 2 | +import type { MessageFunctionContext } from './index.js'; |
| 3 | +import { type MessageNumber, number } from './number.js'; |
| 4 | +import { asPositiveInteger, asString } from './utils.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * `currency` accepts as input numerical values as well as |
| 8 | + * objects wrapping a numerical value that also include a `currency` property. |
| 9 | + * |
| 10 | + * @beta |
| 11 | + */ |
| 12 | +export function currency( |
| 13 | + ctx: MessageFunctionContext, |
| 14 | + exprOpt: Record<string | symbol, unknown>, |
| 15 | + input?: unknown |
| 16 | +): MessageNumber { |
| 17 | + const { source } = ctx; |
| 18 | + const options: Intl.NumberFormatOptions & |
| 19 | + Intl.PluralRulesOptions & { select?: 'exact' | 'cardinal' } = { |
| 20 | + localeMatcher: ctx.localeMatcher |
| 21 | + }; |
| 22 | + let value = input; |
| 23 | + if (typeof value === 'object') { |
| 24 | + const valueOf = value?.valueOf; |
| 25 | + if (typeof valueOf === 'function') { |
| 26 | + Object.assign(options, (value as { options: unknown }).options); |
| 27 | + value = valueOf.call(value); |
| 28 | + } |
| 29 | + } |
| 30 | + if (typeof value === 'string') { |
| 31 | + try { |
| 32 | + value = JSON.parse(value); |
| 33 | + } catch { |
| 34 | + // handled below |
| 35 | + } |
| 36 | + } |
| 37 | + if (typeof value !== 'bigint' && typeof value !== 'number') { |
| 38 | + const msg = 'Input is not numeric'; |
| 39 | + throw new MessageResolutionError('bad-operand', msg, source); |
| 40 | + } |
| 41 | + |
| 42 | + options.style = 'currency'; |
| 43 | + for (const [name, optval] of Object.entries(exprOpt)) { |
| 44 | + if (optval === undefined) continue; |
| 45 | + try { |
| 46 | + switch (name) { |
| 47 | + case 'compactDisplay': |
| 48 | + case 'currency': |
| 49 | + case 'currencySign': |
| 50 | + case 'notation': |
| 51 | + case 'numberingSystem': |
| 52 | + case 'roundingMode': |
| 53 | + case 'roundingPriority': |
| 54 | + case 'trailingZeroDisplay': |
| 55 | + // @ts-expect-error Let Intl.NumberFormat construction fail |
| 56 | + options[name] = asString(optval); |
| 57 | + break; |
| 58 | + case 'minimumIntegerDigits': |
| 59 | + case 'minimumSignificantDigits': |
| 60 | + case 'maximumSignificantDigits': |
| 61 | + case 'roundingIncrement': |
| 62 | + // @ts-expect-error TS types don't know about roundingIncrement |
| 63 | + options[name] = asPositiveInteger(optval); |
| 64 | + break; |
| 65 | + case 'currencyDisplay': { |
| 66 | + const strval = asString(optval); |
| 67 | + if (strval === 'formalSymbol' || strval === 'never') { |
| 68 | + throw new MessageResolutionError( |
| 69 | + 'unsupported-operation', |
| 70 | + `Currency display "${strval}" is not supported on :currency`, |
| 71 | + source |
| 72 | + ); |
| 73 | + } |
| 74 | + options[name] = strval; |
| 75 | + break; |
| 76 | + } |
| 77 | + case 'fractionDigits': { |
| 78 | + const strval = asString(optval); |
| 79 | + if (strval === 'auto') { |
| 80 | + options.minimumFractionDigits = undefined; |
| 81 | + options.maximumFractionDigits = undefined; |
| 82 | + } else { |
| 83 | + const numval = asPositiveInteger(strval); |
| 84 | + options.minimumFractionDigits = numval; |
| 85 | + options.maximumFractionDigits = numval; |
| 86 | + } |
| 87 | + break; |
| 88 | + } |
| 89 | + case 'select': { |
| 90 | + const strval = asString(optval); |
| 91 | + if (strval === 'ordinal') { |
| 92 | + throw new MessageResolutionError( |
| 93 | + 'bad-option', |
| 94 | + 'Ordinal selection is not supported on :currency', |
| 95 | + source |
| 96 | + ); |
| 97 | + } |
| 98 | + // @ts-expect-error Let Intl.NumberFormat construction fail |
| 99 | + options[name] = strval; |
| 100 | + break; |
| 101 | + } |
| 102 | + case 'useGrouping': { |
| 103 | + const strval = asString(optval); |
| 104 | + // @ts-expect-error TS type is wrong |
| 105 | + options[name] = strval === 'never' ? false : strval; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + if (error instanceof MessageError) { |
| 111 | + ctx.onError(error); |
| 112 | + } else { |
| 113 | + const msg = `Value ${optval} is not valid for :currency option ${name}`; |
| 114 | + ctx.onError(new MessageResolutionError('bad-option', msg, source)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (!options.currency) { |
| 120 | + const msg = 'A currency code is required for :currency'; |
| 121 | + throw new MessageResolutionError('bad-operand', msg, source); |
| 122 | + } |
| 123 | + |
| 124 | + return number(ctx, {}, { valueOf: () => value, options }); |
| 125 | +} |
0 commit comments