
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Slugifies strings, even when they contain Unicode.
Make strings URL-safe.
npm install slug
If you are using TypeScript you can install the accompanying types
npm install --save-dev @types/slug
import slug from 'slug'
var print = console.log.bind(console, '>')
print(slug('i love unicode'))
// > i-love-unicode
print(slug('i love unicode', '_')) // If you prefer something else than `-` as separator
// > i_love_unicode
slug.charmap['♥'] = 'freaking love' // change default charmap or use option {charmap:{…}} as 2. argument
print(slug('I ♥ UNICODE'))
// > i-freaking-love-unicode
// To reset modifications to slug.charmap, use slug.reset():
slug.reset()
print(slug('I ♥ UNICODE'))
// > i-unicode
print(slug('Telephone-Number')) // lower case by default
// > telephone-number
print(slug('Telephone-Number', {lower: false})) // If you want to preserve case
// > Telephone-Number
// We try to provide sensible defaults.
// So Cyrillic text will be transliterated as if it were Russian:
print(slug('маленький подъезд'))
// > malenkij-poduezd
// But maybe you know it's Bulgarian:
print(slug('маленький подъезд', { locale: 'bg' }))
// > malenykiy-podaezd
// To set the default locale:
slug.setLocale('bg')
print(slug('маленький подъезд'))
// > malenykiy-podaezd
print(slug('unicode is ☢'))
// > unicode-is
slug.extend({'☢': 'radioactive'})
print(slug('unicode ♥ is ☢'))
// > unicode-is-radioactive
// slug.extend() modifies the default charmap for the entire process.
// If you need to reset charmap, multicharmap, and the default locale, use slug.reset():
slug.reset()
print(slug('unicode ♥ is ☢'))
// > unicode-is
// Custom removal of characters from resulting slug. Let's say that we want to
// remove all numbers for some reason.
print(slug('one 1 two 2 three 3'))
// > one-1-two-2-three-3
print(slug('one 1 two 2 three 3', { remove: /[0-9]/g }))
// > one-two-three
// options is either object or replacement (sets options.replacement)
slug('string', [{options} || 'replacement']);
slug.defaults.mode ='pretty';
slug.defaults.modes['rfc3986'] = {
replacement: '-', // replace spaces with replacement
remove: null, // (optional) regex to remove characters
lower: true, // result in lower case
charmap: slug.charmap, // replace special characters
multicharmap: slug.multicharmap, // replace multiple code unit characters
trim: true, // trim leading and trailing replacement chars
fallback: true // use base64 to generate slug for empty results
};
slug.defaults.modes['pretty'] = {
replacement: '-',
remove: null,
lower: false,
charmap: slug.charmap,
multicharmap: slug.multicharmap,
trim: true,
fallback: true
};
slug
and slugify
packagesHere are some key differences between this package and slugify
.
slug
is ESM-only.slugify
supports CommonJS and ESM.slug
has the lower
option enabled by default, lowercasing all slugs
('On SALE'
becomes 'on-sale'
).slugify
has the lower
option disabled by default ('On SALE'
becomes 'On-SALE'
).slug
removes unrecognized symbols ('$100'
becomes '100'
, '<5'
becomes '5'
, etc.).slugify
maps them to words ('$100'
becomes 'dollar100'
, '<5'
becomes 'less5'
, etc.).slug
will return a short, predictable hash (' '
becomes 'icag'
and '🎉'
becomes '8joiq'
).slugify
will return an empty string (' '
and '🎉'
become ''
).A web playground is available at https://trott.github.io/slug/.
There is also a CLI tool available via npx slug
. It doesn't allow you to
specify options, so it's utility is minimal.
The 'speakingurl' package is another utility for creating slugs from strings. It offers more customization options, such as transliteration, custom character maps, and support for multiple languages. Compared to 'slug', 'speakingurl' provides more flexibility and control over the slug generation process.
The 'slugify' package is a straightforward utility for converting strings into URL-friendly slugs. It is similar to 'slug' but focuses on simplicity and ease of use. 'slugify' is a good choice if you need a lightweight solution without many configuration options.
The 'url-slug' package provides a simple way to create URL-friendly slugs from strings. It offers basic functionality similar to 'slug', with options for custom separators and character replacement. 'url-slug' is a good alternative if you need a minimalistic approach to slug generation.
FAQs
slugifies even utf-8 chars!
The npm package slug receives a total of 372,630 weekly downloads. As such, slug popularity was classified as popular.
We found that slug demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.