Skip to content

Commit

Permalink
Add InternationalCallPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Dec 9, 2024
1 parent 4ebb728 commit 6bf52ef
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ phoneNumber.Country.CallingCode; // 44
phoneNumber.Country.Continent; // Europe
phoneNumber.Country.HasNationalDestinationCodes; // true
phoneNumber.Country.HasTrunkPrefix; // true
phoneNumber.Country.InternationalCallPrefix; // 00
phoneNumber.Country.IsEuropeanUnionMember; // false
phoneNumber.Country.IsNatoMember; // true
phoneNumber.Country.Iso3166Code; // GB
Expand Down
17 changes: 17 additions & 0 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public sealed partial class CountryInfo : IEquatable<CountryInfo>
internal const string Africa = "Africa";
internal const string Asia = "Asia";
internal const string Europe = "Europe";
internal const string ItuInternationalCallPrefix = "00";
internal const string NorthAmerica = "North America";
internal const string Oceania = "Oceania";
internal const string SouthAmerica = "South America";
#if !NET8_0_OR_GREATER
private static readonly ReadOnlyCollection<int> s_emptyIntArray = new(Array.Empty<int>());
private static readonly ReadOnlyDictionary<string, string> s_emptyStringDictionary = new(new Dictionary<string, string>(StringComparer.Ordinal));
#endif

private static readonly ReadOnlyCollection<PhoneNumberFormatter> s_formatters = new(
Expand Down Expand Up @@ -65,6 +67,12 @@ internal CountryInfo()
/// </summary>
public bool HasTrunkPrefix => TrunkPrefix is not null;

/// <summary>
/// Gets the international call prefix.
/// </summary>
/// <remarks>May also be referred to as international direct dialling or international subscriber dialling, defaults to the ITU recommended '00', see https://en.wikipedia.org/wiki/List_of_international_call_prefixes.</remarks>
public string InternationalCallPrefix { get; init; } = ItuInternationalCallPrefix;

/// <summary>
/// Gets a value indicating whether the country is a member of the European Union.
/// </summary>
Expand Down Expand Up @@ -106,6 +114,15 @@ internal CountryInfo()
/// Gets the <see cref="PhoneNumberFormatProvider"/> for the country.
/// </summary>
internal PhoneNumberFormatProvider FormatProvider { get; init; } = ComplexPhoneNumberFormatProvider.Default;
/// <summary>
/// Gets country specific international call prefixes.
/// </summary>
internal ReadOnlyDictionary<string, string> InternationalCallPrefixes { get; init; } =
#if NET8_0_OR_GREATER
ReadOnlyDictionary<string, string>.Empty;
#else
s_emptyStringDictionary;
#endif

/// <summary>
/// Gets the possible lengths of the national destination code.
Expand Down
19 changes: 19 additions & 0 deletions src/PhoneNumbers/CountryInfo_Africa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public partial class CountryInfo
CallingCode = "254",
Continent = Africa,
FormatProvider = SimplePhoneNumberFormatProvider.Default,
InternationalCallPrefix = "000",
InternationalCallPrefixes = new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "255", "007" }, // To call Tanzania from Kenya, subscribers dial 007 instead of +255
{ "256", "006" }, // To call Uganda from Kenya, subscribers dial 006 instead of +256
}.AsReadOnly(),
Iso3166Code = "KE",
Name = "Kenya",
NdcLengths = new ReadOnlyCollection<int>([3, 2]),
Expand All @@ -42,6 +48,7 @@ public partial class CountryInfo
AllowsLocalGeographicDialling = true,
CallingCode = "234",
Continent = Africa,
InternationalCallPrefix = "009",
Iso3166Code = "NG",
Name = "Nigeria",
NdcLengths = new ReadOnlyCollection<int>([4, 3]),
Expand Down Expand Up @@ -70,6 +77,12 @@ public partial class CountryInfo
{
CallingCode = "255",
Continent = Africa,
InternationalCallPrefix = "000",
InternationalCallPrefixes = new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "254", "005" }, // To call Kenya from Tanzania, subscribers dial 005 instead of +254
{ "256", "006" }, // To call Uganda from Tanzania, subscribers dial 006 instead of +256
}.AsReadOnly(),
Iso3166Code = "TZ",
Name = "Tanzania",
NdcLengths = new ReadOnlyCollection<int>([5, 3, 2]),
Expand All @@ -85,6 +98,12 @@ public partial class CountryInfo
CallingCode = "256",
Continent = Africa,
FormatProvider = UGPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = "000",
InternationalCallPrefixes = new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "254", "005" }, // To call Kenya from Uganda, subscribers dial 005 instead of +254
{ "255", "007" }, // To call Tanzania from Uganda, subscribers dial 007 instead of +255
}.AsReadOnly(),
Iso3166Code = "UG",
Name = "Uganda",
NdcLengths = new ReadOnlyCollection<int>([6, 5, 4, 3, 1]),
Expand Down
2 changes: 2 additions & 0 deletions src/PhoneNumbers/CountryInfo_Asia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public partial class CountryInfo
{
CallingCode = "852",
Continent = Asia,
InternationalCallPrefix = "001",
Iso3166Code = "HK",
Name = "Hong Kong",
NsnLengths = new ReadOnlyCollection<int>([8, 9, 11, 12]),
Expand All @@ -36,6 +37,7 @@ public partial class CountryInfo
{
CallingCode = "65",
Continent = Asia,
InternationalCallPrefix = "000",
Iso3166Code = "SG",
Name = "Singapore",
NsnLengths = new ReadOnlyCollection<int>([8, 10, 11]),
Expand Down
1 change: 1 addition & 0 deletions src/PhoneNumbers/CountryInfo_Europe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public partial class CountryInfo
CallingCode = "375",
Continent = Europe,
FormatProvider = SimplePhoneNumberFormatProvider.Default,
InternationalCallPrefix = "8~10",
Iso3166Code = "BY",
Name = "Belarus",
NdcLengths = new ReadOnlyCollection<int>([4, 3, 2]),
Expand Down
23 changes: 23 additions & 0 deletions src/PhoneNumbers/CountryInfo_NorthAmerica.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace PhoneNumbers;
public partial class CountryInfo
{
internal const string NanpCallingCode = "1";
internal const string NanpInternationalCallPrefix = "011";
private static readonly ReadOnlyCollection<int> s_nanpNdcLengths = new([3]);
private static readonly ReadOnlyCollection<int> s_nanpNsnLengths = new([10]);

Expand All @@ -18,6 +19,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "AI",
Name = "Anguilla",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -34,6 +36,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "AG",
Name = "Antigua and Barbuda",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -50,6 +53,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "BS",
Name = "Bahamas",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -66,6 +70,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "BB",
Name = "Barbados",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -82,6 +87,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "BM",
Name = "Bermuda",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -98,6 +104,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "VG",
Name = "British Virgin Islands",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -114,6 +121,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
IsNatoMember = true,
Iso3166Code = "CA",
IsOecdMember = true,
Expand All @@ -132,6 +140,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "KY",
Name = "Cayman Islands",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -148,6 +157,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "DM",
Name = "Dominica",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -164,6 +174,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "GD",
Name = "Grenada",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -180,6 +191,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "JM",
Name = "Jamaica",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -196,6 +208,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "MS",
Name = "Montserrat",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -212,6 +225,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "MP",
Name = "Northern Mariana Island",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -228,6 +242,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "PR",
Name = "Puerto Rico",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -244,6 +259,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "KN",
Name = "Saint Kitts and Nevis",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -260,6 +276,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "LC",
Name = "Saint Lucia",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -276,6 +293,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "VC",
Name = "Saint Vincent and the Grenadines",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -292,6 +310,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "SX",
Name = "Sint Maarten",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -308,6 +327,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "TT",
Name = "Trinidad and Tobago",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -324,6 +344,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "TC",
Name = "Turks and Caicos Islands",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -340,6 +361,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
IsNatoMember = true,
Iso3166Code = "US",
IsOecdMember = true,
Expand All @@ -357,6 +379,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = NorthAmerica,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "VI",
Name = "United States Virgin Islands",
NdcLengths = s_nanpNdcLengths,
Expand Down
3 changes: 3 additions & 0 deletions src/PhoneNumbers/CountryInfo_Oceania.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = Oceania,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "AS",
Name = "American Samoa",
NdcLengths = s_nanpNdcLengths,
Expand All @@ -30,6 +31,7 @@ public partial class CountryInfo
CallingCode = "61",
Continent = Oceania,
FormatProvider = AUPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = "0011",
Iso3166Code = "AU",
IsOecdMember = true,
Name = "Australia",
Expand All @@ -47,6 +49,7 @@ public partial class CountryInfo
CallingCode = NanpCallingCode,
Continent = Oceania,
FormatProvider = NanpPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = NanpInternationalCallPrefix,
Iso3166Code = "GU",
Name = "Guam",
NdcLengths = s_nanpNdcLengths,
Expand Down
1 change: 1 addition & 0 deletions src/PhoneNumbers/CountryInfo_SouthAmerica.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class CountryInfo
CallingCode = "55",
Continent = SouthAmerica,
FormatProvider = BRPhoneNumberFormatProvider.Instance,
InternationalCallPrefix = "0xx",
Iso3166Code = "BR",
Name = "Brazil",
NdcLengths = new ReadOnlyCollection<int>([3, 2]),
Expand Down
12 changes: 12 additions & 0 deletions src/PhoneNumbers/FrameworkExtensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if !NET8_0_OR_GREATER
namespace System.Collections.Generic
{
using System.Collections.ObjectModel;

internal static class CollectionExtensions
{
internal static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TKey : notnull =>
new ReadOnlyDictionary<TKey, TValue>(dictionary);
}
}
#endif
Loading

0 comments on commit 6bf52ef

Please sign in to comment.