What is url-slug?
The url-slug npm package is a utility for converting strings into URL-friendly slugs. It provides a simple and efficient way to generate slugs from text, which can be useful for creating SEO-friendly URLs, filenames, and more.
What are url-slug's main functionalities?
Basic Slug Generation
This feature allows you to convert a string into a URL-friendly slug. Special characters are removed, and spaces are replaced with hyphens.
const urlSlug = require('url-slug');
const slug = urlSlug('Hello World!');
console.log(slug); // Output: 'hello-world'
Custom Separator
You can customize the separator used in the slug. In this example, an underscore is used instead of the default hyphen.
const urlSlug = require('url-slug');
const slug = urlSlug('Hello World!', { separator: '_' });
console.log(slug); // Output: 'hello_world'
Transform Function
This feature allows you to apply a custom transformation function to the string before generating the slug. The example uses a built-in transformer to convert the string to lowercase.
const urlSlug = require('url-slug');
const slug = urlSlug('Hello World!', { transformer: urlSlug.LOWERCASE_TRANSFORMER });
console.log(slug); // Output: 'hello-world'
Custom Transform Function
You can define your own transformation function to customize how the string is processed before generating the slug. In this example, the string is converted to uppercase.
const urlSlug = require('url-slug');
const customTransformer = (str) => str.toUpperCase();
const slug = urlSlug('Hello World!', { transformer: customTransformer });
console.log(slug); // Output: 'HELLO-WORLD'
Other packages similar to url-slug
slugify
The slugify package is another popular utility for generating URL-friendly slugs. It offers similar functionality to url-slug, including custom separators and transformation options. However, slugify is more widely used and has more contributors, which may result in better support and more frequent updates.
speakingurl
The speakingurl package provides advanced options for generating slugs, including transliteration of non-Latin characters and support for multiple languages. It offers more customization options compared to url-slug, making it a good choice for internationalization.
limax
The limax package is designed for generating slugs with a focus on transliteration and Unicode support. It is similar to url-slug but offers more robust handling of non-ASCII characters, making it suitable for applications that require support for multiple languages and character sets.
url-slug
RFC 3986 compliant slug generator with multiple language support. It creates slugs safe for use in URL paths, queries and fragments, and can revert them too.
Install
$ npm install --save url-slug
Usage
import urlSlug from 'url-slug'
urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter')
Documentation
urlSlug(string, [separator], [transformer]), urlSlug.convert(string, [separator], [transformer])
Returns the string value converted to a slug.
string
Type: string
The string that'll be converted.
separator
Type: string
The character used to separate the slug fragments, set to '-'
by default. Can be set to '-'
, '.'
, '_'
, '~'
or ''
.
transformer
Type: function
or false
A function that receives the slug fragments and the current separator as arguments. It must return the slug string. Defaults to the built-in transformer urlSlug.transformers.lowercase
. It can be set to false
if no transformation is desirable.
Example
import urlSlug from 'url-slug'
urlSlug('Comfortably Numb', urlSlug.transformers.uppercase)
urlSlug('á é í ó ú Á É Í Ó Ú ç Ç æ Æ œ Œ ® © € ¥ ª º ¹ ² ½ ¼', '_', false)
urlSlug('Red, red wine, stay close to me…', '', urlSlug.transformers.titlecase)
urlSlug.revert(slug, [separator], [transformer])
Returns the slug value converted to a human readable string.
slug
Type: string
The slug that'll be reverted.
separator
Type: string
or null
The value used to split the slug into fragments, set to null
by default. Can be set to null
, '-'
, '.'
, '_'
, '~'
or ''
. If set to null
, the split will happen on any valid separator character or camel case occurrences. If set to an empty string, only camel case occurrences will be split.
transformer
Type: function
or false
A function that receives the string fragments and the current separator as arguments. Defaults to false
, which means that no transformation will be made.
Example
import urlSlug from 'url-slug'
urlSlug.revert('Replace-every_separator.allowed~andSplitCamelCaseToo')
urlSlug.revert(
'this-title-needs-a-title_case',
'-',
urlSlug.transformers.titlecase
)
urlSlug.UrlSlug([separator], [transformer])
url-slug
constructor, useful if you want to create more instances. If separator
or transform
are set, they will the default values of the instance.
separator
Type: string
Defaults to '-'
. Can be set to '-'
, '.'
, '_'
, '~'
or ''
.
transformer
Type: function
or false
Defaults to urlSlug.transformers.lowercase
. Can be set to a function or false
, if no transformation is desired.
Example
import urlSlug from 'url-slug'
const urlSlugInstance = new urlSlug.UrlSlug('~', urlSlug.transformers.uppercase)
urlSlugInstance.convert('Listen to Fito Páez in Madrid')
Custom transformers
Custom transformers are expressed by a function which receives two arguments, fragments, an array with the resulting words of the conversion, and separator, the current separator string used to join the words. On revert, the separator will always be a space character (' '
). Transformers should always return a string.
Example
import urlSlug from 'url-slug'
urlSlug(
'O\'Neill is an American surfboard, surfwear and equipment brand',
fragments => fragments.join('+').toUpperCase()
)
urlSlug.revert(
'WEIrd_SNAke_CAse',
'_',
(fragments, separator) => fragments.map(fragment => (
fragment.slice(0, -2).toLowerCase() + fragment.slice(-2).toUpperCase()
)).join(separator)
)
Built-in transformers
urlSlug.transformers.lowercase
Converts the result to lowercase.
urlSlug.transformers.uppercase
Converts the result to uppercase.
urlSlug.transformers.titlecase
Converts the result to title case.
License
The MIT License