From de9bb5a7ccb4606efe5ab11f6c1e24407ad4408b Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Thu, 4 Apr 2024 09:59:12 +0000 Subject: [PATCH] Add SharesCallingCodeWith --- src/PhoneNumbers/CountryInfo.cs | 7 +++++++ test/PhoneNumbers.Tests/CountryInfoTests.cs | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/PhoneNumbers/CountryInfo.cs b/src/PhoneNumbers/CountryInfo.cs index e06eea63..31babb25 100644 --- a/src/PhoneNumbers/CountryInfo.cs +++ b/src/PhoneNumbers/CountryInfo.cs @@ -233,6 +233,13 @@ internal string ReadNationalSignificantNumber(string value) return ReadNationalSignificantNumber(value, startIdx); } + /// + /// Gets a value indicting whether this shares a calling code with the specified . + /// + /// True if the both countries share a calling code, otherwise false. + internal bool SharesCallingCodeWith(CountryInfo countryInfo) => + CallingCode.Equals(countryInfo?.CallingCode, StringComparison.Ordinal); + /// Char.IsDigit returns true for more than 0-9 so use a more restricted version. private static bool IsDigit(char charVal) => charVal is >= '0' and <= '9'; diff --git a/test/PhoneNumbers.Tests/CountryInfoTests.cs b/test/PhoneNumbers.Tests/CountryInfoTests.cs index 6afb8864..e810688a 100644 --- a/test/PhoneNumbers.Tests/CountryInfoTests.cs +++ b/test/PhoneNumbers.Tests/CountryInfoTests.cs @@ -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() {