npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yext/phonenumber-util

v0.2.2

Published

Utility for extracting and validating phone numbers

Downloads

723

Readme

Utility for extracting and validating phone numbers. Extracts an array of phone numbers from an inputted string, validates that these numbers appear genuine and provide data about those phone numbers.

License Statements Branches Functions Lines

Scripts

Install dependencies

npm i

Formatting source code

npm run format

Linting

npm run lint

Testing

npm test

Code Coverage Report

npx vitest run --coverage

Warning

The returned object will include a rawNumber value. This value is the return of the exact value passed to the function. No sanitization occurs with this value. If you reference this number, ensure you sanitize it BEFORE passing to this function.

Usage

There is additional functionality exposed as export, but the primary expected use case is:

isValidPhoneNumber

Returns a boolean based on whether the passed number is presumed to be valid or invalid.

This checks for region code, number length and validity of region code and area code (where applicable).

import { isValidPhoneNumber } from '@yext/phonenumber-util';
const validPhoneNumber = '3103496333';
isValidPhoneNumber(validPhoneNumber); // Returns `true` - "310" is an area code for California

const invalidPhoneNumber = '3113496333';
isValidPhoneNumber(invalidPhoneNumber); // Returns `false` - "311" is not a valid area code

const intlNumber = '+380 97 123 4567';
isValidPhoneNumber(intlNumber); // Returns `true` - "380" is the region code for Ukraine

const invalidIntlNumber = '+666 97 123 4567';
isValidPhoneNumber(invalidIntlNumber); // Returns `false` - "666" is not a valid region code

getPhoneParts

Return an object of relevant phone number parts and information.

import { getPhoneParts } from '@yext/phonenumber-util';
const validPhoneNumber = '3496333';
getPhoneParts(validPhoneNumber); // Returns an object, assumed to be US / Canada, region code "1" but no area code can be reliably determined.

const validPhoneNumber = '+923331234567';
getPhoneParts(validPhoneNumber); // Returns an object, assumed to be Pakistan, region code "92".

Example output for Pakistan ("+923331234567"):

{
  areaCode: null,
  e164: "+923331234567",
  format: "+xx xxx xxx xxxx",
  formattedNumber: "+92 333 123 4567",
  href: "tel:+923331234567",
  localNumber: "3331234567",
  rawNumber: "+923331234567",
  regionCode: "92"
}

Example for US with 7 digits provided ("3496200") where no state can be determined. Note that e164 and formattedNumber also cannot be derived:

{
  areaCode: null,
  e164: null,
  format: "(xxx) xxx-xxxx",
  formattedNumber: null,
  href: "tel:3496200",
  localNumber: "3496200",
  rawNumber: "349.6200",
  regionCode: "1"
}

Example for US with full area code provided ("310.349.9999"):

{
  areaCode: "310",
  e164: "+13103499999",
  format: "(xxx) xxx-xxxx",
  formattedNumber: "(310) 349-9999",
  href: "tel:+13103499999",
  localNumber: "3103499999",
  rawNumber: "310.349.9999",
  regionCode: "1"
}

findNumbersInString

Accepts a full string of text and returns an array of phone numbers extracted from within that text.

The objects within this array will be identical to the object provided in the getPhoneParts above with the only exception being the addition of a index and lastIndex integer noting the start and end of the instance of that specific number within the provided string.

Example for US with full area code provided ("Hey there, my number is 310.349.9999. Please give me a call!"):

[
  {
    index: 24,
    lastIndex: 36,
    areaCode: '310',
    e164: '+13103499999',
    format: '(xxx) xxx-xxxx',
    formattedNumber: '(310) 349-9999',
    href: 'tel:+13103499999',
    localNumber: '3103499999',
    rawNumber: '310.349.9999',
    regionCode: '1',
  },
];

Numbers that lack area codes will NOT be returned via findNumbersInString since they cannot be reliably validated.

Example for US with no area code provided ("Hey there, my number is 349.9999. Please give me a call!"):

[];

Geography / Time Functionality

In addition to the above methods, the following methods are also available via a different export.

There is additional functionality exposed as export, but the primary expected use case is:

findTimeFromAreaCode

Returns an object with geographic and time related information for a given region.

NOTE: This is only applicable for United States and Canada.

import { findTimeFromAreaCode } from '@yext/phonenumber-util/geo';
findTimeFromAreaCode('928');

Example output for Arizona:

{
  areaCodeHasMultipleTimezones: false,
  daylightSavings: true,
  estimatedTime: true,
  isQuietHours: false,
  isTCPAQuietHours: false,
  localTime24Hour: "15:00:00",
  localTimeReadable: "3:00:00 PM",
  region: {
    code: "US",
    flag: "🇺🇸",
    name: "United States"
  },
  state: {
    code: "AZ",
    name: "Arizona"
  },
  stateHasMultipleTimezones: false,
  timezoneOffset: "-07:00"
}
import { findTimeFromAreaCode } from '@yext/phonenumber-util/geo';
findTimeFromAreaCode('250', date); // A date object is optional, defaulting to the current time.

Example output for British Columbia:

{
  areaCodeHasMultipleTimezones: true,
  daylightSavings: true,
  estimatedTime: true,
  isCRTCQuietHours: false,
  isQuietHours: false,
  localTime24Hour: "17:00:00",
  localTimeReadable: "5:00:00 PM",
  region: {
    code: "CA",
    flag: "🇨🇦",
    name: "Canada"
  },
  state: {
    code: "BC",
    name: "British Columbia"
  },
  stateHasMultipleTimezones: true,
  timezoneOffset: "-07:00"
}

findRegionFromRegionCode

import { findRegionFromRegionCode } from '@yext/phonenumber-util/geo';
findRegionFromRegionCode('47');

Returns the string name of a given region name based on region code provided.

NOTE: This string is provided in English only (example: "Norway" and not the region-specific name "Norge").

{
  code: "NO",
  flag: "🇳🇴",
  name: "Norway"
}