Skip to content

Commit

Permalink
Closes #694
Browse files Browse the repository at this point in the history
Add a flag for NATO members and parse options
  • Loading branch information
TrevorPilley committed Nov 1, 2024
1 parent 4f279b6 commit d9a61dc
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ phoneNumber.Country.Continent; // Europe
phoneNumber.Country.HasNationalDestinationCodes; // true
phoneNumber.Country.HasTrunkPrefix; // true
phoneNumber.Country.IsEuropeanUnionMember; // false
phoneNumber.Country.IsNatoMember; // true
phoneNumber.Country.Iso3166Code; // GB
phoneNumber.Country.Name; // United Kingdom
phoneNumber.Country.SharesCallingCode; // true
Expand Down Expand Up @@ -140,8 +141,9 @@ ParseOptions.Default.AllowNorthAmericanCountries();
ParseOptions.Default.AllowOceanianCountries();
ParseOptions.Default.AllowSouthAmericanCountries();

// Add all countries supported by the library who are members of the same union:
ParseOptions.Default.AllowEuropeanUnionCountries();
// Add all countries supported by the library who are members of the same union/alliance:
ParseOptions.Default.AllowEuropeanUnionCountries(); // all 27 members
ParseOptions.Default.AllowNatoCountries(); // 31 of 32 members (Albania not supported yet)
// Add all countries supported by the library using the same numbering plan:
ParseOptions.Default.AllowNorthAmericanNumberingPlanCountries();
Expand Down
5 changes: 5 additions & 0 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ internal CountryInfo()
/// </summary>
public bool IsEuropeanUnionMember { get; init; }

/// <summary>
/// Gets a value indicating whether the country is a member of the North Atlantic Treaty Organisation.
/// </summary>
public bool IsNatoMember { get; init; }

/// <summary>
/// Gets the ISO 3166 Alpha-2 code for the country.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/PhoneNumbers/CountryInfo_Asia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public partial class CountryInfo
CallingCode = "90",
Continent = Asia,
FormatProvider = TRPhoneNumberFormatProvider.Instance,
IsNatoMember = true,
Iso3166Code = "TR",
Name = "Turkey",
NdcLengths = new ReadOnlyCollection<int>([3, 0]),
Expand Down
28 changes: 28 additions & 0 deletions src/PhoneNumbers/CountryInfo_Europe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = BEPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "BE",
Name = "Belgium",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand Down Expand Up @@ -74,6 +75,7 @@ public partial class CountryInfo
CallingCode = "359",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "BG",
Name = "Bulgaria",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand All @@ -90,6 +92,7 @@ public partial class CountryInfo
CallingCode = "385",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "HR",
Name = "Croatia",
NdcLengths = new ReadOnlyCollection<int>([5, 4, 3, 2, 1]),
Expand Down Expand Up @@ -121,6 +124,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = CZPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "CZ",
Name = "Czech Republic",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand All @@ -135,6 +139,7 @@ public partial class CountryInfo
CallingCode = "45",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "DK",
Name = "Denmark",
NsnLengths = new ReadOnlyCollection<int>([8, 12]),
Expand All @@ -149,6 +154,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = BasicPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "EE",
Name = "Estonia",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2]),
Expand All @@ -164,6 +170,7 @@ public partial class CountryInfo
CallingCode = "358",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "FI",
Name = "Finland",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2, 1]),
Expand All @@ -180,6 +187,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = FRPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "FR",
Name = "France",
NsnLengths = new ReadOnlyCollection<int>([9, 13]),
Expand All @@ -196,6 +204,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = SimplePhoneNumberFormatProvider.Default,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "DE",
Name = "Germany",
NdcLengths = new ReadOnlyCollection<int>([5, 4, 3, 2]),
Expand Down Expand Up @@ -226,6 +235,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = GRPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "GR",
Name = "Greece",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2]),
Expand Down Expand Up @@ -258,6 +268,7 @@ public partial class CountryInfo
CallingCode = "36",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "HU",
Name = "Hungary",
NdcLengths = new ReadOnlyCollection<int>([2, 1]),
Expand All @@ -272,6 +283,7 @@ public partial class CountryInfo
{
CallingCode = "354",
Continent = Europe,
IsNatoMember = true,
Iso3166Code = "IS",
Name = "Iceland",
NsnLengths = new ReadOnlyCollection<int>([7, 9]),
Expand Down Expand Up @@ -321,6 +333,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = SimplePhoneNumberFormatProvider.Default,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "IT",
Name = "Italy",
NdcLengths = new ReadOnlyCollection<int>([5, 4, 3, 2]),
Expand Down Expand Up @@ -368,6 +381,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = BasicPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "LV",
Name = "Latvia",
NsnLengths = new ReadOnlyCollection<int>([8]),
Expand All @@ -382,6 +396,7 @@ public partial class CountryInfo
CallingCode = "370",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "LT",
Name = "Lithuania",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand All @@ -398,6 +413,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = LUPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "LU",
Name = "Luxembourg",
NsnLengths = new ReadOnlyCollection<int>([4, 5, 6, 7, 8, 9, 10, 11, 12]),
Expand Down Expand Up @@ -452,6 +468,7 @@ public partial class CountryInfo
{
CallingCode = "382",
Continent = Europe,
IsNatoMember = true,
Iso3166Code = "ME",
Name = "Montenegro",
NdcLengths = new ReadOnlyCollection<int>([2]),
Expand All @@ -468,6 +485,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = NLPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "NL",
Name = "Netherlands",
NdcLengths = new ReadOnlyCollection<int>([3, 2]),
Expand All @@ -483,6 +501,7 @@ public partial class CountryInfo
AllowsLocalGeographicDialling = true,
CallingCode = "389",
Continent = Europe,
IsNatoMember = true,
Iso3166Code = "MK",
Name = "North Macedonia",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand All @@ -498,6 +517,7 @@ public partial class CountryInfo
CallingCode = "47",
Continent = Europe,
FormatProvider = NOPhoneNumberFormatProvider.Instance,
IsNatoMember = true,
Iso3166Code = "NO",
Name = "Norway",
NsnLengths = new ReadOnlyCollection<int>([8, 12]),
Expand All @@ -512,6 +532,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = PLPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "PL",
Name = "Poland",
NdcLengths = new ReadOnlyCollection<int>([2]),
Expand All @@ -526,6 +547,7 @@ public partial class CountryInfo
CallingCode = "351",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "PT",
Name = "Portugal",
NdcLengths = new ReadOnlyCollection<int>([3]),
Expand All @@ -541,6 +563,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = ROPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "RO",
Name = "Romania",
NdcLengths = new ReadOnlyCollection<int>([3, 2]),
Expand Down Expand Up @@ -584,6 +607,7 @@ public partial class CountryInfo
CallingCode = "421",
Continent = Europe,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "SK",
Name = "Slovakia",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2, 1]),
Expand All @@ -601,6 +625,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = SLPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "SL",
Name = "Slovenia",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2, 1]),
Expand All @@ -617,6 +642,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = ESPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "ES",
Name = "Spain",
NdcLengths = new ReadOnlyCollection<int>([3, 2]),
Expand All @@ -633,6 +659,7 @@ public partial class CountryInfo
Continent = Europe,
FormatProvider = SEPhoneNumberFormatProvider.Instance,
IsEuropeanUnionMember = true,
IsNatoMember = true,
Iso3166Code = "SE",
Name = "Sweden",
NdcLengths = new ReadOnlyCollection<int>([3, 2, 1]),
Expand Down Expand Up @@ -681,6 +708,7 @@ public partial class CountryInfo
CallingCode = "44",
Continent = Europe,
FormatProvider = GBPhoneNumberFormatProvider.Instance,
IsNatoMember = true,
Iso3166Code = "GB",
Name = "United Kingdom",
NdcLengths = new ReadOnlyCollection<int>([5, 4, 3, 2]),
Expand Down
2 changes: 2 additions & 0 deletions src/PhoneNumbers/CountryInfo_NorthAmerica.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
IsNatoMember = true,
Iso3166Code = "CA",
Name = "Canada",
NdcLengths = s_nanpNdcLengths,
Expand Down Expand Up @@ -338,6 +339,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
IsNatoMember = true,
Iso3166Code = "US",
Name = "United States",
NdcLengths = s_nanpNdcLengths,
Expand Down
9 changes: 9 additions & 0 deletions src/PhoneNumbers/ParseOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public static ParseOptions AllowEuropeanCountries(this ParseOptions parseOptions
public static ParseOptions AllowEuropeanUnionCountries(this ParseOptions parseOptions) =>
Allow(parseOptions, x => x.IsEuropeanUnionMember);

/// <summary>
/// Allows the <see cref="ParseOptions"/> instance to include all supported countries who are members of the North Atlantic Treaty Organisation.
/// </summary>
/// <param name="parseOptions">The <see cref="ParseOptions"/> instance to update.</param>
/// <exception cref="ArgumentNullException">Thrown if the specified <paramref name="parseOptions"/> is null.</exception>
/// <returns>The updated <see cref="ParseOptions"/>.</returns>
public static ParseOptions AllowNatoCountries(this ParseOptions parseOptions) =>
Allow(parseOptions, x => x.IsNatoMember);

/// <summary>
/// Allows the <see cref="ParseOptions"/> instance to include all supported countries in North America.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions test/PhoneNumbers.Tests/CountryInfo_Africa_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void CountryInfo_Egypt()
Assert.Equal(CountryInfo.Africa, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("EG", countryInfo.Iso3166Code);
Assert.Equal("Egypt", countryInfo.Name);
Assert.Equal(new[] { 3, 2, 1 }, countryInfo.NdcLengths);
Expand All @@ -36,6 +37,7 @@ public void CountryInfo_Kenya()
Assert.Equal(CountryInfo.Africa, countryInfo.Continent);
Assert.IsType<SimplePhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("KE", countryInfo.Iso3166Code);
Assert.Equal("Kenya", countryInfo.Name);
Assert.Equal(new[] { 3, 2 }, countryInfo.NdcLengths);
Expand All @@ -56,6 +58,7 @@ public void CountryInfo_Nigeria()
Assert.Equal(CountryInfo.Africa, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("NG", countryInfo.Iso3166Code);
Assert.Equal("Nigeria", countryInfo.Name);
Assert.Equal(new[] { 4, 3 }, countryInfo.NdcLengths);
Expand All @@ -76,6 +79,7 @@ public void CountryInfo_SouthAfrica()
Assert.Equal(CountryInfo.Africa, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("ZA", countryInfo.Iso3166Code);
Assert.Equal("South Africa", countryInfo.Name);
Assert.Equal(new[] { 2 }, countryInfo.NdcLengths);
Expand All @@ -96,6 +100,7 @@ public void CountryInfo_Tanzania()
Assert.Equal(CountryInfo.Africa, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("TZ", countryInfo.Iso3166Code);
Assert.Equal("Tanzania", countryInfo.Name);
Assert.Equal(new[] { 5, 3, 2 }, countryInfo.NdcLengths);
Expand Down
4 changes: 4 additions & 0 deletions test/PhoneNumbers.Tests/CountryInfo_Asia_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void CountryInfo_HongKong()
Assert.Equal(CountryInfo.Asia, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("HK", countryInfo.Iso3166Code);
Assert.Equal("Hong Kong", countryInfo.Name);
Assert.Empty(countryInfo.NdcLengths);
Expand All @@ -36,6 +37,7 @@ public void CountryInfo_Macau()
Assert.Equal(CountryInfo.Asia, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("MO", countryInfo.Iso3166Code);
Assert.Equal("Macau", countryInfo.Name);
Assert.Empty(countryInfo.NdcLengths);
Expand All @@ -56,6 +58,7 @@ public void CountryInfo_Singapore()
Assert.Equal(CountryInfo.Asia, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.False(countryInfo.IsNatoMember);
Assert.Equal("SG", countryInfo.Iso3166Code);
Assert.Equal("Singapore", countryInfo.Name);
Assert.Empty(countryInfo.NdcLengths);
Expand All @@ -76,6 +79,7 @@ public void CountryInfo_Turkey()
Assert.Equal(CountryInfo.Asia, countryInfo.Continent);
Assert.IsType<TRPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.True(countryInfo.IsNatoMember);
Assert.Equal("TR", countryInfo.Iso3166Code);
Assert.Equal("Turkey", countryInfo.Name);
Assert.Equal(new[] { 3, 0 }, countryInfo.NdcLengths);
Expand Down
Loading

0 comments on commit d9a61dc

Please sign in to comment.