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

216 - Slice #3664

Open
akcyp opened this issue Sep 30, 2021 · 0 comments
Open

216 - Slice #3664

akcyp opened this issue Sep 30, 2021 · 0 comments
Labels
216 answer Share answers/solutions to a question en in English

Comments

@akcyp
Copy link

akcyp commented Sep 30, 2021

216 - Slice

Solution

Code

type DigitMap = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
type Digit = DigitMap[number];
type Abs<T extends number> = `${T}` extends `-${infer N}` ? DigitMap[N extends `${Digit}` ? N : never] : never;
type MarkAs<A, T> = A extends T ? A : never;

type NumberToArray<N extends number, R extends any[] = []> =
  R['length'] extends N ? R : NumberToArray<N, [any, ...R]>;
type Increment<A extends number> = MarkAs<[...NumberToArray<A>, any]['length'], number>;

type NegativeMod<
  A extends number,
  B extends number,
  R extends any[] = [],
  C extends any[] = NumberToArray<B>
> =
  R['length'] extends Abs<A> ?
    C['length'] :
    C extends [infer _, ...infer Y] ?
      NegativeMod<A, B, [...R, any], Y> : 0;

type ToPositive<A extends number, B extends number> = `${A}` extends `-${number}` ? NegativeMod<A, B> : A;

type SliceImpl<
  T extends any[],
  S extends number,
  E extends number,
  R extends any[] = [],
  I extends number = 0,
  B extends boolean = false
> =
  T[I] extends undefined ?
    B extends true ? R : [] :
    B extends true ?
      I extends E ? R : SliceImpl<T, S, E, [...R, T[I]], Increment<I>, true> :
      I extends E ? [] :
        I extends S ? SliceImpl<T, S, E, R, I, true> : SliceImpl<T, S, E, R, Increment<I>, false>;

type Slice<Arr extends any[], Start extends number = 0, End extends number = Arr['length']> =
  SliceImpl<
    Arr,
    ToPositive<Start, Arr['length']> extends number ? ToPositive<Start, Arr['length']> : never,
    ToPositive<End, Arr['length']> extends number ? ToPositive<End, Arr['length']> : never
  >;

Test cases

/* _____________ Test Cases _____________ */
import { Equal, Expect } from '@type-challenges/utils'

type Arr = [1, 2, 3, 4, 5]

type cases = [
  // basic
  Expect<Equal<Slice<Arr, 0, 1>, [1]>>,
  Expect<Equal<Slice<Arr, 0, 0>, []>>,
  Expect<Equal<Slice<Arr, 2, 4>, [3, 4]>>,

  // optional args
  Expect<Equal<Slice<[]>, []>>,
  Expect<Equal<Slice<Arr>, Arr>>,
  Expect<Equal<Slice<Arr, 0>, Arr>>,
  Expect<Equal<Slice<Arr, 2>, [3, 4, 5]>>,

  // negative index
  Expect<Equal<Slice<Arr, 0, -1>, [1,2,3,4]>>,
  Expect<Equal<Slice<Arr, -3, -1>, [3,4]>>,

  // invalid
  Expect<Equal<Slice<Arr, 10>, []>>,
  Expect<Equal<Slice<Arr, 1, 0>, []>>,
  Expect<Equal<Slice<Arr, 10, 20>, []>>,
]
@akcyp akcyp added answer Share answers/solutions to a question en in English labels Sep 30, 2021
@github-actions github-actions bot added the 216 label Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
216 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant