Repository URL to install this package:
Version:
0.9.5 ▾
|
import { isString } from 'exotic'
import isValidLength from './isValidLength'
const matchPhone = /^(?=.*?[1-9])[0-9 ()-.+]+$/
const matchAlphabets = /^[a-zA-Z]*$/
// {9,9}
const matchPhoneLoose = /^[1-9][0-9-().\s]$/
const matchSpaceDashSpace = /[\(\)\-\.\+\s]+/gm
const matchPhoneLoosest = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/
// eppPhone match extensible provisioning protocol format
// const eppPhone = /^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/
// nanpPhone match north american number plan format
// const nanpPhone = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
function sanitizeTelephone(value: string): string {
return isString(value) ? value.replace(matchSpaceDashSpace, '') : value
}
/**
* @see https://en.wikipedia.org/wiki/Telephone_numbers_in_the_Solomon_Islands
* @see https://stackoverflow.com/questions/14894899/what-is-the-minimum-length-of-a-valid-international-phone-number
* @see https://en.wikipedia.org/wiki/Telephone_numbering_plan
*
* 5-15 digits
*
* @see isValidLength
* @param {Number | String} value
* @return {Boolean}
*/
function isTelephone(value: string): boolean {
if (isString(value)) {
const strippedValue = sanitizeTelephone(value)
// 5-15
if (isValidLength(strippedValue, 4, 16)) {
return (
matchPhone.test(strippedValue) || matchPhoneLoose.test(strippedValue)
)
} else {
return false
}
} else {
return false
}
}
export { sanitizeTelephone }
export { isTelephone as isValidTelephone }
export { isTelephone }
export default isTelephone