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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@div0ky/sanitize

v1.2.0

Published

Sanitize common fields for contacts, including first names, last names, and full names with support for multiple names, titles, and suffixes.

Readme

Sanitize

A TypeScript utility library for sanitizing common contact-related fields like phone numbers, names, addresses, and more.

Installation

npm install @div0ky/sanitize

Usage

import { sanitize, format } from '@div0ky/sanitize';

// Sanitize first names
const firstName1 = sanitize.firstName('mr. john');  // Returns: 'John'
const firstName2 = sanitize.firstName('bob and jim');  // Returns: 'Bob & Jim'
const firstName3 = sanitize.firstName('jack and s.');  // Returns: 'Jack'
const firstName4 = sanitize.firstName('j.d. smith');  // Returns: 'J D Smith'

// Sanitize last names
const lastName1 = sanitize.lastName('smith jr.');  // Returns: 'Smith'
const lastName2 = sanitize.lastName('thompson and wilson');  // Returns: 'Thompson & Wilson'
const lastName3 = sanitize.lastName('smith/jones');  // Returns: 'Smith/Jones'
const lastName4 = sanitize.lastName('smith\\jones');  // Returns: 'Smith/Jones'

// Example of compound first names with slash-separated last names
const firstName = sanitize.firstName('jack and jill');  // Returns: 'Jack & Jill'
const lastName = sanitize.lastName('smith/jones');  // Returns: 'Smith/Jones'

// Sanitize full names (handles initials and middle names)
const fullName1 = sanitize.fullName('aaron spurlock');  // Returns: 'Aaron Spurlock'
const fullName2 = sanitize.fullName('aaron j. spurlock');  // Returns: 'Aaron J Spurlock'
const fullName3 = sanitize.fullName('j.r. bob smith');  // Returns: 'J R Bob Smith'
const fullName4 = sanitize.fullName('mr. aaron patrick jennings spurlock jr.');  // Returns: 'Aaron PJ Spurlock'
const fullName5 = sanitize.fullName('jack and s. thompson');  // Returns: 'Jack Thompson'

// Sanitize a phone number
const phone = sanitize.phone('(123) 456-7890');  // Returns: '1234567890'

// Sanitize and format a phone number
const rawPhone = '(123) 456-7890';
const sanitizedPhone = sanitize.phone(rawPhone);  // Returns: '1234567890'
const formattedPhone = format.phone(sanitizedPhone);  // Returns: '(123) 456-7890'

// Sanitize an email
const email = sanitize.email('[email protected]');  // Returns: '[email protected]'

// Sanitize an address
const street1 = sanitize.street('123 north main street');  // Returns: '123 N Main St'
const street2 = sanitize.street('456 SOUTH WASHINGTON AVENUE #2B');  // Returns: '456 S Washington Ave #2B'
const street3 = sanitize.street('789 northwest oak drive apt 3');  // Returns: '789 NW Oak Dr #3'
const city = sanitize.city('new york');  // Returns: 'New York'
const state = sanitize.state('ny');  // Returns: 'NY'
const zip = sanitize.zip('12345');  // Returns: '12345'
const invalidZip = sanitize.zip('12345-6789');  // Returns: null

## API

### sanitize.firstName(first_name: string): string

Sanitizes a first name by:
- Removing titles (Mr., Mrs., Dr., etc.)
- Converting multiple names to use "&" (e.g., "Bob and Jim" → "Bob & Jim")
- Removing short second names in compound names (e.g., "Jack and S." → "Jack")
- Replacing periods with spaces in initials (e.g., "J.D." → "J D")
- Applying proper capitalization
- Returns 'Unknown' if input is empty or only whitespace

### sanitize.lastName(last_name: string): string

Sanitizes a last name by:
- Removing suffixes (Jr., Sr., Esq.)
- Handling slash-separated names as distinct last names (e.g., "Smith/Jones")
- Converting "and" to "&" in compound names (e.g., "Thompson and Wilson" → "Thompson & Wilson")
- Applying proper capitalization
- Returns 'Unknown' if input is empty or only whitespace

### sanitize.fullName(full_name: string): string

Sanitizes a full name by:
- Removing titles and suffixes
- Preserving existing initials (e.g., "J.R. Smith" → "J R Smith")
- Converting middle names to initials (e.g., "Aaron Patrick Jennings" → "Aaron PJ")
- Removing short parts in compound names (e.g., "Jack and S. Thompson" → "Jack Thompson")
- Applying proper capitalization
- Returns 'Unknown' if input is empty or only whitespace

### sanitize.phone(phone: string): string | null

Sanitizes a phone number by removing non-numeric characters and ensuring it has 10 digits.
Returns `null` if the input is invalid or doesn't result in exactly 10 digits.

### sanitize.email(email: string): string | null

Sanitizes an email address by converting to lowercase and trimming whitespace.
Returns `null` if the email format is invalid.

### sanitize.street(address: string): string

Sanitizes street addresses following USPS standards where possible:
- Capitalizes first letter of each word except for standardized abbreviations
- Standardizes common street suffixes (St, Ave, Blvd, etc.)
- Standardizes directionals (N, S, E, W, NE, SW, etc.)
- Removes invalid characters
- Handles unit/apartment numbers
- Preserves numeric values including fractions

Returns an empty string if the input is empty or only whitespace.

Examples of standardization:
- "street" → "St"
- "avenue" → "Ave"
- "north" → "N" (when at start or end)
- "apartment 2b" → "#2B"

### sanitize.city(city: string): string

Capitalizes the first letter of each word in a city name and trims excess whitespace.
Returns 'Unknown' if the input is empty or only whitespace.

### sanitize.state(state: string): string | null

Converts state code to uppercase and validates against US state codes.
Returns `null` if the state code is invalid.
Supports all 50 US states, DC, and US territories (PR, VI, AS, GU, MP).

### sanitize.zip(zip: string): string | null

Sanitizes ZIP codes by ensuring they're exactly 5 digits.
Returns `null` if the input doesn't contain exactly 5 digits after removing non-numeric characters.

### format.phone(phone: string): string

Formats a sanitized phone number into a human-readable format.
Returns a string in the format '(XXX) XXX-XXXX'.

## License

MIT