@beenotung/tslib
Version:
utils library in Typescript
63 lines (62 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_sg_mobile_phone_prefix = is_sg_mobile_phone_prefix;
exports.is_sg_mobile_phone = is_sg_mobile_phone;
exports.to_full_sg_mobile_phone = to_full_sg_mobile_phone;
exports.format_sg_mobile_phone = format_sg_mobile_phone;
const utils_1 = require("./utils");
/** ******************************
* Singapore mobile phone number *
*********************************/
/**
* starts with 8, 9
* reference: https://en.wikipedia.org/wiki/Telephone_numbers_in_Singapore
*/
function is_sg_mobile_phone_prefix(tel) {
tel = tel.replace(/^\+65/, '').trim();
switch (tel[0]) {
case '8':
case '9':
return true;
default:
return false;
}
}
/**
* with/without +65 prefix
*/
function is_sg_mobile_phone(tel) {
return to_full_sg_mobile_phone(tel) !== '';
}
/**
* very forgiving
*
* @returns +65xxxxyyyy if valid
* empty string if not valid
*/
function to_full_sg_mobile_phone(tel) {
tel = (0, utils_1.to_tel_digits)(tel);
if (tel.length === 8 && is_sg_mobile_phone_prefix(tel)) {
return '+65' + tel;
}
if (tel.length === 8 + 2 &&
tel.startsWith('65') &&
is_sg_mobile_phone_prefix(tel.substring(2))) {
return '+' + tel;
}
if (tel.length === 8 + 3 &&
tel.startsWith('+65') &&
is_sg_mobile_phone_prefix(tel.substring(3))) {
return tel;
}
return '';
}
/**
* @returns +65 xxxx yyyy if valid
*/
function format_sg_mobile_phone(tel) {
tel = to_full_sg_mobile_phone(tel);
if (!tel)
return tel;
return (0, utils_1.format_tel_with_pattern)(tel, '+65 xxxx yyyy');
}