Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Slug generator with less than 1 KB and no dependencies, RFC 3986 compliant
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.
npm install url-slug
import urlSlug from 'url-slug'
urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter')
// sir-james-paul-mc-cartney-mbe-is-an-english-singer-songwriter
⚠️ Only named exports are available in Node.js.
import { convert } from 'url-slug'
urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter')
// sir-james-paul-mc-cartney-mbe-is-an-english-singer-songwriter
const { convert } = require('url-slug')
urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter')
// sir-james-paul-mc-cartney-mbe-is-an-english-singer-songwriter
Returns value
value converted to a slug.
A string to be slugified.
Name | Description | Default |
---|---|---|
camelCase | Split on camel case occurrences | true |
dictionary | Chars to be replaced | {} |
separator | Character or string used to separate the slug fragments | '-' |
transformer | A built-in transformer or a custom function (null to keep the string unchanged) | LOWERCASE_TRANSFORMER |
import * as urlSlug from 'url-slug'
urlSlug.convert('Comfortably Numb', {
transformer: urlSlug.UPPERCASE_TRANSFORMER,
})
// COMFORTABLY-NUMB
urlSlug.convert('á é í ó ú Á É Í Ó Ú ç Ç ª º ¹ ² ½ ¼', {
separator: '_',
transformer: false,
})
// a_e_i_o_u_A_E_I_O_U_c_C_a_o_1_2_1_2_1_4
urlSlug.convert('Red, red wine, stay close to me…', {
separator: '',
transformer: urlSlug.TITLECASE_TRANSFORMER,
})
// RedRedWineStayCloseToMe
urlSlug.convert('Schwarzweiß', {
dictionary: { ß: 'ss', z: 'z ' },
})
// schwarz-weiss
Returns the value
value converted to a regular sentence.
A slug to be reverted to a sentence.
Name | Description | Default |
---|---|---|
camelCase | Split on camel case occurrences | false |
separator | Character or string to split the slug (null for automatic splitting) | null |
transformer | A built-in transformer or a custom function (null to keep the string unchanged) | false |
import { revert, TITLECASE_TRANSFORMER } from 'url-slug'
revert('Replace-every_separator.allowed~andSplitCamelCaseToo', {
camelCase: true,
})
// Replace every separator allowed and Split Camel Case Too
revert('this-slug-needs-a-title_case', {
separator: '-',
transformer: TITLECASE_TRANSFORMER,
})
// This Slug Needs A Title_case
Custom transformers are expressed by a function that receives two arguments:
fragments
, an array containing the words of a sentence or a slug, and
separator
, which is the separator string set in convert()
options. When
revert()
calls a transformer, the separator
argument will always be a space
character (' '
) — the separator
option will be used to split the slug.
Transformers should always return a string.
import { convert, revert } from 'url-slug'
convert('O’Neill is an American surfboard, surfwear and equipment brand', {
transformer: (fragments) => fragments.join('x').toUpperCase(),
})
// OxNEILLxISxANxAMERICANxSURFBOARDxSURFWEARxANDxEQUIPMENTxBRAND
revert('WEIrd_SNAke_CAse', {
separator: '_',
transformer: (fragments, separator) =>
fragments
.map(
(fragment) =>
fragment.slice(0, -2).toLowerCase() + fragment.slice(-2).toUpperCase()
)
.join(separator),
})
// weiRD snaKE caSE
Converts the result to lowercase. E.g.: // SOME WORDS >> some words
Converts the result to sentence case. E.g.: // sOME WORDS >> Some words
Converts the result to uppercase. E.g.: // some words >> SOME WORDS
Converts the result to title case. E.g.: // sOME wORDS >> Some Words
Any character or an empty string can be used in the separator
property. When
the separator
is an empty string, the revert()
method will split the slug
only on camel case occurrences if camelCase
option is set to true
,
or else it returns an untouched string. The following characters are valid
according to RFC 3986 — defined as unreserved or sub-delims —, and will be
used in revert()
function if automatic splitting is enabled — separator
is
set to null
:
-
, .
, _
, ~
, ^
, -
, .
, _
, ~
, !
, $
, &
, '
, (
, )
, *
,
+
, ,
, ;
or =
dictionary
optionIt must be an object, with keys set as single characters and values as strings of any length:
import { convert } from 'url-slug'
convert('♥øß', {
dictionary: {
'♥': 'love',
ø: 'o',
ß: 'ss',
//...
},
})
// loveoss
To add separators before or after a specific character, add a space before or after the dictionary definition:
import { convert } from 'url-slug'
convert('♥øß', {
dictionary: {
'♥': 'love',
ø: ' o', // A space was added before
ß: 'ss',
//...
},
})
// love-oss
convert('♥øß', {
dictionary: {
'♥': 'love',
ø: ' o ', // A space was added before and after
ß: 'ss',
//...
},
})
// love-o-ss
convert('♥øß', {
dictionary: {
'♥': 'love',
ø: 'o ', // A space was added after
ß: 'ss',
//...
},
})
// loveo-ss
Compatible with any environment with ES6 support.
FAQs
Slug generator with less than 1 KB and no dependencies, RFC 3986 compliant
The npm package url-slug receives a total of 156,735 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.