Skip to content

Instantly share code, notes, and snippets.

@fgalassi
Created July 20, 2024 20:14
Show Gist options
  • Select an option

  • Save fgalassi/d4797738468292b7e1b9ba76ec57d623 to your computer and use it in GitHub Desktop.

Select an option

Save fgalassi/d4797738468292b7e1b9ba76ec57d623 to your computer and use it in GitHub Desktop.
// This is tricky because you cannot do Str extends `${infer Before}${UnionOfSep}${infer After}` ...
type Split<Str, UnionOfSep extends string, CurrentWord extends string = "", Output extends string[] = []> =
Str extends `${infer First}${infer Rest}`
? First extends UnionOfSep
? Split<Rest, UnionOfSep, "", [...Output, CurrentWord]>
: Split<Rest, UnionOfSep, `${CurrentWord}${First}`, Output>
: [...Output, CurrentWord];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment