2024-12-12 05:43:07 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* # Return the slug of a string
|
|
|
|
* @param {string} str input
|
|
|
|
* @returns {string} slug or empty string in case of error
|
|
|
|
* @example
|
|
|
|
* slugify("Hello World") // "hello-world"
|
|
|
|
* slugify("Yes!") // "yes"
|
|
|
|
* slugify("Hello!!! World!") // "hello-world"
|
|
|
|
*/
|
|
|
|
module.exports = function slugify(str) {
|
|
|
|
try {
|
|
|
|
return String(str)
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/ {2,}/g, " ")
|
|
|
|
.replace(/ /g, "-")
|
2024-12-12 06:19:48 +00:00
|
|
|
.replace(/[^a-z0-9]/g, "-")
|
|
|
|
.replace(/-{2,}/g, "-")
|
2024-12-12 05:43:07 +00:00
|
|
|
.replace(/^-/, "")
|
|
|
|
.replace(/-$/, "");
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
console.log(`Slugify ERROR: ${error.message}`);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|