From 7ade904f81b0a930fe1055c814073504af062136 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Mon, 11 Sep 2023 17:18:52 +0000 Subject: [PATCH 1/3] Closes #552 TryParse ignores calling code --- src/PhoneNumbers/PhoneNumber.cs | 8 +++++--- test/PhoneNumbers.Tests/PhoneNumberTests.cs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/PhoneNumbers/PhoneNumber.cs b/src/PhoneNumbers/PhoneNumber.cs index c12e1498..623703d8 100644 --- a/src/PhoneNumbers/PhoneNumber.cs +++ b/src/PhoneNumbers/PhoneNumber.cs @@ -173,10 +173,10 @@ public static PhoneNumber Parse(string value, string countryCode, ParseOptions o } /// - /// Converts the string representation of a phone number to any equivalents using the default . A return value indicates whether the conversion succeeded. + /// Converts the string representation of a phone number to any possible equivalents using the default . A return value indicates whether the conversion succeeded. /// /// A string containing a phone number. - /// The equivalent if the conversion succeeds, otherwise null. + /// The equivalents if the conversion succeeds, otherwise null. /// true if value was converted successfully; otherwise, false. public static bool TryParse(string value, out IEnumerable phoneNumbers) => TryParse(value, ParseOptions.Default, out phoneNumbers); @@ -192,7 +192,9 @@ public static bool TryParse(string value, ParseOptions options, out IEnumerable< { if (options is not null) { - phoneNumbers = options.Countries + var countries = options.GetCountryInfos(value); + + phoneNumbers = (countries.Any() ? countries : options.Countries) .Select(x => options.ParserFactory.GetParser(x).Parse(value)) .Where(x => x.PhoneNumber is not null) .Select(x => x.PhoneNumber) diff --git a/test/PhoneNumbers.Tests/PhoneNumberTests.cs b/test/PhoneNumbers.Tests/PhoneNumberTests.cs index f82ac2bf..9acc507c 100644 --- a/test/PhoneNumbers.Tests/PhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/PhoneNumberTests.cs @@ -249,7 +249,7 @@ public void TryParse_Value_To_PhoneNumber_False_If_ParseOptions_Null() } [Fact] - public void TryParse_Value_With_CallingCode_Any_Country() + public void TryParse_Value_PhoneNumbers_Value_With_CallingCode_UK() { Assert.True(PhoneNumber.TryParse("+442079813000", out IEnumerable phoneNumbers)); Assert.NotNull(phoneNumbers); @@ -258,7 +258,16 @@ public void TryParse_Value_With_CallingCode_Any_Country() } [Fact] - public void TryParse_Value_Without_CallingCode_Any_Country() + public void TryParse_Value_PhoneNumbers_Value_With_CallingCode_US() + { + Assert.True(PhoneNumber.TryParse("+16054567890", out IEnumerable phoneNumbers)); + Assert.NotNull(phoneNumbers); + Assert.Single(phoneNumbers); + Assert.Equal(CountryInfo.UnitedStates, phoneNumbers.Single().Country); + } + + [Fact] + public void TryParse_Value_PhoneNumbers_Value_Without_CallingCode() { Assert.True(PhoneNumber.TryParse("02079813000", out IEnumerable phoneNumbers)); Assert.NotNull(phoneNumbers); From 9011f99c1486a63981ad4d87c017eeb78d04e619 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Mon, 11 Sep 2023 17:20:47 +0000 Subject: [PATCH 2/3] Ignore NETSTANDARD from code spell checker --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 265381d6..17037a1e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,7 @@ "fuget", "inheritdoc", "nanp", + "NETSTANDARD", "nuget", "Oceanian", "Pilley", From 610e8e9907d6b428301cc595489be50fdc58c0e6 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Mon, 11 Sep 2023 17:26:17 +0000 Subject: [PATCH 3/3] Prefer var --- src/PhoneNumbers/CountryInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhoneNumbers/CountryInfo.cs b/src/PhoneNumbers/CountryInfo.cs index a1f84d20..a7902220 100644 --- a/src/PhoneNumbers/CountryInfo.cs +++ b/src/PhoneNumbers/CountryInfo.cs @@ -215,7 +215,7 @@ private string ReadNationalSignificantNumber(string value, int startPos) { var startsWithTrunkPrefix = true; - for (int i = 0; i < TrunkPrefix.Length; i++) + for (var i = 0; i < TrunkPrefix.Length; i++) { if (ar[i] != TrunkPrefix[i]) {