@azizbecha/strkit
Version:
strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.
19 lines • 561 B
JavaScript
/**
* Converts a string to a URL-friendly slug.
*
* @param str - The string to convert to slug
* @returns A URL-friendly slug string
*
* @example
* toSlug('Hello World!'); // "hello-world"
* toSlug('This is a TEST'); // "this-is-a-test"
*/
export default function toSlug(str) {
return str
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '') // Remove special characters
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/-+/g, '-'); // Replace multiple - with single -
}
//# sourceMappingURL=toSlug.js.map