joobang/type-challenges

no - 4803 - Trim Right

Closed this issue · 0 comments

type trim = ' ' | '\n' | '\t' | '\r';

type TrimRight<S extends string> = S extends `${infer Rest}${trim}`
                                    ? TrimRight<Rest>
                                    : S;
/* _____________ Test Cases _____________ */                                        
import type { Equal, Expect } from '@type-challenges/utils'

type test =TrimRight<'str '>;

type cases = [
  Expect<Equal<TrimRight<'str'>, 'str'>>,
  Expect<Equal<TrimRight<'str '>, 'str'>>,
  Expect<Equal<TrimRight<'str     '>, 'str'>>,
  Expect<Equal<TrimRight<'     str     '>, '     str'>>,
  Expect<Equal<TrimRight<'   foo bar  \n\t '>, '   foo bar'>>,
  Expect<Equal<TrimRight<''>, ''>>,
  Expect<Equal<TrimRight<'\n\t '>, ''>>,
]