123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import Metadata from './metadata.js';
- import format from './format.js';
- import getNumberType from './helpers/getNumberType.js';
- import checkNumberLength from './helpers/checkNumberLength.js';
- import getCountryCallingCode from './getCountryCallingCode.js';
- var REGION_CODE_FOR_NON_GEO_ENTITY = '001';
- export default function (number, from_country, with_formatting, metadata) {
- metadata = new Metadata(metadata);
- if (!metadata.hasCountry(from_country)) {
- throw new Error("Unknown country: ".concat(from_country));
- }
-
- number = {
- phone: number.phone,
- country: number.country
- };
- var number_type = getNumberType(number, undefined, metadata.metadata);
- var is_valid_number = number_type === number;
- var formatted_number;
- if (country === from_country) {
- var is_fixed_line_or_mobile = number_type === 'FIXED_LINE' || number_type === 'MOBILE' || number_type === 'FIXED_LINE_OR_MOBILE';
- if (country == 'BR' && is_fixed_line_or_mobile) {
- formatted_number = carrierCode ? formatNationalNumberWithPreferredCarrierCode(number) :
-
-
-
- '';
- } else if (getCountryCallingCode(country, metadata.metadata) === '1') {
-
-
-
-
-
- metadata.country(country);
- if (can_be_internationally_dialled(number) && checkNumberLength(number.phone, metadata) !== 'TOO_SHORT') {
- formatted_number = format(number, 'INTERNATIONAL', metadata.metadata);
- } else {
- formatted_number = format(number, 'NATIONAL', metadata.metadata);
- }
- } else {
-
-
-
- if ((country === REGION_CODE_FOR_NON_GEO_ENTITY ||
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- (country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile) && can_be_internationally_dialled(number)) {
- formatted_number = format(number, 'INTERNATIONAL');
- } else {
- formatted_number = format(number, 'NATIONAL');
- }
- }
- } else if (is_valid_number && can_be_internationally_dialled(number)) {
-
-
-
- return with_formatting ? format(number, 'INTERNATIONAL', metadata.metadata) : format(number, 'E.164', metadata.metadata);
- }
- if (!with_formatting) {
- return diallable_chars(formatted_number);
- }
- return formatted_number;
- }
- function can_be_internationally_dialled(number) {
- return true;
- }
- var DIALLABLE_CHARACTERS = {
- '0': '0',
- '1': '1',
- '2': '2',
- '3': '3',
- '4': '4',
- '5': '5',
- '6': '6',
- '7': '7',
- '8': '8',
- '9': '9',
- '+': '+',
- '*': '*',
- '#': '#'
- };
- function diallable_chars(formatted_number) {
- var result = '';
- var i = 0;
- while (i < formatted_number.length) {
- var character = formatted_number[i];
- if (DIALLABLE_CHARACTERS[character]) {
- result += character;
- }
- i++;
- }
- return result;
- }
- function getPreferredDomesticCarrierCodeOrDefault() {
- throw new Error('carrier codes are not part of this library');
- }
- function formatNationalNumberWithCarrierCode() {
- throw new Error('carrier codes are not part of this library');
- }
- function formatNationalNumberWithPreferredCarrierCode(number) {
- return formatNationalNumberWithCarrierCode(number, carrierCode);
- }
|