From fba6a38d49453d9495278b39c5407cb92a6e93fa Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Tue, 17 Dec 2024 06:00:57 +0000 Subject: [PATCH] Libphonenumber JS API is not able to parse numbers with missing '+' unlike Java. --- .../i18n/phonenumbers/phonenumberutil.js | 2 +- .../i18n/phonenumbers/phonenumberutil_test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index d530dae5e..e2c377be9 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -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); diff --git a/javascript/i18n/phonenumbers/phonenumberutil_test.js b/javascript/i18n/phonenumbers/phonenumberutil_test.js index 4070dd08a..a27690322 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil_test.js +++ b/javascript/i18n/phonenumbers/phonenumberutil_test.js @@ -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() {