Skip to content

Commit

Permalink
Add SharesCallingCodeWith
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Nov 25, 2024
1 parent 59da60e commit d2eac82
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ internal string ReadNationalSignificantNumber(string value)
return ReadNationalSignificantNumber(value, startIdx);
}

/// <summary>
/// Gets a value indicting whether this <see cref="CountryInfo"/> shares a calling code with the specified <see cref="CountryInfo"/>.
/// </summary>
/// <returns>True if the both countries share a calling code, otherwise false.</returns>
internal bool SharesCallingCodeWith(CountryInfo countryInfo) =>
CallingCode.Equals(countryInfo.CallingCode, StringComparison.Ordinal);

/// <remarks>Char.IsDigit returns true for more than 0-9 so use a more restricted version.</remarks>
private static bool IsDigit(char charVal) =>
charVal is >= '0' and <= '9';
Expand Down
14 changes: 14 additions & 0 deletions test/PhoneNumbers.Tests/CountryInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ public void ReadNationalSignificantNumber_Without_TrunkPrefix(string value) =>
.CreateCountryInfo(trunkPrefix: null, nsnLengths: new[] { 8 })
.ReadNationalSignificantNumber(value));

[Fact]
public void ShareCallingCodeWith()
{
Assert.False(CountryInfo.Canada.SharesCallingCodeWith(CountryInfo.France));

Assert.True(CountryInfo.Canada.SharesCallingCodeWith(CountryInfo.UnitedStates));
Assert.True(CountryInfo.Guernsey.SharesCallingCodeWith(CountryInfo.IsleOfMan));
Assert.True(CountryInfo.Guernsey.SharesCallingCodeWith(CountryInfo.Jersey));
Assert.True(CountryInfo.Guernsey.SharesCallingCodeWith(CountryInfo.UnitedKingdom));
Assert.True(CountryInfo.UnitedKingdom.SharesCallingCodeWith(CountryInfo.Guernsey));
Assert.True(CountryInfo.UnitedKingdom.SharesCallingCodeWith(CountryInfo.IsleOfMan));
Assert.True(CountryInfo.UnitedKingdom.SharesCallingCodeWith(CountryInfo.Jersey));
}

[Fact]
public void When_Constructed()
{
Expand Down

0 comments on commit d2eac82

Please sign in to comment.