diff --git a/utils/case.ts b/utils/case.ts index 7308165..9c77efd 100644 --- a/utils/case.ts +++ b/utils/case.ts @@ -1,26 +1,35 @@ +/** + * Capitalize a string + * @param string - String to capitalize + * @param lowercase - Whether to lowercase rest of the string + */ export const capitalize = (string: string, lowercase = false) => lowercase ? string.charAt(0).toUpperCase() + string.slice(1).toLocaleLowerCase() : string.charAt(0).toUpperCase() + string.slice(1); /** - * - * @param str + * Change case of a string to camel case + * @param string - String to change case of * @source - https://stackoverflow.com/a/2970667/1656944 */ -export const camelCase = (str: string) => - str.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => +export const camelCase = (string: string) => + string.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index == 0 ? word.toLowerCase() : word.toUpperCase() ).replace(/\s+/g, ""); /** - * - * @param string + * Change case of a string to kebab case + * @param string - String to change case of * @source - https://gist.github.com/thevangelist/8ff91bac947018c9f3bfaad6487fa149#gistcomment-2183914 */ export const kebabCase = (string: string) => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").toLowerCase(); +/** + * Change case of a string to snake case + * @param string - String to change case of + */ export const snakeCase = (string: string) => kebabCase(string).replace(/-/g, "_"); diff --git a/utils/slugify.ts b/utils/slugify.ts index a8b9580..6d472ff 100644 --- a/utils/slugify.ts +++ b/utils/slugify.ts @@ -1,5 +1,10 @@ import { trim } from "./trim"; +/** + * Convert a string to slug (for URLs) + * @param string - String to slugify + * @param slugSpecialCharacters - Whether to change special characters to hyphen + */ export const slugify = (string: string, slugSpecialCharacters = true) => trim(slugSpecialCharacters ? string.toLowerCase().replace(/[^\u0100-\uFFFF\w\-]/g, "-").replace(/\-\-+/g, "-") :