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

@reallyuseful/phonenumber

v2.0.0

Published

Parser and validator for US and Canadian phone numbers

Downloads

4,139

Readme

@reallyuseful/phonenumber

npm dependencies license node

Parser and validator for US and Canadian phone numbers

Given a phone number in almost any format, this will determine if it follows the correct digit rules to be a valid phone number in the US or Canada. It can extract a phone number’s NPA (area code) and NXX (prefix). It can format phone numbers in standard E.164 format, or in “friendly” or “compact” format.

const PhoneNumber = require('@reallyuseful/phonenumber');

console.log(PhoneNumber.valid('206.867.1234')); // true, follows correct digit pattern
console.log(PhoneNumber.valid('206-123-4567')); // false, invalid digit pattern
console.log(PhoneNumber.valid('206-555-6666')); // false, 555 is not a valid NXX
console.log(PhoneNumber.valid('1-006-867-1234')); // false, 006 is not a valid NPA format

console.log(PhoneNumber.parse('2068671234'));
// breaks the number into parts: { npa: '206', nxx: '867', station: '1234' }

console.log(PhoneNumber.isTollFree('206 8671234')); // false, not toll-free
console.log(PhoneNumber.isTollFree('8002223333')); // true, this is toll-free
console.log(PhoneNumber.isTollFree('(833) 333-3333')); // true, this is toll-free

console.log(PhoneNumber.formatStandard('206 867 1234')); // "+12068671234" (E.164 format)
console.log(PhoneNumber.formatFriendly('12068671234')); // "(206) 867-1234"
console.log(PhoneNumber.formatCompact('(206) 867-1234')); // "206-867-1234"

console.log(PhoneNumber.standard('206 867 1234')); // "+12068671234" (E.164 format)
console.log(PhoneNumber.friendly('12068671234')); // "(206) 867-1234"
console.log(PhoneNumber.compact('(206) 867-1234')); // "206-867-1234"

Input format

The phone number you pass to these functions can be in almost any format. It doesn’t matter if there are spaces, parentheses or other punctuation. If it’s potentially valid, it will be recognized.

Validating a phone number

  • valid(phoneNumber) – Returns true if the number follows North American Numbering Plan rules, which means that it is a potentially valid phone number in the US, Canada and parts of the Caribbean.

This module does not use any external service. It can’t tell if the phone number is actually live or who it belongs to. If you need that information, use a paid service like the Twilio Lookup API.

Numbers that are not recognized

  • Short numbers like 911 and 311 are not recognized.
  • Local-only numbers (7-digits without an area code) are not recognized.

Formatting a phone number

  • formatStandard(phoneNumber) – Returns the phone number in E.164 format. This is recommended for storing phone numbers in a database. It begins with a + sign followed by the “country code” (which is 1 for all countries that are part of the NANP). Example: +12068671234
  • formatFriendly(phoneNumber) – Returns the common display format for phone numbers: (206) 867-1234
  • formatCompact(phoneNumber) – Returns another common display format that is slightly shorter: 206-867-1234

Shorter-named aliases of these functions are available: standard(phoneNumber), friendly(phoneNumber), and compact(phoneNumber).

If you try to format an invalid phone number, InvalidPhoneNumberError will be thrown.

Parsing a phone number

  • parse(phoneNumber) – Extracts the NPA (area code), NXX (prefix) and station (last 4 digits) of the phone number. Example: { "npa": "206", "nxx": "867", "station": "1234" }

If you call this function with an invalid phone number, InvalidPhoneNumberError will be thrown.

Identifying toll-free phone numbers

  • isTollFree(phoneNumber) - Returns true for valid phone numbers in area codes 800, 888, 877, 866, 855, 844, or 833.

If you call this function with an invalid phone number, InvalidPhoneNumberError will be thrown.

Alternatives

This is a lightweight module that handles US/Canadian phone numbers. Its primary use case is validation of data that is going into a database, and formatting that data for display.

If you need something fancier, or need to handle other countries, try google-libphonenumber.