@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.
22 lines • 649 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = toSlug;
/**
* 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"
*/
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