JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1300651
  • Score
    100M100P100Q193173F
  • License MIT

Slugify a string

Package Exports

  • @sindresorhus/slugify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sindresorhus/slugify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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: -

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'

License

MIT © Sindre Sorhus