UNPKG

handoff-app

Version:

Automated documentation toolchain for building client side documentation from figma

28 lines (27 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.slugify = void 0; /** * Converts a string into a slugified version for use in URLs, IDs, or anchors. * * The slugify function transforms the input string as follows: * - Converts all characters to lowercase. * - Trims whitespace from the start and end. * - Replaces all characters that are not alphanumeric, underscores, spaces, hyphens, or dollar signs with a hyphen. * - Converts runs of consecutive spaces, underscores, or hyphens to a single hyphen. * - Removes leading and trailing hyphens. * * @param str - The input string to be slugified. * @returns A slugified version of the input string. * * @example * slugify("Hello, World!") // returns "hello-world" * slugify(" Some $pecial_VAR ") // returns "some-$pecial-var" */ const slugify = (str) => str .toLowerCase() .trim() .replace(/[^\$\w\s-]/g, "-") .replace(/[\s_-]+/g, "-") .replace(/^-+|-+$/g, ""); exports.slugify = slugify;