Skip to content

Instantly share code, notes, and snippets.

@loucyx
Last active April 18, 2024 23:54
Show Gist options
  • Save loucyx/c408344d4ab63d05eb1d908dc018d7fb to your computer and use it in GitHub Desktop.
Save loucyx/c408344d4ab63d05eb1d908dc018d7fb to your computer and use it in GitHub Desktop.
Sponge Bob casing util
/** Takes a string and returns it's upper case */
export const upperCase = <const Source extends string>(source: Source) =>
source.toLocaleUpperCase() as Uppercase<Source>;
/** Takes a string and returns it's lower case */
export const lowerCase = <const Source extends string>(source: Source) =>
source.toLocaleLowerCase() as Lowercase<Source>;
/** Takes a `from` and a `to` and returns a random integer. */
export const randomNumber = (from: number) => (to: number) =>
Math.round(Math.random() * Math.abs(to - from)) + Math.min(from, to);
/** Gets a random index from the given array. */
export const randomIndex = <Item>(source: ReadonlyArray<Item>) =>
randomNumber(0)(source.length - 1);
/** Runs a random function of the given list of functions */
export const randomFunction =
<Source, Output>(functions: ReadonlyArray<(source: Source) => Output>) =>
(source: Source) =>
functions[randomIndex(functions)](source);
/** Runs a function in every character of a string */
export const stringMap =
(mapper: (item: string) => string) => (source: string) =>
[...source].map(mapper).join("");
/** SPOnGE BoB cAse a StRInG */
export const spongeBobCase = stringMap(randomFunction([upperCase, lowerCase]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment