Skip to content

Commit

Permalink
Closes #729
Browse files Browse the repository at this point in the history
Add a new formatter for Unformatted
  • Loading branch information
TrevorPilley committed Nov 3, 2024
1 parent d9a61dc commit 1d35579
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions NuGet.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ phoneNumber.ToString("E.164"); // +441142726444 (E.164 format)
phoneNumber.ToString("E.123"); // +44 114 272 6444 (E.123 international format)
phoneNumber.ToString("N"); // (0114) 272 6444 (E.123 national notation format)
phoneNumber.ToString("RFC3966"); // tel:+44-114-272-644 (RFC3966 format)
phoneNumber.ToString("U"); // 01142726444 (national notation without formatting)
```

Builds for:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ phoneNumber.ToString("E.164"); // +441142726444 (E.164
phoneNumber.ToString("E.123"); // +44 114 272 6444 (E.123 international format)
phoneNumber.ToString("N"); // (0114) 272 6444 (E.123 national notation format)
phoneNumber.ToString("RFC3966"); // tel:+44-114-272-644 (RFC3966 format)
phoneNumber.ToString("U"); // 01142726444 (the national notation without any formatting)
```

### ParseOptions
Expand Down
1 change: 1 addition & 0 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed partial class CountryInfo
E164PhoneNumberFormatter.Instance,
E123PhoneNumberFormatter.Instance,
NationalPhoneNumberFormatter.Instance,
NationalUnformattedPhoneNumberFormatter.Instance,
Rfc3966PhoneNumberFormatter.Instance,
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace PhoneNumbers.Formatters;

/// <summary>
/// A <see cref="PhoneNumberFormatter"/> which returns the phone number in national notation without any formatting.
/// </summary>
internal sealed class NationalUnformattedPhoneNumberFormatter : PhoneNumberFormatter
{
/// <summary>
/// Initialises a new instance of the <see cref="NationalUnformattedPhoneNumberFormatter"/> class.
/// </summary>
private NationalUnformattedPhoneNumberFormatter()
: base("U")
{
}

/// <summary>
/// Gets the <see cref="NationalUnformattedPhoneNumberFormatter"/> instance.
/// </summary>
internal static PhoneNumberFormatter Instance { get; } = new NationalUnformattedPhoneNumberFormatter();

/// <inheritdoc/>
internal override string Format(PhoneNumber phoneNumber) =>
phoneNumber.Country.HasTrunkPrefix
? $"{phoneNumber.Country.TrunkPrefix}{phoneNumber.NationalSignificantNumber}"
: phoneNumber.NationalSignificantNumber;
}
4 changes: 4 additions & 0 deletions test/PhoneNumbers.Tests/CountryInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public void GetFormatter_N_Returns_NationalPhoneNumberFormatter() =>
[Fact]
public void GetFormatter_RFC3966_Returns_Rfc3966PhoneNumberFormatter() =>
Assert.IsType<Rfc3966PhoneNumberFormatter>(CountryInfo.GetFormatter("RFC3966"));

[Fact]
public void GetFormatter_U_Returns_NationalUnformattedPhoneNumberFormatter() =>
Assert.IsType<NationalUnformattedPhoneNumberFormatter>(CountryInfo.GetFormatter("U"));

[Fact]
public void GetFormatter_Throws_For_Invalid_Format() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using PhoneNumbers.Formatters;

namespace PhoneNumbers.Tests.Formatters;

/// <summary>
/// Contains unit tests for the <see cref="NationalUnformattedPhoneNumberFormatter"/> class.
/// </summary>
public class NationalUnformattedPhoneNumberFormatterTests
{
[Fact]
public void CanFormat_Returns_False_For_Not_U() =>
Assert.False(NationalUnformattedPhoneNumberFormatter.Instance.CanFormat("E.164"));

[Fact]
public void CanFormat_Returns_False_For_Null() =>
Assert.False(NationalUnformattedPhoneNumberFormatter.Instance.CanFormat(null));

[Fact]
public void CanFormat_Returns_True_For_U() =>
Assert.True(NationalUnformattedPhoneNumberFormatter.Instance.CanFormat("U"));

[Fact]
public void Instance()
{
Assert.NotNull(NationalUnformattedPhoneNumberFormatter.Instance);
Assert.Same(NationalUnformattedPhoneNumberFormatter.Instance, NationalUnformattedPhoneNumberFormatter.Instance);
}
}
6 changes: 6 additions & 0 deletions test/PhoneNumbers.Tests/PhoneNumber_ToString_Africa_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class PhoneNumber_ToString_Africa_Tests
[InlineData("+20235344239", "E.123", "+20 2 3534 4239")]
[InlineData("+20235344239", "N", "02 3534 4239")]
[InlineData("+20235344239", "RFC3966", "tel:+20-2-3534-4239")]
[InlineData("+20235344239", "U", "0235344239")]
public void Egypt_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

Expand All @@ -16,27 +17,32 @@ public void Egypt_Numbers(string input, string format, string expected) =>
[InlineData("+254703042000", "N", "0703 042000")]
[InlineData("+25420424200", "RFC3966", "tel:+254-20-424200")]
[InlineData("+254703042000", "RFC3966", "tel:+254-703-042000")]
[InlineData("+25420424200", "U", "020424200")]
[InlineData("+254703042000", "U", "0703042000")]
public void Kenya_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+2342094617000", "E.123", "+234 209 461 7000")]
[InlineData("+2342094617000", "N", "(0209) 461 7000")]
[InlineData("+2342094617000", "RFC3966", "tel:+234-209-461-7000")]
[InlineData("+2342094617000", "U", "02094617000")]
public void Nigeria_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+27215616800", "E.123", "+27 21 561 6800")]
[InlineData("+27215616800", "N", "021 561 6800")]
[InlineData("+27215616800", "RFC3966", "tel:+27-21-561-6800")]
[InlineData("+27215616800", "U", "0215616800")]
public void SouthAfrica_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+255222199760", "E.123", "+255 22 219 9760")]
[InlineData("+255222199760", "N", "022 219 9760")]
[InlineData("+255222199760", "RFC3966", "tel:+255-22-219-9760")]
[InlineData("+255222199760", "U", "0222199760")]
public void Tanzania_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
}
5 changes: 5 additions & 0 deletions test/PhoneNumbers.Tests/PhoneNumber_ToString_Asia_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ public class PhoneNumber_ToString_Asia_Tests
[InlineData("+85229616333", "E.123", "+852 2961 6333")]
[InlineData("+85229616333", "N", "2961 6333")]
[InlineData("+85229616333", "RFC3966", "tel:+852-2961-6333")]
[InlineData("+85229616333", "U", "29616333")]
public void HongKong_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+85328000000", "E.123", "+853 2800 0000")]
[InlineData("+85328000000", "N", "2800 0000")]
[InlineData("+85328000000", "RFC3966", "tel:+853-2800-0000")]
[InlineData("+85328000000", "U", "28000000")]
public void Macau_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

[Theory]
[InlineData("+6563773800", "E.123", "+65 6377 3800")]
[InlineData("+6563773800", "N", "6377 3800")]
[InlineData("+6563773800", "RFC3966", "tel:+65-6377-3800")]
[InlineData("+6563773800", "U", "63773800")]
public void Singapore_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));

Expand All @@ -30,6 +33,8 @@ public void Singapore_Numbers(string input, string format, string expected) =>
[InlineData("+903122947200", "N", "(0312) 294 72 00")]
[InlineData("+904441234", "RFC3966", "tel:+90-444-12-34")]
[InlineData("+903122947200", "RFC3966", "tel:+90-312-294-72-00")]
[InlineData("+904441234", "U", "04441234")]
[InlineData("+903122947200", "U", "03122947200")]
public void Turkey_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
}
Loading

0 comments on commit 1d35579

Please sign in to comment.