Socket
Socket
Sign inDemoInstall

libphonenumber-js

Package Overview
Dependencies
Maintainers
1
Versions
392
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libphonenumber-js - npm Package Compare versions

Comparing version 1.7.42 to 1.7.43

23

build/AsYouType.js

@@ -983,12 +983,21 @@ "use strict";

var callingCode = this.countryCallingCode || this.defaultCallingCode;
var nationalNumber = this.nationalNumberDigits; // When an international number without a leading `+` has been autocorrected,
var nationalNumber = this.nationalNumberDigits;
var carrierCode = this.carrierCode; // When an international number without a leading `+` has been autocorrected,
// extract country calling code, because normally it's only extracted
// for international numbers with a leading `+`.
// Could also just use `parsePhoneNumberFromString()` here
// instead of hacking around this single case.
if (!this.isInternational()) {
var _extractCountryCallin2 = (0, _parse_.extractCountryCallingCode)(this.nationalNumberDigits, countryCode, callingCode, this.metadata.metadata),
shorterNationalNumber = _extractCountryCallin2.number;
if (!this.isInternational() && this.nationalNumberDigits === this.digits) {
var _extractCountryCallin2 = (0, _parse_.extractCountryCallingCodeFromInternationalNumberWithoutPlusSign)(this.digits, countryCode, callingCode, this.metadata.metadata),
countryCallingCode = _extractCountryCallin2.countryCallingCode,
number = _extractCountryCallin2.number;
if (shorterNationalNumber !== nationalNumber) {
if (countryCallingCode) {
var _stripNationalPrefixA2 = (0, _parse_.stripNationalPrefixAndCarrierCodeFromCompleteNumber)(number, this.metadata),
shorterNationalNumber = _stripNationalPrefixA2.nationalNumber,
newCarrierCode = _stripNationalPrefixA2.carrierCode;
nationalNumber = shorterNationalNumber;
carrierCode = newCarrierCode;
}

@@ -999,4 +1008,4 @@ }

if (this.carrierCode) {
phoneNumber.carrierCode = this.carrierCode;
if (carrierCode) {
phoneNumber.carrierCode = carrierCode;
} // Phone number extensions are not supported by "As You Type" formatter.

@@ -1003,0 +1012,0 @@

@@ -557,2 +557,21 @@ "use strict";

formatter.getNumber().nationalNumber.should.equal('612902554');
formatter.getNumber().number.should.equal('+33612902554'); // Should also strip national prefix.
formatter.reset();
formatter.input('330612902554').should.equal('330612902554');
formatter.getNumber().country.should.equal('FR');
formatter.getNumber().nationalNumber.should.equal('612902554');
formatter.getNumber().number.should.equal('+33612902554'); // On second thought, this "prepend default area code" feature won't be added,
// because when a user selects "British Virgin Islands" and inputs
// "2291234", then they see "(229) 123-4" which clearly indicates that
// they should input the complete phone number (with area code).
// So, unless a user completely doesn't understand what they're doing,
// they'd input the complete phone number (with area code).
// // Should prepend the default area code in British Virgin Islands.
// // https://github.com/catamphetamine/react-phone-number-input/issues/335
// const formatter2 = new AsYouType('VG')
// formatter2.input('2291234').should.equal('(229) 123-4')
// formatter2.getNumber().country.should.equal('VG')
// formatter2.getNumber().nationalNumber.should.equal('2842291234')
// formatter2.getNumber().number.should.equal('+12842291234')
});

@@ -559,0 +578,0 @@ it('shouldn\'t choose a format when there\'re too many digits for any of them', function () {

@@ -10,3 +10,5 @@ "use strict";

exports.findCountryCode = findCountryCode;
exports.stripNationalPrefixAndCarrierCodeFromCompleteNumber = stripNationalPrefixAndCarrierCodeFromCompleteNumber;
exports.extractCountryCallingCode = extractCountryCallingCode;
exports.extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign;

@@ -228,3 +230,4 @@ var _constants = require("./constants");

/**
* Strips any national prefix (such as 0, 1) present in the number provided.
* Strips any national prefix (such as 0, 1) present in a
* (possibly incomplete) number provided.
* "Carrier codes" are only used in Colombia and Brazil,

@@ -238,3 +241,3 @@ * and only when dialing within those countries from a mobile phone to a fixed line number.

* @param {object} metadata — Metadata with country selected.
* @return {object} `{ nationalNumber: string?, carrierCode: string? }`.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/

@@ -449,33 +452,7 @@

};
} // Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
}
var _stripNationalPrefixA = stripNationalPrefixAndCarrierCode((0, _parseIncompletePhoneNumber["default"])(number), metadata),
var _stripNationalPrefixA = stripNationalPrefixAndCarrierCodeFromCompleteNumber((0, _parseIncompletePhoneNumber["default"])(number), metadata),
nationalNumber = _stripNationalPrefixA.nationalNumber,
carrierCode = _stripNationalPrefixA.carrierCode; // If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch ((0, _getNumberType_.checkNumberLengthForType)(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
nationalNumber = number;
carrierCode = undefined;
}
} // Sometimes there are several countries
carrierCode = _stripNationalPrefixA.carrierCode; // Sometimes there are several countries
// corresponding to the same country phone code

@@ -512,4 +489,56 @@ // (e.g. NANPA countries all having `1` country phone code).

};
} //
}
/**
* Strips national prefix and carrier code from a complete phone number.
* The difference from the non-"FromCompleteNumber" function is that
* it won't extract national prefix if the resultant number is too short
* to be a complete number for the selected phone numbering plan.
* @param {string} number — Complete phone number digits.
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/
function stripNationalPrefixAndCarrierCodeFromCompleteNumber(number, metadata) {
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
var _stripNationalPrefixA2 = stripNationalPrefixAndCarrierCode((0, _parseIncompletePhoneNumber["default"])(number), metadata),
nationalNumber = _stripNationalPrefixA2.nationalNumber,
carrierCode = _stripNationalPrefixA2.carrierCode; // If a national prefix has been extracted, check to see
// if the resultant number isn't too short.
if (nationalNumber.length !== number.length + (carrierCode ? carrierCode.length : 0)) {
// If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch ((0, _getNumberType_.checkNumberLengthForType)(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
// Don't strip the national prefix.
return {
nationalNumber: number
};
}
}
}
return {
nationalNumber: nationalNumber,
carrierCode: carrierCode
};
}
/**

@@ -525,3 +554,3 @@ * Converts a phone number digits (possibly with a `+`)

* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string? }`
* @return {object} `{ countryCallingCode: string?, number: string }`
* @example

@@ -563,29 +592,11 @@ * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.

if (country || callingCode) {
var countryCallingCode = country ? (0, _getCountryCallingCode["default"])(country, metadata) : callingCode;
var _extractCountryCallin2 = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata),
countryCallingCode = _extractCountryCallin2.countryCallingCode,
shorterNumber = _extractCountryCallin2.number;
if (number.indexOf(countryCallingCode) === 0) {
metadata = new _metadata["default"](metadata);
metadata.selectNumberingPlan(country, callingCode);
var possibleShorterNumber = number.slice(countryCallingCode.length);
var _stripNationalPrefixA2 = stripNationalPrefixAndCarrierCode(possibleShorterNumber, metadata),
possibleShorterNationalNumber = _stripNationalPrefixA2.nationalNumber;
var _stripNationalPrefixA3 = stripNationalPrefixAndCarrierCode(number, metadata),
nationalNumber = _stripNationalPrefixA3.nationalNumber; // If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (!(0, _util.matchesEntirely)(nationalNumber, metadata.nationalNumberPattern()) && (0, _util.matchesEntirely)(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || (0, _getNumberType_.checkNumberLengthForType)(nationalNumber, undefined, metadata) === 'TOO_LONG') {
return {
countryCallingCode: countryCallingCode,
number: possibleShorterNumber
};
}
if (countryCallingCode) {
return {
countryCallingCode: countryCallingCode,
number: shorterNumber
};
}

@@ -633,2 +644,47 @@ }

}
/**
* Sometimes some people incorrectly input international phone numbers
* without the leading `+`. This function corrects such input.
* @param {string} number — Phone number digits.
* @param {string?} country
* @param {string?} callingCode
* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string }`.
*/
function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata) {
var countryCallingCode = country ? (0, _getCountryCallingCode["default"])(country, metadata) : callingCode;
if (number.indexOf(countryCallingCode) === 0) {
metadata = new _metadata["default"](metadata);
metadata.selectNumberingPlan(country, callingCode);
var possibleShorterNumber = number.slice(countryCallingCode.length);
var _stripNationalPrefixA3 = stripNationalPrefixAndCarrierCode(possibleShorterNumber, metadata),
possibleShorterNationalNumber = _stripNationalPrefixA3.nationalNumber;
var _stripNationalPrefixA4 = stripNationalPrefixAndCarrierCode(number, metadata),
nationalNumber = _stripNationalPrefixA4.nationalNumber; // If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (!(0, _util.matchesEntirely)(nationalNumber, metadata.nationalNumberPattern()) && (0, _util.matchesEntirely)(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || (0, _getNumberType_.checkNumberLengthForType)(nationalNumber, undefined, metadata) === 'TOO_LONG') {
return {
countryCallingCode: countryCallingCode,
number: possibleShorterNumber
};
}
}
return {
number: number
};
}
//# sourceMappingURL=parse_.js.map

@@ -24,3 +24,3 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { matchesEntirely } from './util';
import { extractCountryCallingCode as _extractCountryCallingCode, extractFormattedPhoneNumber, findCountryCode, stripNationalPrefixAndCarrierCode } from './parse_';
import { extractCountryCallingCode as _extractCountryCallingCode, extractFormattedPhoneNumber, findCountryCode, stripNationalPrefixAndCarrierCode, stripNationalPrefixAndCarrierCodeFromCompleteNumber, extractCountryCallingCodeFromInternationalNumberWithoutPlusSign } from './parse_';
import { FIRST_GROUP_PATTERN, formatNationalNumberUsingFormat, applyInternationalSeparatorStyle } from './format_';

@@ -970,12 +970,21 @@ import { checkNumberLengthForType } from './getNumberType_';

var callingCode = this.countryCallingCode || this.defaultCallingCode;
var nationalNumber = this.nationalNumberDigits; // When an international number without a leading `+` has been autocorrected,
var nationalNumber = this.nationalNumberDigits;
var carrierCode = this.carrierCode; // When an international number without a leading `+` has been autocorrected,
// extract country calling code, because normally it's only extracted
// for international numbers with a leading `+`.
// Could also just use `parsePhoneNumberFromString()` here
// instead of hacking around this single case.
if (!this.isInternational()) {
var _extractCountryCallin2 = _extractCountryCallingCode(this.nationalNumberDigits, countryCode, callingCode, this.metadata.metadata),
shorterNationalNumber = _extractCountryCallin2.number;
if (!this.isInternational() && this.nationalNumberDigits === this.digits) {
var _extractCountryCallin2 = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(this.digits, countryCode, callingCode, this.metadata.metadata),
countryCallingCode = _extractCountryCallin2.countryCallingCode,
number = _extractCountryCallin2.number;
if (shorterNationalNumber !== nationalNumber) {
if (countryCallingCode) {
var _stripNationalPrefixA2 = stripNationalPrefixAndCarrierCodeFromCompleteNumber(number, this.metadata),
shorterNationalNumber = _stripNationalPrefixA2.nationalNumber,
newCarrierCode = _stripNationalPrefixA2.carrierCode;
nationalNumber = shorterNationalNumber;
carrierCode = newCarrierCode;
}

@@ -986,4 +995,4 @@ }

if (this.carrierCode) {
phoneNumber.carrierCode = this.carrierCode;
if (carrierCode) {
phoneNumber.carrierCode = carrierCode;
} // Phone number extensions are not supported by "As You Type" formatter.

@@ -990,0 +999,0 @@

@@ -550,2 +550,21 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

formatter.getNumber().nationalNumber.should.equal('612902554');
formatter.getNumber().number.should.equal('+33612902554'); // Should also strip national prefix.
formatter.reset();
formatter.input('330612902554').should.equal('330612902554');
formatter.getNumber().country.should.equal('FR');
formatter.getNumber().nationalNumber.should.equal('612902554');
formatter.getNumber().number.should.equal('+33612902554'); // On second thought, this "prepend default area code" feature won't be added,
// because when a user selects "British Virgin Islands" and inputs
// "2291234", then they see "(229) 123-4" which clearly indicates that
// they should input the complete phone number (with area code).
// So, unless a user completely doesn't understand what they're doing,
// they'd input the complete phone number (with area code).
// // Should prepend the default area code in British Virgin Islands.
// // https://github.com/catamphetamine/react-phone-number-input/issues/335
// const formatter2 = new AsYouType('VG')
// formatter2.input('2291234').should.equal('(229) 123-4')
// formatter2.getNumber().country.should.equal('VG')
// formatter2.getNumber().nationalNumber.should.equal('2842291234')
// formatter2.getNumber().number.should.equal('+12842291234')
});

@@ -552,0 +571,0 @@ it('shouldn\'t choose a format when there\'re too many digits for any of them', function () {

@@ -198,3 +198,4 @@ // This is a port of Google Android `libphonenumber`'s

/**
* Strips any national prefix (such as 0, 1) present in the number provided.
* Strips any national prefix (such as 0, 1) present in a
* (possibly incomplete) number provided.
* "Carrier codes" are only used in Colombia and Brazil,

@@ -208,3 +209,3 @@ * and only when dialing within those countries from a mobile phone to a fixed line number.

* @param {object} metadata — Metadata with country selected.
* @return {object} `{ nationalNumber: string?, carrierCode: string? }`.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/

@@ -416,33 +417,7 @@

};
} // Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
}
var _stripNationalPrefixA = stripNationalPrefixAndCarrierCode(parseIncompletePhoneNumber(number), metadata),
var _stripNationalPrefixA = stripNationalPrefixAndCarrierCodeFromCompleteNumber(parseIncompletePhoneNumber(number), metadata),
nationalNumber = _stripNationalPrefixA.nationalNumber,
carrierCode = _stripNationalPrefixA.carrierCode; // If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch (checkNumberLengthForType(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
nationalNumber = number;
carrierCode = undefined;
}
} // Sometimes there are several countries
carrierCode = _stripNationalPrefixA.carrierCode; // Sometimes there are several countries
// corresponding to the same country phone code

@@ -479,4 +454,56 @@ // (e.g. NANPA countries all having `1` country phone code).

};
} //
}
/**
* Strips national prefix and carrier code from a complete phone number.
* The difference from the non-"FromCompleteNumber" function is that
* it won't extract national prefix if the resultant number is too short
* to be a complete number for the selected phone numbering plan.
* @param {string} number — Complete phone number digits.
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/
export function stripNationalPrefixAndCarrierCodeFromCompleteNumber(number, metadata) {
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
var _stripNationalPrefixA2 = stripNationalPrefixAndCarrierCode(parseIncompletePhoneNumber(number), metadata),
nationalNumber = _stripNationalPrefixA2.nationalNumber,
carrierCode = _stripNationalPrefixA2.carrierCode; // If a national prefix has been extracted, check to see
// if the resultant number isn't too short.
if (nationalNumber.length !== number.length + (carrierCode ? carrierCode.length : 0)) {
// If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch (checkNumberLengthForType(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
// Don't strip the national prefix.
return {
nationalNumber: number
};
}
}
}
return {
nationalNumber: nationalNumber,
carrierCode: carrierCode
};
}
/**

@@ -492,3 +519,3 @@ * Converts a phone number digits (possibly with a `+`)

* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string? }`
* @return {object} `{ countryCallingCode: string?, number: string }`
* @example

@@ -502,3 +529,2 @@ * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.

export function extractCountryCallingCode(number, country, callingCode, metadata) {

@@ -531,29 +557,11 @@ if (!number) {

if (country || callingCode) {
var countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode;
var _extractCountryCallin2 = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata),
countryCallingCode = _extractCountryCallin2.countryCallingCode,
shorterNumber = _extractCountryCallin2.number;
if (number.indexOf(countryCallingCode) === 0) {
metadata = new Metadata(metadata);
metadata.selectNumberingPlan(country, callingCode);
var possibleShorterNumber = number.slice(countryCallingCode.length);
var _stripNationalPrefixA2 = stripNationalPrefixAndCarrierCode(possibleShorterNumber, metadata),
possibleShorterNationalNumber = _stripNationalPrefixA2.nationalNumber;
var _stripNationalPrefixA3 = stripNationalPrefixAndCarrierCode(number, metadata),
nationalNumber = _stripNationalPrefixA3.nationalNumber; // If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLengthForType(nationalNumber, undefined, metadata) === 'TOO_LONG') {
return {
countryCallingCode: countryCallingCode,
number: possibleShorterNumber
};
}
if (countryCallingCode) {
return {
countryCallingCode: countryCallingCode,
number: shorterNumber
};
}

@@ -601,2 +609,46 @@ }

}
/**
* Sometimes some people incorrectly input international phone numbers
* without the leading `+`. This function corrects such input.
* @param {string} number — Phone number digits.
* @param {string?} country
* @param {string?} callingCode
* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string }`.
*/
export function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata) {
var countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode;
if (number.indexOf(countryCallingCode) === 0) {
metadata = new Metadata(metadata);
metadata.selectNumberingPlan(country, callingCode);
var possibleShorterNumber = number.slice(countryCallingCode.length);
var _stripNationalPrefixA3 = stripNationalPrefixAndCarrierCode(possibleShorterNumber, metadata),
possibleShorterNationalNumber = _stripNationalPrefixA3.nationalNumber;
var _stripNationalPrefixA4 = stripNationalPrefixAndCarrierCode(number, metadata),
nationalNumber = _stripNationalPrefixA4.nationalNumber; // If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLengthForType(nationalNumber, undefined, metadata) === 'TOO_LONG') {
return {
countryCallingCode: countryCallingCode,
number: possibleShorterNumber
};
}
}
return {
number: number
};
}
//# sourceMappingURL=parse_.js.map
{
"name": "libphonenumber-js",
"version": "1.7.42",
"version": "1.7.43",
"description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",

@@ -5,0 +5,0 @@ "main": "index.common.js",

@@ -27,3 +27,5 @@ // This is an enhanced port of Google Android `libphonenumber`'s

findCountryCode,
stripNationalPrefixAndCarrierCode
stripNationalPrefixAndCarrierCode,
stripNationalPrefixAndCarrierCodeFromCompleteNumber,
extractCountryCallingCodeFromInternationalNumberWithoutPlusSign
} from './parse_'

@@ -927,10 +929,14 @@

let nationalNumber = this.nationalNumberDigits
let carrierCode = this.carrierCode
// When an international number without a leading `+` has been autocorrected,
// extract country calling code, because normally it's only extracted
// for international numbers with a leading `+`.
if (!this.isInternational()) {
// Could also just use `parsePhoneNumberFromString()` here
// instead of hacking around this single case.
if (!this.isInternational() && this.nationalNumberDigits === this.digits) {
const {
number: shorterNationalNumber
} = extractCountryCallingCode(
this.nationalNumberDigits,
countryCallingCode,
number
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
this.digits,
countryCode,

@@ -940,4 +946,12 @@ callingCode,

)
if (shorterNationalNumber !== nationalNumber) {
if (countryCallingCode) {
const {
nationalNumber: shorterNationalNumber,
carrierCode: newCarrierCode
} = stripNationalPrefixAndCarrierCodeFromCompleteNumber(
number,
this.metadata
)
nationalNumber = shorterNationalNumber
carrierCode = newCarrierCode
}

@@ -950,4 +964,4 @@ }

)
if (this.carrierCode) {
phoneNumber.carrierCode = this.carrierCode
if (carrierCode) {
phoneNumber.carrierCode = carrierCode
}

@@ -954,0 +968,0 @@ // Phone number extensions are not supported by "As You Type" formatter.

@@ -651,2 +651,22 @@ import metadata from '../metadata.min.json'

formatter.getNumber().nationalNumber.should.equal('612902554')
formatter.getNumber().number.should.equal('+33612902554')
// Should also strip national prefix.
formatter.reset()
formatter.input('330612902554').should.equal('330612902554')
formatter.getNumber().country.should.equal('FR')
formatter.getNumber().nationalNumber.should.equal('612902554')
formatter.getNumber().number.should.equal('+33612902554')
// On second thought, this "prepend default area code" feature won't be added,
// because when a user selects "British Virgin Islands" and inputs
// "2291234", then they see "(229) 123-4" which clearly indicates that
// they should input the complete phone number (with area code).
// So, unless a user completely doesn't understand what they're doing,
// they'd input the complete phone number (with area code).
// // Should prepend the default area code in British Virgin Islands.
// // https://github.com/catamphetamine/react-phone-number-input/issues/335
// const formatter2 = new AsYouType('VG')
// formatter2.input('2291234').should.equal('(229) 123-4')
// formatter2.getNumber().country.should.equal('VG')
// formatter2.getNumber().nationalNumber.should.equal('2842291234')
// formatter2.getNumber().number.should.equal('+12842291234')
})

@@ -653,0 +673,0 @@

@@ -216,3 +216,4 @@ // This is a port of Google Android `libphonenumber`'s

/**
* Strips any national prefix (such as 0, 1) present in the number provided.
* Strips any national prefix (such as 0, 1) present in a
* (possibly incomplete) number provided.
* "Carrier codes" are only used in Colombia and Brazil,

@@ -226,3 +227,3 @@ * and only when dialing within those countries from a mobile phone to a fixed line number.

* @param {object} metadata — Metadata with country selected.
* @return {object} `{ nationalNumber: string?, carrierCode: string? }`.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/

@@ -416,15 +417,6 @@ export function stripNationalPrefixAndCarrierCode(number, metadata) {

// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
let {
const {
nationalNumber,
carrierCode
} = stripNationalPrefixAndCarrierCode(
} = stripNationalPrefixAndCarrierCodeFromCompleteNumber(
parseIncompletePhoneNumber(number),

@@ -434,19 +426,2 @@ metadata

// If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch (checkNumberLengthForType(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
nationalNumber = number
carrierCode = undefined
}
}
// Sometimes there are several countries

@@ -483,4 +458,52 @@ // corresponding to the same country phone code

//
/**
* Strips national prefix and carrier code from a complete phone number.
* The difference from the non-"FromCompleteNumber" function is that
* it won't extract national prefix if the resultant number is too short
* to be a complete number for the selected phone numbering plan.
* @param {string} number — Complete phone number digits.
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/
export function stripNationalPrefixAndCarrierCodeFromCompleteNumber(number, metadata) {
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
const {
nationalNumber,
carrierCode
} = stripNationalPrefixAndCarrierCode(
parseIncompletePhoneNumber(number),
metadata
)
// If a national prefix has been extracted, check to see
// if the resultant number isn't too short.
if (nationalNumber.length !== number.length + (carrierCode ? carrierCode.length : 0)) {
// If not using legacy generated metadata (before version `1.0.18`)
// then it has "possible lengths", so use those to validate the number length.
if (metadata.possibleLengths()) {
// "We require that the NSN remaining after stripping the national prefix and
// carrier code be long enough to be a possible length for the region.
// Otherwise, we don't do the stripping, since the original number could be
// a valid short number."
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
switch (checkNumberLengthForType(nationalNumber, undefined, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// case 'IS_POSSIBLE_LOCAL_ONLY':
// Don't strip the national prefix.
return { nationalNumber: number }
}
}
}
return { nationalNumber, carrierCode }
}
/**
* Converts a phone number digits (possibly with a `+`)

@@ -495,3 +518,3 @@ * into a calling code and the rest phone number digits.

* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string? }`
* @return {object} `{ countryCallingCode: string?, number: string }`
* @example

@@ -536,40 +559,15 @@ * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.

if (country || callingCode) {
const countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode
if (number.indexOf(countryCallingCode) === 0) {
metadata = new Metadata(metadata)
metadata.selectNumberingPlan(country, callingCode)
const possibleShorterNumber = number.slice(countryCallingCode.length)
const {
nationalNumber: possibleShorterNationalNumber,
} = stripNationalPrefixAndCarrierCode(
possibleShorterNumber,
metadata
)
const {
nationalNumber
} = stripNationalPrefixAndCarrierCode(
number,
metadata
)
// If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (
(
!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
&&
matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
)
||
checkNumberLengthForType(nationalNumber, undefined, metadata) === 'TOO_LONG'
) {
return {
countryCallingCode,
number: possibleShorterNumber
}
const {
countryCallingCode,
number: shorterNumber
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
number,
country,
callingCode,
metadata
)
if (countryCallingCode) {
return {
countryCallingCode,
number: shorterNumber
}

@@ -613,1 +611,59 @@ }

}
/**
* Sometimes some people incorrectly input international phone numbers
* without the leading `+`. This function corrects such input.
* @param {string} number — Phone number digits.
* @param {string?} country
* @param {string?} callingCode
* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string }`.
*/
export function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
number,
country,
callingCode,
metadata
) {
const countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode
if (number.indexOf(countryCallingCode) === 0) {
metadata = new Metadata(metadata)
metadata.selectNumberingPlan(country, callingCode)
const possibleShorterNumber = number.slice(countryCallingCode.length)
const {
nationalNumber: possibleShorterNationalNumber,
} = stripNationalPrefixAndCarrierCode(
possibleShorterNumber,
metadata
)
const {
nationalNumber
} = stripNationalPrefixAndCarrierCode(
number,
metadata
)
// If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (
(
!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
&&
matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
)
||
checkNumberLengthForType(nationalNumber, undefined, metadata) === 'TOO_LONG'
) {
return {
countryCallingCode,
number: possibleShorterNumber
}
}
}
return { number }
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc