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

get-clean-string

v4.0.0

Published

Module to clean a string removing special characters and replacing accents for ascii equivalents.

Downloads

1,291

Readme

get-clean-string

Zero-dependency string sanitiser — strips special characters, normalises accents to ASCII, and collapses whitespace into a configurable separator.

npm version license


Features

  • Unicode-complete accent stripping — uses String.prototype.normalize('NFD') and Unicode property escapes instead of a hand-written character table, so every precomposed Latin character is handled automatically.
  • Ligature expansionæ → ae, œ → oe, þ → th, ij → ij, and more.
  • Special character removal — punctuation and symbols are replaced with a space or removed entirely.
  • Configurable separator — collapse whitespace runs into any character (e.g. - for slugs).
  • Per-call and global overrides — customise the replacement map at the factory level or on individual calls.
  • Unknown input coercion — any value is safely coerced via String() before processing.
  • TypeScript-first — ships .d.ts declarations; fully typed API with zero any.
  • Dual ESM / CJS build — works in modern ESM projects and legacy CommonJS.
  • Zero runtime dependencies.

Requirements

Node.js >=18.0.0


Install

npm install get-clean-string

Import

ESM (recommended)

import createCleaner from 'get-clean-string'

Or using the named export:

import { createCleaner } from 'get-clean-string'

CommonJS

const { createCleaner } = require('get-clean-string')
// default export is also available:
// const createCleaner = require('get-clean-string').default

TypeScript

import createCleaner, { type CharacterOverrides } from 'get-clean-string'

Quick start

import createCleaner from 'get-clean-string'

const clean = createCleaner()

clean('Héllo, Wörld!')   // → 'hello world'
clean('acción')          // → 'accion'
clean('naïve café')      // → 'naive cafe'
clean('  too   many   spaces  ')  // → 'too many spaces'
clean('北京市')           // → ''   (non-ASCII with no mapping is dropped)

API

createCleaner(defaultSeparator?, globalOverrides?)

Factory function. Returns a reusable cleanString function.

| Parameter | Type | Default | Description | |---|---|---|---| | defaultSeparator | string | ' ' | Separator used to join tokens on every call. | | globalOverrides | CharacterOverrides | — | Character replacements applied on every call made by the returned function. |

const clean = createCleaner()           // default space separator, no overrides
const slug  = createCleaner('-')        // dash separator — useful for URL slugs
const custom = createCleaner(' ', { '&': 'and' })  // global override

cleanString(input?, separator?, localOverrides?)

The function returned by createCleaner. Processes input and returns a clean ASCII string.

| Parameter | Type | Default | Description | |---|---|---|---| | input | unknown | '' | Value to clean. Non-strings are coerced via String(). | | separator | string | factory's defaultSeparator | Overrides the separator for this call only. | | localOverrides | CharacterOverrides | — | Extra character replacements for this call only. Does not affect future calls. |

const clean = createCleaner('-')

clean('hello world')                   // → 'hello-world'
clean('hello world', '_')             // → 'hello_world'  (per-call separator)
clean('cats & dogs', '-', { '&': 'and' })  // → 'cats-and-dogs'

CharacterOverrides

type CharacterOverrides = Record<string, string>

An object mapping single characters to their replacement strings. Keys with more than one character are silently ignored.

const overrides: CharacterOverrides = {
  '&': 'and',   // & → 'and'
  "'": "'",      // keep apostrophes instead of removing them
  '8': 'eight', // replace the digit 8 with the word
}

Usage examples

Accent and diacritic stripping

Handled automatically via Unicode NFD — every precomposed Latin character works, not just those in a hardcoded list.

const clean = createCleaner()

clean('résumé')     // → 'resume'
clean('Ångström')   // → 'angstrom'
clean('façade')     // → 'facade'
clean('München')    // → 'munchen'
clean('Ñoño')       // → 'nono'

Ligature expansion

clean('æon')        // → 'aeon'
clean('œuvre')      // → 'oeuvre'
clean('þorn')       // → 'thorn'
clean('łódź')       // → 'lodz'

Special character removal

Punctuation and symbols are replaced with a space. Quotes and apostrophes are removed entirely.

clean('hello! world?')         // → 'hello world'
clean('price: $9.99')          // → 'price 9.99'
clean("dad's car")             // → 'dads car'
clean('[email protected]')           // → 'foo bar.com'

URL slugs

const slug = createCleaner('-')

slug('Hello, World!')          // → 'hello-world'
slug('Ação & Reação')          // → 'acao-reacao'  (Portuguese)
slug('  multiple   spaces  ')  // → 'multiple-spaces'

Global overrides (applied to every call)

const clean = createCleaner(' ', { '&': 'and', '@': 'at' })

clean('cats & dogs')           // → 'cats and dogs'
clean('[email protected]')      // → 'user at example.com'

Per-call overrides (scoped to a single call)

Local overrides are merged on top of the global map for that call only. They do not affect subsequent calls.

const clean = createCleaner()

// Keep apostrophes for this call
clean("dad's car", ' ', { "'": "'" })  // → "dad's car"

// Next call is unaffected — apostrophes are removed again
clean("dad's car")                      // → 'dads car'

Non-string input

Any value is safely coerced via String() before processing.

const clean = createCleaner()

clean(2024)          // → '2024'
clean(null)          // → 'null'
clean(true)          // → 'true'
clean(undefined)     // → ''      (default parameter)

TypeScript

import createCleaner, { type CharacterOverrides } from 'get-clean-string'

const overrides: CharacterOverrides = { '&': 'and' }
const clean = createCleaner(' ', overrides)

const result: string = clean('cats & dogs')
// → 'cats and dogs'

Testing

# Unit tests (source)
npm test

# Integration tests (compiled dist — ESM + CJS)
npm run test:integration

# Both suites
npm run test:all

# Unit tests with coverage
npm run coverage

Migrating from v3

| | v3 | v4 | |---|---|---| | Import (CJS) | const clean = require('get-clean-string')() | const { createCleaner } = require('get-clean-string') | | Import (ESM) | import f from 'get-clean-string'; const clean = f() | import createCleaner from 'get-clean-string' | | Factory name | anonymous default export | createCleaner (named + default) | | Input type | string | unknown (coerced via String()) | | Accent handling | ~350-entry hand-written map | Unicode NFD — complete coverage | | Node.js | — | >=18.0.0 |

The default export is preserved, so existing call sites that do import f from 'get-clean-string'; const clean = f() continue to work without changes.


License

Copyright © 2026, Juan Convers.
Released under the MIT License.