项目作者: furier

项目描述 :
A library for formatting phone numbers
高级语言: C#
项目地址: git://github.com/furier/PhoneNumberFormatter.git
创建时间: 2014-02-25T16:20:13Z
项目社区:https://github.com/furier/PhoneNumberFormatter

开源协议:

下载


PhoneNumberFormatter

A library for formatting phone numbers.

NuGet

  1. Install-Package PhoneNumberFormatter

Configurations

  1. ##############################################
  2. #
  3. # A: Country Code
  4. # B: Number of digits in phone numbers
  5. # C: Phone number format to be returned
  6. # D: Aliases
  7. #
  8. # Format => A|B|C|D....|D
  9. #
  10. ##############################################
  11. #Norwegian phone number country code metadata.
  12. 47|8|3.2.3|norge|norway|nor|no
  13. #Swedish phone number country code metadata.
  14. 46|9|3.3.3|sverige|sweden|swe|se

Comments starts with # and will be ignored by the configuration parser.
New countries can be added by following the convention described above or at the top in the CountryCodes.txt file.

Some countries have a range of digits count in their phone numbers, which is currently not supported and would have to be added.

Examples

  1. private async void Test(dynamic phoneNumber)
  2. {
  3. var formattedPhoneNumber = await PhoneNumberFormatter.PhoneNumberFormatter
  4. .FormatAsync(phoneNumber.Number, phoneNumber.Country);
  5. Console.WriteLine("Unformatted phone number: <{0}> with country <{1}> has been formatted to <{2}>", phoneNumber.Number, phoneNumber.Country, formattedPhoneNumber);
  6. }
  7. await PhoneNumberFormatter.CountryMetaDataRetriever.InitializeAsync();
  8. var phoneNumbers = new List<dynamic>
  9. {
  10. new {Number = "+4712345678", Country = ""},
  11. new {Number = "+47 1 23 45 67 8", Country = ""},
  12. new {Number = "12345678", Country = "Norway"},
  13. new {Number = "1 23 45 67 8", Country = "No"},
  14. new {Number = "1-2-3-4-5-6-7-8", Country = "Nor"},
  15. new {Number = "1.2.3.4.5.6.7.8", Country = ""}
  16. };
  17. foreach(var phoneNumber in phoneNumbers)
  18. {
  19. Test(phoneNumber);
  20. }

This should output the following if default configurations are active.

  1. Unformatted phone number: <+4712345678> with country <> has been formatted to <+47 123 45 678>
  2. Unformatted phone number: <+47 1 23 45 67 8> with country <> has been formatted to <+47 123 45 678>
  3. Unformatted phone number: <12345678> with country <Norway> has been formatted to <+47 123 45 678>
  4. Unformatted phone number: <1 23 45 67 8> with country <No> has been formatted to <+47 123 45 678>
  5. Unformatted phone number: <1-2-3-4-5-6-7-8> with country <Nor> has been formatted to <+47 123 45 678>
  6. Unformatted phone number: <1.2.3.4.5.6.7.8> with country <> has been formatted to <12 34 56 78>

The MIT License (MIT)

Copyright (c) 2013 Sander Struijk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.