UNPKG

phone-number-generator-js

Version:

Generates a random phone number following the E.164 international standard

149 lines (114 loc) โ€ข 12.9 kB
# Phone number generator · [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) ## What is phone number generator? phone-number-generator-js a versatile JavaScript library that generates random mobile phone numbers in the E.164 format. With this library, you can easily generate a completely random phone number or configure it to generate numbers specific to a particular country. It supports a wide range of countries, with a total of 227 supported countries, allowing you to generate phone numbers that are valid and appropriate for various regions across the globe. By default, if no configuration is provided, a totally random phone number will be generated from any of the 226 supported countries. ### Relevant links: - [E.164 format definition](https://en.wikipedia.org/wiki/E.164) - [What is E.164?](https://www.twilio.com/docs/glossary/what-e164) - [What is the E.164 Format, and how to use it correctly](https://voipstudio.com/blog/what-is-the-e-164-format-and-how-to-use-it-correctly/) ## Install ``` npm install phone-number-generator-js // or yarn add phone-number-generator-js ``` ## Usage ```javascript const { generatePhoneNumber } = require("phone-number-generator-js"); // or import { generatePhoneNumber } from "phone-number-generator-js"; ``` ### 1. Simple usage ```javascript generatePhoneNumber(); // '+85265698900' ``` ### 2. With config #### Using countryName enum ```javascript generatePhoneNumber({ countryName: CountryNames.Austria }); // '+436508162890' ``` #### Using withoutCountryCode flag (defaults to false) ```javascript generatePhoneNumber({ countryName: CountryNames.Austria, withoutCountryCode: true, }); // '5483746575' ``` ## Test ``` npm run test // or yarn test ``` ## Build ``` npm run build // or yarn build ``` ## Supported countries: | Countries | | | | | --------------------------------------------- | ----------------------------------- | ----------------------------------- | ------------------------------ | | Afghanistan ๐Ÿ‡ฆ๐Ÿ‡ซ | Albania ๐Ÿ‡ฆ๐Ÿ‡ฑ | Algeria ๐Ÿ‡ฉ๐Ÿ‡ฟ | American Samoa ๐Ÿ‡ฆ๐Ÿ‡ธ | | Andorra ๐Ÿ‡ฆ๐Ÿ‡ฉ | Angola ๐Ÿ‡ฆ๐Ÿ‡ด | Anguilla ๐Ÿ‡ฆ๐Ÿ‡ฎ | Antigua and Barbuda ๐Ÿ‡ฆ๐Ÿ‡ฌ | | Argentina ๐Ÿ‡ฆ๐Ÿ‡ท | Armenia ๐Ÿ‡ฆ๐Ÿ‡ฒ | Aruba ๐Ÿ‡ฆ๐Ÿ‡ผ | Australia ๐Ÿ‡ฆ๐Ÿ‡บ | | Austria ๐Ÿ‡ฆ๐Ÿ‡น | Azerbaijan ๐Ÿ‡ฆ๐Ÿ‡ฟ | Bahamas ๐Ÿ‡ง๐Ÿ‡ธ | Bahrain ๐Ÿ‡ง๐Ÿ‡ญ | | Bangladesh ๐Ÿ‡ง๐Ÿ‡ฉ | Barbados ๐Ÿ‡ง๐Ÿ‡ง | Belarus ๐Ÿ‡ง๐Ÿ‡พ | Belgium ๐Ÿ‡ง๐Ÿ‡ช | | Belize ๐Ÿ‡ง๐Ÿ‡ฟ | Benin ๐Ÿ‡ง๐Ÿ‡ฏ | Bermuda ๐Ÿ‡ง๐Ÿ‡ฒ | Bhutan ๐Ÿ‡ง๐Ÿ‡น | | Bolivia ๐Ÿ‡ง๐Ÿ‡ด | Bosnia and Herzegovina ๐Ÿ‡ง๐Ÿ‡ฆ | Botswana ๐Ÿ‡ง๐Ÿ‡ผ | Brazil ๐Ÿ‡ง๐Ÿ‡ท | | British Virgin Islands ๐Ÿ‡ป๐Ÿ‡ฌ | Brunei Darussalam ๐Ÿ‡ง๐Ÿ‡ณ | Bulgaria ๐Ÿ‡ง๐Ÿ‡ฌ | Burkina Faso ๐Ÿ‡ง๐Ÿ‡ซ | | Burundi ๐Ÿ‡ง๐Ÿ‡ฎ | Cambodia ๐Ÿ‡ฐ๐Ÿ‡ญ | Cameroon ๐Ÿ‡จ๐Ÿ‡ฒ | Canada ๐Ÿ‡จ๐Ÿ‡ฆ | | Cape Verde ๐Ÿ‡จ๐Ÿ‡ป | Cayman Islands ๐Ÿ‡ฐ๐Ÿ‡พ | Central African Republic ๐Ÿ‡จ๐Ÿ‡ซ | Chad ๐Ÿ‡น๐Ÿ‡ฉ | | Chile ๐Ÿ‡จ๐Ÿ‡ฑ | China ๐Ÿ‡จ๐Ÿ‡ณ | Colombia ๐Ÿ‡จ๐Ÿ‡ด | Comoros ๐Ÿ‡ฐ๐Ÿ‡ฒ | | Congo ๐Ÿ‡จ๐Ÿ‡ฉ | Cook Islands ๐Ÿ‡จ๐Ÿ‡ฐ | Costa Rica ๐Ÿ‡จ๐Ÿ‡ท | Cรดte D'Ivoire ๐Ÿ‡จ๐Ÿ‡ฎ | | Croatia ๐Ÿ‡ญ๐Ÿ‡ท | Cuba ๐Ÿ‡จ๐Ÿ‡บ | Curaรงao ๐Ÿ‡จ๐Ÿ‡ผ | Cyprus ๐Ÿ‡จ๐Ÿ‡พ | | Czech Republic ๐Ÿ‡จ๐Ÿ‡ฟ | Democratic Republic of the Congo ๐Ÿ‡จ๐Ÿ‡ฉ | Denmark ๐Ÿ‡ฉ๐Ÿ‡ฐ | Djibouti ๐Ÿ‡ฉ๐Ÿ‡ฏ | | Dominica ๐Ÿ‡ฉ๐Ÿ‡ฒ | Dominican Republic ๐Ÿ‡ฉ๐Ÿ‡ด | East Timor ๐Ÿ‡น๐Ÿ‡ฑ | Ecuador ๐Ÿ‡ช๐Ÿ‡จ | | Egypt ๐Ÿ‡ช๐Ÿ‡ฌ | El Salvador ๐Ÿ‡ธ๐Ÿ‡ป | Equatorial Guinea ๐Ÿ‡ฌ๐Ÿ‡ถ | Eritrea ๐Ÿ‡ช๐Ÿ‡ท | | Estonia ๐Ÿ‡ช๐Ÿ‡ช | Eswatini ๐Ÿ‡ธ๐Ÿ‡ฟ | Ethiopia ๐Ÿ‡ช๐Ÿ‡น | Falkland Islands (Malvinas) ๐Ÿ‡ซ๐Ÿ‡ฐ | | Faroe Islands ๐Ÿ‡ซ๐Ÿ‡ด | Fiji ๐Ÿ‡ซ๐Ÿ‡ฏ | Finland ๐Ÿ‡ซ๐Ÿ‡ฎ | France ๐Ÿ‡ซ๐Ÿ‡ท | | French Guiana ๐Ÿ‡ฌ๐Ÿ‡ซ | French Polynesia ๐Ÿ‡ต๐Ÿ‡ซ | Gabon ๐Ÿ‡ฌ๐Ÿ‡ฆ | Gambia ๐Ÿ‡ฌ๐Ÿ‡ฒ | | Georgia ๐Ÿ‡ฌ๐Ÿ‡ช | Germany ๐Ÿ‡ฉ๐Ÿ‡ช | Ghana ๐Ÿ‡ฌ๐Ÿ‡ญ | Gibraltar ๐Ÿ‡ฌ๐Ÿ‡ฎ | | Greece ๐Ÿ‡ฌ๐Ÿ‡ท | Greenland ๐Ÿ‡ฌ๐Ÿ‡ฑ | Grenada ๐Ÿ‡ฌ๐Ÿ‡ฉ | Guadeloupe ๐Ÿ‡ฌ๐Ÿ‡ต | | Guam ๐Ÿ‡ฌ๐Ÿ‡บ | Guatemala ๐Ÿ‡ฌ๐Ÿ‡น | Guinea ๐Ÿ‡ฌ๐Ÿ‡ณ | Guinea-Bissau ๐Ÿ‡ฌ๐Ÿ‡ผ | | Guyana ๐Ÿ‡ฌ๐Ÿ‡พ | Haiti ๐Ÿ‡ญ๐Ÿ‡น | Honduras ๐Ÿ‡ญ๐Ÿ‡ณ | Hong Kong ๐Ÿ‡ญ๐Ÿ‡ฐ | | Hungary ๐Ÿ‡ญ๐Ÿ‡บ | Iceland ๐Ÿ‡ฎ๐Ÿ‡ธ | India ๐Ÿ‡ฎ๐Ÿ‡ณ | Indonesia ๐Ÿ‡ฎ๐Ÿ‡ฉ | | Iran ๐Ÿ‡ฎ๐Ÿ‡ท | Iraq ๐Ÿ‡ฎ๐Ÿ‡ถ | Ireland ๐Ÿ‡ฎ๐Ÿ‡ช | Israel ๐Ÿ‡ฎ๐Ÿ‡ฑ | | Italy ๐Ÿ‡ฎ๐Ÿ‡น | Jamaica ๐Ÿ‡ฏ๐Ÿ‡ฒ | Japan ๐Ÿ‡ฏ๐Ÿ‡ต | Jordan ๐Ÿ‡ฏ๐Ÿ‡ด | | Kazakhstan ๐Ÿ‡ฐ๐Ÿ‡ฟ | Kenya ๐Ÿ‡ฐ๐Ÿ‡ช | Korea, Republic of ๐Ÿ‡ฐ๐Ÿ‡ท | | Kuwait ๐Ÿ‡ฐ๐Ÿ‡ผ | Kyrgyzstan ๐Ÿ‡ฐ๐Ÿ‡ฌ | Lao People's Democratic Republic ๐Ÿ‡ฑ๐Ÿ‡ฆ | Latvia ๐Ÿ‡ฑ๐Ÿ‡ป | | Lebanon ๐Ÿ‡ฑ๐Ÿ‡ง | Lesotho ๐Ÿ‡ฑ๐Ÿ‡ธ | Liberia ๐Ÿ‡ฑ๐Ÿ‡ท | Libya ๐Ÿ‡ฑ๐Ÿ‡พ | | Liechtenstein ๐Ÿ‡ฑ๐Ÿ‡ฎ | Lithuania ๐Ÿ‡ฑ๐Ÿ‡น | Luxembourg ๐Ÿ‡ฑ๐Ÿ‡บ | Macao ๐Ÿ‡ฒ๐Ÿ‡ด | | Macedonia, the Former Yugoslav Republic Of ๐Ÿ‡ฒ๐Ÿ‡ฐ | Madagascar ๐Ÿ‡ฒ๐Ÿ‡ฌ | Malawi ๐Ÿ‡ฒ๐Ÿ‡ผ | Malaysia ๐Ÿ‡ฒ๐Ÿ‡พ | | Maldives ๐Ÿ‡ฒ๐Ÿ‡ป | Mali ๐Ÿ‡ฒ๐Ÿ‡ฑ | Malta ๐Ÿ‡ฒ๐Ÿ‡น | Marshall Islands ๐Ÿ‡ฒ๐Ÿ‡ญ | | Martinique ๐Ÿ‡ฒ๐Ÿ‡ถ | Mauritania ๐Ÿ‡ฒ๐Ÿ‡ท | Mauritius ๐Ÿ‡ฒ๐Ÿ‡บ | Mexico ๐Ÿ‡ฒ๐Ÿ‡ฝ | | Micronesia, Federated States Of ๐Ÿ‡ซ๐Ÿ‡ฒ | Moldova, Republic of ๐Ÿ‡ฒ๐Ÿ‡ฉ | Monaco ๐Ÿ‡ฒ๐Ÿ‡จ | | Mongolia ๐Ÿ‡ฒ๐Ÿ‡ณ | Montenegro ๐Ÿ‡ฒ๐Ÿ‡ช | Montserrat ๐Ÿ‡ฒ๐Ÿ‡ธ | Morocco ๐Ÿ‡ฒ๐Ÿ‡ฆ | | Mozambique ๐Ÿ‡ฒ๐Ÿ‡ฟ | Myanmar ๐Ÿ‡ฒ๐Ÿ‡ฒ | Namibia ๐Ÿ‡ณ๐Ÿ‡ฆ | Nauru ๐Ÿ‡ณ๐Ÿ‡ท | | Nepal ๐Ÿ‡ณ๐Ÿ‡ต | Netherlands ๐Ÿ‡ณ๐Ÿ‡ฑ | New Caledonia ๐Ÿ‡ณ๐Ÿ‡จ | New Zealand ๐Ÿ‡ณ๐Ÿ‡ฟ | | Nicaragua ๐Ÿ‡ณ๐Ÿ‡ฎ | Niger ๐Ÿ‡ณ๐Ÿ‡ช | Nigeria ๐Ÿ‡ณ๐Ÿ‡ฌ | Niue ๐Ÿ‡ณ๐Ÿ‡บ | | Norfolk Island ๐Ÿ‡ณ๐Ÿ‡ซ | Northern Mariana Islands ๐Ÿ‡ฒ๐Ÿ‡ต | Norway ๐Ÿ‡ณ๐Ÿ‡ด | Oman ๐Ÿ‡ด๐Ÿ‡ฒ | | Pakistan ๐Ÿ‡ต๐Ÿ‡ฐ | Palau ๐Ÿ‡ต๐Ÿ‡ผ | Panama ๐Ÿ‡ต๐Ÿ‡ฆ | Papua New Guinea ๐Ÿ‡ต๐Ÿ‡ฌ | | Paraguay ๐Ÿ‡ต๐Ÿ‡พ | Peru ๐Ÿ‡ต๐Ÿ‡ช | Philippines ๐Ÿ‡ต๐Ÿ‡ญ | Pitcairn ๐Ÿ‡ต๐Ÿ‡ณ | | Poland ๐Ÿ‡ต๐Ÿ‡ฑ | Portugal ๐Ÿ‡ต๐Ÿ‡น | Puerto Rico ๐Ÿ‡ต๐Ÿ‡ท | Qatar ๐Ÿ‡ถ๐Ÿ‡ฆ | | Rรฉunion ๐Ÿ‡ท๐Ÿ‡ช | Romania ๐Ÿ‡ท๐Ÿ‡ด | Russian Federation ๐Ÿ‡ท๐Ÿ‡บ | Rwanda ๐Ÿ‡ท๐Ÿ‡ผ | | Saint Helena ๐Ÿ‡ธ๐Ÿ‡ญ | Saint Kitts And Nevis ๐Ÿ‡ฐ๐Ÿ‡ณ | Saint Lucia ๐Ÿ‡ฑ๐Ÿ‡จ | Saint Pierre And Miquelon ๐Ÿ‡ต๐Ÿ‡ฒ | | Saint Vincent And The Grenedines ๐Ÿ‡ป๐Ÿ‡จ | Samoa ๐Ÿ‡ผ๐Ÿ‡ธ | San Marino ๐Ÿ‡ธ๐Ÿ‡ฒ | Sao Tome and Principe ๐Ÿ‡ธ๐Ÿ‡น | | Saudi Arabia ๐Ÿ‡ธ๐Ÿ‡ฆ | Senegal ๐Ÿ‡ธ๐Ÿ‡ณ | Serbia ๐Ÿ‡ท๐Ÿ‡ธ | Seychelles ๐Ÿ‡ธ๐Ÿ‡จ | | Sierra Leone ๐Ÿ‡ธ๐Ÿ‡ฑ | Singapore ๐Ÿ‡ธ๐Ÿ‡ฌ | Sint Maarten ๐Ÿ‡ธ๐Ÿ‡ฝ | Slovakia ๐Ÿ‡ธ๐Ÿ‡ฐ | | Slovenia ๐Ÿ‡ธ๐Ÿ‡ฎ | Solomon Islands ๐Ÿ‡ธ๐Ÿ‡ง | Somalia ๐Ÿ‡ธ๐Ÿ‡ด | South Africa ๐Ÿ‡ฟ๐Ÿ‡ฆ | | South Sudan ๐Ÿ‡ธ๐Ÿ‡ธ | Spain ๐Ÿ‡ช๐Ÿ‡ธ | Sri Lanka ๐Ÿ‡ฑ๐Ÿ‡ฐ | Sudan ๐Ÿ‡ธ๐Ÿ‡ฉ | | Suriname ๐Ÿ‡ธ๐Ÿ‡ท | Sweden ๐Ÿ‡ธ๐Ÿ‡ช | Switzerland ๐Ÿ‡จ๐Ÿ‡ญ | Syrian Arab Republic ๐Ÿ‡ธ๐Ÿ‡พ | | Taiwan ๐Ÿ‡น๐Ÿ‡ผ | Tajikistan ๐Ÿ‡น๐Ÿ‡ฏ | Tanzania, United Republic of ๐Ÿ‡น๐Ÿ‡ฟ | Thailand ๐Ÿ‡น๐Ÿ‡ญ | | Timor-Leste ๐Ÿ‡น๐Ÿ‡ฑ | Togo ๐Ÿ‡น๐Ÿ‡ฌ | Tokelau ๐Ÿ‡น๐Ÿ‡ฐ | Tonga ๐Ÿ‡น๐Ÿ‡ด | | Trinidad and Tobago ๐Ÿ‡น๐Ÿ‡น | Tunisia ๐Ÿ‡น๐Ÿ‡ณ | Turkey ๐Ÿ‡น๐Ÿ‡ท | Turkmenistan ๐Ÿ‡น๐Ÿ‡ฒ | | Turks and Caicos Islands ๐Ÿ‡น๐Ÿ‡จ | Tuvalu ๐Ÿ‡น๐Ÿ‡ป | Uganda ๐Ÿ‡บ๐Ÿ‡ฌ | Ukraine ๐Ÿ‡บ๐Ÿ‡ฆ | | United Arab Emirates ๐Ÿ‡ฆ๐Ÿ‡ช | United Kingdom ๐Ÿ‡ฌ๐Ÿ‡ง | United States ๐Ÿ‡บ๐Ÿ‡ธ | Uruguay ๐Ÿ‡บ๐Ÿ‡พ | | Uzbekistan ๐Ÿ‡บ๐Ÿ‡ฟ | Vanuatu ๐Ÿ‡ป๐Ÿ‡บ | Vietnam ๐Ÿ‡ป๐Ÿ‡ณ | Yemen ๐Ÿ‡พ๐Ÿ‡ช | | Virgin Islands, British ๐Ÿ‡ป๐Ÿ‡ฌ | Virgin Islands, U.S. ๐Ÿ‡ป๐Ÿ‡ฎ | Wallis and Futuna ๐Ÿ‡ผ๐Ÿ‡ซ | | Zambia ๐Ÿ‡ฟ๐Ÿ‡ฒ | Zimbabwe ๐Ÿ‡ฟ๐Ÿ‡ผ | ## Credits 1. Validating that the generated phone number is valid & following the E.164 format - [libphonenumber-js](https://gitlab.com/catamphetamine/libphonenumber-js) 2. Countries phone number data (country code, mobile starting digits & length) - [phone](https://github.com/AfterShip/phone)