Skip to content

Commit

Permalink
🏷️ Add TSDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 16, 2020
1 parent 0074592 commit a777df9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions utils/case.ts
Original file line number Diff line number Diff line change
@@ -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, "_");
5 changes: 5 additions & 0 deletions utils/slugify.ts
Original file line number Diff line number Diff line change
@@ -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, "-") :
Expand Down

0 comments on commit a777df9

Please sign in to comment.