@sindresorhus/slugify
Slugify a string
Last updated 3 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @sindresorhus/slugify 
SYNC missed versions from official npm registry.

slugify Build Status

Slugify a string

Useful for URLs, filenames, and IDs.

It correctly handles German umlauts, Vietnamese, Arabic, Russian, Romanian, Turkish 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: -

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

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

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

Type: boolean
Default: true

Make the slug lowercase.

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

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.

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

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.

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

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

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

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

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

Related

License

MIT © Sindre Sorhus

Current Tags

  • 0.9.1                                ...           latest (3 months ago)

11 Versions

  • 0.9.1                                ...           3 months ago
  • 0.9.0                                ...           4 months ago
  • 0.8.0                                ...           5 months ago
  • 0.7.0                                ...           6 months ago
  • 0.6.0                                ...           8 months ago
  • 0.5.0                                ...           8 months ago
  • 0.4.0                                ...           8 months ago
  • 0.3.0                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago

Copyright 2014 - 2016 © taobao.org |