Skip to content

Instantly share code, notes, and snippets.

@codemile
Created April 20, 2021 20:05
Show Gist options
  • Select an option

  • Save codemile/e02483ea77d782a041aaf48363d3fc3c to your computer and use it in GitHub Desktop.

Select an option

Save codemile/e02483ea77d782a041aaf48363d3fc3c to your computer and use it in GitHub Desktop.
Some handy string functions written in TypeScript
/**
* "document_id" -> "document id"
* "documentId" -> "document id"
*/
export const toSpaces = (str: string): string =>
str && str
.replace(/[-_]/g, ' ')
.replace(/([A-Z])/g, ' $1');
/**
* "Hello World" -> "hello_world"
*/
export const toSnakeCase = (str: string): string =>
str && str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('_');
/**
* "Hello World" -> "hello-world"
*/
export const toKebabCase = (str: string): string =>
str && str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('-');
/**
* "Hello World" -> "helloWorld"
*/
export const toCamelCase = (str: string): string =>
str && str
.toLowerCase()
.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr: string) => chr.toUpperCase());
/**
* "camelCase" -> "Camelcase"
* "Hello World" -> "Hello World"
* "Hello_world" -> "Hello_world"
*/
export const toPascalCase = (str: string): string =>
str && str
.replace(/\w\S*/g, m => m.charAt(0).toUpperCase() + m.substr(1).toLowerCase());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment