slugify
Converts any string into a clean, URL-safe slug with built-in Unicode transliteration for dozens of languages.
Repository Health
Technical Analysis
Slugify is a zero-dependency JavaScript library that turns any string into a clean, URL-friendly slug. It ships with an extensive character map covering Latin, Greek, Cyrillic, Arabic, Armenian, Georgian, Persian, Thai script, and currency symbols, so accented and non-Latin text is transliterated to its closest ASCII equivalent instead of being stripped outright.
Beyond the default character map, slugify supports per-locale overrides for languages such as German, Bulgarian, Danish, Ukrainian, and Vietnamese where the general transliteration doesn’t match local convention, a runtime extend() API for adding or overriding characters, and configurable options for the replacement character, character removal, casing, and trimming. It ships as a single dependency-free module with TypeScript typings and works equally in Node.js, the browser, and AMD/CommonJS module loaders.
What You Get
- A single
slugify(string, options)function with no runtime dependencies - An extensive built-in charMap covering Latin, Greek, Cyrillic, Arabic, Armenian, Georgian, Persian, Thai script, and currency symbols
- Locale-specific overrides for languages such as German, Bulgarian, Danish, Ukrainian, and Vietnamese where transliteration rules differ from the default map
- A runtime
extend()method to add or override character mappings without patching the library - Configurable options for replacement character, character-removal regex, case conversion, strict mode, and trimming
- Bundled TypeScript type definitions (
slugify.d.ts)
Common Use Cases
- Generating SEO-friendly URL slugs from blog post or product titles
- Creating safe filenames from user-uploaded file titles
- Normalizing non-Latin usernames or tags into ASCII-safe identifiers
- Building consistent anchor IDs from heading text for in-page navigation
Under The Hood
Architecture
Slugify ships as a single-file UMD module (slugify.js) exporting one function via a factory pattern that supports CommonJS, AMD, and browser globals. The runtime logic reduces the input string character-by-character against two embedded lookup tables — charMap for the default transliteration set and locales for a small set of per-language overrides — then applies the removal regex, replacement-character substitution, and an optional trim/lowercase pass. There’s no internal layering or external dependencies, and the only build step (bin/build.js) regenerates the embedded charMap/locales JSON blobs in slugify.js from the source-of-truth files in config/, so changing the core transliteration logic only ever touches this one module.
Tech Stack
The library is vanilla ES2015+ JavaScript with zero runtime dependencies. Its devDependencies are limited to mocha (test runner), nyc (coverage), coveralls (CI coverage reporting), and github-changes (changelog generation from merged PRs). It bundles hand-written TypeScript typings (slugify.d.ts), targets Node >=8 per engines, and requires no bundler, framework, or database — it is consumed directly via require, an AMD loader, or a browser global.
Code Quality
Tests in test/slugify.js and test/locales.js use Mocha with Node’s built-in assert module, covering whitespace handling, allowed/removed characters, every major charMap group (Latin, Greek, Cyrillic, Turkish, Kazakh, Czech, Polish, Latvian, Serbian, currency symbols, and general symbols), the extend() API, the strict/trim/lower/replacement/remove options, and edge cases such as empty-string charmap entries and Unicode normalization. Coverage is measured with nyc and reported to Coveralls, and CI runs the suite via a GitHub Actions workflow. Error handling is limited to a single thrown Error for a missing string argument; there is no ESLint or Prettier configuration in the repo.
API Design
The public API is a single function, slugify(string, options), with sensible defaults (dash replacement, trim enabled) and a shorthand where passing a string as the second argument is treated directly as the replacement character — keeping the common case to one call with no configuration object required. More advanced needs (custom charmaps, per-locale overrides, strict ASCII-only output) are opt-in via a plain options object, and the extend() static method offers a lightweight way to augment the charMap process-wide without forking the package. The README documents every option with runnable examples.