faker-js / faker

Generate massive amounts of fake data in the browser and node.js
https://fakerjs.dev
Other
12.49k stars 897 forks source link

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

Open ispringle opened 1 year ago

ispringle commented 1 year ago

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

github-actions[bot] commented 1 year ago

Thank you for your feature proposal.

We marked it as "waiting for user interest" for now to gather some feedback from our community:

ST-DDT commented 1 year ago

For this we would have to decide whether we want to go for fake or regex patterns.

matthewmayer commented 1 year ago

https://github.com/faker-js/faker/blob/next/src/locales/en/phone_number/formats.ts it seems we are already using the ! character in the patterns to ensure the area and branch don't start with 1s? So the main change would be to ensure the numbers also can't end with 11?

ispringle commented 1 year ago

Sorry this took so long for me to get back to...

I was not aware of the ! that would take care of the leading digit in the two triplets. I guess then we'd need some way of signifying "not two 1s in a row" (?!11).