Skip to content

An immutable data structure representing a specific phone number and accompanying methods. It contains class and instance methods of creating, parsing, validating, and formatting phone numbers. Based on google-libphonenumber, which is in turn based on Google's libphonenumber.

License

jakguru/phone-object

Repository files navigation

Phone Object

Phone Object is a comprehensive, immutable object representation of a phone number, complete with an array of properties detailing different aspects of the said phone number.

Inspired by Google's libphonenumber, our library extends its capabilities by parsing phone numbers from a diverse range of formats. This library is capable of validating phone numbers per country, identifying the type of line a phone number belongs to (like mobile, landline, etc.), and suggesting potential timezones associated with the phone number.

This project is perfect for anyone who needs robust and efficient phone number parsing and handling in their applications. Whether you're validating user-input phone numbers, normalizing phone numbers into a standard format, or simply analyzing phone number data, the Phone Object Library has got you covered.

For more information, view the documentation.

Installation

npm install @jakguru/phone-object

or

yarn add @jakguru/phone-object

Usage

Import / Require the library

import { Phone } from '@jakguru/phone-object'

or

import Phone from '@jakguru/phone-object'

or

const { Phone } = require('@jakguru/phone-object')

Create a new Phone Object

const phone = new Phone('202-456-141', 'US')
const phone = new Phone('202-456-1414', 'US')
const phone = new Phone('(202) 456-1414', 'US')
const phone = new Phone('+1 (202) 456-1414', 'US')
const phone = new Phone('202.456.1414', 'US')
const phone = new Phone('202/4561414', 'US')
const phone = new Phone('1 202 456 1414', 'US')
const phone = new Phone('+12024561414', 'US')
const phone = new Phone('1 202-456-1414' 'US')

Get the phone number in different formats

phone.raw // 2024561414
phone.national // (202) 456-1414
phone.international // +1 202-456-1414
phone.e164 // +12024561414

Get the country code

phone.country // US

Get a timezone suggestion

phone.timezone // America/New_York

Get the type of line

phone.valid // true
phone.type // FIXED_LINE_OR_MOBILE
phone.mobile // true

Serialize for storage

phone.serialize() // <base64 encoded string>

Deserialize from storage

const phone = Phone.deserialize(<base64 encoded string>)

About

An immutable data structure representing a specific phone number and accompanying methods. It contains class and instance methods of creating, parsing, validating, and formatting phone numbers. Based on google-libphonenumber, which is in turn based on Google's libphonenumber.

Resources

License

Stars

Watchers

Forks

Packages

No packages published