@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
70 lines (69 loc) • 2.64 kB
JavaScript
function formatNumberWithCode(phoneNumberString, code = '1') {
if (phoneNumberString.match(/[a-zA-Z]/g)) {
return null;
}
const cleaned = ('' + phoneNumberString).replace(/\D/g, '');
// Dynamically insert the country code into the regex
let regexPattern = new RegExp(`^(${code})?(\\d{3})(\\d{0,3})(\\d{0,4})$`);
let match = cleaned.match(regexPattern);
if (match) {
let intlCode = match[1] ? `+${match[1]} ` : `+${code} `; // Use the matched code or the provided code
const divider = code === '1' ? '-' : ' ';
const number = [
intlCode,
'',
match[2],
divider,
match[3],
divider,
match[4],
]
.join('')
.replace(/-+$/, '');
return number;
}
return null;
}
export function isValidNumber(number) {
var _a;
const { code } = stripCode(number);
const formatted = (_a = formatNumberWithCode(number, code)) === null || _a === void 0 ? void 0 : _a.replace(/[^0-9]/g, '');
return (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 11 || (formatted === null || formatted === void 0 ? void 0 : formatted.length) === 12;
}
function stripCode(number) {
let code = `1`; // Default to North American country code
const cleaned = number.replace(/(?!^\+)[^\d]/g, '');
// Explicitly check for '+' sign to distinguish international codes
if (cleaned.startsWith('+90') ||
(cleaned.startsWith('90') && cleaned.length > 10)) {
code = `90`;
}
else if (cleaned.startsWith('+49') ||
(cleaned.startsWith('49') && cleaned.length > 10)) {
code = `49`;
}
// Adjust the condition to ensure that '905...' numbers without a '+' are treated as North American
else if (cleaned.startsWith('905') && cleaned.length === 10) {
code = `1`;
}
return { code, phoneWithoutCode: cleaned.replace('+' + code, '') };
}
export default function formatPhoneNumber(val, shouldFailSilently = true) {
const { code, phoneWithoutCode } = stripCode(val);
const formatted = formatNumberWithCode(phoneWithoutCode, code);
if (!formatted) {
if (!shouldFailSilently) {
throw new Error('INVALID_PHONE_NUMBER');
}
else {
return val;
}
}
// remove trailing spaces
const cleaned = formatted.replace(/\s+$/, '');
return cleaned;
}
export function isDummyNumber(phone) {
const cleanedValue = phone.replace(/\D/g, '');
return cleanedValue.startsWith('1555') || cleanedValue.startsWith('555');
}