@beenotung/tslib
Version:
utils library in Typescript
83 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_email = exports.to_full_hk_mobile_phone = exports.is_hk_mobile_phone = exports.is_hk_mobile_phone_prefix = void 0;
/**
* 4, 7, 8 heading are allowed since 2018
* news: https://skypost.ulifestyle.com.hk/article/2006268/
* */
function is_hk_mobile_phone_prefix(s) {
s = s.replace(/^\+852/, '').trim();
switch (s[0]) {
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
}
exports.is_hk_mobile_phone_prefix = is_hk_mobile_phone_prefix;
/**
* with/without +852 prefix
* */
function is_hk_mobile_phone(x) {
return to_full_hk_mobile_phone(x) !== '';
}
exports.is_hk_mobile_phone = is_hk_mobile_phone;
/**
* very forgiving
*
* @return +852xxxxyyyy if valid
* empty string if not valid
* */
function to_full_hk_mobile_phone(s) {
if (typeof s === 'number') {
s = s.toString();
}
s = s
.split('')
.filter(x => '0' <= x && x <= '9')
.join('');
if (s.length === 8 && is_hk_mobile_phone_prefix(s)) {
return '+852' + s;
}
if (s.length === 8 + 3 &&
s.startsWith('852') &&
is_hk_mobile_phone_prefix(s.substr(3))) {
return '+' + s;
}
if (s.length === 8 + 4 &&
s.startsWith('+852') &&
is_hk_mobile_phone_prefix(s.substr(4))) {
return s;
}
return '';
}
exports.to_full_hk_mobile_phone = to_full_hk_mobile_phone;
function is_email(s) {
if (!s) {
return false;
}
const ss = s.split('@');
if (ss.length !== 2) {
return false;
}
const domain = ss[1];
if (domain !== domain.trim()) {
return false;
}
const names = domain.split('.');
if (names.some(s => !s) || names.length < 2) {
return false;
}
const username = ss[0];
if (username !== username.trim()) {
return false;
}
return username.length > 0;
}
exports.is_email = is_email;
//# sourceMappingURL=validate.js.map