Percentage Parser
提出詳細
type ParsePercentage<T extends string> = T extends '%' ? '%' : '' type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' type ParseDigits<T extends string, Sum extends string = ''> = T extends `${infer First}${infer Rest}` ? First extends Digit ? ParseDigits<Rest, `${Sum}${First}`> : [Sum, ParsePercentage<T>] : [Sum, ParsePercentage<T>] type PercentageParser<A extends string> = A extends `${infer First}${infer Rest}` ? First extends ('+' | '-') ? [First, ...ParseDigits<Rest>] : ['', ...ParseDigits<A>] : ['', '', '']
提出日時 | 2023-08-12 09:23:58 |
---|---|
問題 | Percentage Parser |
ユーザー | tekihei2317 |
ステータス | 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>>, ]