Slugifies strings, even when they contain Unicode.
Make strings URL-safe.
- Respects RFC 3986
- No dependencies
- Works in browser (
window.slug
) and AMD/CommonJS-flavoured module loaders
npm install slug
If you are using TypeScript you can install the accompanying types
npm install --save-dev @types/slug
Example
var slug = require('slug')
var print = console.log.bind(console, '>')
print(slug('i love unicode'))
print(slug('i love unicode', '_'))
slug.charmap['♥'] = 'freaking love'
print(slug('I ♥ UNICODE'))
slug.reset()
print(slug('I ♥ UNICODE'))
print(slug('Telephone-Number'))
print(slug('Telephone-Number', {lower: false}))
print(slug('маленький подъезд'))
print(slug('маленький подъезд', { locale: 'bg' }))
slug.setLocale('bg')
print(slug('маленький подъезд'))
print(slug('unicode is ☢'))
slug.extend({'☢': 'radioactive'})
print(slug('unicode ♥ is ☢'))
slug.reset()
print(slug('unicode ♥ is ☢'))
print(slug('one 1 two 2 three 3'))
print(slug('one 1 two 2 three 3', { remove: /[0-9]/g }))
options
slug('string', [{options} || 'replacement']);
slug.defaults.mode ='pretty';
slug.defaults.modes['rfc3986'] = {
replacement: '-',
remove: null,
lower: true,
charmap: slug.charmap,
multicharmap: slug.multicharmap,
trim: true,
fallback: true
};
slug.defaults.modes['pretty'] = {
replacement: '-',
remove: null,
lower: false,
charmap: slug.charmap,
multicharmap: slug.multicharmap,
trim: true,
fallback: true
};
Differences between slug
and slugify
packages
Here are some key differences between this package and slugify
.
- Defaults:
slug
has the lower
option enabled by default, lowercasing all slugs
('On SALE'
becomes 'on-sale'
).
slugify
has the lower
option disabled by default ('On SALE'
becomes 'On-SALE'
). - Symbols:
slug
removes unrecognized symbols ('$100'
becomes '100'
, '<5'
becomes '5'
, etc.).
slugify
maps them to words ('$100'
becomes 'dollar100'
, '<5'
becomes 'less5'
, etc.). - Empty Output:
slug
will return a short, predictable hash (' '
becomes 'icag'
and '🎉'
becomes '8joiq'
).
slugify
will return an empty string (' '
and '🎉'
become ''
). - Stability:
slug
is planning a new release that will drop support for CommonJS
and only support ESM modules.
slugify
will continue to support CommonJS and is likely to remain stable for the foreseeable future.
Playground
A (painfully minimal) web playground is available at
https://trott.github.io/slug/. It doesn't allow you to specify options, so it's utility is minimal. Pull requests welcome to add the ability to
specify options.
There is also a (similarly minimal) CLI tool available via npx slug
.
As with the web playground, it doesn't allow you to specify options, so
it's utility is minimal.