npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-slug

v1.0.7

Published

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

Readme

🚀 better-slug

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

| 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