|
| 1 | +import { MessageResolutionError } from '../errors.js'; |
| 2 | +import type { MessageFunctionContext } from './index.js'; |
| 3 | +import { MessageNumber, number } from './number.js'; |
| 4 | +import { asPositiveInteger } from './utils.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * `math` accepts a numeric value as input and adds or subtracts an integer value from it |
| 8 | + * |
| 9 | + * @beta |
| 10 | + */ |
| 11 | +export function math( |
| 12 | + ctx: MessageFunctionContext, |
| 13 | + mathOpt: Record<string | symbol, unknown>, |
| 14 | + input?: unknown |
| 15 | +): MessageNumber { |
| 16 | + const { source } = ctx; |
| 17 | + let options: unknown = undefined; |
| 18 | + let value = input; |
| 19 | + if (typeof value === 'object') { |
| 20 | + const valueOf = value?.valueOf; |
| 21 | + if (typeof valueOf === 'function') { |
| 22 | + options = (value as { options: unknown }).options; |
| 23 | + value = valueOf.call(value); |
| 24 | + } |
| 25 | + } |
| 26 | + if (typeof value === 'string') { |
| 27 | + try { |
| 28 | + value = JSON.parse(value); |
| 29 | + } catch { |
| 30 | + // handled below |
| 31 | + } |
| 32 | + } |
| 33 | + if (typeof value !== 'bigint' && typeof value !== 'number') { |
| 34 | + const msg = 'Input is not numeric'; |
| 35 | + throw new MessageResolutionError('bad-operand', msg, source); |
| 36 | + } |
| 37 | + |
| 38 | + let add: number; |
| 39 | + let sub: number; |
| 40 | + try { |
| 41 | + add = 'add' in mathOpt ? asPositiveInteger(mathOpt.add) : -1; |
| 42 | + sub = 'subtract' in mathOpt ? asPositiveInteger(mathOpt.subtract) : -1; |
| 43 | + } catch (error) { |
| 44 | + throw new MessageResolutionError('bad-option', String(error), source); |
| 45 | + } |
| 46 | + if (add < 0 === sub < 0) { |
| 47 | + const msg = |
| 48 | + 'Exactly one of "add" or "subtract" is required as a :math option'; |
| 49 | + throw new MessageResolutionError('bad-option', msg, source); |
| 50 | + } |
| 51 | + const delta = add < 0 ? -sub : add; |
| 52 | + if (typeof value === 'number') value += delta; |
| 53 | + else value += BigInt(delta); |
| 54 | + |
| 55 | + return number(ctx, {}, { valueOf: () => value, options }); |
| 56 | +} |
0 commit comments