Slugify a string with comprehensive Unicode transliteration and extensive customization options
94
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.
Your module should export a function sanitizeTitle(title, options) that:
Handles various whitespace patterns:
Processes special characters appropriately:
Supports customization:
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"Create a test file sanitize.test.js with the following test cases:
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
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
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
@generates
/**
* 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
}Provides string slugification with Unicode support and customization options.
Install with Tessl CLI
npx tessl i tessl/npm-sindresorhus--slugifydocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10