Skip to content

Instantly share code, notes, and snippets.

@ljbc1994
Created May 7, 2022 15:37
Show Gist options
  • Save ljbc1994/36be98c2e4fe87baf6a2c18f7987d180 to your computer and use it in GitHub Desktop.
Save ljbc1994/36be98c2e4fe87baf6a2c18f7987d180 to your computer and use it in GitHub Desktop.
If you need a spongebob type
type CharMap<T extends string = 'aA-bB-cC-dD-eE-fF-gG-hH-iI-jJ-kK-lL-mM-nN-oO-pP-qQ-rR-sS-tT-uU-vV-wW-xX-yY-zZ'> =
T extends `${infer Lower}-${infer Upper}`
? Lower | CharMap<Upper>
: T
type CharSplit<T extends string> = T extends `${infer Lower}${infer Upper}` ? [Lower, Upper] : T
type CharObj = { [T in CharMap as CharSplit<T>[0]]: CharSplit<T>[1] };
type GetUpperCase<TChar extends keyof CharObj> = CharObj[TChar]
type Spongebobify<TPhrase extends string, TResult extends string = '', TFlip = 0> =
TPhrase extends `${infer Head}${infer Tail}`
? Head extends keyof CharObj
? Spongebobify<Tail, `${TResult}${TFlip extends 1 ? GetUpperCase<Head> : Head}`, TFlip extends 1 ? 0 : 1>
: Spongebobify<Tail, `${TResult}${Head}`, TFlip>
: TResult
type Test = Spongebobify<'my name is mary celeste'>
// type Test = "mY nAmE iS mArY cElEsTe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment