import type {UnknownArray} from '../unknown-array.d.ts'; import type {TupleOf} from '../tuple-of.d.ts'; import type {StringReverse} from '../string-reverse.d.ts'; /** Returns the sum of two non-negative integers. It adds digit by digit, so it is not limited by the maximum length of a tuple. It is exact while both numbers are within `Number.MAX_SAFE_INTEGER`, and returns `number` for a result that is too large to be represented exactly. @example ``` type A = NonNegativeIntegerSum<999_999, 1>; //=> 1000000 ``` */ export type NonNegativeIntegerSum = ToNumber, StringReverse<`${B}`>>>; /** Returns the difference of two non-negative integers, where `A` is not less than `B`. It subtracts digit by digit, so it is not limited by the maximum length of a tuple. It is exact while both numbers are within `Number.MAX_SAFE_INTEGER`, and returns `number` for a result that is too large to be represented exactly. @example ``` type A = NonNegativeIntegerDifference<1_000_000, 1>; //=> 999999 ``` */ export type NonNegativeIntegerDifference = ToNumber, StringReverse<`${B}`>>>>; // A string that is not exactly a number, like one with leading zeros or one too large to be represented exactly, becomes `number`. // This differs from the public `StringToNumber`, which returns `never` for such a string. `Sum` and `Subtract` need `number` here, because a result that is too large is still a number, only not an exact one. type ToNumber = S extends `${infer N extends number}` ? N : never; type TrimLeadingZeros = S extends `0${infer Rest}` ? Rest extends '' ? '0' : TrimLeadingZeros : S; // Splits off the least significant digit of the reversed digits, as a tuple of that length. When there are no digits left, the digit is `0`, so a shorter number is padded with zeros. type SplitDigit = S extends `${infer Digit extends number}${infer Rest}` ? [TupleOf, Rest] : [[], '']; type Ten = TupleOf<10>; // The digits are reversed, so they are added from the least significant one, and each digit of the result is put in front of the previous ones. type SumOfReversedDigits = `${A}${B}` extends '' ? `${Carry['length'] extends 0 ? '' : '1'}${Result}` : [SplitDigit, SplitDigit] extends [[infer DigitA extends UnknownArray, infer RestA extends string], [infer DigitB extends UnknownArray, infer RestB extends string]] ? [...DigitA, ...DigitB, ...Carry] extends [...Ten, ...infer Digit] ? SumOfReversedDigits : SumOfReversedDigits : never; // Should never happen // Like `SumOfReversedDigits`, but it borrows from the next digit. `A` is not less than `B`, so nothing is borrowed after the last digit. type DifferenceOfReversedDigits = A extends '' ? Result : [SplitDigit, SplitDigit] extends [[infer DigitA extends UnknownArray, infer RestA extends string], [infer DigitB extends UnknownArray, infer RestB extends string]] ? DigitA extends [...DigitB, ...Borrow, ...infer Digit] ? DifferenceOfReversedDigits : [...Ten, ...DigitA] extends [...DigitB, ...Borrow, ...infer Digit] ? DifferenceOfReversedDigits : never // Should never happen : never; // Should never happen export {};