Skip to content

Commit

Permalink
Add SharesNdcWith
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Dec 9, 2024
1 parent 6bf52ef commit 19e3e80
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PhoneNumbers/PhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,12 @@ public override string ToString() =>
/// <returns>The string representation of the value of this instance as specified by the format.</returns>
public string ToString(string format) =>
CountryInfo.GetFormatter(format).Format(this);

/// <summary>
/// Gets a value indicting whether this <see cref="PhoneNumber"/> shares a national destination code with the specified <see cref="PhoneNumber"/>.
/// </summary>
/// <param name="phoneNumber">The <see cref="PhoneNumber"/> to compare against.</param>
/// <returns>True if the both phone numbers share a national destination code, otherwise false.</returns>
internal bool SharesNdcWith(PhoneNumber phoneNumber) =>
NationalDestinationCode?.Equals(phoneNumber.NationalDestinationCode, StringComparison.Ordinal) == true;
}
23 changes: 23 additions & 0 deletions test/PhoneNumbers.Tests/PhoneNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ public void Parse_Value_With_Custom_ParseOptions()
Assert.Equal("The value '+441142726444' could not be successfully parsed into a phone number for any country enabled in ParseOptions.", exception.Message);
}

[Fact]
public void SharesNdcWith()
{
var phoneNumber1 = TestHelper.CreateGeographicPhoneNumber(
trunkPrefix: default,
ndc: "12345",
sn: "667788");

var phoneNumber2 = TestHelper.CreateGeographicPhoneNumber(
trunkPrefix: default,
ndc: "12345",
sn: "667788");

Assert.True(phoneNumber1.SharesNdcWith(phoneNumber2));

var phoneNumber3 = TestHelper.CreateGeographicPhoneNumber(
trunkPrefix: default,
ndc: "12346",
sn: "667788");

Assert.False(phoneNumber1.SharesNdcWith(phoneNumber3));
}

[Fact]
public void ToString_Returns_Default_Formatted_PhoneNumber()
{
Expand Down

0 comments on commit 19e3e80

Please sign in to comment.