UNPKG

messageformat

Version:

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

56 lines (55 loc) 2.09 kB
import { MessageError, MessageFunctionError } from "../errors.js"; import { getMessageNumber, readNumericOperand } from "./number.js"; import { asPositiveInteger, asString } from "./utils.js"; /** * `unit` accepts as input numerical values as well as * objects wrapping a numerical value that also include a `unit` property. * * @beta */ export function unit(ctx, exprOpt, operand) { const input = readNumericOperand(operand); const options = Object.assign({}, input.options, { localeMatcher: ctx.localeMatcher, style: 'unit' }); for (const [name, optval] of Object.entries(exprOpt)) { if (optval === undefined) continue; try { switch (name) { case 'signDisplay': case 'roundingMode': case 'roundingPriority': case 'trailingZeroDisplay': case 'unit': case 'unitDisplay': case 'useGrouping': // @ts-expect-error Let Intl.NumberFormat construction fail options[name] = asString(optval); break; case 'minimumIntegerDigits': case 'minimumFractionDigits': case 'maximumFractionDigits': case 'minimumSignificantDigits': case 'maximumSignificantDigits': case 'roundingIncrement': // @ts-expect-error TS types don't know about roundingIncrement options[name] = asPositiveInteger(optval); break; } } catch (error) { if (error instanceof MessageError) { ctx.onError(error); } else { ctx.onError('bad-option', `Value ${optval} is not valid for :currency option ${name}`); } } } if (!options.unit) { throw new MessageFunctionError('bad-operand', 'A unit identifier is required for :unit'); } return getMessageNumber(ctx, input.value, options, false); }