UNPKG

@yar.ua/numerals

Version:

Number to text - Inflector for Ukrainian numerals

30 lines (29 loc) 924 B
export class YarNumBaseError extends Error { } export class InternalDataError extends YarNumBaseError { } export class InputError extends YarNumBaseError { constructor(description) { super(`Invalid input: ${description}`); } } export class RangeError extends YarNumBaseError { constructor(digits, max_len) { super(`Provided number is too large with ${digits.length} out of ${max_len} digits.`); } } export function ensureInteger(whole) { if (typeof whole === "number") { if (isNaN(whole)) { throw new InputError("input is not a number"); } whole = whole.toLocaleString("fullwide", { useGrouping: false }); } if (typeof whole !== "string") { throw new InputError("input integer should be a string or a number"); } if (!/^[\s\d]*$/.test(whole)) { throw new InputError("input is not an integer"); } return whole; }