Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/tryparse any #553

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"fuget",
"inheritdoc",
"nanp",
"NETSTANDARD",
"nuget",
"Oceanian",
"Pilley",
Expand Down
2 changes: 1 addition & 1 deletion src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
{
Expand Down
8 changes: 5 additions & 3 deletions src/PhoneNumbers/PhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public static PhoneNumber Parse(string value, string countryCode, ParseOptions o
}

/// <summary>
/// Converts the string representation of a phone number to any <see cref="PhoneNumber"/> equivalents using the default <see cref="ParseOptions"/>. A return value indicates whether the conversion succeeded.
/// Converts the string representation of a phone number to any possible <see cref="PhoneNumber"/> equivalents using the default <see cref="ParseOptions"/>. A return value indicates whether the conversion succeeded.
/// </summary>
/// <param name="value">A string containing a phone number.</param>
/// <param name="phoneNumbers">The <see cref="PhoneNumber"/> equivalent if the conversion succeeds, otherwise null.</param>
/// <param name="phoneNumbers">The <see cref="PhoneNumber"/> equivalents if the conversion succeeds, otherwise null.</param>
/// <returns><c>true</c> if value was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(string value, out IEnumerable<PhoneNumber> phoneNumbers) =>
TryParse(value, ParseOptions.Default, out phoneNumbers);
Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions test/PhoneNumbers.Tests/PhoneNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhoneNumber> phoneNumbers));
Assert.NotNull(phoneNumbers);
Expand All @@ -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<PhoneNumber> 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<PhoneNumber> phoneNumbers));
Assert.NotNull(phoneNumbers);
Expand Down
Loading