PercentageParser
介绍
实现类型 PercentageParser<T extends string>
。根据规则 /^(\+|\-)?(\d*)?(\%)?$/
匹配类型 T。
匹配的结果由三部分组成,分别是:[正负号
, 数字
, 单位
],如果没有匹配,则默认是空字符串。
例如:
ts
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", ""]
View on GitHubts
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", ""]
起点
ts
/* _____________ Your Code Here _____________ */typePercentageParser <A extends string> = any/* _____________ Test Cases _____________ */typeCase0 = ['', '', '']typeCase1 = ['+', '', '']typeCase2 = ['+', '1', '']typeCase3 = ['+', '100', '']typeCase4 = ['+', '100', '%']typeCase5 = ['', '100', '%']typeCase6 = ['-', '100', '%']typeCase7 = ['-', '100', '']typeCase8 = ['-', '1', '']typeCase9 = ['', '', '%']typeCase10 = ['', '1', '']typeCase11 = ['', '100', '']typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <''>,Case0 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+'>,Case1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+1'>,Case2 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+100'>,Case3 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+100%'>,Case4 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'100%'>,Case5 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-100%'>,Case6 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-100'>,Case7 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-1'>,Case8 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'%'>,Case9 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'1'>,Case10 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'100'>,Case11 >>,]
take the challengets
/* _____________ Your Code Here _____________ */typePercentageParser <A extends string> = any/* _____________ Test Cases _____________ */typeCase0 = ['', '', '']typeCase1 = ['+', '', '']typeCase2 = ['+', '1', '']typeCase3 = ['+', '100', '']typeCase4 = ['+', '100', '%']typeCase5 = ['', '100', '%']typeCase6 = ['-', '100', '%']typeCase7 = ['-', '100', '']typeCase8 = ['-', '1', '']typeCase9 = ['', '', '%']typeCase10 = ['', '1', '']typeCase11 = ['', '100', '']typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <''>,Case0 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+'>,Case1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+1'>,Case2 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+100'>,Case3 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'+100%'>,Case4 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'100%'>,Case5 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-100%'>,Case6 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-100'>,Case7 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'-1'>,Case8 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'%'>,Case9 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'1'>,Case10 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <PercentageParser <'100'>,Case11 >>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypePercentageParser <A extends string> = any
ts
// most populartypePercentageParser <A extends string> = any