@beenotung/tslib
Version:
utils library in Typescript
73 lines (72 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_hk_mobile_phone_prefix = is_hk_mobile_phone_prefix;
exports.is_hk_mobile_phone = is_hk_mobile_phone;
exports.to_full_hk_mobile_phone = to_full_hk_mobile_phone;
exports.format_hk_mobile_phone = format_hk_mobile_phone;
const utils_1 = require("./utils");
/** ******************************
* Hong Kong mobile phone number *
*********************************/
/**
* 4, 7, 8 heading are allowed since 2018
* news: https://skypost.ulifestyle.com.hk/article/2006268/
* */
function is_hk_mobile_phone_prefix(tel) {
tel = tel.replace(/^\+852/, '').trim();
switch (tel[0]) {
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
}
/**
* with/without +852 prefix
* */
function is_hk_mobile_phone(tel) {
return to_full_hk_mobile_phone(tel) !== '';
}
/**
* very forgiving
*
* @return +852xxxxyyyy if valid
* empty string if not valid
* */
function to_full_hk_mobile_phone(tel) {
if (typeof tel === 'number') {
tel = tel.toString();
}
tel = tel
.split('')
.filter(x => '0' <= x && x <= '9')
.join('');
if (tel.length === 8 && is_hk_mobile_phone_prefix(tel)) {
return '+852' + tel;
}
if (tel.length === 8 + 3 &&
tel.startsWith('852') &&
is_hk_mobile_phone_prefix(tel.substring(3))) {
return '+' + tel;
}
if (tel.length === 8 + 4 &&
tel.startsWith('+852') &&
is_hk_mobile_phone_prefix(tel.substring(4))) {
return tel;
}
return '';
}
/**
* @returns +852 xxxx yyyy if valid
*/
function format_hk_mobile_phone(tel) {
tel = to_full_hk_mobile_phone(tel);
if (!tel)
return tel;
return (0, utils_1.format_tel_with_pattern)(tel, '+852 xxxx yyyy');
}