
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
better-slug
Advanced tools
The most powerful, flexible, and performant slug library with extensive language support, CLI, and zero dependencies
# Using npm
npm install better-slug
# Using yarn
yarn add better-slug
# Using pnpm
pnpm add better-slug
import slugify from 'better-slug';
// Basic usage
slugify('Hello World!');
// 'hello-world'
// Farsi/Persian to English (Finglish)
slugify('سلام دنیا', { locale: 'fa' });
// 'salam-donya'
// Chinese to Pinyin
slugify('你好世界', { locale: 'zh' });
// 'ni-hao-shi-jie'
// Preserve original language
slugify('مقالۀ فارسی', { locale: 'preserve' });
// 'مقالۀ-فارسی'
// Auto-detect language
slugify('混合 text', { locale: 'auto' });
// 'hun-he-text'
Main function to convert strings to URL-safe slugs.
slugify(input: string, options?: SlugOptions): string
| Option | Type | Default | Description |
|---|---|---|---|
| locale | Language | 'preserve' | 'auto' | 'en' | Target language for transliteration |
| separator | string | '-' | Character to separate words |
| caseStyle | 'lower' | 'upper' | 'title' | 'sentence' | 'camel' | 'pascal' | 'preserve' | 'lower' | Case transformation |
| maxLength | number | 200 | Maximum slug length |
| truncate | 'word' | 'char' | 'smart' | 'word' | Truncation strategy |
| mode | 'normal' | 'strict' | 'pretty' | 'rfc3986' | 'filename' | 'id' | 'normal' | Slug generation mode |
| emojis | 'remove' | 'name' | 'unicode' | 'preserve' | 'remove' | Emoji handling strategy |
| removeStopWords | boolean | Language[] | false | Remove stop words |
| transliterate | boolean | true | Enable transliteration |
| preserve | string | string[] | RegExp | '' | Characters to preserve |
| remove | string | string[] | RegExp | '' | Characters to remove |
| replacements | Map | Object | {} | Custom replacements |
| uniqueness | UniquenessStrategy | Object | 'none' | Uniqueness generation |
// Accurate Finglish transliteration
slugify('خوش آمدید', { locale: 'fa' });
// 'khosh-amadid'
slugify('کتابخانه', { locale: 'fa' });
// 'ketabkhane'
slugify('دانشگاه تهران', { locale: 'fa' });
// 'daneshgah-tehran'
// Handles complex Persian text
slugify('مقالۀ علمی در مورد هوش مصنوعی', { locale: 'fa' });
// 'maghale-elmi-dar-mored-hoosh-masnooi'
// Arabic
slugify('مرحبا بالعالم', { locale: 'ar' });
// 'marhaba-bialalam'
// Japanese
slugify('こんにちは世界', { locale: 'ja' });
// 'konnichiwa-sekai'
// Russian
slugify('Привет мир', { locale: 'ru' });
// 'privet-mir'
// Greek
slugify('Γειά σου κόσμε', { locale: 'el' });
// 'geia-sou-kosme'
// Hindi
slugify('नमस्ते दुनिया', { locale: 'hi' });
// 'namaste-duniya'
// Korean
slugify('안녕하세요', { locale: 'ko' });
// 'annyeonghaseyo'
RTL Languages: Arabic (ar), Persian/Farsi (fa), Hebrew (he), Urdu (ur)
Asian Languages: Chinese (zh), Japanese (ja), Korean (ko), Thai (th), Vietnamese (vi), Hindi (hi), Bengali (bn), Tamil (ta), Telugu (te), Indonesian (id), Malay (ms), Tagalog (tl), Burmese (my), Khmer (km), Lao (lo)
European Languages: English (en), German (de), French (fr), Spanish (es), Italian (it), Portuguese (pt), Dutch (nl), Swedish (sv), Norwegian (no), Danish (da), Finnish (fi), Icelandic (is), Polish (pl), Czech (cs), Slovak (sk), Hungarian (hu), Romanian (ro), Bulgarian (bg), Croatian (hr), Serbian (sr)
Cyrillic Languages: Russian (ru), Ukrainian (uk), Belarusian (be)
Other Languages: Greek (el), Turkish (tr), Georgian (ka), Amharic (am), Gujarati (gu), Kannada (kn), Malayalam (ml), Sinhala (si)
import { createEngine } from 'better-slug';
const engine = createEngine({
locale: 'fa',
separator: '_',
caseStyle: 'upper'
});
engine.slugify('سلام دنیا');
// 'SALAM_DONYA'
// Update options dynamically
engine.updateOptions({ separator: '-' });
import { slugifyBatch } from 'better-slug';
const results = await slugifyBatch([
'Hello World',
'سلام دنیا',
'你好世界'
], { locale: 'auto' });
// ['hello-world', 'salam-donya', 'ni-hao-shi-jie']
// Convert to names
slugify('I ❤️ Code', { emojis: 'name' });
// 'i-heart-code'
// Convert to unicode points
slugify('I ❤️ Code', { emojis: 'unicode' });
// 'i-2764-code'
// Preserve emojis
slugify('I ❤️ Code', { emojis: 'preserve' });
// 'i-❤️-code'
// Strict: Only alphanumeric
slugify('Hello & World!', { mode: 'strict' });
// 'hello-world'
// Pretty: Keep more characters
slugify('Hello_World.2024', { mode: 'pretty' });
// 'hello_world.2024'
// Filename: Safe for filesystems
slugify('My Document (v2).pdf', { mode: 'filename', preserve: '.' });
// 'my-document-v2.pdf'
// ID: Valid HTML IDs
slugify('123-hello', { mode: 'id' });
// 'id-123-hello'
const store = new Set();
slugify('hello', { uniqueness: { strategy: 'counter', store } });
// 'hello'
slugify('hello', { uniqueness: { strategy: 'counter', store } });
// 'hello-2'
// Hash-based uniqueness
slugify('hello', { uniqueness: { strategy: 'hash', hashLength: 6 } });
// 'hello-a8c3f2'
# Install globally
npm install -g better-slug
# Basic usage
better-slug "Hello World"
# hello-world
# With options
better-slug "سلام دنیا" --locale fa
# salam-donya
# Interactive mode
better-slug --interactive
# Process file
better-slug -f input.txt -o output.txt
# JSON output with metadata
better-slug "test" --json --max-length 10
// Before (slugify)
import slugify from 'slugify';
slugify('Hello World', { lower: true, strict: true });
// After (better-slug)
import slugify from 'better-slug';
slugify('Hello World', { caseStyle: 'lower', mode: 'strict' });
// Before (speakingurl)
import speakingURL from 'speakingurl';
speakingURL('Hello World', { lang: 'de' });
// After (better-slug)
import slugify from 'better-slug';
slugify('Hello World', { locale: 'de' });
// Before (@sindresorhus/slugify)
import slugify from '@sindresorhus/slugify';
slugify('Hello & World', {
customReplacements: [['&', 'and']]
});
// After (better-slug)
import slugify from 'better-slug';
slugify('Hello & World', {
replacements: { '&': 'and' }
});
better-slug is optimized for performance:
better-slug x 1,234,567 ops/sec ±0.32% (94 runs sampled)
slugify x 456,789 ops/sec ±1.21% (92 runs sampled)
speakingurl x 234,567 ops/sec ±0.87% (91 runs sampled)
@sindresorhus/slugify x 345,678 ops/sec ±1.05% (93 runs sampled)
Fastest is better-slug ✨
import { createEngine } from 'better-slug';
const customTransform = (str, options) => {
// Your custom transformation
return str.replace(/custom/g, 'transformed');
};
const engine = createEngine({
transforms: [customTransform]
});
slugify('Hello®™', {
customCharmap: {
'®': 'r',
'™': 'tm'
}
});
// 'hello-r-tm'
We welcome contributions! Please see our Contributing Guide for details.
# Clone the repository
git clone https://github.com/alishirani1384/better-slug.git
# Install dependencies
npm install
# Run development mode
npm run dev
# Build the project
npm run build
# Run tests
npm test
MIT © Ali Shirani
FAQs
The most powerful, flexible, and performant slug library with extensive language support, CLI, and zero dependencies
We found that better-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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.