121 lines
4.4 KiB
TypeScript
121 lines
4.4 KiB
TypeScript
// Format a number as a US phone number: (XXX) XXX-XXXX.
|
|
export function formatPhoneNumber(number: string | number): string {
|
|
const digits = String(number).replace(/\D/g, "").padStart(10, "0").slice(0, 10);
|
|
const areaCode = digits.slice(0, 3);
|
|
const centralOfficeCode = digits.slice(3, 6);
|
|
const lineNumber = digits.slice(6, 10);
|
|
return "(" + areaCode + ") " + centralOfficeCode + "-" + lineNumber;
|
|
}
|
|
|
|
// Format a number as a US zip code (5 or 9 digits).
|
|
export function formatZipCode(number: string | number): string {
|
|
const num = typeof number === "string" ? parseInt(number, 10) : number;
|
|
|
|
if (num <= 99999) {
|
|
return String(num).padStart(5, "0");
|
|
}
|
|
|
|
const digits = String(num).padStart(9, "0");
|
|
const zipCode = digits.slice(0, 5);
|
|
const plus4 = digits.slice(5, 9);
|
|
return zipCode + "-" + plus4;
|
|
}
|
|
|
|
// Format a number as a US Tax ID (EIN): XX-XXXXXXX.
|
|
export function formatTaxId(number: string | number): string {
|
|
const digits = String(number).replace(/\D/g, "").padStart(9, "0").slice(0, 9);
|
|
const prefix = digits.slice(0, 2);
|
|
const identifier = digits.slice(2, 9);
|
|
return prefix + "-" + identifier;
|
|
}
|
|
|
|
export function formatNumber(number: number): string {
|
|
return new Intl.NumberFormat("en-US").format(number);
|
|
}
|
|
|
|
export function formatDecimal(number: number, decimalPlaces: number = 2): string {
|
|
return new Intl.NumberFormat("en-US", {
|
|
minimumFractionDigits: decimalPlaces,
|
|
maximumFractionDigits: decimalPlaces,
|
|
}).format(number);
|
|
}
|
|
|
|
// State Code Utilities
|
|
|
|
const stateCodeMap: Record<string, string> = {
|
|
"Alabama": "AL", "Alaska": "AK", "Arizona": "AZ", "Arkansas": "AR", "California": "CA",
|
|
"Colorado": "CO", "Connecticut": "CT", "Delaware": "DE", "District of Columbia": "DC", "Florida": "FL",
|
|
"Georgia": "GA", "Hawaii": "HI", "Idaho": "ID", "Illinois": "IL", "Indiana": "IN",
|
|
"Iowa": "IA", "Kansas": "KS", "Kentucky": "KY", "Louisiana": "LA", "Maine": "ME",
|
|
"Maryland": "MD", "Massachusetts": "MA", "Michigan": "MI", "Minnesota": "MN", "Mississippi": "MS",
|
|
"Missouri": "MO", "Montana": "MT", "Nebraska": "NE", "Nevada": "NV", "New Hampshire": "NH",
|
|
"New Jersey": "NJ", "New Mexico": "NM", "New York": "NY", "North Carolina": "NC", "North Dakota": "ND",
|
|
"Ohio": "OH", "Oklahoma": "OK", "Oregon": "OR", "Pennsylvania": "PA", "Puerto Rico": "PR",
|
|
"Rhode Island": "RI", "South Carolina": "SC", "South Dakota": "SD", "Tennessee": "TN", "Texas": "TX",
|
|
"Utah": "UT", "Vermont": "VT", "Virgin Islands": "VI", "Virginia": "VA", "Washington": "WA",
|
|
"West Virginia": "WV", "Wisconsin": "WI", "Wyoming": "WY",
|
|
};
|
|
|
|
export function stateToStateCode(state: string): string {
|
|
if (stateCodeMap[state]) {
|
|
return stateCodeMap[state];
|
|
}
|
|
|
|
const stateLower = state.toLowerCase();
|
|
for (const [stateName, code] of Object.entries(stateCodeMap)) {
|
|
if (stateName.toLowerCase() === stateLower) {
|
|
return code;
|
|
}
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
export function stateCodeToState(code: string): string {
|
|
for (const [state, stateCode] of Object.entries(stateCodeMap)) {
|
|
if (stateCode === code.toUpperCase()) {
|
|
return state;
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
export function isValidStateCode(code: string): boolean {
|
|
return Object.values(stateCodeMap).includes(code.toUpperCase());
|
|
}
|
|
|
|
export function formatDate(date: string | Date): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return new Intl.DateTimeFormat("en-US", {
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
year: "numeric",
|
|
}).format(d);
|
|
}
|
|
|
|
export function formatDateLong(date: string | Date): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return new Intl.DateTimeFormat("en-US", {
|
|
month: "long",
|
|
day: "numeric",
|
|
year: "numeric",
|
|
}).format(d);
|
|
}
|
|
|
|
export function formatDateTime(date: string | Date): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return new Intl.DateTimeFormat("en-US", {
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
year: "numeric",
|
|
hour: "numeric",
|
|
minute: "2-digit",
|
|
hour12: true,
|
|
}).format(d);
|
|
}
|
|
|
|
export function formatPercent(value: number, decimalPlaces: number = 2, isDecimal: boolean = false): string {
|
|
const percent = isDecimal ? value * 100 : value;
|
|
return percent.toFixed(decimalPlaces) + "%";
|
|
}
|