35 lines
953 B
JavaScript
35 lines
953 B
JavaScript
"use strict";
|
|
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, divider, allowTrailingDash) {
|
|
const finalSlugDivider = divider || "-";
|
|
try {
|
|
if (!str)
|
|
return "";
|
|
let finalStr = String(str)
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/ {2,}/g, " ")
|
|
.replace(/ /g, finalSlugDivider)
|
|
.replace(/[^a-z0-9]/g, finalSlugDivider)
|
|
.replace(/-{2,}|_{2,}/g, finalSlugDivider)
|
|
.replace(/^-/, "");
|
|
if (allowTrailingDash) {
|
|
return finalStr;
|
|
}
|
|
return finalStr.replace(/-$/, "");
|
|
}
|
|
catch (error) {
|
|
console.log(`Slugify ERROR: ${error.message}`);
|
|
return "";
|
|
}
|
|
}
|