PhoneNumbersNA 1.0.13
See the version list below for details.
dotnet add package PhoneNumbersNA --version 1.0.13
NuGet\Install-Package PhoneNumbersNA -Version 1.0.13
<PackageReference Include="PhoneNumbersNA" Version="1.0.13" />
paket add PhoneNumbersNA --version 1.0.13
#r "nuget: PhoneNumbersNA, 1.0.13"
// Install PhoneNumbersNA as a Cake Addin #addin nuget:?package=PhoneNumbersNA&version=1.0.13 // Install PhoneNumbersNA as a Cake Tool #tool nuget:?package=PhoneNumbersNA&version=1.0.13
PhoneNumbersNA
A library for parsing phone numbers. Built around the North American Numbering Plan and the NANPA formal specification.
Find this package on NuGet! 🚀
Helping you Parse and Validate Phone Numbers ☎️
The core of this library is the
var checkParse = PhoneNumber.TryParse(string input, out var phoneNumber);
function that accepts a string that you would like to parse into a single phone number. It mimics the format of int.TryParse(string input, out var value)
to make it easy to use and reason about. The PhoneNumber type that is returned in the out variable contains a variety of useful properties including a 10 digit string named DialedNumber which is what you would literally dial on a phone's keypad to place a call to that number. The components of the DialedNumber are also provided as integers to make them easy to work with and cheap to store. Please note, to convert the NPA, NXX, and XXXX properties to strings you'll need to use the phoneNumber.NPA.ToString("000")
method to preserve any leading zeros that aren't represented by the integer format. To make this senario less error pront we've added phoneNumber.GetNPAAsString()
to handle this formatting issue for you. We've also exposed versions of all the methods that accept strings with alternate versions that accept ReadOnlySpan<char>
to help you save some memory.
To parse a string that may contain many phone numbers use the extension method on the String class included in this library:
var stringlyTypedPhoneNumbers = "12060009999 15030006969 18750001111".ExtractDialedNumbers();
which will return an array of 10 digit phone numbers as strings. If you prefer the strongly typed version you can use the
var stronglyTypedPhoneNumbers = "12060009999 15030006969 18750001111".ExtractPhoneNumbers();
extension method to get an IEnumerable<PhoneNumber>
result.
Alternatively you can call the parsing methods directly using:
var stringlyTypedPhoneNumbers = PhoneNumbersNA.Parse.AsDialedNumbers("12060009999 15030006969 18750001111");
var stronglyTypedPhoneNumbers = PhoneNumbersNA.Parse.AsPhoneNumbers("12060009999 15030006969 18750001111");
If you simply want a yes or no answer to whether a string is a valid NANP phone number you can use the
var checkValid = "12060991111".IsValidPhoneNumber();
to get a Boolean where a value of true means that the string is a valid phone number. There are also extension methods in a similar format for checking if a string is a valid NPA, NXX, XXXX and regular methods that are accessible by calling
var checkValid = PhoneNumbersNA.AreaCode.ValidPhoneNumber("maybeAPhoneNumber");
or accept integers like
var checkValid = PhoneNumbersNA.AreaCode.ValidPhoneNumber(int npa, int nxx, int xxxx);
A common scenario when working with 3rd party VOIP API's like the Teli API or the Call48 API is to query for blocks of available phone numbers by the NPA (Area Code) prefix for those phone numbers. This library provides a list of every active NANP Area Code as an array on integers that is accessible by calling PhoneNumbersNA.AreaCode.All
. If you want a list of only the active non-local, non-geographic area codes you can call PhoneNumbersNA.AreaCode.NonGeographic
and similarly tollfree only Area Codes are available under PhoneNumbersNA.AreaCode.TollFree
.
Some APIs require you to provide the name of the state the area code you are looking for existing within geographically. To that end you can use the PhoneNumbers.AreaCode.AreaCodesByState
array to get a list of objects containing strings for both the short and long versions of state names and an array of all the area codes in that specific state.
Often phone number purchasing APIs make a distinction between Local phone numbers and Tollfree phone numbers. The PhoneNumber class contains a Type property that is accessible after you call the TryParse method on the string representation of a phone number. This Type property is an Enum with a value of Tollfree, NonGeographic, or Local depending on how the phone number was parsed. Using this information, you can chose the correct API endpoint to submit purchase order for that number to. If you prefer to figure out the Type of the number directly you can use the string extension methods
var checkNonGeographic = "9990221111".IsNonGeographic()
or
var checkTollFree = "9990221111".IsTollfree()
to get a Boolean as an answer.
This library is used in production by Accelerate Networks and grew organically out of a large set of utility functions that have now been condensed into PhoneNumbersNA. 🥳
Performance 🚅
You can run the benchmarks for this library on your local machine by cloning this repo and then opening the solution file in Visual Studio 2022. Select the PhoneNumbersNA.Benchmark console app and then run it as a "Release" build. The benchmarks typically take about 3 minutes to run. Alternatively you can install the .NET SDK and use .NET CLI to build the project in release mode and run it.
Here are the benchmarks for the current version of PhoneNumbersNA:
This is quite an improvement over the .NET 6 version. Memory consumption is cut by 2/3rds in the 84, 887, and 8870 phone number benchmarks. For single numbers and in our 84 phone number benchmark performance is also improved by about 50%. These gains come mostly from the use of ReadOnlySpan<char>
and CollectionsMarshall.AsSpan(List<char>)
where string
and new string(List<char>.ToArray())
was previously used. These changes have prevented many unnecessary string allocations which is why memory consumption has been reduced so significantly. This library will never be allocation free as we're parsing and creating new strings, but we can get close.
How to Contribute 🤝
Please start by creating a new issue with a description of the problem and a method to reproduce it.
How to run this project locally 🏃
- Clone the repo to your machine
- .NET 8.0 or greater is required (included in Visual Studio 2022)
- Use Visual Studio 2022 Community Edition or VSCode with the dotnet CLI and CSharp language extension to edit, build, and run the test suite.
- Double click the "PhoneNumbersNA.sln" file to open the solution in Visual Studio 2022.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.15 | 181 | 11/5/2024 |
1.0.14 | 820 | 4/12/2024 |
1.0.13 | 364 | 2/22/2024 |
1.0.12 | 1,925 | 5/1/2023 |
1.0.11 | 219 | 4/28/2023 |
1.0.10 | 557 | 3/9/2023 |
1.0.9 | 299 | 2/24/2023 |
1.0.8 | 849 | 7/23/2022 |
1.0.7 | 477 | 5/25/2022 |
1.0.6 | 505 | 4/3/2022 |
1.0.5 | 510 | 2/24/2022 |
1.0.4 | 465 | 2/22/2022 |
1.0.3 | 493 | 10/31/2021 |
1.0.2 | 312 | 10/19/2021 |
1.0.1 | 321 | 10/19/2021 |
1.0.0 | 341 | 10/18/2021 |