[Typescript] 43. Medium - Percentage Parser
typescript 43 Medium parser
2023-09-14 08:59:12 时间
Implement PercentageParser. According to the /^(\+|\-)?(\d*)?(\%)?$/
regularity to match T and get three matches.
The structure should be: [plus or minus
, number
, unit
] 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简介
- [Typescript] Function scope in typescript
- [Typescript] Making TypeScript Stick - 5 - Extends
- [Typescript] Intersections types
- [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS
- [TypeScript] Generic Functions, class, Type Inference and Generics
- [Angular] Short Imports with TypeScript Path Mapping
- [TS] Implement a singly linked list in TypeScript
- [TypeScript] Function Overloads in Typescript
- [Typescript] 136. Medium - NonEmptyArray
- [Typescript] 98. Medium - Append to object
- [Typescript] Function scope in typescript
- [Typescript] 68. Medium - Fill
- [Typescript] Convert a js project to typescript
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc
- [Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [TypeScript] Collect Related Strings in a String Enum in TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- SAP Spartacus TypeScript和编译后的JavaScript命名规范
- SpringBoot学习笔记(八)——JWT、(Vue3、Axios、Vue-Router、TypeScript实现授权与验证示例)
- TypeScript学习笔记(四)—— TypeScript提高