CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sindresorhus--slugify

Slugify a string with comprehensive Unicode transliteration and extensive customization options

94

1.34x
Overview
Eval results
Files

task.mdevals/scenario-9/

Blog Post Title Sanitizer

Build a module that processes blog post titles to create clean, URL-friendly identifiers while handling various whitespace patterns and special characters that commonly appear in user-generated content.

Requirements

Your module should export a function sanitizeTitle(title, options) that:

  1. Handles various whitespace patterns:

    • Trims leading and trailing whitespace
    • Collapses multiple consecutive spaces into single separators
    • Handles tabs, newlines, and other whitespace characters
    • Preserves intentional spacing between words
  2. Processes special characters appropriately:

    • Converts common symbols to readable equivalents (e.g., '&' to 'and', '♥' to 'love')
    • Removes punctuation marks like exclamation points, question marks, and parentheses
    • Handles currency symbols and mathematical operators
    • Deals with emoji and special Unicode characters
  3. Supports customization:

    • Allow custom separator (default: hyphen)
    • Support for preserving specific characters when needed
    • Option to control trailing separator behavior

Input Examples

Your function should handle titles like:

  • " Hello World "
  • "How to Cook & Bake!?"
  • "2024: A Year in Review"
  • "I ♥ Programming"
  • "What's New?" (with apostrophe)
  • "Product @ $50.00"
  • "Tab\tSeparated\tTitle"
  • "Line\nBreak\nTitle"

Test Cases

Create a test file sanitize.test.js with the following test cases:

Basic Whitespace Handling

  • It trims leading and trailing spaces from " Hello World " to produce "hello-world" @test

  • It collapses multiple consecutive spaces in "Hello World" to produce "hello-world" @test

  • It handles tab characters in "Hello\tWorld" to produce "hello-world" @test

  • It handles newline characters in "Hello\nWorld" to produce "hello-world" @test

Special Character Handling

  • It converts ampersand in "Bread & Butter" to produce "bread-and-butter" @test

  • It removes question marks in "What's New?" to produce "whats-new" @test

  • It handles heart emoji in "I ♥ Code" to produce "i-love-code" @test

  • It removes dollar signs in "Price $100" to produce "price-100" @test

Custom Options

  • It uses custom separator when separator option is "_" for "Hello World" to produce "hello_world" @test

  • It preserves trailing dash when preserveTrailingDash is true for "Hello-" to produce "hello-" @test

Implementation

@generates

API

/**
 * Sanitizes a blog post title into a URL-friendly identifier
 * @param {string} title - The blog post title to sanitize
 * @param {Object} options - Configuration options
 * @param {string} options.separator - Character to use as separator (default: '-')
 * @param {boolean} options.preserveTrailingDash - Whether to preserve trailing dash (default: false)
 * @param {string[]} options.preserveCharacters - Array of characters to preserve (default: [])
 * @returns {string} The sanitized title
 */
export function sanitizeTitle(title, options = {}) {
  // Implementation here
}

Dependencies { .dependencies }

@sindresorhus/slugify { .dependency }

Provides string slugification with Unicode support and customization options.

Install with Tessl CLI

npx tessl i tessl/npm-sindresorhus--slugify

tile.json