UNPKG

@churchapps/apphelper

Version:

Library of helper functions for React and NextJS ChurchApps

33 lines 1.38 kB
import slug from "slug"; export class SlugHelper { static slugifyString(string, type, removeCharacters) { const charactersToRemove = removeCharacters ? removeCharacters : ["for", "and", "nor", "but", "or", "yet", "so", "the", "a", "an"]; const characStr = charactersToRemove.join("|"); if (type === "urlPath") { slug.extend({ '/': '/' }); //To keep '/' in the url since it's a special character. } const initialSlug = slug(string, { remove: new RegExp('\\b(' + characStr + ')\\b', 'gi') }); const verfiedSlug = this.numerifySlug(initialSlug); return verfiedSlug; } //remove multiple numbers in sequence static numerifySlug(slug) { let initialString = slug; const regex = /\d+(?:-\d+)+|\d+/g; const matchedArray = initialString.match(regex); if (matchedArray) { matchedArray.forEach((data) => { const length = data.length; let splitResult = data; if (length > 1) { const array = data.split(""); splitResult = array[0]; } const replacedString = initialString.replace(data, splitResult); initialString = replacedString; }); } return initialString; } } //# sourceMappingURL=SlugHelper.js.map