Percentage Parser
提出詳細
type Sign = "+" | "-" type Percent = "%" type ParseSign<A extends string> = A extends `${infer Head}${infer Tail}` ? Head extends Sign ? [Head,...ParsePercent<Tail>] : ["",...ParsePercent<A>] : ["","",""] // whole string is empty1 type ParsePercent<B> = B extends `${infer N extends number}` ? [`${N}`, ""] : B extends `${infer N extends number}${Percent}` ? [`${N}`, Percent] // special cases : B extends "" ? ["",""] : B extends Percent ? ["","%"] : never type PercentageParser<A extends string> = ParseSign<A>
提出日時 | 2023-09-15 01:36:29 |
---|---|
問題 | Percentage Parser |
ユーザー | sankantsu |
ステータス | Accepted |
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>>, ]