What is slug?
The 'slug' npm package is a utility for creating URL-friendly slugs from strings. It converts strings into a format that can be safely used in URLs by removing or replacing special characters, spaces, and other non-URL-safe characters.
What are slug's main functionalities?
Basic Slug Creation
This feature allows you to create a basic slug from a string. It converts the string to lowercase and replaces spaces and special characters with hyphens.
const slug = require('slug');
const title = 'Hello World!';
const urlSlug = slug(title);
console.log(urlSlug); // Output: 'hello-world'
Custom Separator
This feature allows you to specify a custom separator instead of the default hyphen. In this example, underscores are used as separators.
const slug = require('slug');
const title = 'Hello World!';
const urlSlug = slug(title, { replacement: '_' });
console.log(urlSlug); // Output: 'hello_world'
Remove Special Characters
This feature automatically removes special characters from the string, making it URL-safe.
const slug = require('slug');
const title = 'Hello, World!';
const urlSlug = slug(title);
console.log(urlSlug); // Output: 'hello-world'
Multi-language Support
This feature supports multiple languages, converting non-Latin characters to their Latin equivalents.
const slug = require('slug');
const title = '你好,世界';
const urlSlug = slug(title);
console.log(urlSlug); // Output: 'ni-hao-shi-jie'
Other packages similar to slug
speakingurl
The 'speakingurl' package is another utility for creating slugs from strings. It offers more customization options, such as transliteration, custom character maps, and support for multiple languages. Compared to 'slug', 'speakingurl' provides more flexibility and control over the slug generation process.
slugify
The 'slugify' package is a straightforward utility for converting strings into URL-friendly slugs. It is similar to 'slug' but focuses on simplicity and ease of use. 'slugify' is a good choice if you need a lightweight solution without many configuration options.
url-slug
The 'url-slug' package provides a simple way to create URL-friendly slugs from strings. It offers basic functionality similar to 'slug', with options for custom separators and character replacement. 'url-slug' is a good alternative if you need a minimalistic approach to slug generation.
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.