Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

slug

Trott1.4mMIT11.0.0TypeScript support: definitely-typed

slugifies even utf-8 chars!

slugify, slug, string, utf8, utf-8, unicode, url

readme

slug

Slugifies strings, even when they contain Unicode.

Make strings URL-safe.

  • Respects RFC 3986
  • No dependencies
  • Works in the browser or in Node.js
npm install slug

If you are using TypeScript you can install the accompanying types

npm install --save-dev @types/slug

Example

import slug from 'slug'
var print = console.log.bind(console, '>')

print(slug('i love unicode'))
// > i-love-unicode

print(slug('i love unicode', '_')) // If you prefer something else than `-` as separator
// > i_love_unicode

slug.charmap['♥'] = 'freaking love' // change default charmap or use option {charmap:{…}} as 2. argument
print(slug('I ♥ UNICODE'))
// > i-freaking-love-unicode

// To reset modifications to slug.charmap, use slug.reset():
slug.reset()
print(slug('I ♥ UNICODE'))
// > i-unicode

print(slug('Telephone-Number')) // lower case by default
// > telephone-number

print(slug('Telephone-Number', {lower: false})) // If you want to preserve case
// > Telephone-Number

// We try to provide sensible defaults.
// So Cyrillic text will be transliterated as if it were Russian:
print(slug('маленький подъезд'))
// > malenkij-poduezd

// But maybe you know it's Bulgarian:
print(slug('маленький подъезд', { locale: 'bg' }))
// > malenykiy-podaezd

// To set the default locale:
slug.setLocale('bg')
print(slug('маленький подъезд'))
// > malenykiy-podaezd

print(slug('unicode is ☢'))
// > unicode-is

slug.extend({'☢': 'radioactive'})
print(slug('unicode ♥ is ☢'))
// > unicode-is-radioactive

// slug.extend() modifies the default charmap for the entire process.
// If you need to reset charmap, multicharmap, and the default locale, use slug.reset():

slug.reset()
print(slug('unicode ♥ is ☢'))
// > unicode-is

// Custom removal of characters from resulting slug. Let's say that we want to
// remove all numbers for some reason.
print(slug('one 1 two 2 three 3'))
// > one-1-two-2-three-3
print(slug('one 1 two 2 three 3', { remove: /[0-9]/g }))
// > one-two-three

options

// options is either object or replacement (sets options.replacement)
slug('string', [{options} || 'replacement']);
slug.defaults.mode ='pretty';
slug.defaults.modes['rfc3986'] = {
    replacement: '-',      // replace spaces with replacement
    remove: null,          // (optional) regex to remove characters
    lower: true,           // result in lower case
    charmap: slug.charmap, // replace special characters
    multicharmap: slug.multicharmap, // replace multiple code unit characters
    trim: true,             // trim leading and trailing replacement chars
    fallback: true          // use base64 to generate slug for empty results
};
slug.defaults.modes['pretty'] = {
    replacement: '-',
    remove: null,
    lower: false,
    charmap: slug.charmap,
    multicharmap: slug.multicharmap,
    trim: true,
    fallback: true
};

Differences between slug and slugify packages

Here are some key differences between this package and slugify.

  • Stability: slug is ESM-only.
    slugify supports CommonJS and ESM.
  • Defaults: slug has the lower option enabled by default, lowercasing all slugs ('On SALE' becomes 'on-sale').
    slugify has the lower option disabled by default ('On SALE' becomes 'On-SALE').
  • Symbols: slug removes unrecognized symbols ('$100' becomes '100', '<5' becomes '5', etc.).
    slugify maps them to words ('$100' becomes 'dollar100', '<5' becomes 'less5', etc.).
  • Empty Output: slug will return a short, predictable hash (' ' becomes 'icag' and '🎉' becomes '8joiq').
    slugify will return an empty string (' ' and '🎉' become '').

Playground

A web playground is available at https://trott.github.io/slug/.

There is also a CLI tool available via npx slug. It doesn't allow you to specify options, so it's utility is minimal.

changelog

11.0.0 (2025-05-22)

chore

  • throw on lone surrogates (#492) (8ea2c6b)
  • update supported Node.js versions to 20.x and later (#493) (d23bfa7)

BREAKING CHANGES

  • Strings will throw if not well-formed (lone surrogates).
  • update supported Node.js versions to 20.x and later

10.0.0 (2024-10-17)

BREAKING CHANGES

  • This module only supports ESM. CommonJS and non-ESM script tags are no longer supported.

  • chore: add web-test-runner

  • chore: add coverage reporting for CLI tests

  • chore: fail CLI tests if coverage is not 100%

9.1.0 (2024-05-24)

Features

  • add remove option field to playground (#445) (8888d69)

9.0.0 (2024-03-07)

chore

BREAKING CHANGES

  • Drop support for Node.js older than 18.x

8.2.3 (2023-07-18)

Bug Fixes

8.2.2 (2022-10-02)

Bug Fixes

8.2.1 (2022-10-02)

Bug Fixes

8.2.0 (2022-10-02)

Features

8.1.0 (2022-10-02)

Features

  • add a (painfully minimal) web playground (#350) (4e1aa40)

8.0.0 (2022-08-31)

chore

BREAKING CHANGES

  • Loading via AMD will no longer work out-of-the-box.

7.0.0 (2022-08-31)

chore

  • drop support for Node.js 12.x and 17.x (#344) (a109d37)

BREAKING CHANGES

  • Drop support for Node.js earlier than 14.x.

6.1.0 (2022-08-31)

Features

6.0.0 (2022-08-16)

chore

BREAKING CHANGES

  • drop support for IE11

5.3.0 (2022-03-04)

Features

  • add bugs entry to package.json (ca72836)

5.2.0 (2021-12-27)

Features

  • add ability to set default locale via setLocale() (#269) (add368d)

5.1.1 (2021-12-13)

Bug Fixes

  • consolidate replacement chars from extend() (#267) (df7a488)

5.1.0 (2021-06-15)

Features

5.0.1 (2021-05-09)

Bug Fixes

  • enable characters composed of multiple code points in .extend() (#217) (5919b6d)

5.0.0 (2021-05-03)

chore

BREAKING CHANGES

  • Drop support for Node.js 10.x which is now EOL.

4.1.0 (2021-05-03)

Features

4.0.4 (2021-04-16)

Bug Fixes

  • honor special character replacements in charmap (f5e18c9)

4.0.3 (2021-02-24)

Bug Fixes

4.0.2 (2020-10-30)

Bug Fixes

  • update from deprecated license specification (bc16c49)

4.0.1 (2020-10-22)

Bug Fixes

4.0.0 (2020-10-21)

chore

BREAKING CHANGES

  • symbols are removed

3.5.2 (2020-10-20)

Bug Fixes

  • correct errors in code samples (f74890f)

3.5.1 (2020-10-13)

Bug Fixes

  • add ة،ء missing Arabic characters (#1) (0366d3a)

3.5.0 (2020-10-11)

Features

  • add support for React Native (7962f8b)

3.4.0 (2020-10-11)

Features

3.3.6 (2020-10-11)

Bug Fixes

  • reset() should reset all multicharmap defaults (a429809)

3.3.5 (https://github.com/Trott/slug/compare/v3.3.4...v3.3.5) (2020-09-29)

Bug Fixes

* prevent slug function from mutating option object (41916a3 (https://github.com/Trott/slug/commit/41916a32c6a6cdd3af1fe2405224e8b91c7760d4))

3.3.4 (https://github.com/Trott/slug/compare/v3.3.3...v3.3.4)

Bug Fixes

* fix: add German locale for parity with slugify

3.3.3

chore: add missing Latin/Greek char

3.3.2

fix: remove template strings for IE11 compatibility

3.3.1

fix: use let for ie11 compat

chore: move regex to improve performance

chore: add documentation and tests for remove option

3.3.0

feat: make Base64 fallback in browser far more robust

fix: disregard lone surrogates in Node.js, as in the browser

fix: enable browser compatibility for all code points

3.2.0

feat: add support for Azerbaijani characters

feat: add support for Slovak characters

3.1.0

feat: add support for Georgian characters

feat: add locale sr (Serbian)

feat: support locale bg (Bulgarian)

fix: repair markdown layout for README file

3.0.1

fix: remove erroneous "bin" entry in package.json

3.0.0

BREAKING CHANGE: make output lowercase by default (#43)

BREAKING CHANGE: more aggressively remove punctuation by default (#39)

BREAKING CHANGE: do not load/use unicode symbol table (#50)

BREAKING CHANGE: require first argument to be a string (#32)

BREAKING CHANGE: remove CLI (#52)

feat: support Devanagari for Hindi, Sanskrit, and other languages (#55)

feat: add Kazakh characters (#42)

feat: add Serbian transliterations (#45)

feat: add ruble, bitcoin, tenge (#47)

feat: implement .extend() (#51)

feat: implement .reset() and browser testing (#53)

2.1.1

Restore IE 11 support by removing arrow functions.

2.1.0

Fallback to base64 if slug is empty. This allows minimal out-of-the-box support for character sets not in the default mappings.

2.0.0

Add support for Farsi. (Thanks, @hassan-jahan!)

This is being released as a breaking change because it changes some of the slugs for Arabic strings. It probably won't break anything, but just in case....