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

274 - Integers Comparator #26784

Open
JohnLi1999 opened this issue Apr 24, 2023 · 0 comments
Open

274 - Integers Comparator #26784

JohnLi1999 opened this issue Apr 24, 2023 · 0 comments
Labels
274 answer Share answers/solutions to a question en in English

Comments

@JohnLi1999
Copy link

JohnLi1999 commented Apr 24, 2023

// Improved version
type CompareDigits<D1 extends string, D2 extends string> = D1 extends D2
  ? Comparison.Equal
  : '9876543210' extends `${string}${D1}${string}${D2}${string}`
  ? Comparison.Greater
  : Comparison.Lower;

type ComparePositiveIntegers<
  A extends string,
  B extends string,
  TempResult extends Comparison = Comparison.Equal
> = A extends `${infer AF}${infer AR}`
  ? B extends `${infer BF}${infer BR}`
    ? TempResult extends Comparison.Equal
      ? ComparePositiveIntegers<AR, BR, CompareDigits<AF, BF>>
      : ComparePositiveIntegers<AR, BR, TempResult>
    : Comparison.Greater
  : B extends `${infer _}${infer __}`
  ? Comparison.Lower
  : TempResult;

type Comparator<
  A extends number,
  B extends number
> = `${A}` extends `-${infer AbsA}`
  ? `${B}` extends `-${infer AbsB}`
    ? ComparePositiveIntegers<AbsB, AbsA>
    : Comparison.Lower
  : `${B}` extends `-${number}`
  ? Comparison.Greater
  : ComparePositiveIntegers<`${A}`, `${B}`>;

Reference 1
Reference 2

// My own version
type NumberToTuple<T, U extends 1[] = []> = U['length'] extends T
  ? U
  : NumberToTuple<T, [...U, 1]>;

type GreaterOrEqual<
  A extends number,
  B extends number
> = NumberToTuple<A> extends [...NumberToTuple<B>, ...infer _] ? true : false;

type StringLength<
  S extends string,
  T extends 1[] = []
> = S extends `${infer _}${infer R}` ? StringLength<R, [...T, 1]> : T['length'];

type EqualLengthStringComparator<
  A extends string,
  B extends string
> = A extends `${infer A1 extends number}${infer AR}`
  ? B extends `${infer B1 extends number}${infer BR}`
    ? A1 extends B1
      ? EqualLengthStringComparator<AR, BR>
      : GreaterOrEqual<A1, B1> extends true
      ? Comparison.Greater
      : Comparison.Lower
    : Comparison.Equal
  : Comparison.Equal;

type PositivesComparator<
  A extends number,
  B extends number,
  ALen extends number = StringLength<`${A}`>,
  BLen extends number = StringLength<`${B}`>
> = ALen extends BLen
  ? EqualLengthStringComparator<`${A}`, `${B}`>
  : GreaterOrEqual<ALen, BLen> extends true
  ? Comparison.Greater
  : Comparison.Lower;

type Comparator<A extends number, B extends number> = A extends B
  ? Comparison.Equal
  : `${A}` extends `-${infer T extends number}`
  ? `${B}` extends `-${infer U extends number}`
    ? PositivesComparator<U, T>
    : Comparison.Lower
  : `${B}` extends `-${number}`
  ? Comparison.Greater
  : PositivesComparator<A, B>;
@JohnLi1999 JohnLi1999 added answer Share answers/solutions to a question en in English labels Apr 24, 2023
@github-actions github-actions bot added the 274 label Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
274 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant