Skip to content

Commit

Permalink
Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Sep 27, 2023
1 parent c4c8fd8 commit 3217ffc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/PhoneNumbers/CountryInfo_NorthAmerica.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public partial class CountryInfo
{
CallingCode = "52",
Continent = NorthAmerica,
FormatProvider = MXPhoneNumberFormatProvider.Instance,
Iso3166Code = "MX",
Name = "Mexico",
NdcLengths = new ReadOnlyCollection<int>(new[] { 2 }),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace PhoneNumbers.Formatters.FormatProviders;

/// <summary>
/// A <see cref="PhoneNumberFormatProvider"/> for Mexico numbers.
/// </summary>
internal sealed class MXPhoneNumberFormatProvider : ComplexPhoneNumberFormatProvider
{
private MXPhoneNumberFormatProvider()
{
}

internal static PhoneNumberFormatProvider Instance { get; } = new MXPhoneNumberFormatProvider();

protected override string ProvideFormat(PhoneNumber phoneNumber, bool international) =>
phoneNumber.NationalSignificantNumber!.Length switch
{
10 => "## #### ####",
_ => base.ProvideFormat(phoneNumber, international),
};
}
2 changes: 1 addition & 1 deletion test/PhoneNumbers.Tests/CountryInfo_NorthAmerica_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void CountryInfo_Mexico()
Assert.False(countryInfo.AllowsLocalGeographicDialling);
Assert.Equal("52", countryInfo.CallingCode);
Assert.Equal(CountryInfo.NorthAmerica, countryInfo.Continent);
Assert.IsType<ComplexPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.IsType<MXPhoneNumberFormatProvider>(countryInfo.FormatProvider);
Assert.False(countryInfo.IsEuropeanUnionMember);
Assert.Equal("MX", countryInfo.Iso3166Code);
Assert.Equal("Mexico", countryInfo.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public void Grenada_Numbers(string input, string format, string expected) =>
public void Jamaica_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+525550154158", "E.123", "+52 55 5015 4158")]
[InlineData("+525550154158", "N", "55 5015 4158")]
[InlineData("+525550154158", "RFC3966", "tel:+52-55-5015-4158")]
public void Mexico_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+16644913789", "E.123", "+1 664-491-3789")]
[InlineData("+16644913789", "N", "(664) 491-3789")]
Expand Down

0 comments on commit 3217ffc

Please sign in to comment.