UNPKG

@gravityforms/utils

Version:
31 lines (30 loc) 938 B
/** * @module slugify * @description Converts a string to a slug. * * @since 1.1.17 * * @param {string} text The text to slugify. * * @return {string} Returns the slugified string. * * @example * import { slugify } from "@gravityforms/utils"; * * function Example() { * const str = 'my Mixed *** #string'; * const slug = slugify( str ); * } * */ export default function slugify( text = '' ) { return text .toString() // Cast to string (optional) .normalize( 'NFKD' ) // The normalize() using NFKD method returns the Unicode Normalization Form of a given string. .toLowerCase() // Convert the string to lowercase letters .trim() // Remove whitespace from both sides of a string (optional) .replace( /\s+/g, '-' ) // Replace spaces with - .replace( /[^\w-]+/g, '' ) // Remove all non-word chars .replace( /--+/g, '-' ) // Replace multiple - with single - .replace( /-$/g, '' ); // Remove trailing - }