url-slug-lite-ts
v1.0.0
Published
Ultra-fast (<1 KB), zero-dependency Unicode URL slugifier with Latin accent transliteration and separator customization.
Maintainers
Readme
url-slug-lite-ts
Ultra-fast, lightweight Unicode URL slugifier with Latin accent transliteration, decamelization, and SEO-friendly truncation in TypeScript with zero dependencies.
💡 Why url-slug-lite-ts?
Creating clean, readable, SEO-optimized URL slugs should not require bloated multi-megabyte libraries:
- Zero Dependencies: Pure TypeScript implementation with zero external runtime dependencies.
- Unicode Accent Transliteration: Normalizes accented characters (
é->e,ñ->n,ü->u,ç->c) using standard Unicode NFD decomposition. - Smart Decamelization: Splits
camelCaseandPascalCasewords naturally (myAwesomePost->my-awesome-post). - Safe Truncation: Supports
maxLengthtrimming without leaving dangling hyphens or broken words. - Dual ESM & CommonJS: Full support for Node.js, modern bundlers (Vite, Next.js, Webpack, esbuild), and edge runtimes (Cloudflare Workers, Deno, Bun).
📦 Installation
# npm
npm install url-slug-lite-ts
# pnpm
pnpm add url-slug-lite-ts
# yarn
yarn add url-slug-lite-ts
# bun
bun add url-slug-lite-ts🚀 Quick Start
import { slugify } from 'url-slug-lite-ts';
// Standard sentence
slugify('Hello World! Building Modern Web Apps');
// => 'hello-world-building-modern-web-apps'
// Accents & Diacritics
slugify('Café & Crème Brûlée Délicieuse');
// => 'cafe-and-creme-brulee-delicieuse'
// CamelCase & PascalCase
slugify('TypeScript4Life and React19Updates');
// => 'typescript-4-life-and-react-19-updates'🛠️ Real-World Examples
1. Next.js / Nuxt Blog Post Routing
Generate clean URLs for dynamic CMS pages:
import { slugify } from 'url-slug-lite-ts';
interface ArticleInput {
title: string;
category: string;
}
export function generateArticleSlug(article: ArticleInput): string {
const categorySlug = slugify(article.category);
const titleSlug = slugify(article.title, { maxLength: 60 });
return `/${categorySlug}/${titleSlug}`;
}
const url = generateArticleSlug({
category: 'Web Development',
title: 'Top 10 Zero-Dependency NPM Packages You Must Use in 2026!'
});
console.log(url);
// => '/web-development/top-10-zero-dependency-npm-packages-you-must-use-in-2026'2. E-Commerce Product SKU & Slug Generation
import { slugify } from 'url-slug-lite-ts';
const product = {
name: 'Sony WH-1000XM5 Wireless Noise-Canceling Headphones (Black)',
id: 'prod_98765'
};
const productSlug = `${slugify(product.name, { maxLength: 40 })}-${product.id}`;
console.log(productSlug);
// => 'sony-wh-1000xm5-wireless-noise-canceling-prod_98765'3. File System Sanitization
Clean user-uploaded filenames before storing in S3 or local disk:
import { slugify } from 'url-slug-lite-ts';
export function sanitizeFileName(originalName: string): string {
const ext = originalName.slice(originalName.lastIndexOf('.'));
const base = originalName.slice(0, originalName.lastIndexOf('.'));
return `${slugify(base, { separator: '_' })}${ext.toLowerCase()}`;
}
console.log(sanitizeFileName('My Financial Report (Q3 2026) — Final Draft.PDF'));
// => 'my_financial_report_q3_2026_final_draft.pdf'4. Custom Separators and Preserved Casing
import { slugify } from 'url-slug-lite-ts';
// Custom underscore separator
slugify('Database Migration Step 1', { separator: '_' });
// => 'database_migration_step_1'
// Preserving uppercase formatting
slugify('ISO Standards Code 9001', { lowercase: false });
// => 'ISO-Standards-Code-9001'📖 API Reference
slugify(input, options?)
Converts any arbitrary text string into a clean URL slug.
- Parameters:
input(string): The source text to format.options(SlugOptions, optional): Configuration options.
- Returns:
string(formatted slug).
Options (SlugOptions):
| Option | Type | Default | Description |
|---|---|---|---|
| separator | string | '-' | String used to separate words |
| lowercase | boolean | true | Whether to convert characters to lowercase |
| decamelize | boolean | true | Split camelCase and PascalCase words with spaces before slugifying |
| strict | boolean | true | Strips all non-alphanumeric characters (except separator) |
| maxLength | number | undefined | Maximum length of resulting slug without trailing separators |
🏷️ TypeScript Interfaces
export interface SlugOptions {
/**
* Character used to replace whitespace and special characters.
* @default '-'
*/
separator?: string;
/**
* Whether to transform the resulting slug to lowercase.
* @default true
*/
lowercase?: boolean;
/**
* Whether to split camelCase or PascalCase phrases into words.
* @default true
*/
decamelize?: boolean;
/**
* Removes symbols, punctuation, and non-alphanumeric characters.
* @default true
*/
strict?: boolean;
/**
* Truncates the slug to a maximum character length without leaving trailing separators.
*/
maxLength?: number;
}🧪 Testing
Every release is tested with 100% test coverage using Vitest:
npm test💖 Support the Project
If url-slug-lite-ts kept your URLs clean and SEO-friendly, please consider sponsoring:
👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
