Coder Social home page Coder Social logo

correcttel's Introduction

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 Information Retrieval: Retrieves comprehensive information about a phone number, including formatting, country code, and operator.
  • 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 } = require('phone-number-formatter-corrector');

const formattedNumber = formatPhoneNumber('677123456', 'CM');
console.log(formattedNumber); // Expected output: +237677123456

const operator = getMobileOperator('677123456', 'CM');
console.log(operator); // Expected output: MTN (or the appropriate operator for this number)

const phoneNumberInfo = getPhoneNumberInfo('677123456', 'CM');
console.log(`Phone Number Info: ${JSON.stringify(phoneNumberInfo)}`);
// Assertions to validate the phone number information
console.assert(phoneNumberInfo.formattedNumber == formattedNumber);
console.assert(phoneNumberInfo.countryCode == 'CM');
console.assert(phoneNumberInfo.operator == operator);

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, and mobile operator.

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.

correcttel's People

Contributors

jessicanono avatar mthias88 avatar ntsieyo avatar sabiou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.