stringzy
Version:
A versatile string manipulation library providing a range of text utilities for JavaScript and Node.js applications.
26 lines (25 loc) • 1.15 kB
TypeScript
export type PhoneFormat = 'us' | 'in' | 'international';
/**
* Formats a phone number string into a readable format based on the given region.
*
* Strips all non-digit characters before formatting. Supports formatting for:
* - `'us'`: U.S. numbers with 10 digits → `(123) 456-7890`
* - `'in'`: Indian numbers with 10 digits or 12 digits starting with '91' → `+91-12345-67890`
* - `'international'`: Generic international format assuming last 10 digits are the local number.
*
* If the number does not match expected formats, it returns the original input.
*
* @param {string} phone - The phone number string to format.
* @param {PhoneFormat} [format='us'] - The desired formatting style: `'us'`, `'in'`, or `'international'`.
* @returns {string} The formatted phone number, or the original input if formatting fails.
*
* @example
* formatPhone("1234567890"); // "(123) 456-7890"
*
* @example
* formatPhone("+91 9876543210", "in"); // "+91-98765-43210"
*
* @example
* formatPhone("009199876543210", "international"); // "+0091 (998) 765-43210"
*/
export declare function formatPhone(phone: string, format?: PhoneFormat): string;