Gathering detailed insights and metrics for @sindresorhus/slugify
Gathering detailed insights and metrics for @sindresorhus/slugify
Gathering detailed insights and metrics for @sindresorhus/slugify
Gathering detailed insights and metrics for @sindresorhus/slugify
npm install @sindresorhus/slugify
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,570 Stars
67 Commits
76 Forks
20 Watching
1 Branches
19 Contributors
Updated on 26 Nov 2024
JavaScript (94.5%)
TypeScript (5.5%)
Cumulative downloads
Total Downloads
Last day
-24.3%
200,597
Compared to previous day
Last week
3.4%
1,340,109
Compared to previous week
Last month
7%
5,597,841
Compared to previous month
Last year
20.1%
47,436,996
Compared to previous year
2
Slugify a string
Useful for URLs, filenames, and IDs.
It handles most major languages, including German (umlauts), Vietnamese, Arabic, Russian, and more.
$ npm install @sindresorhus/slugify
1import slugify from '@sindresorhus/slugify'; 2 3slugify('I ♥ Dogs'); 4//=> 'i-love-dogs' 5 6slugify(' Déjà Vu! '); 7//=> 'deja-vu' 8 9slugify('fooBar 123 $#%'); 10//=> 'foo-bar-123' 11 12slugify('я люблю единорогов'); 13//=> 'ya-lyublyu-edinorogov'
Type: string
String to slugify.
Type: object
Type: string
Default: '-'
1import slugify from '@sindresorhus/slugify'; 2 3slugify('BAR and baz'); 4//=> 'bar-and-baz' 5 6slugify('BAR and baz', {separator: '_'}); 7//=> 'bar_and_baz' 8 9slugify('BAR and baz', {separator: ''}); 10//=> 'barandbaz'
Type: boolean
Default: true
Make the slug lowercase.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('Déjà Vu!'); 4//=> 'deja-vu' 5 6slugify('Déjà Vu!', {lowercase: false}); 7//=> 'Deja-Vu'
Type: boolean
Default: true
Convert camelcase to separate words. Internally it does fooBar
→ foo bar
.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('fooBar'); 4//=> 'foo-bar' 5 6slugify('fooBar', {decamelize: false}); 7//=> 'foobar'
Type: Array<string[]>
Default: [ ['&', ' and '], ['🦄', ' unicorn '], ['♥', ' love '] ]
Add your own custom replacements.
The replacements are run on the original string before any other transformations.
This only overrides a default replacement if you set an item with the same key, like &
.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('Foo@unicorn', { 4 customReplacements: [ 5 ['@', 'at'] 6 ] 7}); 8//=> 'fooatunicorn'
Add a leading and trailing space to the replacement to have it separated by dashes:
1import slugify from '@sindresorhus/slugify'; 2 3slugify('foo@unicorn', { 4 customReplacements: [ 5 ['@', ' at '] 6 ] 7}); 8//=> 'foo-at-unicorn'
Another example:
1import slugify from '@sindresorhus/slugify'; 2 3slugify('I love 🐶', { 4 customReplacements: [ 5 ['🐶', 'dogs'] 6 ] 7}); 8//=> 'i-love-dogs'
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.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('_foo_bar'); 4//=> 'foo-bar' 5 6slugify('_foo_bar', {preserveLeadingUnderscore: true}); 7//=> '_foo-bar'
Type: boolean
Default: false
If your string ends with a dash, it will be preserved in the slugified string.
For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('foo-bar-'); 4//=> 'foo-bar' 5 6slugify('foo-bar-', {preserveTrailingDash: true}); 7//=> 'foo-bar-'
Type: string[]
Default: []
Preserve certain characters.
It cannot contain the separator
.
For example, if you want to slugify URLs, but preserve the HTML fragment #
character.
1import slugify from '@sindresorhus/slugify'; 2 3slugify('foo_bar#baz', {preserveCharacters: ['#']}); 4//=> 'foo-bar#baz'
Returns a new instance of slugify(string, options?)
with a counter to handle multiple occurrences of the same string.
1import {slugifyWithCounter} from '@sindresorhus/slugify'; 2 3const slugify = slugifyWithCounter(); 4 5slugify('foo bar'); 6//=> 'foo-bar' 7 8slugify('foo bar'); 9//=> 'foo-bar-2' 10 11slugify.reset(); 12 13slugify('foo bar'); 14//=> 'foo-bar'
If, for example, you have a document with multiple sections where each subsection has an example.
1## Section 1 2 3### Example 4 5## Section 2 6 7### Example
You can then use slugifyWithCounter()
to generate unique HTML id
's to ensure anchors will link to the right headline.
Reset the counter
1import {slugifyWithCounter} from '@sindresorhus/slugify'; 2 3const slugify = slugifyWithCounter(); 4 5slugify('foo bar'); 6//=> 'foo-bar' 7 8slugify('foo bar'); 9//=> 'foo-bar-2' 10 11slugify.reset(); 12 13slugify('foo bar'); 14//=> 'foo-bar'
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More