zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] 115. Hard - Drop String

typescript string drop Hard 115
2023-09-14 09:00:44 时间

Drop the specified chars from a string.

For example:

type Butterfly = DropString<'foobar!', 'fb'> // 'ooar!'

 

/* _____________ Your Code Here _____________ */
type StringToUnion<S> = S extends `${infer F}${infer RT}` ? F | StringToUnion<RT>: never;
type DropString<S, R> = R extends '' ? S : S extends `${infer F}${infer RT}`
  ? F extends StringToUnion<R> ? DropString<RT, R>: `${F}${ DropString<RT, R>}` 
  : S;
  
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<DropString<'butter fly!', ''>, 'butter fly!'>>,
  Expect<Equal<DropString<'butter fly!', ' '>, 'butterfly!'>>,
  Expect<Equal<DropString<'butter fly!', 'but'>, 'er fly!'>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, '     e r f l y ! '>>,
  Expect<Equal<DropString<'    butter fly!        ', ' '>, 'butterfly!'>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, '     e r f l y ! '>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', 'tub'>, '     e r f l y ! '>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', 'b'>, '  u t t e r f l y ! '>>,
  Expect<Equal<DropString<' b u t t e r f l y ! ', 't'>, ' b u   e r f l y ! '>>,
]