Integers Comparator
提出詳細
enum Comparison { Greater, Equal, Lower, } type Negate<C extends Comparison> = C extends Comparison.Greater ? Comparison.Lower : C extends Comparison.Lower ? Comparison.Greater : Comparison.Equal type CompareDigit<A extends string | number, B extends string | number> = `${A}` extends `${B}` ? Comparison.Equal : `0123456789` extends `${string}${A}${string}${B}${string}` ? Comparison.Lower : Comparison.Greater type ComparePositives<A extends string | number, B extends string | number> = `${A}` extends `${infer A1}${infer RA}` ? `${B}` extends `${infer B1}${infer RB}` ? CompareDigit<A1, B1> extends Comparison.Equal ? ComparePositives<RA, RB> : CompareDigit<A1, B1> : Comparison.Greater : `${B}` extends `${infer _}${infer _}` ? Comparison.Lower : Comparison.Equal type ComparatorHelper<A extends string | number, B extends string | number, IsNegative extends boolean = false> = IsNegative extends true ? Negate<ComparePositives<A, B>> : ComparePositives<A, B> type Comparator<A extends number, B extends number> = `${A}` extends `-${infer Aabs}` ? `${B}` extends `-${infer Babs}` ? ComparatorHelper<Aabs, Babs, true> : Comparison.Lower : `${B}` extends `-${number}` ? Comparison.Greater : ComparatorHelper<A, B>
提出日時 | 2024-09-14 16:20:15 |
---|---|
問題 | Integers Comparator |
ユーザー | ookkoouu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Comparator<5, 5>, Comparison.Equal>>, Expect<Equal<Comparator<5, 6>, Comparison.Lower>>, Expect<Equal<Comparator<5, 8>, Comparison.Lower>>, Expect<Equal<Comparator<5, 0>, Comparison.Greater>>, Expect<Equal<Comparator<-5, 0>, Comparison.Lower>>, Expect<Equal<Comparator<0, 0>, Comparison.Equal>>, Expect<Equal<Comparator<0, -5>, Comparison.Greater>>, Expect<Equal<Comparator<5, -3>, Comparison.Greater>>, Expect<Equal<Comparator<5, -7>, Comparison.Greater>>, Expect<Equal<Comparator<-5, -7>, Comparison.Greater>>, Expect<Equal<Comparator<-5, -3>, Comparison.Lower>>, Expect<Equal<Comparator<-25, -30>, Comparison.Greater>>, Expect<Equal<Comparator<15, -23>, Comparison.Greater>>, Expect<Equal<Comparator<40, 37>, Comparison.Greater>>, Expect<Equal<Comparator<-36, 36>, Comparison.Lower>>, Expect<Equal<Comparator<27, 27>, Comparison.Equal>>, Expect<Equal<Comparator<-38, -38>, Comparison.Equal>>, ]