Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@sindresorhus/slugify
Advanced tools
@sindresorhus/slugify is a simple and efficient npm package used to convert strings into URL-friendly slugs. It handles various edge cases, such as special characters, spaces, and diacritics, making it a reliable choice for generating slugs from user input or other text data.
Basic Slugification
This feature converts a basic string into a URL-friendly slug by replacing spaces with hyphens and converting the text to lowercase.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Hello World!');
console.log(slug); // 'hello-world'
Custom Replacement Characters
This feature allows you to customize the separator character used in the slug, such as using an underscore instead of a hyphen.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Hello World!', { separator: '_' });
console.log(slug); // 'hello_world'
Handling Diacritics
This feature removes diacritics from characters, ensuring that the resulting slug is composed of standard ASCII characters.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Crème brûlée');
console.log(slug); // 'creme-brulee'
Custom Replacement Map
This feature allows you to define custom replacements for specific characters or strings, providing greater control over the slugification process.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('foo@bar', { customReplacements: [['@', 'at']] });
console.log(slug); // 'foo-at-bar'
The 'slug' package is another popular choice for generating URL-friendly slugs. It offers similar functionality to @sindresorhus/slugify, including handling special characters and diacritics. However, it provides more configuration options and supports transliteration for non-Latin characters.
The 'speakingurl' package is designed to create human-readable slugs from strings. It supports a wide range of languages and character sets, making it a versatile choice for internationalization. Compared to @sindresorhus/slugify, it offers more extensive language support and customization options.
The 'limax' package is a slug generator that focuses on transliteration and supports multiple languages. It is similar to @sindresorhus/slugify in terms of basic functionality but provides additional features for handling non-Latin scripts and complex transliterations.
Slugify a string
Useful for URLs, filenames, and IDs.
It correctly handles German umlauts, Vietnamese, Arabic, Russian, Romanian, Turkish, and more.
$ npm install @sindresorhus/slugify
const slugify = require('@sindresorhus/slugify');
slugify('I ♥ Dogs');
//=> 'i-love-dogs'
slugify(' Déjà Vu! ');
//=> 'deja-vu'
slugify('fooBar 123 $#%');
//=> 'foo-bar-123'
slugify('I ♥ 🦄 & 🐶', {
customReplacements: [
['🐶', 'dog']
]
});
//=> 'i-love-unicorn-and-dog'
Type: string
String to slugify.
Type: object
Type: string
Default: '-'
const slugify = require('@sindresorhus/slugify');
slugify('BAR and baz');
//=> 'bar-and-baz'
slugify('BAR and baz', {separator: '_'});
//=> 'bar_and_baz'
Type: boolean
Default: true
Make the slug lowercase.
const slugify = require('@sindresorhus/slugify');
slugify('Déjà Vu!');
//=> 'deja-vu'
slugify('Déjà Vu!', {lowercase: false});
//=> 'Deja-Vu'
Type: boolean
Default: true
Convert camelcase to separate words. Internally it does fooBar
→ foo bar
.
const slugify = require('@sindresorhus/slugify');
slugify('fooBar');
//=> 'foo-bar'
slugify('fooBar', {decamelize: false});
//=> 'foobar'
Type: Array<string[]>
Default: [ ['&', ' and '], ['🦄', ' unicorn '], ['♥', ' love '] ]
Specifying this only replaces the default if you set an item with the same key, like &
. The replacements are run on the original string before any other transformations.
const slugify = require('@sindresorhus/slugify');
slugify('Foo@unicorn', {
customReplacements: [
['@', 'at']
]
});
//=> 'fooatunicorn'
Add a leading and trailing space to the replacement to have it separated by dashes:
const slugify = require('@sindresorhus/slugify');
slugify('foo@unicorn', {
customReplacements: [
['@', ' at ']
]
});
//=> 'foo-at-unicorn'
Type: boolean
Default: false
If your string starts with an underscore, it will be preserved in the slugified string.
Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
const slugify = require('@sindresorhus/slugify');
slugify('_foo_bar');
//=> 'foo-bar'
slugify('_foo_bar', {preserveLeadingUnderscore: true});
//=> '_foo-bar'
FAQs
Slugify a string
The npm package @sindresorhus/slugify receives a total of 992,216 weekly downloads. As such, @sindresorhus/slugify popularity was classified as popular.
We found that @sindresorhus/slugify 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.