slug.mjs
18 lines and 1 functions in DrDustinEdwards/prelum, at commit f7e03af on 2026-10-25. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| slugify | 10 to 18 | 2 | 100% | OkCRAP 2 | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. Imported by index.mjs.
Source
/**
* One slug rule for every site: accents folded to their base letter (café → cafe), every run of anything else that is
* not a-z or 0-9 made one hyphen, no hyphen at either end, and at most `max` characters, cut back to a whole word when it
* can be. The empty string when nothing is left, so a caller with a fallback writes `slugify(x) || "entry"`.
*
* @param {string} text
* @param {{ max?: number }} [options]
* @returns {string}
*/
export function slugify(text, { max = Infinity } = {}) {
const slug = String(text)
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
return slug.length <= max ? slug : slug.slice(0, max).replace(/-+$/, "");
}