Skip to content

Quickstart

Getting started with @nhtio/phone-object is quick and easy. Just follow the steps below and you'll be up and running in no time.

Installation

You can install @nhtio/phone-object directly from your preferred package manager

sh
npm i @nhtio/phone-object
sh
pnpm add @nhtio/phone-object
sh
yarn add @nhtio/phone-object

Usage

Import the Phone Object

typescript
import { Phone } from '@nhtio/phone-object'

Create a new Phone Object

typescript
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

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

Get the country code

typescript
phone.country // US

Get a timezone suggestion

typescript
phone.timezone // America/New_York

Get the type of line

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

Serialize for storage

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

Deserialize from storage

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