clean-slug-lite
v1.0.1
Published
Fast, zero-dependency, Unicode-aware URL slug generator for SEO, blogs, CMS, and clean URLs with symbol replacement and smart truncation.
Maintainers
Readme
🪶 clean-slug-lite
Fast, zero-dependency, Unicode-aware URL slug generator for SEO, blogs, CMS, and clean URLs with symbol replacement and smart word-boundary truncation.
✨ Features
- ⚡ Zero Dependencies — Ultra-lightweight (~1.5 KB), pure native regex & Unicode algorithms.
- 🌍 Smart Accent Normalization — Automatically cleans diacritics (
Café->cafe,München->munchen,español->espanol). - 🔣 Intelligent Symbol Expansion — Expands
&toand,@toat,%topercent, and currencies ($,€,£,₹). - ✂️ Word-Boundary Truncation —
maxLengthtrims cleanly at whole words rather than slicing words in half. - 🌐 Unicode Preservation Option — Keep Cyrillic, Chinese, Arabic, or Devanagari scripts intact with
preserveUnicode: true. - 🛠️ Custom Replacement Maps — Add tailored dictionary replacements (e.g.
C#->c-sharp). - 📦 Dual ESM & CommonJS — Works in Node.js, Next.js, Vite, React, Express, and browsers.
📥 Installation
npm install clean-slug-liteor with Yarn / pnpm:
pnpm add clean-slug-lite
# or
yarn add clean-slug-lite🚀 Quick Start
1. Basic Slug Generation
import { slugify } from 'clean-slug-lite';
console.log(slugify('Hello World!'));
// "hello-world"
console.log(slugify('Rock & Roll @ 100%'));
// "rock-and-roll-at-100-percent"2. Accents & Diacritics Normalization
import { slugify } from 'clean-slug-lite';
console.log(slugify('Café au Lait'));
// "cafe-au-lait"
console.log(slugify('München Oktoberfest'));
// "munchen-oktoberfest"
console.log(slugify('¿Cómo estás?'));
// "como-estas"3. Word-Boundary Truncation (maxLength)
Ensures words aren't abruptly cut into unreadable fragments:
import { slugify } from 'clean-slug-lite';
const title = 'The quick brown fox jumps over the lazy dog';
// Cleanly cuts at the nearest word boundary:
console.log(slugify(title, { maxLength: 25 }));
// "the-quick-brown-fox-jumps"4. Custom Word Replacements
import { slugify } from 'clean-slug-lite';
const slug = slugify('Full-stack guide to C# and .NET', {
customReplacements: {
'C#': 'c-sharp',
'.NET': 'dotnet',
},
});
console.log(slug);
// "full-stack-guide-to-c-sharp-and-dotnet"5. Validate Slugs (isValidSlug)
import { isValidSlug } from 'clean-slug-lite';
console.log(isValidSlug('valid-clean-slug-123')); // true
console.log(isValidSlug('Invalid Slug!')); // false⚙️ Options Reference
slugify(text, options)| Option | Type | Default | Description |
|---|---|---|---|
| separator | string | '-' | Word separator character (e.g. '-' or '_'). |
| lower | boolean | true | Convert result to lower case. |
| trim | boolean | true | Strip leading and trailing separators. |
| maxLength | number | undefined | Truncate length at word boundary. |
| preserveUnicode | boolean | false | Preserve non-Latin scripts (Cyrillic, Devanagari, etc.). |
| replaceSymbols | boolean | true | Expands symbols like &, @, %, +, currencies. |
| customReplacements | Record<string, string> | undefined | Key-value pairs of terms to replace before slugification. |
🛠 Development & Testing
# Install dependencies
npm install
# Run unit tests
npm test
# Build dual ESM/CJS bundles + .d.ts types
npm run build💖 Support the Project
If you find this package useful, consider sponsoring its ongoing development and maintenance: 👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
