Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
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.
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'
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.
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.
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.
RFC 3986 compliant slug generator with support for multiple languages. It creates safe slugs for use in urls—and can revert them.
$ npm install url-slug
var urlSlug = require('url-slug');
// Convert to common slug format, using defaults
urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter');
// sir-james-paul-mc-cartney-mbe-is-an-english-singer-songwriter
// Uppercase with default separator
urlSlug('Comfortably Numb', null, 'uppercase');
// COMFORTABLY-NUMB
// Use an underscore separator and don't touch the string case
urlSlug('á é í ó ú Á É Í Ó Ú ç Ç æ Æ œ Œ ® © € ¥ ª º ¹ ² ½ ¼', '_', false);
// a_e_i_o_u_A_E_I_O_U_c_C_ae_AE_oe_OE_r_c_EU_Y_a_o_1_2_1_2_1_4
// Titlecased without a separator
urlSlug('Red, red wine, stay close to me…', '', 'titlecase');
// RedRedWineStayCloseToMe
// Use a custom separator and uppercase the string (the separator '.' was ignored, because spaces were replaced)
urlSlug('O\'Neill is an American surfboard, surfwear and equipment brand', '.', function (sentence) {
return sentence.replace(/ /g, '+').toUpperCase();
});
// O+NEILL+IS+AN+AMERICAN+SURFBOARD+SURFWEAR+AND+EQUIPMENT+BRAND
// Automatic reversion of slugs
urlSlug.revert('Replace-every_separator.allowed~andSplitCamelCase');
// Replace every separator allowed and Split Camel Case
// Precise reversion, setting the separator and converting the sentence to title case
urlSlug.revert('this-title-needs-a-title_case', '-', 'titlecase');
// This Title Needs A Title_case
// Create a new instance with its own defaults
var custom = new urlSlug.UrlSlug('~', 'uppercase');
custom.convert('Listen to Fito Páez in Madrid');
// LISTEN~TO~FITO~PAEZ~IN~MADRID
Converts a sentence into a slug.
'-'
— can be any of '-._~'
characters or an empty string; a null
or undefined
value will set the default separator'lowercase'
— can be 'lowercase'
, 'uppercase'
, 'titlecase'
or a custom function; if set to false
, no transform will take place; a null
or undefined
value will set the default transformReverts a slug back to a sentence.
null
— can be any of '-._~'
characters or an empty string; a null
or undefined
will set to match all possible separator characters and camel case occurrences; an empty string will set to match only camel case occurrencesnull
— can be 'lowercase'
, 'uppercase'
, 'titlecase'
or a custom function; if set to false
, null
or undefined
no transform will take placeurl-slug constructor, use this if you need another instance. If separator or transform are set, they will be used as the default values of the instance.
'-'
'lowercase'
FAQs
Slug generator with less than 1 KB and no dependencies, RFC 3986 compliant
The npm package url-slug receives a total of 214,895 weekly downloads. As such, url-slug popularity was classified as popular.
We found that url-slug demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.