New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

better-slug

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-slug

The most powerful, flexible, and performant slug library with extensive language support, CLI, and zero dependencies

latest
Source
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

🚀 better-slug

The most powerful, flexible, and performant slug library with 50+ language support

npm version npm downloads bundle size license TypeScript

📋 Table of Contents

✨ 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): string

Options

OptionTypeDefaultDescription
localeLanguage | 'preserve' | 'auto''en'Target language for transliteration
separatorstring'-'Character to separate words
caseStyle'lower' | 'upper' | 'title' | 'sentence' | 'camel' | 'pascal' | 'preserve''lower'Case transformation
maxLengthnumber200Maximum 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
removeStopWordsboolean | Language[]falseRemove stop words
transliteratebooleantrueEnable transliteration
preservestring | string[] | RegExp''Characters to preserve
removestring | string[] | RegExp''Characters to remove
replacementsMap | Object{}Custom replacements
uniquenessUniquenessStrategy | 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'
View all 50+ supported languages

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
Made with ❤️ for developers worldwide
If you find this package useful, please ⭐ star it on GitHub!

Keywords

slug

FAQs

Package last updated on 09 Nov 2025

Did you know?

Socket

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.

Install

Related posts