UNPKG

@jil/args

Version:

A convention based argument parsing and formatting library, with strict validation checks

20 lines (14 loc) 521 B
import {ArgsError} from '../errors'; import {Option, ValueType} from '../types'; export function formatValue(value: ValueType, format?: Option<ValueType>['format']): ValueType { let nextValue = value; const prevType = typeof nextValue; if (typeof format === 'function' && prevType !== 'boolean') { nextValue = format(nextValue); const nextType = typeof nextValue; if (nextType !== prevType) { throw new ArgsError('VALUE_INVALID_FORMAT', [prevType, nextType]); } } return nextValue; }