UNPKG

messageformat

Version:

Intl.MessageFormat / Unicode MessageFormat 2 parser, runtime and polyfill

41 lines (40 loc) 1.46 kB
import { getMessageNumber, readNumericOperand } from "./number.js"; import { asPositiveInteger, asString } from "./utils.js"; /** * The function `:percent` is a selector and formatter for percent values. * * @beta */ export function percent(ctx, exprOpt, operand) { const input = readNumericOperand(operand); const options = Object.assign({}, input.options, { localeMatcher: ctx.localeMatcher, style: 'percent' }); for (const [name, optval] of Object.entries(exprOpt)) { if (optval === undefined) continue; try { switch (name) { case 'roundingMode': case 'roundingPriority': case 'signDisplay': case 'trailingZeroDisplay': case 'useGrouping': // @ts-expect-error Let Intl.NumberFormat construction fail options[name] = asString(optval); break; case 'minimumFractionDigits': case 'maximumFractionDigits': case 'minimumSignificantDigits': case 'maximumSignificantDigits': options[name] = asPositiveInteger(optval); break; } } catch { ctx.onError('bad-option', `Value ${optval} is not valid for :percent option ${name}`); } } return getMessageNumber(ctx, input.value, options, true); }