Slugify a string
Clone or download
Latest commit e7af667 Oct 31, 2018
Permalink
Failed to load latest commit information.
.editorconfig Init Apr 18, 2018
.gitattributes Meta tweaks Oct 30, 2018
.gitignore Init Apr 18, 2018
.npmrc Init Apr 18, 2018
.travis.yml Meta tweaks Oct 30, 2018
index.js Add `decamelize` option Oct 31, 2018
license Init Apr 18, 2018
overridable-replacements.js Add support for Vietnamese Oct 31, 2018
package.json 0.6.0 Oct 31, 2018
readme.md Readme tweaks Oct 31, 2018
replacements.js Add support for Romanian Oct 31, 2018
test.js Add `decamelize` option Oct 31, 2018

readme.md

slugify Build Status

Slugify a string

Useful for URLs, filenames, and IDs.

It correctly handles German umlauts, Vietnamese, Arabic, Russian, Romanian, and more.

Install

$ npm install @sindresorhus/slugify

Usage

const slugify = require('@sindresorhus/slugify');

slugify('I ♥ Dogs');
//=> 'i-love-dogs'

slugify('  Déjà Vu!  ');
//=> 'deja-vu'

slugify('fooBar 123 $#%');
//=> 'foo-bar-123'

slugify('I ♥ 🦄 & 🐶', {
	customReplacements: [
		['🐶', 'dog']
	]
});
//=> 'i-love-unicorn-and-dog'

API

slugify(input, [options])

input

Type: string

options

Type: Object

separator

Type: string
Default: -

slugify('BAR and baz');
//=> 'bar-and-baz'

slugify('BAR and baz', {separator: '_'});
//=> 'bar_and_baz'
lowercase

Type: boolean
Default: true

Make the slug lowercase.

slugify('Déjà Vu!');
//=> 'deja-vu'

slugify('Déjà Vu!', {lowercase: false});
//=> 'Deja-Vu'
decamelize

Type: boolean
Default: true

Convert camelcase to separate words. Internally it does fooBarfoo bar.

slugify('fooBar');
//=> 'foo-bar'

slugify('fooBar', {decamelize: false});
//=> 'foobar'
customReplacements

Type: Array<string[]>
Default: [ ['&', ' and '], ['🦄', ' unicorn '], ['♥', ' love '] ]

Specifying this only replaces the default if you set an item with the same key, like &. The replacements are run on the original string before any other transformations.

slugify('Foo@unicorn', {
	customReplacements: [
		['@', 'at']
	]
});
//=> 'fooatunicorn'

Add a leading and trailing space to the replacement to have it separated by dashes:

slugify('foo@unicorn', {
	customReplacements: [
		['@', ' at ']
	]
});
//=> 'foo-at-unicorn'

Related

License

MIT © Sindre Sorhus