What is @types/google-libphonenumber?
@types/google-libphonenumber provides TypeScript type definitions for the google-libphonenumber library, which is a powerful library for parsing, formatting, and validating international phone numbers.
What are @types/google-libphonenumber's main functionalities?
Parsing Phone Numbers
This feature allows you to parse a phone number into a structured format. The code sample demonstrates how to parse a US phone number.
const libphonenumber = require('google-libphonenumber');
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('+14155552671', 'US');
console.log(number);
Formatting Phone Numbers
This feature allows you to format a parsed phone number into various formats such as international, national, E.164, etc. The code sample demonstrates formatting a US phone number into the international format.
const libphonenumber = require('google-libphonenumber');
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('+14155552671', 'US');
const formattedNumber = phoneUtil.format(number, libphonenumber.PhoneNumberFormat.INTERNATIONAL);
console.log(formattedNumber);
Validating Phone Numbers
This feature allows you to validate whether a phone number is valid for a given region. The code sample demonstrates validating a US phone number.
const libphonenumber = require('google-libphonenumber');
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('+14155552671', 'US');
const isValid = phoneUtil.isValidNumber(number);
console.log(isValid);
Other packages similar to @types/google-libphonenumber
libphonenumber-js
libphonenumber-js is a simpler and smaller alternative to google-libphonenumber. It provides similar functionalities for parsing, formatting, and validating phone numbers but with a smaller footprint and easier integration.
awesome-phonenumber
awesome-phonenumber is another alternative that wraps google-libphonenumber and provides additional features such as handling phone number types and regions more easily. It aims to simplify the usage of google-libphonenumber.