Skip to content

Commit

Permalink
Libphonenumber JS API is not able to parse numbers with missing '+' u…
Browse files Browse the repository at this point in the history
…nlike Java.
  • Loading branch information
mandlil authored Dec 17, 2024
1 parent 4246175 commit fba6a38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion javascript/i18n/phonenumbers/phonenumberutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3852,7 +3852,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.maybeExtractCountryCode =
var generalDesc = defaultRegionMetadata.getGeneralDesc();
/** @type {!RegExp} */
var validNumberPattern =
new RegExp(generalDesc.getNationalNumberPatternOrDefault());
new RegExp('^(?:' + generalDesc.getNationalNumberPatternOrDefault() + ')$');
// Passing null since we don't need the carrier code.
this.maybeStripNationalPrefixAndCarrierCode(
potentialNationalNumber, defaultRegionMetadata, null);
Expand Down
18 changes: 18 additions & 0 deletions javascript/i18n/phonenumbers/phonenumberutil_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,24 @@ function testMaybeExtractCountryCode() {
} catch (e) {
fail('Should not have thrown an exception: ' + e.toString());
}
number = new i18n.phonenumbers.PhoneNumber();
try {
phoneNumber = '821064588888';
metadata = phoneUtil.getMetadataForRegion(RegionCode.KR);
countryCallingCode = 82;
numberToFill = new goog.string.StringBuffer();
assertEquals(
'Should have extracted the country calling code of the ' +
'region passed in',
countryCallingCode,
phoneUtil.maybeExtractCountryCode(
phoneNumber, metadata, numberToFill, true, number));
assertEquals(
'Did figure out CountryCodeSource correctly',
CCS.FROM_NUMBER_WITHOUT_PLUS_SIGN, number.getCountryCodeSource());
} catch (e) {
fail('Should not have thrown an exception: ' + e.toString());
}
}

function testParseNationalNumber() {
Expand Down

0 comments on commit fba6a38

Please sign in to comment.