• [Typescript] 43. Medium Percentage Parser


    Implement PercentageParser. According to the /^(\+|\-)?(\d*)?(\%)?$/ regularity to match T and get three matches.

    The structure should be: [plus or minusnumberunit] If it is not captured, the default is an empty string.

    For example:

    type PString1 = ''
    type PString2 = '+85%'
    type PString3 = '-85%'
    type PString4 = '85%'
    type PString5 = '85'
    
    type R1 = PercentageParser<PString1> // expected ['', '', '']
    type R2 = PercentageParser<PString2> // expected ["+", "85", "%"]
    type R3 = PercentageParser<PString3> // expected ["-", "85", "%"]
    type R4 = PercentageParser<PString4> // expected ["", "85", "%"]
    type R5 = PercentageParser<PString5> // expected ["", "85", ""]
    /* _____________ Your Code Here _____________ */
    
    type PercentageParser<A extends string> = A extends `+${infer N}%` 
      ? ['+', N, '%']
      : A extends `-${infer N}%`
        ? ['-', N, '%']
        : A extends `${infer N}%`
          ? ['', N, '%']
          : A extends `+${infer N}`
            ? ['+', N, '']
            : A extends `-${infer N}`
              ? ['-', N, '']
              : A extends `${infer N}`
                ? ['', N, '']
                : ['', '', '']
    
    type x = PercentageParser<'100%'>
    type t = '%' extends '00%' ? true: false
    /* _____________ Test Cases _____________ */
    import type { Equal, Expect } from '@type-challenges/utils'
    
    type Case0 = ['', '', '']
    type Case1 = ['+', '', '']
    type Case2 = ['+', '1', '']
    type Case3 = ['+', '100', '']
    type Case4 = ['+', '100', '%']
    type Case5 = ['', '100', '%']
    type Case6 = ['-', '100', '%']
    type Case7 = ['-', '100', '']
    type Case8 = ['-', '1', '']
    type Case9 = ['', '', '%']
    type Case10 = ['', '1', '']
    type Case11 = ['', '100', '']
    
    type cases = [
      Expect<Equal<PercentageParser<''>, Case0>>,
      Expect<Equal<PercentageParser<'+'>, Case1>>,
      Expect<Equal<PercentageParser<'+1'>, Case2>>,
      Expect<Equal<PercentageParser<'+100'>, Case3>>,
      Expect<Equal<PercentageParser<'+100%'>, Case4>>,
      Expect<Equal<PercentageParser<'100%'>, Case5>>,
      Expect<Equal<PercentageParser<'-100%'>, Case6>>,
      Expect<Equal<PercentageParser<'-100'>, Case7>>,
      Expect<Equal<PercentageParser<'-1'>, Case8>>,
      Expect<Equal<PercentageParser<'%'>, Case9>>,
      Expect<Equal<PercentageParser<'1'>, Case10>>,
      Expect<Equal<PercentageParser<'100'>, Case11>>,
    ]
  • 相关阅读:
    NLP(五)
    pyinstaller+wxpython+selinum
    C++ 动态库和静态库
    谷粒商城(三) 部署
    Centos使用KVM创建虚拟机
    C++指针
    C++异常处理
    C++流类库与输入/输出
    C++泛型程序设计及STL的结构
    selenium java maven testNg环境搭建
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16756925.html
Copyright © 2020-2023  润新知