UNPKG

messageformat

Version:

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

36 lines (35 loc) 1.25 kB
import { MessageFunctionError } from "../errors.js"; import { number, readNumericOperand } from "./number.js"; import { asPositiveInteger } from "./utils.js"; /** * `offset` accepts a numeric value as input and adds or subtracts an integer value from it * * @beta */ export function offset(ctx, exprOpt, operand) { let { value, options } = readNumericOperand(operand); let add; try { add = 'add' in exprOpt ? asPositiveInteger(exprOpt.add) : -1; } catch { throw new MessageFunctionError('bad-option', `Value ${exprOpt.add} is not valid for :offset option add`); } let sub; try { sub = 'subtract' in exprOpt ? asPositiveInteger(exprOpt.subtract) : -1; } catch { throw new MessageFunctionError('bad-option', `Value ${exprOpt.subtract} is not valid for :offset option subtract`); } if (add < 0 === sub < 0) { const msg = 'Exactly one of "add" or "subtract" is required as an :offset option'; throw new MessageFunctionError('bad-option', msg); } const delta = add < 0 ? -sub : add; if (typeof value === 'number') value += delta; else value += BigInt(delta); return number(ctx, {}, { valueOf: () => value, options }); }