@beenotung/tslib
Version:
utils library in Typescript
126 lines (125 loc) • 4.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is_mobile_phone = is_mobile_phone;
exports.to_full_mobile_phone = to_full_mobile_phone;
exports.format_mobile_phone = format_mobile_phone;
const tslib_1 = require("tslib");
const registry_1 = require("./tel/registry");
// export utility functions
tslib_1.__exportStar(require("./tel/utils"), exports);
// export all region-specific functions
tslib_1.__exportStar(require("./tel/hk"), exports);
tslib_1.__exportStar(require("./tel/sg"), exports);
tslib_1.__exportStar(require("./tel/au"), exports);
tslib_1.__exportStar(require("./tel/cn"), exports);
tslib_1.__exportStar(require("./tel/mo"), exports);
tslib_1.__exportStar(require("./tel/ae"), exports);
tslib_1.__exportStar(require("./tel/th"), exports);
tslib_1.__exportStar(require("./tel/in"), exports);
tslib_1.__exportStar(require("./tel/jp"), exports);
tslib_1.__exportStar(require("./tel/vn"), exports);
tslib_1.__exportStar(require("./tel/id"), exports);
tslib_1.__exportStar(require("./tel/my"), exports);
tslib_1.__exportStar(require("./tel/us"), exports);
tslib_1.__exportStar(require("./tel/ch"), exports);
tslib_1.__exportStar(require("./tel/fr"), exports);
tslib_1.__exportStar(require("./tel/de"), exports);
tslib_1.__exportStar(require("./tel/it"), exports);
tslib_1.__exportStar(require("./tel/es"), exports);
tslib_1.__exportStar(require("./tel/nl"), exports);
tslib_1.__exportStar(require("./tel/be"), exports);
tslib_1.__exportStar(require("./tel/at"), exports);
/** ********************************
* Combined for multiple countries *
***********************************/
function is_mobile_phone(tel) {
return to_full_mobile_phone(tel) !== '';
}
/**
* check if the tel is mobile phone number in:
* - HK (Hong Kong)
* - SG (Singapore)
* - AU (Australia)
* - CN (China Mainland)
* - MO (Macau)
* - AE (UAE/Dubai)
* - TH (Thailand)
* - IN (India)
* - JP (Japan)
* - VN (Vietnam)
* - ID (Indonesia)
* - MY (Malaysia)
* - US (United States)
* - CH (Switzerland)
* - FR (France)
* - DE (Germany)
* - IT (Italy)
* - ES (Spain)
* - NL (Netherlands)
* - BE (Belgium)
* - AT (Austria)
*
* Note: Some numbers without country codes are ambiguous and may match multiple countries.
* Countries with more unique prefixes are checked first to minimize conflicts.
* For accurate detection, include the country code (e.g., +33 for France).
*
* @returns the full tel number with country code, or empty string if not valid
*/
function to_full_mobile_phone(tel) {
if (typeof tel == 'number') {
tel = tel.toString();
}
// If country code is present, check that country first
if (tel.startsWith('+')) {
for (const handler of registry_1.tel_handlers) {
if (tel.startsWith(handler.country_code)) {
const result = handler.to_full(tel);
if (result)
return result;
}
}
}
// No country code or not found: Check all countries in priority order
// Countries with unique patterns are checked first to minimize false matches
for (const handler of registry_1.tel_handlers) {
const result = handler.to_full(tel);
if (result)
return result;
}
return '';
}
/**
* format the mobile phone number with pattern if valid:
* - HK: +852 xxxx yyyy
* - SG: +65 xxxx yyyy
* - AU: +61 4xx xxx xxx
* - CN: +86 1nn xxxx xxxx
* - MO: +853 xxxx yyyy
* - AE: +971 5x xxx xxxx (accepts both local 05x xxx xxxx and international formats)
* - TH: +66 8x xxx xxxx
* - IN: +91 xxxxx xxxxx
* - JP: +81 90 xxxx xxxx
* - VN: +84 9xx xxx xxxx
* - ID: +62 8xx xxx xxx (format varies by length: 9-11 digits)
* - MY: +60 XX XXX XXXX (format varies by length: 9 or 10 digits)
* - US: +1 xxx xxx xxxx
* - CH: +41 7x xxx xx xx
* - FR: +33 x xx xx xx xx
* - DE: +49 15x xxx xxxx (variable length)
* - IT: +39 3xx xxx xxxx
* - ES: +34 xxx xxx xxx
* - NL: +31 6 xxxx xxxx
* - BE: +32 4xx xx xx xx
* - AT: +43 6xx xxx xxxx (variable length)
*/
function format_mobile_phone(tel) {
tel = to_full_mobile_phone(tel);
if (!tel)
return tel;
for (const handler of registry_1.tel_handlers) {
if (tel.startsWith(handler.country_code)) {
return handler.format(tel);
}
}
throw new Error(`not supported mobile phone number: ${tel}`);
}