datasquirel/dist/package-shared/utils/slugify.js
Benjamin Toby a3561da53d Updates
2025-01-10 20:35:05 +01:00

30 lines
734 B
JavaScript

"use strict";
// @ts-check
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = slugify;
/**
* # Return the slug of a string
*
* @example
* slugify("Hello World") // "hello-world"
* slugify("Yes!") // "yes"
* slugify("Hello!!! World!") // "hello-world"
*/
function slugify(str) {
try {
return String(str)
.trim()
.toLowerCase()
.replace(/ {2,}/g, " ")
.replace(/ /g, "-")
.replace(/[^a-z0-9]/g, "-")
.replace(/-{2,}/g, "-")
.replace(/^-/, "")
.replace(/-$/, "");
}
catch ( /** @type {any} */error) {
console.log(`Slugify ERROR: ${error.message}`);
return "";
}
}