-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathsum.d.ts
78 lines (67 loc) · 2.72 KB
/
sum.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type {NumberAbsolute, BuildTuple, TupleMax, ReverseSign} from './internal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {Subtract} from './subtract';
/**
Returns the sum of two numbers.
Note:
- A or B can only support `-999` ~ `999`.
@example
```
import type {Sum} from 'type-fest';
Sum<111, 222>;
//=> 333
Sum<-111, 222>;
//=> 111
Sum<111, -222>;
//=> -111
Sum<PositiveInfinity, -9999>;
//=> PositiveInfinity
Sum<PositiveInfinity, NegativeInfinity>;
//=> number
```
@category Numeric
*/
// TODO: Support big integer.
export type Sum<A extends number, B extends number> =
// Handle cases when A or B is the actual "number" type
number extends A | B ? number
// Handle cases when A and B are both +/- infinity
: A extends B & (PositiveInfinity | NegativeInfinity) ? A // A or B could be used here as they are equal
// Handle cases when A and B are opposite infinities
: A | B extends PositiveInfinity | NegativeInfinity ? number
// Handle cases when A is +/- infinity
: A extends PositiveInfinity | NegativeInfinity ? A
// Handle cases when B is +/- infinity
: B extends PositiveInfinity | NegativeInfinity ? B
// Handle cases when A or B is 0 or it's the same number with different signs
: A extends 0 ? B : B extends 0 ? A : A extends ReverseSign<B> ? 0
// Handle remaining regular cases
: SumPostChecks<A, B>;
/**
Adds two numbers A and B, such that they are not equal with different signs and neither of them are 0, +/- infinity or the `number` type
*/
type SumPostChecks<A extends number, B extends number, AreNegative = [IsNegative<A>, IsNegative<B>]> =
AreNegative extends [false, false]
// When both numbers are positive we can add them together
? SumPositives<A, B>
: AreNegative extends [true, true]
// When both numbers are negative we add the absolute values and then reverse the sign
? ReverseSign<SumPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
// When the signs are different we can subtract the absolute values, remove the sign
// and then reverse the sign if the larger absolute value is negative
: NumberAbsolute<Subtract<NumberAbsolute<A>, NumberAbsolute<B>>> extends infer Result extends number
? TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_ extends number
? Max_ extends A | B
// The larger absolute value is positive, so the result is positive
? Result
// The larger absolute value is negative, so the result is negative
: ReverseSign<Result>
: never
: never;
/**
Adds two positive numbers.
*/
type SumPositives<A extends number, B extends number> =
[...BuildTuple<A>, ...BuildTuple<B>]['length'] extends infer Result extends number
? Result
: never;