goobs-frontend
Version:
A comprehensive React-based libary for building modern web applications
126 lines • 4.97 kB
TypeScript
/**
* Shared format / parse / validate utilities for the formatted Field
* components (PhoneNumber, CreditCardNumber, USD, Percentage,
* AccountNumber, CVV, RoutingNumber). Each helper used to live
* duplicated inside its respective component file; consolidating here
* means one place to fix bugs and one source of truth for the
* formatting rules.
*
* Naming convention:
* - `format<X>(raw)` — accept a raw string of digits, return the
* visually-formatted string (e.g. "5551234567" → "+1 (555) 123-4567")
* - `parse<X>(formatted)` — strip formatting, return the raw value
* (string of digits, or number for currency/percentage)
* - `validate<X>(raw)` — return boolean
*/
/**
* Strip non-digit characters and clamp to 10 digits (US numbers
* without the country code; the "+1" is added by the formatter).
*
* @param formatted - Any phone string, formatted or raw.
* @returns Up to 10 raw digits.
*/
export declare function parsePhone(formatted: string): string;
/**
* Format a raw 10-digit US phone number string into "+1 (XXX) XXX-XXXX".
* Partial inputs format progressively — typing "555" returns
* "+1 (555". This matches what users expect during typing.
*
* @param raw - Digits (or any string; non-digits are stripped).
* @returns The progressively formatted phone string.
*/
export declare function formatPhone(raw: string): string;
/**
* Format a numeric value as "$X,XXX.XX". Uses Intl.NumberFormat for
* locale-correct grouping separators.
*
* @param value - The numeric amount (NaN returns '').
* @param precision - Fraction digits (default 2).
* @returns The formatted USD string.
*/
export declare function formatCurrency(value: number, precision?: number): string;
/**
* Parse a formatted currency string back to a number. Strips `$`,
* commas, and any other non-numeric characters except the decimal
* point and leading minus sign. Returns NaN for unparsable input.
*
* @param formatted - The formatted currency string.
* @returns The numeric value, or NaN.
*/
export declare function parseCurrency(formatted: string): number;
/**
* Format a numeric value as "XX.XX%" (NaN returns '').
*
* @param value - The numeric percentage.
* @param precision - Fraction digits (default 2).
* @returns The formatted percentage string.
*/
export declare function formatPercentage(value: number, precision?: number): string;
/**
* Parse a formatted percentage string back to a number.
*
* @param formatted - The formatted percentage string.
* @returns The numeric value, or NaN.
*/
export declare function parsePercentage(formatted: string): number;
export type CardType = 'visa' | 'mastercard' | 'amex' | 'discover' | 'diners' | 'jcb' | 'unionpay' | 'unknown';
/**
* Detect the card type from the leading digits of a card number.
* Order matters — `unionpay` is checked before `discover` because
* their BIN ranges overlap.
*
* @param raw - The card number (formatted or raw).
* @returns The detected card network, or 'unknown'.
*/
export declare function detectCardType(raw: string): CardType;
/**
* Format a raw card number into spaced groups based on card type.
* Amex uses 4-6-5; everyone else uses 4-4-4-4.
*
* @param raw - The card number digits (non-digits stripped).
* @returns The space-grouped card number.
*/
export declare function formatCardNumber(raw: string): string;
/**
* Strip formatting from a card number string.
*
* @param formatted - The formatted card number.
* @returns The raw digits.
*/
export declare function parseCardNumber(formatted: string): string;
/**
* Luhn (mod-10) checksum. Returns true when the digit string passes,
* false otherwise. Empty/short strings return false.
*
* @param raw - The digit string to check (non-digits stripped).
* @returns Whether the checksum passes.
*/
export declare function luhnValidate(raw: string): boolean;
/**
* Validate a US ABA routing number using the standard
* `3·d0 + 7·d1 + 1·d2 + 3·d3 + 7·d4 + 1·d5 + 3·d6 + 7·d7 + 1·d8 ≡ 0 (mod 10)`
* checksum. Empty/short inputs return false.
*
* @param raw - The routing-number string (non-digits stripped).
* @returns Whether the ABA checksum passes.
*/
export declare function validateRoutingNumber(raw: string): boolean;
/**
* Strip non-digit characters from a string. Used by every numeric
* formatter as the parsing primitive.
*
* @param value - Any string.
* @returns The digits only.
*/
export declare function digitsOnly(value: string): string;
/**
* Mask a string showing only the last `tail` characters. Used by
* AccountNumber + CreditCardNumber + CVV when displaying a saved value.
*
* @param value - The string to mask.
* @param tail - How many trailing characters stay visible (default 4).
* @param mask - The mask character (default '•').
* @returns The masked string.
*/
export declare function maskTail(value: string, tail?: number, mask?: string): string;
//# sourceMappingURL=formatters.d.ts.map