Last active
April 18, 2024 23:54
-
-
Save loucyx/c408344d4ab63d05eb1d908dc018d7fb to your computer and use it in GitHub Desktop.
Sponge Bob casing util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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