/** * Group digits. */ type GroupDigits = string; /** * Group place. */ export type GroupPlace = bigint; /** * Group of digits and its place. * * The place refers to the order which the digits are grouped, e.g. for a number like * * 1,234,567 * * The groups would be: * * ['001', 2] * ['234', 1] * ['567', 0] * * Note that groups do not necessarily have the same length of digits, such in the case of * South Asian numbering system: * * 1,00,00,000 * * The groups would be: * * ['01', 3] * ['00', 2] * ['00', 1] * ['000', 0] */ export type Group = [GroupDigits, GroupPlace]; /** * Index of the group digits in a {@link Group|group}. */ export const GROUP_DIGITS_INDEX = 0 as const; /** * Index of the group place in a {@link Group|group}. */ export const GROUP_PLACE_INDEX = 1 as const; /** * System for stringifying and parsing numbers. */ export interface StringifySystem { /** * Creates a negative string. * @param s - The string to make negative. */ makeNegative: (s: string) => string; /** * Creates a group string. * @param group - The group digits. * @param place - The group place. * @param options - Options to use when creating the group. */ makeGroups: (groups: Group[], options?: T) => string[]; /** * Groups a string. * @param value - The string to group. */ group: (value: string) => Group[]; /** * Finalizes a string. * @param tokens - The tokens to finalize. */ finalize: (tokens: string[]) => string; /** * Tokenizes a string. * @param value - The string to tokenize. */ tokenize: (value: string) => string[]; /** * Parses groups from a string. * @param value - The string to parse groups from. */ parseGroups: (value: string[]) => Group[]; /** * Combines groups into a string. * @param value - The groups to combine. */ combineGroups: (value: Group[]) => string; } /** * Error thrown when an invalid token is encountered. */ export class InvalidTokenError extends Error { constructor(token: string) { super(`Invalid token: ${token}`); } }