{"version":3,"sources":["../source/formatNumberForMobileDialing.js"],"names":["Metadata","format","get_number_type","checkNumberLengthForType","getCountryCallingCode","REGION_CODE_FOR_NON_GEO_ENTITY","COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX","number","from_country","with_formatting","metadata","hasCountry","Error","phone","country","number_type","undefined","is_valid_number","formatted_number","is_fixed_line_or_mobile","formatNationalNumberWithCarrierCode","getPreferredDomesticCarrierCodeOrDefault","length","formatNationalNumberWithPreferredCarrierCode","can_be_internationally_dialled","diallable_chars","DIALLABLE_CHARACTERS","result","i","character"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AAEA,OAAOA,QAAP,MAAqB,YAArB;AACA,OAAOC,MAAP,MAAmB,WAAnB;AACA,OAAOC,eAAP,IAA0BC,wBAA1B,QAA0D,kBAA1D;AACA,OAAOC,qBAAP,MAAkC,yBAAlC;AAEA,IAAMC,8BAA8B,GAAG,KAAvC;AAEA;;;;;AAIA,IAAMC,oCAAoC,GAAG,GAA7C;AAEA;;;;;;;;;;;;;AAYA,eAAe,UAASC,MAAT,EAAiBC,YAAjB,EAA+BC,eAA/B,EAAgDC,QAAhD,EACf;AACCA,EAAAA,QAAQ,GAAG,IAAIV,QAAJ,CAAaU,QAAb,CAAX,CADD,CAGC;;AACA,MAAI,CAACA,QAAQ,CAACC,UAAT,CAAoBH,YAApB,CAAL,EACA;AACC,UAAM,IAAII,KAAJ,4BAA8BJ,YAA9B,EAAN;AACA,GAPF,CASC;AACA;;;AACAD,EAAAA,MAAM,GACN;AACCM,IAAAA,KAAK,EAAKN,MAAM,CAACM,KADlB;AAECC,IAAAA,OAAO,EAAGP,MAAM,CAACO;AAFlB,GADA;AAMA,MAAMC,WAAW,GAAGb,eAAe,CAACK,MAAD,EAASS,SAAT,EAAoBN,QAAQ,CAACA,QAA7B,CAAnC;AACA,MAAMO,eAAe,GAAGF,WAAW,KAAKR,MAAxC;AAEA,MAAIW,gBAAJ;;AAEA,MAAIJ,OAAO,KAAKN,YAAhB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A;AACCF,MAAAA,MAAM,IAAIE,SAAV;AACA;;AACDD,IAAAA,CAAC;AACD;;AAED,SAAOD,MAAP;AACA;;AAED,SAASN,wCAAT,GACA;AACC,QAAM,IAAIT,KAAJ,CAAU,4CAAV,CAAN;AACA;;AAED,SAASQ,mCAAT,GACA;AACC,QAAM,IAAIR,KAAJ,CAAU,4CAAV,CAAN;AACA;;AAED,SAASW,4CAAT,GACA;AACC,QAAM,IAAIX,KAAJ,CAAU,4CAAV,CAAN;AACA","sourcesContent":["// This function is copy-pasted from\r\n// https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js\r\n// It hasn't been tested.\r\n// Carriers codes aren't part of this library.\r\n// Send a PR if you want to add them.\r\n\r\nimport Metadata from './metadata'\r\nimport format from './format_'\r\nimport get_number_type, { checkNumberLengthForType } from './getNumberType_'\r\nimport getCountryCallingCode from './getCountryCallingCode'\r\n\r\nconst REGION_CODE_FOR_NON_GEO_ENTITY = '001'\r\n\r\n/**\r\n * The prefix that needs to be inserted in front of a Colombian landline number\r\n * when dialed from a mobile phone in Colombia.\r\n */\r\nconst COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = '3'\r\n\r\n/**\r\n * Returns a number formatted in such a way that it can be dialed from a mobile\r\n * phone in a specific region. If the number cannot be reached from the region\r\n * (e.g. some countries block toll-free numbers from being called outside of the\r\n * country), the method returns an empty string.\r\n *\r\n * @param {object} number - a `parse()`d phone number to be formatted.\r\n * @param {string} from_country - the region where the call is being placed.\r\n * @param {boolean} with_formatting - whether the number should be returned with\r\n * formatting symbols, such as spaces and dashes.\r\n * @return {string}\r\n */\r\nexport default function(number, from_country, with_formatting, metadata)\r\n{\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// Validate `from_country`.\r\n\tif (!metadata.hasCountry(from_country))\r\n\t{\r\n\t\tthrow new Error(`Unknown country: ${from_country}`)\r\n\t}\r\n\r\n\t// Not using the extension, as that part cannot normally be dialed\r\n\t// together with the main number.\r\n\tnumber =\r\n\t{\r\n\t\tphone : number.phone,\r\n\t\tcountry : number.country\r\n\t}\r\n\r\n\tconst number_type = get_number_type(number, undefined, metadata.metadata)\r\n\tconst is_valid_number = number_type === number\r\n\r\n\tlet formatted_number\r\n\r\n\tif (country === from_country)\r\n\t{\r\n\t\tconst is_fixed_line_or_mobile =\r\n\t\t\tnumber_type === 'FIXED_LINE' ||\r\n\t\t\tnumber_type === 'MOBILE' ||\r\n\t\t\tnumber_type === 'FIXED_LINE_OR_MOBILE'\r\n\r\n\t\t// Carrier codes may be needed in some countries. We handle this here.\r\n\t\tif (country === 'CO' && number_type === 'FIXED_LINE')\r\n\t\t{\r\n\t\t\tformatted_number = formatNationalNumberWithCarrierCode\r\n\t\t\t(\r\n\t\t\t\tnumber,\r\n\t\t\t\tCOLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX\r\n\t\t\t)\r\n\t\t}\r\n\t\telse if (country == 'BR' && is_fixed_line_or_mobile)\r\n\t\t{\r\n\t\t\tformatted_number =\r\n\t\t\t\t// Historically, we set this to an empty string when parsing with raw\r\n\t\t\t\t// input if none was found in the input string. However, this doesn't\r\n\t\t\t\t// result in a number we can dial. For this reason, we treat the empty\r\n\t\t\t\t// string the same as if it isn't set at all.\r\n\t\t\t\tgetPreferredDomesticCarrierCodeOrDefault(number).length > 0 ?\r\n\t\t\t\tformatNationalNumberWithPreferredCarrierCode(number, '') :\r\n\t\t\t\t// Brazilian fixed line and mobile numbers need to be dialed with a\r\n\t\t\t\t// carrier code when called within Brazil. Without that, most of the\r\n\t\t\t\t// carriers won't connect the call. Because of that, we return an\r\n\t\t\t\t// empty string here.\r\n\t\t\t\t''\r\n\t\t}\r\n\t\telse if (getCountryCallingCode(country, metadata.metadata) === '1')\r\n\t\t{\r\n\t\t\t// For NANPA countries, we output international format for numbers that\r\n\t\t\t// can be dialed internationally, since that always works, except for\r\n\t\t\t// numbers which might potentially be short numbers, which are always\r\n\t\t\t// dialled in national format.\r\n\r\n\t\t\t// Select country for `checkNumberLengthForType()`.\r\n\t\t\tmetadata.country(country)\r\n\r\n\t\t\tif (can_be_internationally_dialled(number) &&\r\n\t\t\t\tcheckNumberLengthForType(number.phone, undefined, metadata) !== 'TOO_SHORT')\r\n\t\t\t{\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t// For non-geographic countries, Mexican and Chilean fixed line and\r\n\t\t\t// mobile numbers, we output international format for numbers that can be\r\n\t\t\t// dialed internationally, as that always works.\r\n\t\t\tif\r\n\t\t\t(\r\n\t\t\t\t(\r\n\t\t\t\t\tcountry === REGION_CODE_FOR_NON_GEO_ENTITY\r\n\t\t\t\t\t||\r\n\t\t\t\t\t// MX fixed line and mobile numbers should always be formatted in\r\n\t\t\t\t\t// international format, even when dialed within MX. For national\r\n\t\t\t\t\t// format to work, a carrier code needs to be used, and the correct\r\n\t\t\t\t\t// carrier code depends on if the caller and callee are from the\r\n\t\t\t\t\t// same local area. It is trickier to get that to work correctly than\r\n\t\t\t\t\t// using international format, which is tested to work fine on all\r\n\t\t\t\t\t// carriers.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// CL fixed line numbers need the national prefix when dialing in the\r\n\t\t\t\t\t// national format, but don't have it when used for display. The\r\n\t\t\t\t\t// reverse is true for mobile numbers. As a result, we output them in\r\n\t\t\t\t\t// the international format to make it work.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// UZ mobile and fixed-line numbers have to be formatted in\r\n\t\t\t\t\t// international format or prefixed with special codes like 03, 04\r\n\t\t\t\t\t// (for fixed-line) and 05 (for mobile) for dialling successfully\r\n\t\t\t\t\t// from mobile devices. As we do not have complete information on\r\n\t\t\t\t\t// special codes and to be consistent with formatting across all\r\n\t\t\t\t\t// phone types we return the number in international format here.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t((country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile)\r\n\t\t\t\t)\r\n\t\t\t\t&&\r\n\t\t\t\tcan_be_internationally_dialled(number)\r\n\t\t\t)\r\n\t\t\t{\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL')\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL')\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\telse if (is_valid_number && can_be_internationally_dialled(number))\r\n\t{\r\n\t\t// We assume that short numbers are not diallable from outside their region,\r\n\t\t// so if a number is not a valid regular length phone number, we treat it as\r\n\t\t// if it cannot be internationally dialled.\r\n\t\treturn with_formatting ?\r\n\t\t\tformat(number, 'INTERNATIONAL', metadata.metadata) :\r\n\t\t\tformat(number, 'E.164', metadata.metadata)\r\n\t}\r\n\r\n\tif (!with_formatting)\r\n\t{\r\n\t\treturn diallable_chars(formatted_number)\r\n\t}\r\n\r\n\treturn formatted_number\r\n}\r\n\r\nfunction can_be_internationally_dialled(number)\r\n{\r\n\treturn true\r\n}\r\n\r\n/**\r\n * A map that contains characters that are essential when dialling. That means\r\n * any of the characters in this map must not be removed from a number when\r\n * dialling, otherwise the call will not reach the intended destination.\r\n */\r\nconst DIALLABLE_CHARACTERS =\r\n{\r\n\t'0': '0',\r\n\t'1': '1',\r\n\t'2': '2',\r\n\t'3': '3',\r\n\t'4': '4',\r\n\t'5': '5',\r\n\t'6': '6',\r\n\t'7': '7',\r\n\t'8': '8',\r\n\t'9': '9',\r\n\t'+': '+',\r\n\t'*': '*',\r\n\t'#': '#'\r\n}\r\n\r\nfunction diallable_chars(formatted_number)\r\n{\r\n\tlet result = ''\r\n\r\n\tlet i = 0\r\n\twhile (i < formatted_number.length)\r\n\t{\r\n\t\tconst character = formatted_number[i]\r\n\t\tif (DIALLABLE_CHARACTERS[character])\r\n\t\t{\r\n\t\t\tresult += character\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn result\r\n}\r\n\r\nfunction getPreferredDomesticCarrierCodeOrDefault()\r\n{\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\nfunction formatNationalNumberWithCarrierCode()\r\n{\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\nfunction formatNationalNumberWithPreferredCarrierCode()\r\n{\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}"],"file":"formatNumberForMobileDialing.js"}