You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.2 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = extractCountryCallingCode;
var _stripIddPrefix = _interopRequireDefault(require("./stripIddPrefix.js"));
var _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = _interopRequireDefault(require("./extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"));
var _metadata = _interopRequireDefault(require("../metadata.js"));
var _constants = require("../constants.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
/**
* Converts a phone number digits (possibly with a `+`)
* into a calling code and the rest phone number digits.
* The "rest phone number digits" could include
* a national prefix, carrier code, and national
* (significant) number.
* @param {string} number — Phone number digits (possibly with a `+`).
* @param {string} [country] — Default country.
* @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string }`
* @example
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
* extractCountryCallingCode('2133734253', 'US', null, metadata)
* extractCountryCallingCode('2133734253', null, '1', metadata)
* extractCountryCallingCode('+12133734253', null, null, metadata)
* extractCountryCallingCode('+12133734253', 'RU', null, metadata)
*/
function extractCountryCallingCode(number, country, callingCode, metadata) {
if (!number) {
return {};
} // If this is not an international phone number,
// then either extract an "IDD" prefix, or extract a
// country calling code from a number by autocorrecting it
// by prepending a leading `+` in cases when it starts
// with the country calling code.
// https://wikitravel.org/en/International_dialling_prefix
// https://github.com/catamphetamine/libphonenumber-js/issues/376
if (number[0] !== '+') {
// Convert an "out-of-country" dialing phone number
// to a proper international phone number.
var numberWithoutIDD = (0, _stripIddPrefix["default"])(number, country, callingCode, metadata); // If an IDD prefix was stripped then
// convert the number to international one
// for subsequent parsing.
if (numberWithoutIDD && numberWithoutIDD !== number) {
number = '+' + numberWithoutIDD;
} else {
// Check to see if the number starts with the country calling code
// for the default country. If so, we remove the country calling code,
// and do some checks on the validity of the number before and after.
// https://github.com/catamphetamine/libphonenumber-js/issues/376
if (country || callingCode) {
var _extractCountryCallin = (0, _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign["default"])(number, country, callingCode, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
shorterNumber = _extractCountryCallin.number;
if (countryCallingCode) {
return {
countryCallingCode: countryCallingCode,
number: shorterNumber
};
}
}
return {
number: number
};
}
} // Fast abortion: country codes do not begin with a '0'
if (number[1] === '0') {
return {};
}
metadata = new _metadata["default"](metadata); // The thing with country phone codes
// is that they are orthogonal to each other
// i.e. there's no such country phone code A
// for which country phone code B exists
// where B starts with A.
// Therefore, while scanning digits,
// if a valid country code is found,
// that means that it is the country code.
//
var i = 2;
while (i - 1 <= _constants.MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
var _countryCallingCode = number.slice(1, i);
if (metadata.hasCallingCode(_countryCallingCode)) {
metadata.selectNumberingPlan(_countryCallingCode);
return {
countryCallingCode: _countryCallingCode,
number: number.slice(i)
};
}
i++;
}
return {};
}
//# sourceMappingURL=extractCountryCallingCode.js.map