首页 > 其他分享 >[Typescript] 43. Medium - Percentage Parser

[Typescript] 43. Medium - Percentage Parser

时间:2022-10-06 02:22:12浏览次数:45  
标签:Typescript 43 PercentageParser extends Expect infer Percentage type 100

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>>,
]

 

标签:Typescript,43,PercentageParser,extends,Expect,infer,Percentage,type,100
From: https://www.cnblogs.com/Answer1215/p/16756925.html

相关文章

  • P4303 [AHOI2006]基因匹配
    初始方程为:\[f_{i,j}=\max(f_{i-1,j-1}+1,f_{i-1,j},f_{i,j-1})\]对于每一个\(i\)来说,只有五次由\(f_{i-1,j-1}\)来转移(组成DNA序列的每一种碱基在该序列中正好出......
  • TypeScript中typeof的简单介绍
    简单介绍typeof我们都知道js提供了typeof,用来获取基本数据的类型。实际上,TS也提供了typeof操作符。可以在【类型上下文】中进行类型查询。只能够进行变量或者属性查......
  • 343.integer-break 整数拆分
    题目描述343.整数拆分解题思路还是寻找递推关系,设\(dp_n\)为正整数\(n\)所求的最大乘积。这里可以注意到:\(n>4\)时,\(dp_n=\max\[dp_{n-3}*3,\dp_{n-4}*......
  • [Typescript] 42. Medium - Remove Index Signature
    Implement RemoveIndexSignature<T> ,excludetheindexsignaturefromobjecttypes.Forexample:typeFoo={[key:string]:any;foo():void;}typeA=......
  • 线性DP-2430. 对字母串可执行的最大删除数
    问题描述给你一个仅由小写英文字母组成的字符串s。在一步操作中,你可以:删除整个字符串s,或者对于满足 1<=i<=s.length/2的任意i,如果s中的前i个字母和......
  • 43rd 2022/10/4 模拟赛总结30
    这次还行?rank5,其实也不是多高不可攀,就是认真打,暑假时就上过前五好多次其实比赛历程也很简单第一题很忽悠,思路乱的一批,但是这次冷静下来把思路理清就切了很简单的概率D......
  • TypeScript(3)基础类型
    基础类型​​TypeScript​​​支持与​​JavaScript​​​几乎相同的数据类型,此外还提供了实用的枚举类型方便我们使用。 布尔值最基本的数据类型就是简单的true/false......
  • P5431 【模板】乘法逆元 2
    1#include<bits/stdc++.h>2usingnamespacestd;3typedeflonglongll;4constintN=5e6+10;5llfac[N],sv[N],inv[N],a[N];6lln,p,k;7v......
  • React中常见的TypeScript定义实战
    一引沿Fiber架构是React16中引入的新概念,目的就是解决大型React应用卡顿,React在遍历更新每一个节点的时候都不是用的真实DOM,都是采用虚拟DOM,所以可以理解成fiber就是R......
  • [Typescript] Tips: Create your own 'objectKeys' function using generics and the
    TheloosenessofObject.keyscanbearealpainpointwhenusingTypeScript.Luckily,it'sprettysimpletocreateatighterversionusinggenericsandthekey......