@beenotung/tslib
Version:
utils library in Typescript
64 lines (63 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_in_mobile_phone_prefix = is_in_mobile_phone_prefix;
exports.is_in_mobile_phone = is_in_mobile_phone;
exports.to_full_in_mobile_phone = to_full_in_mobile_phone;
exports.format_in_mobile_phone = format_in_mobile_phone;
const utils_1 = require("./utils");
/** ******************************
* India mobile phone number *
*********************************/
/**
* starts with 9, 8, 7, or 6
* Mobile numbers (including pagers) on GSM, WCDMA, LTE and NR networks start with either 9, 8, 7 or 6
* reference: https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India
*/
function is_in_mobile_phone_prefix(tel) {
tel = tel.replace(/^\+91/, '').trim();
if (tel.length < 1)
return false;
const firstDigit = tel[0];
return ['9', '8', '7', '6'].includes(firstDigit);
}
/**
* with/without +91 prefix
*/
function is_in_mobile_phone(tel) {
return to_full_in_mobile_phone(tel) !== '';
}
/**
* very forgiving
*
* @returns +91xxxxxxxxxx if valid (10 digits after country code)
* empty string if not valid
*/
function to_full_in_mobile_phone(tel) {
tel = (0, utils_1.to_tel_digits)(tel);
// 10 digits (local format)
if (tel.length === 10 && is_in_mobile_phone_prefix(tel)) {
return '+91' + tel;
}
// 10 digits with country code 91 (without +)
if (tel.length === 10 + 2 &&
tel.startsWith('91') &&
is_in_mobile_phone_prefix(tel.substring(2))) {
return '+' + tel;
}
// 10 digits with country code +91
if (tel.length === 10 + 3 &&
tel.startsWith('+91') &&
is_in_mobile_phone_prefix(tel.substring(3))) {
return tel;
}
return '';
}
/**
* @returns +91 xxxxx xxxxx if valid
*/
function format_in_mobile_phone(tel) {
tel = to_full_in_mobile_phone(tel);
if (!tel)
return tel;
return (0, utils_1.format_tel_with_pattern)(tel, '+91 xxxxx xxxxx');
}