1wkk / type-challenges

Collection of TypeScript type challenges with online judge
https://tsch.js.org/
MIT License
0 stars 0 forks source link

110 - Capitalize #28

Open 1wkk opened 1 year ago

1wkk commented 1 year ago
type MyUppercase<T> = T extends 'a'
  ? 'A'
  : T extends 'b'
  ? 'B'
  : T extends 'c'
  ? 'C'
  : T extends 'd'
  ? 'D'
  : T extends 'e'
  ? 'E'
  : T extends 'f'
  ? 'F'
  : T extends 'g'
  ? 'G'
  : T extends 'h'
  ? 'H'
  : T extends 'i'
  ? 'I'
  : T extends 'j'
  ? 'J'
  : T extends 'k'
  ? 'K'
  : T extends 'l'
  ? 'L'
  : T extends 'm'
  ? 'M'
  : T extends 'n'
  ? 'N'
  : T extends 'o'
  ? 'O'
  : T extends 'p'
  ? 'P'
  : T extends 'q'
  ? 'Q'
  : T extends 'r'
  ? 'R'
  : T extends 's'
  ? 'S'
  : T extends 't'
  ? 'T'
  : T extends 'u'
  ? 'U'
  : T extends 'v'
  ? 'V'
  : T extends 'w'
  ? 'W'
  : T extends 'x'
  ? 'X'
  : T extends 'y'
  ? 'Y'
  : T extends 'z'
  ? 'Z'
  : T

type MyCapitalize<S extends string> = S extends `${infer F}${infer R}`
  ? `${MyUppercase<F>}${R}`
  : S

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<MyCapitalize<'foobar'>, 'Foobar'>>,
  Expect<Equal<MyCapitalize<'FOOBAR'>, 'FOOBAR'>>,
  Expect<Equal<MyCapitalize<'foo bar'>, 'Foo bar'>>,
  Expect<Equal<MyCapitalize<''>, ''>>,
  Expect<Equal<MyCapitalize<'a'>, 'A'>>,
  Expect<Equal<MyCapitalize<'b'>, 'B'>>,
  Expect<Equal<MyCapitalize<'c'>, 'C'>>,
  Expect<Equal<MyCapitalize<'d'>, 'D'>>,
  Expect<Equal<MyCapitalize<'e'>, 'E'>>,
  Expect<Equal<MyCapitalize<'f'>, 'F'>>,
  Expect<Equal<MyCapitalize<'g'>, 'G'>>,
  Expect<Equal<MyCapitalize<'h'>, 'H'>>,
  Expect<Equal<MyCapitalize<'i'>, 'I'>>,
  Expect<Equal<MyCapitalize<'j'>, 'J'>>,
  Expect<Equal<MyCapitalize<'k'>, 'K'>>,
  Expect<Equal<MyCapitalize<'l'>, 'L'>>,
  Expect<Equal<MyCapitalize<'m'>, 'M'>>,
  Expect<Equal<MyCapitalize<'n'>, 'N'>>,
  Expect<Equal<MyCapitalize<'o'>, 'O'>>,
  Expect<Equal<MyCapitalize<'p'>, 'P'>>,
  Expect<Equal<MyCapitalize<'q'>, 'Q'>>,
  Expect<Equal<MyCapitalize<'r'>, 'R'>>,
  Expect<Equal<MyCapitalize<'s'>, 'S'>>,
  Expect<Equal<MyCapitalize<'t'>, 'T'>>,
  Expect<Equal<MyCapitalize<'u'>, 'U'>>,
  Expect<Equal<MyCapitalize<'v'>, 'V'>>,
  Expect<Equal<MyCapitalize<'w'>, 'W'>>,
  Expect<Equal<MyCapitalize<'x'>, 'X'>>,
  Expect<Equal<MyCapitalize<'y'>, 'Y'>>,
  Expect<Equal<MyCapitalize<'z'>, 'Z'>>
]
1wkk commented 1 year ago
type Alphabet = {
  a: 'A'
  b: 'B'
  c: 'C'
  d: 'D'
  e: 'E'
  f: 'F'
  g: 'G'
  h: 'H'
  i: 'I'
  j: 'J'
  k: 'K'
  l: 'L'
  m: 'M'
  n: 'N'
  o: 'O'
  p: 'P'
  q: 'Q'
  r: 'R'
  s: 'S'
  t: 'T'
  u: 'U'
  v: 'V'
  w: 'W'
  x: 'X'
  y: 'Y'
  z: 'Z'
}

type MyUppercase<T> = T extends keyof Alphabet ? Alphabet[T] : T