Repository URL to install this package:
Version:
0.9.7 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const exotic_1 = require("exotic");
const isValidLength_1 = __importDefault(require("./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) {
return exotic_1.isString(value) ? value.replace(matchSpaceDashSpace, '') : value;
}
exports.sanitizeTelephone = sanitizeTelephone;
/**
* @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) {
if (exotic_1.isString(value)) {
const strippedValue = sanitizeTelephone(value);
// 5-15
if (isValidLength_1.default(strippedValue, 4, 16)) {
return (matchPhone.test(strippedValue) || matchPhoneLoose.test(strippedValue));
}
else {
return false;
}
}
else {
return false;
}
}
exports.isValidTelephone = isTelephone;
exports.isTelephone = isTelephone;
exports.default = isTelephone;
//# sourceMappingURL=isValidTelephone.js.map