Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type-challenges solution ( extreme ) #44

Open
jiangshanmeta opened this issue Sep 26, 2021 · 0 comments
Open

type-challenges solution ( extreme ) #44

jiangshanmeta opened this issue Sep 26, 2021 · 0 comments

Comments

@jiangshanmeta
Copy link
Owner

jiangshanmeta commented Sep 26, 2021

type-challenges

原项目地址

216・Slice

type Repeat<T,C extends number,R extends T[] = []> = R['length'] extends C?R:Repeat<T,C,[...R,T]>

type MakePositive<
  N extends number,
  L extends any[],
  C extends any[] = []
> = 

`${N}` extends `-${string}`?
  L extends [any,...infer R]?
    `-${C['length']}` extends `${N}`? L['length']: MakePositive<N,R,[1,...C]>
    : 0
  : N


type SliceNormal<
  Arr extends any[],
  Start extends number,
  End extends number,
  Pre extends any[] = [],
  R extends any[] = []
> = 
Arr extends [infer F,...infer L]?
  Pre['length'] extends End? R
    : Pre['length'] extends Start? SliceNormal<L,Start,End,[...Pre,F],[F]>
      : R extends []? SliceNormal<L,Start,End,[...Pre,F],[]>: SliceNormal<L,Start,End,[...Pre,F],[...R,F]>
  : R


type Slice<
  Arr extends any[], 
  Start extends number = 0, 
  End extends number = Arr['length']
> = SliceNormal<
      Arr,
      MakePositive<Start,Repeat<0,Arr['length']>>,
      MakePositive<End,Repeat<0,Arr['length']>> 
    >

274・Integers Comparator

enum Comparison {
  Greater,
  Equal,
  Lower,
}

type Make10<T extends any[]> = [
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
]

type Make1<S extends string,T extends any[] = []> = `${T['length']}` extends S?T:Make1<S,[0,...T]>

type ToTuple<S extends string,T extends any[] = []> = S extends `${infer F}${infer R}`?ToTuple<R,[...Make10<T>,...Make1<F>]>:T

type ToNumber<S extends string> = ToTuple<S>['length']

type LargerThan<
  A extends number, 
  B extends number,
  AList extends number[] = [],
  BList extends number[] = [],
> = AList['length'] extends A?
      false:
      BList['length'] extends B?
        true: LargerThan<A,B,[1,...AList],[1,...BList]>

type Comparator<A extends number, B extends number> = A extends B?
  Comparison.Equal:
  `${A}` extends `-${infer A1}`?
    `${B}` extends `-${infer B1}`?
      LargerThan<ToNumber<A1>,ToNumber<B1>> extends true?Comparison.Lower:Comparison.Greater
      :Comparison.Lower
  : `${B}` extends `-${infer B1}`?
      Comparison.Greater:LargerThan<A,B> extends true?Comparison.Greater:Comparison.Lower ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant