📜  awesome-phonenumber (1)

📅  最后修改于: 2023-12-03 14:59:25.948000             🧑  作者: Mango

Awesome-phonenumber

Introduction

awesome-phonenumber is a JavaScript library that can be used to validate, format, and parse phone numbers from around the world.

Features
  • Validate phone numbers
  • Format phone numbers
  • Parse phone numbers from strings
  • Extract country calling codes from phone numbers
  • Convert phone numbers into local and international formats
  • Get information about the phone number's region, carrier, and line type
  • Support for multiple languages including English and French
Installation

You can install awesome-phonenumber using npm:

npm install awesome-phonenumber
Usage

To use awesome-phonenumber, you first need to create a PhoneNumber object:

const PhoneNumber = require('awesome-phonenumber');
const number = new PhoneNumber('+1 (123) 456-7890');

Once you have a PhoneNumber object, you can perform various operations on it:

Validate a phone number

number.isValid(); // true

Format a phone number

number.getNumber('e164'); // '+11234567890'
number.getNumber('national'); // '(123) 456-7890'
number.getNumber('international'); // '+1 123-456-7890'

Parse a phone number from a string

const number2 = PhoneNumber.parse('123-456-7890', 'US');
number2.getNumber('e164'); // '+14123456789'

Extract country calling codes from a phone number

number.getCountryCode(); // 1

Convert a phone number into local and international formats

number.getLocalNumber(); // '123-456-7890'
number.getInternationalNumber(); // '+1 123-456-7890'

Get information about the phone number's region, carrier, and line type

number.getRegionCode(); // 'US'
number.getCarrier(); // null
number.getLineType(); // 'FIXED_LINE_OR_MOBILE'
Conclusion

awesome-phonenumber is a powerful and flexible library for working with phone numbers in JavaScript. Its wide range of features makes it an essential tool for developers who need to work with phone numbers from around the world. Try it out today!