stringzy
Version:
A versatile string manipulation library providing a range of text utilities for JavaScript and Node.js applications.
26 lines (25 loc) • 992 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatPhone = formatPhone;
function formatPhone(phone, format = 'us') {
const digits = phone.replace(/\D/g, '');
if (format === 'us' && digits.length === 10) {
return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}`;
}
else if (format === 'international' && digits.length >= 10) {
const countryCode = digits.slice(0, -10);
const areaCode = digits.slice(-10, -7);
const firstPart = digits.slice(-7, -4);
const lastPart = digits.slice(-4);
return `+${countryCode} (${areaCode}) ${firstPart}-${lastPart}`;
}
else if (format === 'in') {
if (digits.length === 10) {
return `+91-${digits.slice(0, 5)}-${digits.slice(5)}`;
}
else if (digits.length === 12 && digits.startsWith('91')) {
return `+91-${digits.slice(2, 7)}-${digits.slice(7)}`;
}
}
return phone;
}