@matters/slugify
Version:
Slugify a string
135 lines (89 loc) • 2.25 kB
Markdown
> Slugify a string
Useful for URLs, filenames, and IDs.
It correctly handles [German umlauts](https://en.wikipedia.org/wiki/Germanic_umlaut), Vietnamese, Arabic, Russian, Romanian, Turkish and more.
```
$ npm install @sindresorhus/slugify
```
```js
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'
```
Type: `string`
Type: `Object`
Type: `string`<br>
Default: `-`
```js
slugify('BAR and baz');
//=> 'bar-and-baz'
slugify('BAR and baz', {separator: '_'});
//=> 'bar_and_baz'
```
Type: `boolean`<br>
Default: `true`
Make the slug lowercase.
```js
slugify('Déjà Vu!');
//=> 'deja-vu'
slugify('Déjà Vu!', {lowercase: false});
//=> 'Deja-Vu'
```
Type: `boolean`<br>
Default: `true`
Convert camelcase to separate words. Internally it does `fooBar` → `foo bar`.
```js
slugify('fooBar');
//=> 'foo-bar'
slugify('fooBar', {decamelize: false});
//=> 'foobar'
```
Type: `Array<string[]>`<br>
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.
```js
slugify('Foo@unicorn', {
customReplacements: [
['@', 'at']
]
});
//=> 'fooatunicorn'
```
Add a leading and trailing space to the replacement to have it separated by dashes:
```js
slugify('foo@unicorn', {
customReplacements: [
['@', ' at ']
]
});
//=> 'foo-at-unicorn'
```
- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename
MIT © [Sindre Sorhus](https://sindresorhus.com)