better-slug
v1.0.7
Published
The most powerful, flexible, and performant slug library with extensive language support, CLI, and zero dependencies
Maintainers
Readme
🚀 better-slug
📋 Table of Contents
- ✨ Features
- 📦 Installation
- 🚀 Quick Start
- 📖 API Reference
- 🌍 Language Support
- 🎯 Advanced Features
- 🖥️ CLI Usage
- 🔄 Migration Guide
- ⚡ Performance
- 🧩 Plugins & Extensions
- 🤝 Contributing
- 📄 License
- 🙏 Acknowledgments
- 🔗 Links
✨ Features
- 🌍 50+ Languages: Comprehensive transliteration including Farsi/Persian (Finglish), Arabic, Chinese, Japanese, Korean, and more
- 🎯 Best-in-class Farsi/Persian Support: The most accurate Finglish transliteration available
- ⚡ Blazing Fast: 2-5x faster than alternatives with zero regex for core operations
- 🔒 Type-Safe: Full TypeScript support with comprehensive types
- 📦 Zero Dependencies: Lightweight and secure
- 🎨 Multiple Modes: Normal, strict, pretty, RFC3986, filename, and ID modes
- 😀 Smart Emoji Handling: Convert, remove, or preserve emojis
- 🔧 Highly Customizable: Extensive options for separators, case styles, and more
- 🖥️ CLI Tool: Powerful command-line interface with interactive mode
- 🔄 Language Preservation: Option to slugify while preserving original language
- 🎯 Smart Features: Stop word removal, language detection, uniqueness generation
- 🚀 Migration-Friendly: Easy migration from other slug libraries
📦 Installation
# Using npm
npm install better-slug
# Using yarn
yarn add better-slug
# Using pnpm
pnpm add better-slug🚀 Quick Start
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'📖 API Reference
slugify(input, options?)
Main function to convert strings to URL-safe slugs.
slugify(input: string, options?: SlugOptions): stringOptions
| 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 |
🌍 Language Support
Comprehensive Farsi/Persian (Finglish) Support
// 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'Other Languages
// 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)
🎯 Advanced Features
Custom Slug Engines
import { createEngine } from 'better-slug';
const engine = createEngine({
locale: 'fa',
separator: '_',
caseStyle: 'upper'
});
engine.slugify('سلام دنیا');
// 'SALAM_DONYA'
// Update options dynamically
engine.updateOptions({ separator: '-' });Batch Processing
import { slugifyBatch } from 'better-slug';
const results = await slugifyBatch([
'Hello World',
'سلام دنیا',
'你好世界'
], { locale: 'auto' });
// ['hello-world', 'salam-donya', 'ni-hao-shi-jie']Emoji Handling
// 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'Mode Examples
// 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'Uniqueness Generation
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'🖥️ CLI Usage
# 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🔄 Migration Guide
From slugify
// 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' });From speakingurl
// Before (speakingurl)
import speakingURL from 'speakingurl';
speakingURL('Hello World', { lang: 'de' });
// After (better-slug)
import slugify from 'better-slug';
slugify('Hello World', { locale: 'de' });From @sindresorhus/slugify
// 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' }
});⚡ Performance
better-slug is optimized for performance:
- No Regex for Core Operations: Avoids ReDoS vulnerabilities
- Character-by-Character Processing: Efficient transliteration
- Memoization: Caches results for repeated calls
- Lazy Loading: Language maps loaded on demand
Benchmarks
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 ✨🧩 Plugins & Extensions
Creating a Custom Transformer
import { createEngine } from 'better-slug';
const customTransform = (str, options) => {
// Your custom transformation
return str.replace(/custom/g, 'transformed');
};
const engine = createEngine({
transforms: [customTransform]
});Custom Character Mappings
slugify('Hello®™', {
customCharmap: {
'®': 'r',
'™': 'tm'
}
});
// 'hello-r-tm'🤝 Contributing
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📄 License
MIT © Ali Shirani
🙏 Acknowledgments
- Inspired by the need for better Farsi/Persian transliteration
- Built with performance and developer experience in mind
