es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
19 lines (18 loc) • 608 B
JavaScript
/**
* Converts a string into a URL-friendly slug.
* @param {string} text - The text to convert into a slug.
* @returns {string} The slugified text.
* @example
* console.log(slugify('Hello World!')); // 'hello-world'
* console.log(slugify('This is a test')); // 'this-is-a-test'
* console.log(slugify('¡Hola Mundo!')); // 'hola-mundo'
* console.log(slugify(' Spaces ')); // 'spaces'
*/
export function slugify(text) {
return text
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/(^-|-$)/g, '');
}