Skip to content

Support random and valid North American Numbering Plan phone numbers #2303

Open
@ispringle

Description

@ispringle

Clear and concise description of the problem

https://fakerjs.dev/api/phone.html#number

This won't generate valid and random numbers though. At least not valid US/CA (aka the North American Numbering Plan, or those phones using the country code +1) phone numbers. A +1 number must broadly speaking follow the pattern:

^[2-9](?!11)\d{2}-?[2-9](?!11)\d{2}-?[0-9]{4}$

There are three parts to a +1 number:

  1. area code
  2. central office or branch code
  3. line number

The line number can be any four digits (\d{4}). The first two triplets (area and branch) cannot start with the number 1 ([2-9]\d{2}) and they cannot end with the two ones. Currently there is no way (that I can tell) to create valid +1 phone numbers with Faker. I can manually set the area and branch to a fixed number and just generate random line numbers, however for some use cases this is not acceptable (for example determining the area code).

Suggested solution

I have two potential solutions.

  1. The existing faker.phone.number method could accept a more detailed string. This could be achieved by either allowing for some regex-like pattern to be passed or providing some additional syntax beyond just replacing the pound with a random digit.
  2. Make the existing faker.phone.number output phone numbers that are proper and valid North American Numbering Plan numbers. This seems like the solution more inline with how faker is setup, as to the best of my knowledge most of the default faker API methods return US formatted data and if you wish to use say a Polish locale you need to import that separately.

Alternative

No response

Additional context

Here is the wikipedia article on the North American Numbering plan: https://en.wikipedia.org/wiki/North_American_Numbering_Plan

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: featureRequest for new featurem: phoneSomething is referring to the phone modulep: 1-normalNothing urgents: waiting for user interestWaiting for more users interested in this feature

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions