npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phone-number-formatter-corrector

v1.0.14

Published

This library provides a simple and efficient way to format and correct phone numbers, ensuring they adhere to international standards. It is designed to be easy to use, with a focus on accuracy and reliability.

Downloads

3

Readme

Phone Number Formatter and Corrector

A JavaScript library to automatically format and correct phone numbers for international dialing, with a focus on accuracy and ease of use.

Features

  • Automatic Formatting: Converts phone numbers into the E.164 standard format, making them ready for international calls.
  • Country Code Correction: Adds the correct country code if missing or incorrect.
  • Mobile Operator Detection: Identifies the mobile operator based on the phone number’s prefix.
  • Phone Number Length Validation: Validates the length of a phone number to ensure it is correct for the specified country.
  • Phone Number Information Retrieval: Retrieves comprehensive information about a phone number, including formatting, country code, operator, and length validation.
  • Caching: Utilizes caching to store previously formatted and corrected phone numbers for quicker access in future requests.
  • Error Handling: Provides clear error messages for invalid or incorrectly formatted phone numbers.

Installation

Install the package using npm:

npm install phone-number-formatter-corrector

Usage

const { formatPhoneNumber, getMobileOperator, getPhoneNumberInfo, isNumberValidForRegion } = require('./index');

const phoneNumber = '17645685126';
const code = 'DE';
//TODO: we might move this list to a test-config file and import 
// if it becomes to long.
// To add more validation, please add an object which has a countryCode key and provide
// the list of good and bad numbers. If tests is failing for you, please create an issue 
// in github and we will track it.
const testPhoneNumberList = [
    {
        'countryCode': 'CM',
        'numbers': ['6960923457683', '696092445', '696092545', '677123456'],
        'badNumbers': ['6960923450', '6960924450', '6960925450', '6771234560']
    }
]
const formattedNumber = formatPhoneNumber(phoneNumber, code);

const operator = getMobileOperator(phoneNumber, code);

const phoneNumberInfo = getPhoneNumberInfo(phoneNumber, code);

console.log(`Original Number: ${phoneNumber}`);
console.log(`Formatted Number: ${formattedNumber}`);
console.log(`Mobile Operator: ${operator}`);
console.log(`Phone Number Info: ${JSON.stringify(phoneNumberInfo)}`);
console.assert(phoneNumberInfo.formattedNumber == formattedNumber);
console.assert(phoneNumberInfo.countryCode == code);
console.assert(phoneNumberInfo.operator == operator);



testPhoneNumberList.forEach(function (obj) {
    obj.numbers.forEach(e => console.assert(isNumberValidForRegion(e, obj.countryCode), `${e}(${obj.countryCode}) is expected to be correct, but failed.`));
});

testPhoneNumberList.forEach(function (obj) {
    obj.badNumbers.forEach(e => console.assert(!isNumberValidForRegion(e, obj.countryCode), `${e}(${obj.countryCode}) is expected to fail.`));
});

// here our tests have passed. We just dummy log the phone number info to operators
// this just shows the importance of the isValid attribut on the phone number info object.
testPhoneNumberList.forEach(function (obj) {
    obj.numbers.forEach(e => console.log(`${JSON.stringify(getPhoneNumberInfo(e, obj.countryCode))}`));
});

// We see here that we could process a number, but this number is not valid.
console.log("");
testPhoneNumberList.forEach(function (obj) {
    obj.badNumbers.forEach(e => console.log(`${JSON.stringify(getPhoneNumberInfo(e, obj.countryCode))}`));
});

API

formatPhoneNumber(number, countryCode)

  • number (String): The phone number to be formatted.
  • countryCode (String): The country code to be used if the phone number is not in international format.

getMobileOperator(number, countryCode)

  • number (String): The phone number for which the mobile operator will be determined.
  • countryCode (String): The country code for the provided phone number.

getPhoneNumberInfo(number, countryCode)

  • number (String): The phone number to retrieve information for.
  • countryCode (String): The country code for the provided phone number.
  • Returns an object with the phone number information, including the formatted number, country code, mobile operator, and length validation.

isPhoneNumberLengthCorrect(number, countryCode)

  • number (String): The phone number to validate the length for.
  • countryCode (String): The country code for the provided phone number.
  • Returns an object with the validation result code and message. A result code of 0 indicates the length is correct, -1 indicates incorrect length, and -2 indicates no fixed length was found for the country.

Technologies Used

  • google-libphonenumber: For parsing, formatting, and validating international phone numbers.
  • node-cache: For caching previously formatted and corrected phone numbers.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.