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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dephub/slug

v1.0.1

Published

URL-friendly slug generator with semantic comparison and validation

Downloads

9

Readme

@dephub/slug 🔤

URL-friendly slug generator with semantic comparison and validation. Convert strings to clean slugs with full Unicode support.

NPM version ESM-only


Features ✨

  • 🎯 Simple API - slug(), isSlug(), compare()
  • 🔒 Type Safe - Full TypeScript support with zero configuration
  • 🌍 Unicode Support - Handles accented characters and diacritics
  • 🛠️ Dual Interface - Use as CLI tool or programmatic API
  • Lightweight - Zero dependencies, focused functionality
  • 🔧 Configurable - Custom separators, case handling, and fallbacks

Installation 📲

npm install @dephub/slug
# or
pnpm add @dephub/slug
# or
yarn add @dephub/slug

Usage 🎉

CLI Usage

# Generate slugs
slug generate "Hello World!"
# Output: hello-world

slug generate "Café & Bar" --separator "_"
# Output: cafe_bar

# Validate slugs
slug validate "valid-slug-123"
# Output: ✅ Valid slug format

slug validate "Invalid Slug!"
# Output: ❌ Invalid slug format

# Compare strings
slug compare "Café au lait" "cafe-au-lait"
# Output: ✅ Strings are equivalent when slugified

Programmatic Usage

import { slug, isSlug, compare } from '@dephub/slug';

// Basic slug generation
slug('  Héllo Wörld!!!  '); // "hello-world"

// Custom options
slug('Hello World', { separator: '_' }); // "hello_world"
slug('Hello World', { lowercase: false }); // "Hello-World"
slug('###', { fallback: 'home' }); // "home"

// Validation
isSlug('valid-slug-name'); // true
isSlug('Invalid Slug!'); // false

// Semantic comparison
compare('Café au lait', 'cafe-au-lait'); // true
compare('user-profile', 'user_profile'); // false

Advanced Examples

// SEO-friendly URLs
const title = 'My Awesome Blog Post!';
const urlSlug = slug(title); // "my-awesome-blog-post"

// Username normalization
const username = slug(userInput, { separator: '_' });
if (!isSlug(username)) {
  throw new Error('Invalid username format');
}

// Database lookup with semantic comparison
function findProduct(name: string) {
  return products.find((product) => compare(product.name, name));
}

API Reference 📚

slug(str, options?)

Convert string to URL-friendly slug.

Parameters:

  • str (string) - Input string to convert
  • options (SlugOptions) - Configuration options
    • separator (string) - Character to replace spaces (default: '-')
    • lowercase (boolean) - Convert to lowercase (default: true)
    • fallback (string) - Fallback if result empty (default: 'default-slug')

Returns: string

Throws: TypeError if input is not a string

isSlug(str)

Check if string matches valid slug format (lowercase, hyphens, alphanumeric).

Parameters:

  • str (string) - String to validate

Returns: boolean

compare(a, b, options?)

Compare two strings by their slugified versions.

Parameters:

  • a (string) - First string to compare
  • b (string) - Second string to compare
  • options (SlugOptions) - Slug configuration options

Returns: boolean


CLI Commands

slug generate <input>

Convert string to URL-friendly slug.

Options:

  • --separator - Separator character (default: "-")
  • --no-lowercase - Keep original case
  • --fallback - Fallback text if result is empty

slug validate <input>

Check if string is valid slug format.

slug compare <first> <second>

Compare two strings by slugified versions.

Options:

  • --separator - Separator character
  • --no-lowercase - Keep original case

Integration Examples

With File System

import { slug } from '@dephub/slug';
import { writeFile } from '@dephub/write';

async function createSluggedFile(content: string, filename: string) {
  const sluggedName = slug(filename);
  await writeFile(`./${sluggedName}.txt`, content);
}

With Configuration

import { slug, compare } from '@dephub/slug';
import { log } from '@dephub/log';

const categoryName = 'User Settings';
const normalized = slug(categoryName, { separator: '_' });
log(`Category: ${normalized}`); // "user_settings"

License 📄

MIT License – see LICENSE for details.

Author: Estarlin R (estarlincito.com)