From f182a6e7d083ecee32cbe19a1bd362f71027ac74 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Tue, 5 Nov 2024 17:48:47 +0000 Subject: [PATCH] Add SharesNdcWith --- src/PhoneNumbers/PhoneNumber.cs | 8 +++++++ test/PhoneNumbers.Tests/PhoneNumberTests.cs | 23 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/PhoneNumbers/PhoneNumber.cs b/src/PhoneNumbers/PhoneNumber.cs index d50c8ccb..efe4c048 100644 --- a/src/PhoneNumbers/PhoneNumber.cs +++ b/src/PhoneNumbers/PhoneNumber.cs @@ -216,4 +216,12 @@ public override string ToString() => /// The string representation of the value of this instance as specified by the format. public string ToString(string format) => CountryInfo.GetFormatter(format).Format(this); + + /// + /// Gets a value indicting whether this shares a national destination code with the specified . + /// + /// The to compare against. + /// True if the both phone numbers share a national destination code, otherwise false. + internal bool SharesNdcWith(PhoneNumber phoneNumber) => + NationalDestinationCode?.Equals(phoneNumber.NationalDestinationCode, StringComparison.Ordinal) == true; } diff --git a/test/PhoneNumbers.Tests/PhoneNumberTests.cs b/test/PhoneNumbers.Tests/PhoneNumberTests.cs index 2cd628c1..b1c1efd6 100644 --- a/test/PhoneNumbers.Tests/PhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/PhoneNumberTests.cs @@ -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() {