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

Package detail

slugify

simov14.3mMIT1.6.6TypeScript support: included

Slugifies a String

slugify, slug, url, urlify

readme

slugify

npm-version coveralls-status

var slugify = require('slugify')

slugify('some string') // some-string

// if you prefer something other than '-' as separator
slugify('some string', '_')  // some_string
  • Vanilla ES2015 JavaScript
    • If you need to use Slugify with older browsers, consider using version 1.4.7
  • No dependencies
  • Coerces foreign symbols to their English equivalent (check out the charMap for more details)
  • Works in the browser (window.slugify) and AMD/CommonJS-flavored module loaders

Options

slugify('some string', {
  replacement: '-',  // replace spaces with replacement character, defaults to `-`
  remove: undefined, // remove characters that match regex, defaults to `undefined`
  lower: false,      // convert to lower case, defaults to `false`
  strict: false,     // strip special characters except replacement, defaults to `false`
  locale: 'vi',      // language code of the locale to use
  trim: true         // trim leading and trailing replacement chars, defaults to `true`
})

Remove

For example, to remove *+~.()'"!:@ from the result slug, you can use slugify('..', {remove: /[*+~.()'"!:@]/g}).

  • If the value of remove is a regular expression, it should be a character class and only a character class. It should also use the global flag. (For example: /[*+~.()'"!:@]/g.) Otherwise, the remove option might not work as expected.
  • If the value of remove is a string, it should be a single character. Otherwise, the remove option might not work as expected.

Locales

The main charmap.json file contains all known characters and their transliteration. All new characters should be added there first. In case you stumble upon a character already set in charmap.json, but not transliterated correctly according to your language, then you have to add those characters in locales.json to override the already existing transliteration in charmap.json, but for your locale only.

You can get the correct language code of your language from here.

Extend

Out of the box slugify comes with support for a handful of Unicode symbols. For example the (radioactive) symbol is not defined in the charMap and therefore it will be stripped by default:

slugify('unicode ♥ is ☢') // unicode-love-is

However you can extend the supported symbols, or override the existing ones with your own:

slugify.extend({'☢': 'radioactive'})
slugify('unicode ♥ is ☢') // unicode-love-is-radioactive

Keep in mind that the extend method extends/overrides the default charMap for the entire process. In case you need a fresh instance of the slugify's charMap object you have to clean up the module cache first:

delete require.cache[require.resolve('slugify')]
var slugify = require('slugify')

Contribute

  1. Add chars to charmap.json
  2. Run tests npm test
  3. The tests will build the charmap in index.js and will sort the charmap.json
  4. Commit all modified files

Originally this was a vanilla javascript port of node-slug.
Note that the original slug module has been ported to vanilla javascript too.

changelog

Change Log

v1.6.6 (2023-03-26)

  • #174 correctly handle empty strings in charmaps (@iliazeus)
  • #169 Add changelog (@simov)
  • #168 chore: document limitations of the remove option (#168) (@Trott)
  • #157 Run CI in Node.js 18 (@stscoundrel)
  • #151 Update README.md (#151) (@lorand-horvath)

v1.6.5 (2022-01-03)

  • #146 chore: use GitHub Actions for testing (#146) (@Trott)

v1.6.4 (2021-12-16)

  • #145 fix: consolidate replacement chars from extend() (#145) (@Trott)
  • #142 More Yoruba support (@9jaboy)

v1.6.3 (2021-11-22)

  • #139 Added few more locales (@daniel1901)
  • #138 Add 'ß' (ss) to german locale (@maxkrickl)

v1.6.2 (2021-11-07)

  • #136 Add Bulgarian locale. (@haltsir)

v1.6.1 (2021-10-04)

  • #132 Add arabic and persian characters and numbers to charmap (@kiron)
  • #128 Added danish support to locales.json (@ItzLue)

v1.6.0 (2021-07-15)

  • #125 Update README.md (@SuecoMarcus)
  • #123 feat: add "trim" option (#123) (@Trott)
  • #119 adding Armenian support (@ashotjanibekyan)

v1.5.3 (2021-05-11)

  • #116 fix: remove replacement char from start and end in strict mode (#116) (@Trott)

v1.5.2 (2021-05-08)

  • #115 fix: trim leading and trailing replacement chars (#115) (@Trott)

v1.5.1 (2021-05-02)

  • #111 Two new special chars for charmap.json (@ovidasas)
  • #113 Added Ukrainian characters (@YegorShtonda)
  • #107 Indicate that ES2015 is required & use Object.assign (@realityking)

v1.4.7 (2021-02-21)

  • #104 added more translations for german locale (@roydigerhund)
  • #103 Add a French locale to replace symbols like "&", "<", "|" by their French equivalent (#103) (@saadyousfi)

v1.4.5 (2020-07-26)

  • #87 Add German locale (@schwigri)

v1.4.2 (2020-06-23)

  • #71 add turkish currency character (@ugurh)
  • #79 Ensure duplicate replacement characters are removed in strict mode (@thompsonsj)

v1.3.6 (2019-11-03)

  • #63 Fix broken link in README (@roschaefer)

v1.3.5 (2019-09-08)

  • #47 Update eslint and related packages; fix new linting errors (@realityking)

v1.3.4 (2018-12-06)

  • #45 Added % handling (#44) (@chucksense)
  • #44 Added % handling (#44) (Chuck Burt)

v1.3.1 (2018-08-05)

  • #34 docs: align example code (@adrukh)

v1.3.0 (2018-05-13)

  • #28 Minor typo fix (@chmac)

v1.2.9 (2017-12-30)

  • #27 Support TypeScript types for extend properties (@rohmanhm)

v1.2.8 (2017-12-23)

  • #25 fix es6 import (@sylvaindumont)

v1.2.7 (2017-12-17)

  • #23 add georgian alphabet (@xxzefgh)

v1.2.6 (2017-11-24)

  • #19 fix - ts typings now export slugify correctly (@neonerd)

v1.2.3 (2017-11-13)

  • #14 add typescript support (@sylvaindumont)

v1.2.2 (2017-11-09)

  • #7 Added support for Serbian characters (@slavkobabic)
  • #11 Add Russian Ruble (@toptalo)

v1.1.0 (2017-01-08)

  • #6 Fix then/than homophone grammar mistake. (@PatrickNausha)

v1.0.1 (2016-08-08)

  • #1 Update README.md (@mrzmyr)