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 🙏

© 2025 – Pkg Stats / Ryan Hefner

e164num

v2.0.0

Published

**Light validation and manipulation of E.164 phone numbers.**

Readme

e164num

Light validation and manipulation of E.164 phone numbers.

github license npm version

Features

  • E.164 phone number validation and formatting
  • Real-time input handling
  • US number formatting with proper spacing
  • Secure number masking
  • TypeScript ready, zero dependencies

Installation

npm install e164num
# or
yarn add e164num
# or
pnpm add e164num

Usage

import {
  isValidE164PhoneNumber,
  formatPartialUSPhoneNumber,
  getPartialE164PhoneNumber,
  maskPhoneNumber,
} from 'e164num'

// Validate a complete E.164 phone number (e.g., on form submission)
isValidE164PhoneNumber('+17871234567') // returns true
isValidE164PhoneNumber('+1') // returns false

// Validate a partial E.164 phone number (e.g., for intermediate validation)
isValidPartialE164PhoneNumber('+1787') // returns true
isValidPartialE164PhoneNumber('abc') // returns false

// Format a US phone number for display
formatPartialUSPhoneNumber('+17871234567') // returns "(787) 123-4567"

// Convert partial number to E.164 format (great for input fields)
getPartialE164PhoneNumber('7871234567') // returns "+17871234567"
getPartialE164PhoneNumber('787123', { code: '+34', maxLength: 12 }) // returns "+34787123"
getPartialE164PhoneNumber('') // returns ""
getPartialE164PhoneNumber(undefined) // returns ""

// Mask a phone number for display
maskPhoneNumber('+17871234567') // returns "+1*******567"

API Reference

isValidE164PhoneNumber(e164PhoneNumber: string): boolean

Validates if a string is a complete, valid E.164 phone number. For US numbers (+1), it checks for the correct length. For international numbers, it validates the basic E.164 format. Use this function for final validation, such as when a form is submitted.

isValidPartialE164PhoneNumber(partialE164PhoneNumber: string): boolean

Validates if a string follows the E.164 format rules but may be incomplete. This is useful for intermediate validation states, such as validating a phone number that's been pasted in or when you need to check if the current input could potentially become a valid E.164 number. Empty string is considered valid as a starting point for input.

getPartialE164PhoneNumber(phoneNumber: string | undefined, defaultCountryInfo?: { code: string, maxLength: number }): string

Converts a partial phone number to E.164 format. This is the ideal function for processing phone numbers in input fields as the user types, as it handles formatting and country code insertion in real-time.

  • Returns empty string for undefined or empty input
  • defaultCountryInfo defaults to { code: '+1', maxLength: 12 } for US numbers
  • Automatically adds the country code if not present
  • Truncates numbers to the specified maximum length

formatPartialUSPhoneNumber(e164PhoneNumber: string): string

Formats a US phone number with proper spacing and punctuation: (XXX) XXX-XXXX. Works with both regular and masked phone numbers - masked digits will be shown as bullet points (•) in the formatted output.

Example with masked number:

formatPartialUSPhoneNumber('+1******1234') // returns "(•••) •••-1234"

maskPhoneNumber(e164PhoneNumber: string): string

Masks a phone number for display, showing only the country code and last 3 digits. All other digits are replaced with asterisks.

E.164 Format

E.164 is the international standard format for phone numbers. An E.164 number can have a maximum of 15 digits and is usually written with a leading + symbol.

Format: +[country code][area code][local phone number]

Examples:

  • US: +17871234567
  • UK: +447911123456
  • Spain: +34612345678

License

MIT License - see the LICENSE file for details.