uom-types 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/functions-ho.cjs +6 -11
- package/dist/functions-ho.d.cts +17 -20
- package/dist/functions-ho.d.mts +17 -20
- package/dist/functions-ho.mjs +7 -11
- package/dist/functions.cjs +4 -4
- package/dist/functions.d.cts +33 -32
- package/dist/functions.d.mts +33 -32
- package/dist/functions.mjs +4 -4
- package/dist/index.d.cts +23 -9
- package/dist/index.d.mts +23 -9
- package/dist/si-units.d.cts +1 -1
- package/dist/si-units.d.mts +1 -1
- package/package.json +9 -4
package/dist/functions-ho.cjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Add a value by the given value.
|
|
5
5
|
*/
|
|
6
6
|
function add(a) {
|
|
7
|
-
return (b) => (
|
|
7
|
+
return (b) => (b + a);
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Subtract one value from the given value.
|
|
@@ -16,12 +16,14 @@ function sub(a) {
|
|
|
16
16
|
* Multiple a value by the given value.
|
|
17
17
|
*/
|
|
18
18
|
function mul(a) {
|
|
19
|
-
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Casting to actual type fails for some reason.
|
|
20
|
+
return (b) => (b * a);
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Divide one value by the given value.
|
|
23
24
|
*/
|
|
24
25
|
function div(a) {
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Casting to actual type fails for some reason.
|
|
25
27
|
return (b) => (b / a);
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
@@ -39,14 +41,8 @@ function modSafe(a) {
|
|
|
39
41
|
/**
|
|
40
42
|
* Put a number to the power of the given value.
|
|
41
43
|
*/
|
|
42
|
-
function pow(
|
|
43
|
-
return ((
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Take the square root of the given value.
|
|
47
|
-
*/
|
|
48
|
-
function sqrt() {
|
|
49
|
-
return pow(0.5);
|
|
44
|
+
function pow(exponent) {
|
|
45
|
+
return ((base) => base ** exponent);
|
|
50
46
|
}
|
|
51
47
|
/**
|
|
52
48
|
* Equal: Compare if a value is equal to the given value.
|
|
@@ -90,5 +86,4 @@ exports.mod = mod;
|
|
|
90
86
|
exports.modSafe = modSafe;
|
|
91
87
|
exports.mul = mul;
|
|
92
88
|
exports.pow = pow;
|
|
93
|
-
exports.sqrt = sqrt;
|
|
94
89
|
exports.sub = sub;
|
package/dist/functions-ho.d.cts
CHANGED
|
@@ -1,58 +1,55 @@
|
|
|
1
|
-
import { Divide, UnknownUnit, Inverse
|
|
1
|
+
import { Multiply, Divide, UnknownUnit, Inverse } from '#uom-types';
|
|
2
|
+
import { Decimal } from '#uom-types/si-units';
|
|
2
3
|
|
|
3
|
-
type
|
|
4
|
+
type OperationIO<T extends number> = T extends UnknownUnit ? T : number;
|
|
4
5
|
/**
|
|
5
6
|
* Add a value by the given value.
|
|
6
7
|
*/
|
|
7
|
-
declare function add<T extends number>(a: T): (b: T) =>
|
|
8
|
+
declare function add<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
8
9
|
/**
|
|
9
10
|
* Subtract one value from the given value.
|
|
10
11
|
*/
|
|
11
|
-
declare function sub<T extends number>(a: T): (b: T) =>
|
|
12
|
+
declare function sub<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
12
13
|
/**
|
|
13
14
|
* Multiple a value by the given value.
|
|
14
15
|
*/
|
|
15
|
-
declare function mul<
|
|
16
|
+
declare function mul<A extends number>(a: OperationIO<A>): <B extends number>(b: OperationIO<B>) => OperationIO<Multiply<B, A>>;
|
|
16
17
|
/**
|
|
17
18
|
* Divide one value by the given value.
|
|
18
19
|
*/
|
|
19
|
-
declare function div<
|
|
20
|
+
declare function div<A extends number>(a: OperationIO<A>): <B extends number>(b: OperationIO<B>) => OperationIO<Divide<B, A>>;
|
|
20
21
|
/**
|
|
21
22
|
* Modulo operator.
|
|
22
23
|
*/
|
|
23
|
-
declare function mod<T extends number>(a: T): (b: T) =>
|
|
24
|
+
declare function mod<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
24
25
|
/**
|
|
25
26
|
* Perform mathematic modular arithmetic.
|
|
26
27
|
*/
|
|
27
|
-
declare function modSafe<T extends number>(a: T): (b: T) =>
|
|
28
|
-
type PowFunction<
|
|
28
|
+
declare function modSafe<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
29
|
+
type PowFunction<E extends number, B extends number> = E extends -1 ? (b: OperationIO<B>) => OperationIO<Inverse<B>> : E extends 0 ? (b: OperationIO<B>) => Decimal | 1 : E extends 0.5 ? (b: OperationIO<B>) => OperationIO<Divide<B, 2>> : E extends 1 ? (b: OperationIO<B>) => OperationIO<B> : E extends 2 ? (b: OperationIO<B>) => OperationIO<Multiply<B, B>> : E extends 3 ? (b: OperationIO<B>) => OperationIO<Multiply<B, Multiply<B, B>>> : E extends 4 ? (b: OperationIO<B>) => OperationIO<Multiply<B, Multiply<B, Multiply<B, B>>>> : (b: OperationIO<B>) => OperationIO<number>;
|
|
29
30
|
/**
|
|
30
31
|
* Put a number to the power of the given value.
|
|
31
32
|
*/
|
|
32
|
-
declare function pow<
|
|
33
|
-
/**
|
|
34
|
-
* Take the square root of the given value.
|
|
35
|
-
*/
|
|
36
|
-
declare function sqrt<T extends number>(): (value: T) => OperationResult<Divide<T, 2>>;
|
|
33
|
+
declare function pow<E extends number, B extends number>(exponent: E extends UnknownUnit ? never : E): PowFunction<E, B>;
|
|
37
34
|
/**
|
|
38
35
|
* Equal: Compare if a value is equal to the given value.
|
|
39
36
|
*/
|
|
40
|
-
declare function eq<
|
|
37
|
+
declare function eq<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
41
38
|
/**
|
|
42
39
|
* Greater Than: Compare if a value is greater than the given value.
|
|
43
40
|
*/
|
|
44
|
-
declare function gt<
|
|
41
|
+
declare function gt<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
45
42
|
/**
|
|
46
43
|
* Greater Than or Equal: Compare if a value is greater than or equal to the given value.
|
|
47
44
|
*/
|
|
48
|
-
declare function gte<
|
|
45
|
+
declare function gte<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
49
46
|
/**
|
|
50
47
|
* Less Than: Compare if a value is less than the given value.
|
|
51
48
|
*/
|
|
52
|
-
declare function lt<
|
|
49
|
+
declare function lt<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
53
50
|
/**
|
|
54
51
|
* Less Than or Equal: Compare if a value is less than or equal to the given value.
|
|
55
52
|
*/
|
|
56
|
-
declare function lte<
|
|
53
|
+
declare function lte<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
57
54
|
|
|
58
|
-
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow,
|
|
55
|
+
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow, sub };
|
package/dist/functions-ho.d.mts
CHANGED
|
@@ -1,58 +1,55 @@
|
|
|
1
|
-
import { Divide, UnknownUnit, Inverse
|
|
1
|
+
import { Multiply, Divide, UnknownUnit, Inverse } from '#uom-types';
|
|
2
|
+
import { Decimal } from '#uom-types/si-units';
|
|
2
3
|
|
|
3
|
-
type
|
|
4
|
+
type OperationIO<T extends number> = T extends UnknownUnit ? T : number;
|
|
4
5
|
/**
|
|
5
6
|
* Add a value by the given value.
|
|
6
7
|
*/
|
|
7
|
-
declare function add<T extends number>(a: T): (b: T) =>
|
|
8
|
+
declare function add<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
8
9
|
/**
|
|
9
10
|
* Subtract one value from the given value.
|
|
10
11
|
*/
|
|
11
|
-
declare function sub<T extends number>(a: T): (b: T) =>
|
|
12
|
+
declare function sub<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
12
13
|
/**
|
|
13
14
|
* Multiple a value by the given value.
|
|
14
15
|
*/
|
|
15
|
-
declare function mul<
|
|
16
|
+
declare function mul<A extends number>(a: OperationIO<A>): <B extends number>(b: OperationIO<B>) => OperationIO<Multiply<B, A>>;
|
|
16
17
|
/**
|
|
17
18
|
* Divide one value by the given value.
|
|
18
19
|
*/
|
|
19
|
-
declare function div<
|
|
20
|
+
declare function div<A extends number>(a: OperationIO<A>): <B extends number>(b: OperationIO<B>) => OperationIO<Divide<B, A>>;
|
|
20
21
|
/**
|
|
21
22
|
* Modulo operator.
|
|
22
23
|
*/
|
|
23
|
-
declare function mod<T extends number>(a: T): (b: T) =>
|
|
24
|
+
declare function mod<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
24
25
|
/**
|
|
25
26
|
* Perform mathematic modular arithmetic.
|
|
26
27
|
*/
|
|
27
|
-
declare function modSafe<T extends number>(a: T): (b: T) =>
|
|
28
|
-
type PowFunction<
|
|
28
|
+
declare function modSafe<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => OperationIO<T>;
|
|
29
|
+
type PowFunction<E extends number, B extends number> = E extends -1 ? (b: OperationIO<B>) => OperationIO<Inverse<B>> : E extends 0 ? (b: OperationIO<B>) => Decimal | 1 : E extends 0.5 ? (b: OperationIO<B>) => OperationIO<Divide<B, 2>> : E extends 1 ? (b: OperationIO<B>) => OperationIO<B> : E extends 2 ? (b: OperationIO<B>) => OperationIO<Multiply<B, B>> : E extends 3 ? (b: OperationIO<B>) => OperationIO<Multiply<B, Multiply<B, B>>> : E extends 4 ? (b: OperationIO<B>) => OperationIO<Multiply<B, Multiply<B, Multiply<B, B>>>> : (b: OperationIO<B>) => OperationIO<number>;
|
|
29
30
|
/**
|
|
30
31
|
* Put a number to the power of the given value.
|
|
31
32
|
*/
|
|
32
|
-
declare function pow<
|
|
33
|
-
/**
|
|
34
|
-
* Take the square root of the given value.
|
|
35
|
-
*/
|
|
36
|
-
declare function sqrt<T extends number>(): (value: T) => OperationResult<Divide<T, 2>>;
|
|
33
|
+
declare function pow<E extends number, B extends number>(exponent: E extends UnknownUnit ? never : E): PowFunction<E, B>;
|
|
37
34
|
/**
|
|
38
35
|
* Equal: Compare if a value is equal to the given value.
|
|
39
36
|
*/
|
|
40
|
-
declare function eq<
|
|
37
|
+
declare function eq<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
41
38
|
/**
|
|
42
39
|
* Greater Than: Compare if a value is greater than the given value.
|
|
43
40
|
*/
|
|
44
|
-
declare function gt<
|
|
41
|
+
declare function gt<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
45
42
|
/**
|
|
46
43
|
* Greater Than or Equal: Compare if a value is greater than or equal to the given value.
|
|
47
44
|
*/
|
|
48
|
-
declare function gte<
|
|
45
|
+
declare function gte<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
49
46
|
/**
|
|
50
47
|
* Less Than: Compare if a value is less than the given value.
|
|
51
48
|
*/
|
|
52
|
-
declare function lt<
|
|
49
|
+
declare function lt<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
53
50
|
/**
|
|
54
51
|
* Less Than or Equal: Compare if a value is less than or equal to the given value.
|
|
55
52
|
*/
|
|
56
|
-
declare function lte<
|
|
53
|
+
declare function lte<T extends number>(a: OperationIO<T>): (b: OperationIO<T>) => boolean;
|
|
57
54
|
|
|
58
|
-
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow,
|
|
55
|
+
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow, sub };
|
package/dist/functions-ho.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Add a value by the given value.
|
|
3
3
|
*/
|
|
4
4
|
function add(a) {
|
|
5
|
-
return (b) => (
|
|
5
|
+
return (b) => (b + a);
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Subtract one value from the given value.
|
|
@@ -14,12 +14,14 @@ function sub(a) {
|
|
|
14
14
|
* Multiple a value by the given value.
|
|
15
15
|
*/
|
|
16
16
|
function mul(a) {
|
|
17
|
-
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Casting to actual type fails for some reason.
|
|
18
|
+
return (b) => (b * a);
|
|
18
19
|
}
|
|
19
20
|
/**
|
|
20
21
|
* Divide one value by the given value.
|
|
21
22
|
*/
|
|
22
23
|
function div(a) {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Casting to actual type fails for some reason.
|
|
23
25
|
return (b) => (b / a);
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
@@ -37,14 +39,8 @@ function modSafe(a) {
|
|
|
37
39
|
/**
|
|
38
40
|
* Put a number to the power of the given value.
|
|
39
41
|
*/
|
|
40
|
-
function pow(
|
|
41
|
-
return ((
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Take the square root of the given value.
|
|
45
|
-
*/
|
|
46
|
-
function sqrt() {
|
|
47
|
-
return pow(0.5);
|
|
42
|
+
function pow(exponent) {
|
|
43
|
+
return ((base) => base ** exponent);
|
|
48
44
|
}
|
|
49
45
|
/**
|
|
50
46
|
* Equal: Compare if a value is equal to the given value.
|
|
@@ -77,4 +73,4 @@ function lte(a) {
|
|
|
77
73
|
return (b) => b <= a;
|
|
78
74
|
}
|
|
79
75
|
|
|
80
|
-
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow,
|
|
76
|
+
export { add, div, eq, gt, gte, lt, lte, mod, modSafe, mul, pow, sub };
|
package/dist/functions.cjs
CHANGED
|
@@ -44,8 +44,8 @@ function mod(a, b) {
|
|
|
44
44
|
function modSafe(a, b) {
|
|
45
45
|
return (((a % b) + b) % b);
|
|
46
46
|
}
|
|
47
|
-
function pow(
|
|
48
|
-
return
|
|
47
|
+
function pow(base, exponent) {
|
|
48
|
+
return base ** exponent;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Take the square root of the given value.
|
|
@@ -105,13 +105,13 @@ function min(values) {
|
|
|
105
105
|
* Takes the sum of all the values in the given collection.
|
|
106
106
|
*/
|
|
107
107
|
function sum(values) {
|
|
108
|
-
return values.reduce(add, 0);
|
|
108
|
+
return values.reduce((add), 0);
|
|
109
109
|
}
|
|
110
110
|
/**
|
|
111
111
|
* Takes the product of all the values in the given collection.
|
|
112
112
|
*/
|
|
113
113
|
function product(values) {
|
|
114
|
-
return values.reduce(mul, 1);
|
|
114
|
+
return values.reduce((mul), 1);
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
117
|
* Equal: Compare if two values are equal.
|
package/dist/functions.d.cts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Multiply, Divide, Inverse, UnknownUnit } from '#uom-types';
|
|
2
|
+
import { Decimal } from '#uom-types/si-units';
|
|
2
3
|
|
|
3
|
-
type
|
|
4
|
+
type OperationIO<T extends number> = T extends UnknownUnit ? T : number;
|
|
4
5
|
/**
|
|
5
6
|
* Add two values together.
|
|
6
7
|
*/
|
|
7
|
-
declare function add<T extends number>(a: T
|
|
8
|
+
declare function add<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
8
9
|
/**
|
|
9
10
|
* Subtract one value from another.
|
|
10
11
|
*/
|
|
11
|
-
declare function sub<T extends number>(a: T
|
|
12
|
+
declare function sub<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
12
13
|
/**
|
|
13
14
|
* Multiple two values together.
|
|
14
15
|
*/
|
|
15
|
-
declare function mul<
|
|
16
|
+
declare function mul<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Multiply<A, B>>;
|
|
16
17
|
/**
|
|
17
18
|
* Divide one value by another.
|
|
18
19
|
*/
|
|
19
|
-
declare function div<
|
|
20
|
+
declare function div<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Divide<A, B>>;
|
|
20
21
|
/**
|
|
21
22
|
* Modulo operator.
|
|
22
23
|
*
|
|
@@ -24,7 +25,7 @@ declare function div<T extends number>(a: T, b: T): OperationResult<T>;
|
|
|
24
25
|
* @param b - Must be an integer.
|
|
25
26
|
* @returns `a % b`
|
|
26
27
|
*/
|
|
27
|
-
declare function mod<T extends number>(a: T
|
|
28
|
+
declare function mod<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
28
29
|
/**
|
|
29
30
|
* Perform mathematic modular arithmetic.
|
|
30
31
|
*
|
|
@@ -32,102 +33,102 @@ declare function mod<T extends number>(a: T, b: T): OperationResult<T>;
|
|
|
32
33
|
* @param b - Must be a positive integer.
|
|
33
34
|
* @returns An integer between zero (inclusive) and `b` (exclusive).
|
|
34
35
|
*/
|
|
35
|
-
declare function modSafe<T extends number>(a: T
|
|
36
|
+
declare function modSafe<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
36
37
|
/**
|
|
37
38
|
* Put a number to the power of -1.
|
|
38
39
|
*/
|
|
39
|
-
declare function pow<
|
|
40
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: -1): OperationIO<Inverse<B>>;
|
|
40
41
|
/**
|
|
41
42
|
* Put a number to the power of 0.
|
|
42
43
|
*/
|
|
43
|
-
declare function pow<
|
|
44
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 0): OperationIO<B> extends UnknownUnit ? Decimal : 1;
|
|
44
45
|
/**
|
|
45
46
|
* Put a number to the power of 1/2.
|
|
46
47
|
*/
|
|
47
|
-
declare function pow<
|
|
48
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 0.5): OperationIO<Divide<B, 2>>;
|
|
48
49
|
/**
|
|
49
50
|
* Put a number to the power of 1.
|
|
50
51
|
*/
|
|
51
|
-
declare function pow<
|
|
52
|
+
declare function pow<E extends number>(base: OperationIO<E>, exponent: 1): E;
|
|
52
53
|
/**
|
|
53
54
|
* Put a number to the power of 2.
|
|
54
55
|
*/
|
|
55
|
-
declare function pow<
|
|
56
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 2): OperationIO<Multiply<B, B>>;
|
|
56
57
|
/**
|
|
57
58
|
* Put a number to the power of 3.
|
|
58
59
|
*/
|
|
59
|
-
declare function pow<
|
|
60
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 3): OperationIO<Multiply<B, Multiply<B, B>>>;
|
|
60
61
|
/**
|
|
61
62
|
* Put a number to the power of 4.
|
|
62
63
|
*/
|
|
63
|
-
declare function pow<
|
|
64
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 4): OperationIO<Multiply<B, Multiply<B, Multiply<B, B>>>>;
|
|
64
65
|
/**
|
|
65
66
|
* Put one number to the power of the other.
|
|
66
67
|
*/
|
|
67
|
-
declare function pow<
|
|
68
|
+
declare function pow<B extends number, E extends number>(base: OperationIO<B>, exponent: E extends UnknownUnit ? never : E): number;
|
|
68
69
|
/**
|
|
69
70
|
* Take the square root of the given value.
|
|
70
71
|
*/
|
|
71
|
-
declare function sqrt<T extends number>(value: T):
|
|
72
|
+
declare function sqrt<T extends number>(value: OperationIO<T>): OperationIO<Divide<T, 2>>;
|
|
72
73
|
/**
|
|
73
74
|
* Inverse the given value.
|
|
74
75
|
*/
|
|
75
|
-
declare function inverse<T extends number>(value: T):
|
|
76
|
+
declare function inverse<T extends number>(value: OperationIO<T>): OperationIO<Inverse<T>>;
|
|
76
77
|
/**
|
|
77
78
|
* Make the given value negative.
|
|
78
79
|
*/
|
|
79
|
-
declare function negate<T extends number>(value: T):
|
|
80
|
+
declare function negate<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
80
81
|
/**
|
|
81
82
|
* Returns the absolute value of a number.
|
|
82
83
|
*/
|
|
83
|
-
declare function abs<T extends number>(value: T):
|
|
84
|
+
declare function abs<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
84
85
|
/**
|
|
85
86
|
* Returns the greatest integer less than or equal to the given value.
|
|
86
87
|
*/
|
|
87
|
-
declare function floor<T extends number>(value: T):
|
|
88
|
+
declare function floor<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
88
89
|
/**
|
|
89
90
|
* Returns the smallest integer greater than or equal the given value.
|
|
90
91
|
*/
|
|
91
|
-
declare function ceil<T extends number>(value: T):
|
|
92
|
+
declare function ceil<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
92
93
|
/**
|
|
93
94
|
* Returns the given value rounded to the nearest integer.
|
|
94
95
|
*/
|
|
95
|
-
declare function round<T extends number>(value: T):
|
|
96
|
+
declare function round<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
96
97
|
/**
|
|
97
98
|
* Returns the larger value in the given collection.
|
|
98
99
|
*/
|
|
99
|
-
declare function max<T extends number>(values: Iterable<T>):
|
|
100
|
+
declare function max<T extends number>(values: Iterable<T>): OperationIO<T>;
|
|
100
101
|
/**
|
|
101
102
|
* Returns the smallest value in the given collection.
|
|
102
103
|
*/
|
|
103
|
-
declare function min<T extends number>(values: Iterable<T>):
|
|
104
|
+
declare function min<T extends number>(values: Iterable<T>): OperationIO<T>;
|
|
104
105
|
/**
|
|
105
106
|
* Takes the sum of all the values in the given collection.
|
|
106
107
|
*/
|
|
107
|
-
declare function sum<T extends number>(values: ReadonlyArray<T>):
|
|
108
|
+
declare function sum<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
|
|
108
109
|
/**
|
|
109
110
|
* Takes the product of all the values in the given collection.
|
|
110
111
|
*/
|
|
111
|
-
declare function product<T extends number>(values: ReadonlyArray<T>):
|
|
112
|
+
declare function product<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
|
|
112
113
|
/**
|
|
113
114
|
* Equal: Compare if two values are equal.
|
|
114
115
|
*/
|
|
115
|
-
declare function eq<A extends number>(a: A
|
|
116
|
+
declare function eq<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
116
117
|
/**
|
|
117
118
|
* Greater Than: Compare if the first value is greater than the second.
|
|
118
119
|
*/
|
|
119
|
-
declare function gt<A extends number>(a: A
|
|
120
|
+
declare function gt<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
120
121
|
/**
|
|
121
122
|
* Greater Than or Equal: Compare if the first value is greater than or equal to the second.
|
|
122
123
|
*/
|
|
123
|
-
declare function gte<A extends number>(a: A
|
|
124
|
+
declare function gte<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
124
125
|
/**
|
|
125
126
|
* Less Than: Compare if the first value is less than the second.
|
|
126
127
|
*/
|
|
127
|
-
declare function lt<A extends number>(a: A
|
|
128
|
+
declare function lt<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
128
129
|
/**
|
|
129
130
|
* Less Than or Equal: Compare if the first value is less than or equal to the second.
|
|
130
131
|
*/
|
|
131
|
-
declare function lte<A extends number>(a: A
|
|
132
|
+
declare function lte<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
132
133
|
|
|
133
134
|
export { abs, add, ceil, div, eq, floor, gt, gte, inverse, lt, lte, max, min, mod, modSafe, mul, negate, pow, product, round, sqrt, sub, sum };
|
package/dist/functions.d.mts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Multiply, Divide, Inverse, UnknownUnit } from '#uom-types';
|
|
2
|
+
import { Decimal } from '#uom-types/si-units';
|
|
2
3
|
|
|
3
|
-
type
|
|
4
|
+
type OperationIO<T extends number> = T extends UnknownUnit ? T : number;
|
|
4
5
|
/**
|
|
5
6
|
* Add two values together.
|
|
6
7
|
*/
|
|
7
|
-
declare function add<T extends number>(a: T
|
|
8
|
+
declare function add<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
8
9
|
/**
|
|
9
10
|
* Subtract one value from another.
|
|
10
11
|
*/
|
|
11
|
-
declare function sub<T extends number>(a: T
|
|
12
|
+
declare function sub<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
12
13
|
/**
|
|
13
14
|
* Multiple two values together.
|
|
14
15
|
*/
|
|
15
|
-
declare function mul<
|
|
16
|
+
declare function mul<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Multiply<A, B>>;
|
|
16
17
|
/**
|
|
17
18
|
* Divide one value by another.
|
|
18
19
|
*/
|
|
19
|
-
declare function div<
|
|
20
|
+
declare function div<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Divide<A, B>>;
|
|
20
21
|
/**
|
|
21
22
|
* Modulo operator.
|
|
22
23
|
*
|
|
@@ -24,7 +25,7 @@ declare function div<T extends number>(a: T, b: T): OperationResult<T>;
|
|
|
24
25
|
* @param b - Must be an integer.
|
|
25
26
|
* @returns `a % b`
|
|
26
27
|
*/
|
|
27
|
-
declare function mod<T extends number>(a: T
|
|
28
|
+
declare function mod<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
28
29
|
/**
|
|
29
30
|
* Perform mathematic modular arithmetic.
|
|
30
31
|
*
|
|
@@ -32,102 +33,102 @@ declare function mod<T extends number>(a: T, b: T): OperationResult<T>;
|
|
|
32
33
|
* @param b - Must be a positive integer.
|
|
33
34
|
* @returns An integer between zero (inclusive) and `b` (exclusive).
|
|
34
35
|
*/
|
|
35
|
-
declare function modSafe<T extends number>(a: T
|
|
36
|
+
declare function modSafe<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
|
|
36
37
|
/**
|
|
37
38
|
* Put a number to the power of -1.
|
|
38
39
|
*/
|
|
39
|
-
declare function pow<
|
|
40
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: -1): OperationIO<Inverse<B>>;
|
|
40
41
|
/**
|
|
41
42
|
* Put a number to the power of 0.
|
|
42
43
|
*/
|
|
43
|
-
declare function pow<
|
|
44
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 0): OperationIO<B> extends UnknownUnit ? Decimal : 1;
|
|
44
45
|
/**
|
|
45
46
|
* Put a number to the power of 1/2.
|
|
46
47
|
*/
|
|
47
|
-
declare function pow<
|
|
48
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 0.5): OperationIO<Divide<B, 2>>;
|
|
48
49
|
/**
|
|
49
50
|
* Put a number to the power of 1.
|
|
50
51
|
*/
|
|
51
|
-
declare function pow<
|
|
52
|
+
declare function pow<E extends number>(base: OperationIO<E>, exponent: 1): E;
|
|
52
53
|
/**
|
|
53
54
|
* Put a number to the power of 2.
|
|
54
55
|
*/
|
|
55
|
-
declare function pow<
|
|
56
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 2): OperationIO<Multiply<B, B>>;
|
|
56
57
|
/**
|
|
57
58
|
* Put a number to the power of 3.
|
|
58
59
|
*/
|
|
59
|
-
declare function pow<
|
|
60
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 3): OperationIO<Multiply<B, Multiply<B, B>>>;
|
|
60
61
|
/**
|
|
61
62
|
* Put a number to the power of 4.
|
|
62
63
|
*/
|
|
63
|
-
declare function pow<
|
|
64
|
+
declare function pow<B extends number>(base: OperationIO<B>, exponent: 4): OperationIO<Multiply<B, Multiply<B, Multiply<B, B>>>>;
|
|
64
65
|
/**
|
|
65
66
|
* Put one number to the power of the other.
|
|
66
67
|
*/
|
|
67
|
-
declare function pow<
|
|
68
|
+
declare function pow<B extends number, E extends number>(base: OperationIO<B>, exponent: E extends UnknownUnit ? never : E): number;
|
|
68
69
|
/**
|
|
69
70
|
* Take the square root of the given value.
|
|
70
71
|
*/
|
|
71
|
-
declare function sqrt<T extends number>(value: T):
|
|
72
|
+
declare function sqrt<T extends number>(value: OperationIO<T>): OperationIO<Divide<T, 2>>;
|
|
72
73
|
/**
|
|
73
74
|
* Inverse the given value.
|
|
74
75
|
*/
|
|
75
|
-
declare function inverse<T extends number>(value: T):
|
|
76
|
+
declare function inverse<T extends number>(value: OperationIO<T>): OperationIO<Inverse<T>>;
|
|
76
77
|
/**
|
|
77
78
|
* Make the given value negative.
|
|
78
79
|
*/
|
|
79
|
-
declare function negate<T extends number>(value: T):
|
|
80
|
+
declare function negate<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
80
81
|
/**
|
|
81
82
|
* Returns the absolute value of a number.
|
|
82
83
|
*/
|
|
83
|
-
declare function abs<T extends number>(value: T):
|
|
84
|
+
declare function abs<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
84
85
|
/**
|
|
85
86
|
* Returns the greatest integer less than or equal to the given value.
|
|
86
87
|
*/
|
|
87
|
-
declare function floor<T extends number>(value: T):
|
|
88
|
+
declare function floor<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
88
89
|
/**
|
|
89
90
|
* Returns the smallest integer greater than or equal the given value.
|
|
90
91
|
*/
|
|
91
|
-
declare function ceil<T extends number>(value: T):
|
|
92
|
+
declare function ceil<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
92
93
|
/**
|
|
93
94
|
* Returns the given value rounded to the nearest integer.
|
|
94
95
|
*/
|
|
95
|
-
declare function round<T extends number>(value: T):
|
|
96
|
+
declare function round<T extends number>(value: OperationIO<T>): OperationIO<T>;
|
|
96
97
|
/**
|
|
97
98
|
* Returns the larger value in the given collection.
|
|
98
99
|
*/
|
|
99
|
-
declare function max<T extends number>(values: Iterable<T>):
|
|
100
|
+
declare function max<T extends number>(values: Iterable<T>): OperationIO<T>;
|
|
100
101
|
/**
|
|
101
102
|
* Returns the smallest value in the given collection.
|
|
102
103
|
*/
|
|
103
|
-
declare function min<T extends number>(values: Iterable<T>):
|
|
104
|
+
declare function min<T extends number>(values: Iterable<T>): OperationIO<T>;
|
|
104
105
|
/**
|
|
105
106
|
* Takes the sum of all the values in the given collection.
|
|
106
107
|
*/
|
|
107
|
-
declare function sum<T extends number>(values: ReadonlyArray<T>):
|
|
108
|
+
declare function sum<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
|
|
108
109
|
/**
|
|
109
110
|
* Takes the product of all the values in the given collection.
|
|
110
111
|
*/
|
|
111
|
-
declare function product<T extends number>(values: ReadonlyArray<T>):
|
|
112
|
+
declare function product<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
|
|
112
113
|
/**
|
|
113
114
|
* Equal: Compare if two values are equal.
|
|
114
115
|
*/
|
|
115
|
-
declare function eq<A extends number>(a: A
|
|
116
|
+
declare function eq<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
116
117
|
/**
|
|
117
118
|
* Greater Than: Compare if the first value is greater than the second.
|
|
118
119
|
*/
|
|
119
|
-
declare function gt<A extends number>(a: A
|
|
120
|
+
declare function gt<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
120
121
|
/**
|
|
121
122
|
* Greater Than or Equal: Compare if the first value is greater than or equal to the second.
|
|
122
123
|
*/
|
|
123
|
-
declare function gte<A extends number>(a: A
|
|
124
|
+
declare function gte<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
124
125
|
/**
|
|
125
126
|
* Less Than: Compare if the first value is less than the second.
|
|
126
127
|
*/
|
|
127
|
-
declare function lt<A extends number>(a: A
|
|
128
|
+
declare function lt<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
128
129
|
/**
|
|
129
130
|
* Less Than or Equal: Compare if the first value is less than or equal to the second.
|
|
130
131
|
*/
|
|
131
|
-
declare function lte<A extends number>(a: A
|
|
132
|
+
declare function lte<A extends number>(a: OperationIO<A>, b: A): boolean;
|
|
132
133
|
|
|
133
134
|
export { abs, add, ceil, div, eq, floor, gt, gte, inverse, lt, lte, max, min, mod, modSafe, mul, negate, pow, product, round, sqrt, sub, sum };
|
package/dist/functions.mjs
CHANGED
|
@@ -42,8 +42,8 @@ function mod(a, b) {
|
|
|
42
42
|
function modSafe(a, b) {
|
|
43
43
|
return (((a % b) + b) % b);
|
|
44
44
|
}
|
|
45
|
-
function pow(
|
|
46
|
-
return
|
|
45
|
+
function pow(base, exponent) {
|
|
46
|
+
return base ** exponent;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Take the square root of the given value.
|
|
@@ -103,13 +103,13 @@ function min(values) {
|
|
|
103
103
|
* Takes the sum of all the values in the given collection.
|
|
104
104
|
*/
|
|
105
105
|
function sum(values) {
|
|
106
|
-
return values.reduce(add, 0);
|
|
106
|
+
return values.reduce((add), 0);
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
109
|
* Takes the product of all the values in the given collection.
|
|
110
110
|
*/
|
|
111
111
|
function product(values) {
|
|
112
|
-
return values.reduce(mul, 1);
|
|
112
|
+
return values.reduce((mul), 1);
|
|
113
113
|
}
|
|
114
114
|
/**
|
|
115
115
|
* Equal: Compare if two values are equal.
|
package/dist/index.d.cts
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
type BrandUnit<T extends Record<string, UnitValue>> = {
|
|
2
|
-
__exactKeys: keyof T
|
|
3
|
-
__uom_types: {
|
|
4
|
-
[U in keyof T]: BrandUnitConfigValue<T[U]
|
|
5
|
-
}
|
|
2
|
+
__exactKeys: keyof ExcludeZeroExponentsKeys<T>;
|
|
3
|
+
__uom_types: RemoveNever<{
|
|
4
|
+
[U in keyof T]: U extends keyof ExcludeZeroExponentsKeys<T> ? BrandUnitConfigValue<T[U]> : never;
|
|
5
|
+
}>;
|
|
6
|
+
};
|
|
7
|
+
type RemoveNever<T> = {
|
|
8
|
+
[K in keyof T as [T[K]] extends [never] ? never : K]: T[K];
|
|
6
9
|
};
|
|
7
10
|
type BrandUnitConfigValue<T extends UnitValue> = ExcludeZeroExponents<T> & {
|
|
8
11
|
__exactKeys: keyof ExcludeZeroExponents<T>;
|
|
9
12
|
};
|
|
10
13
|
type GetUnitConfig<T> = T extends UnitFull<infer C> ? C : never;
|
|
14
|
+
type ExcludeZeroExponentsKeys<T extends Record<string, UnitValue>> = {
|
|
15
|
+
[K in keyof T as T[K] extends {
|
|
16
|
+
exponent: 0;
|
|
17
|
+
} ? never : K]: T[K];
|
|
18
|
+
};
|
|
11
19
|
type ExcludeZeroExponents<T extends UnitValue> = {
|
|
12
20
|
[K in keyof T as T[K] extends 0 ? never : K]: T[K];
|
|
13
21
|
};
|
|
@@ -16,7 +24,7 @@ type GetUnitValues<T extends Record<string, UnitValue>, U> = U extends keyof T ?
|
|
|
16
24
|
};
|
|
17
25
|
type KeysOfTwoObjects<A, B> = keyof A | keyof B;
|
|
18
26
|
type KeysOfKeyOfTwoObjects<A, B, K> = K extends keyof A ? K extends keyof B ? keyof A[K] | keyof B[K] : keyof A[K] : K extends keyof B ? keyof B[K] : never;
|
|
19
|
-
type Inverse<T extends number> = T extends
|
|
27
|
+
type Inverse<T extends number> = T extends UnknownUnit ? InverseUnit<T> : number;
|
|
20
28
|
type NegativeExponent<T extends Exponent> = T extends -6 ? 6 : T extends -5 ? 5 : T extends -4 ? 4 : T extends -3 ? 3 : T extends -2 ? 2 : T extends -1 ? 0 : T extends 0 ? 1 : T extends 1 ? -1 : T extends 2 ? -2 : T extends 3 ? -3 : T extends 4 ? -4 : T extends 5 ? -5 : T extends 6 ? -6 : never;
|
|
21
29
|
type InverseUnit<T extends UnknownUnit> = InverseUnitCore<T> extends Record<string, UnitValue> ? Unit<InverseUnitCore<T>> : never;
|
|
22
30
|
type InverseUnitCore<T extends UnknownUnit> = {
|
|
@@ -24,17 +32,23 @@ type InverseUnitCore<T extends UnknownUnit> = {
|
|
|
24
32
|
[E in keyof GetUnitConfig<T>[U]]: NegativeExponent<GetUnitConfig<T>[U][E]>;
|
|
25
33
|
};
|
|
26
34
|
};
|
|
27
|
-
type Multiply<A extends number, B extends number> = A extends
|
|
28
|
-
type MultiplyUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnitsCore<A, B> extends Record<string, UnitValue> ? Unit<MultiplyUnitsCore<A, B
|
|
35
|
+
type Multiply<A extends number, B extends number> = A extends UnknownUnit ? B extends UnknownUnit ? MultiplyUnits<A, B> : A : B extends UnknownUnit ? B : number;
|
|
36
|
+
type MultiplyUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnitsCore<A, B> extends Record<string, UnitValue> ? Unit<FlatternAlias<MultiplyUnitsCore<A, B>>> : never;
|
|
29
37
|
type MultiplyUnitsCore<A extends UnknownUnit, B extends UnknownUnit> = {
|
|
30
38
|
[U in KeysOfTwoObjects<GetUnitConfig<A>, GetUnitConfig<B>>]: {
|
|
31
39
|
[E in KeysOfKeyOfTwoObjects<GetUnitConfig<A>, GetUnitConfig<B>, U>]: SumExponents<E extends keyof GetUnitValues<GetUnitConfig<A>, U> ? GetUnitValues<GetUnitConfig<A>, U>[E] extends Exponent ? GetUnitValues<GetUnitConfig<A>, U>[E] : 0 : 0, E extends keyof GetUnitValues<GetUnitConfig<B>, U> ? GetUnitValues<GetUnitConfig<B>, U>[E] extends Exponent ? GetUnitValues<GetUnitConfig<B>, U>[E] : 0 : 0>;
|
|
32
40
|
};
|
|
33
41
|
};
|
|
34
|
-
type Divide<A extends number, B extends number> = A extends
|
|
42
|
+
type Divide<A extends number, B extends number> = A extends UnknownUnit ? B extends UnknownUnit ? DivideUnits<A, B> : A : B extends UnknownUnit ? Inverse<B> : number;
|
|
35
43
|
type DivideUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnits<A, InverseUnit<B> extends UnknownUnit ? InverseUnit<B> : never>;
|
|
36
|
-
type SubExponents<A extends Exponent, B extends Exponent> = SumExponents<A, NegativeExponent<B>>;
|
|
37
44
|
type SumExponents<A extends Exponent, B extends Exponent> = A extends -6 ? B extends 0 ? A : B extends 1 ? -5 : B extends 2 ? -4 : B extends 3 ? -3 : B extends 4 ? -2 : B extends 5 ? -1 : B extends 6 ? 0 : never : A extends -5 ? B extends -1 ? -6 : B extends 0 ? A : B extends 1 ? -4 : B extends 2 ? -3 : B extends 3 ? -2 : B extends 4 ? -1 : B extends 5 ? 0 : B extends 6 ? 1 : never : A extends -4 ? B extends -2 ? -6 : B extends -1 ? -5 : B extends 0 ? A : B extends 1 ? -3 : B extends 2 ? -2 : B extends 3 ? -1 : B extends 4 ? 0 : B extends 5 ? 1 : B extends 6 ? 2 : never : A extends -3 ? B extends -3 ? -6 : B extends -2 ? -5 : B extends -1 ? -4 : B extends 0 ? A : B extends 1 ? -2 : B extends 2 ? -1 : B extends 3 ? 0 : B extends 4 ? 1 : B extends 5 ? 2 : B extends 6 ? 3 : never : A extends -2 ? B extends -4 ? -6 : B extends -3 ? -5 : B extends -2 ? -4 : B extends -1 ? -3 : B extends 0 ? A : B extends 1 ? -1 : B extends 2 ? 0 : B extends 3 ? 1 : B extends 4 ? 2 : B extends 5 ? 3 : B extends 6 ? 4 : never : A extends -1 ? B extends -5 ? -6 : B extends -4 ? -5 : B extends -3 ? -4 : B extends -2 ? -3 : B extends -1 ? -2 : B extends 0 ? A : B extends 1 ? 0 : B extends 2 ? 1 : B extends 3 ? 2 : B extends 4 ? 3 : B extends 5 ? 4 : B extends 6 ? 5 : never : A extends 0 ? B : A extends 1 ? B extends -6 ? -5 : B extends -5 ? -4 : B extends -4 ? -3 : B extends -3 ? -2 : B extends -2 ? -1 : B extends -1 ? 0 : B extends 0 ? A : B extends 1 ? 2 : B extends 2 ? 3 : B extends 3 ? 4 : B extends 4 ? 5 : B extends 5 ? 6 : never : A extends 2 ? B extends -6 ? -4 : B extends -5 ? -3 : B extends -4 ? -2 : B extends -3 ? -1 : B extends -2 ? 0 : B extends -1 ? 1 : B extends 0 ? A : B extends 1 ? 3 : B extends 2 ? 4 : B extends 3 ? 5 : B extends 4 ? 6 : never : A extends 3 ? B extends -6 ? -3 : B extends -5 ? -2 : B extends -4 ? -1 : B extends -3 ? 0 : B extends -2 ? 1 : B extends -1 ? 2 : B extends 0 ? A : B extends 1 ? 4 : B extends 2 ? 5 : B extends 3 ? 6 : never : A extends 4 ? B extends -6 ? -2 : B extends -5 ? -1 : B extends -4 ? 0 : B extends -3 ? 1 : B extends -2 ? 2 : B extends -1 ? 3 : B extends 0 ? A : B extends 1 ? 5 : B extends 2 ? 6 : never : A extends 5 ? B extends -6 ? -1 : B extends -5 ? 0 : B extends -4 ? 1 : B extends -3 ? 2 : B extends -2 ? 3 : B extends -1 ? 4 : B extends 0 ? A : B extends 1 ? 6 : never : A extends 6 ? B extends -6 ? 0 : B extends -5 ? 1 : B extends -4 ? 2 : B extends -3 ? 3 : B extends -2 ? 4 : B extends -1 ? 5 : B extends 0 ? A : never : never;
|
|
45
|
+
/**
|
|
46
|
+
* Flatten a complex type such as a union or intersection of objects into a
|
|
47
|
+
* single object.
|
|
48
|
+
*/
|
|
49
|
+
type FlatternAlias<T> = {
|
|
50
|
+
[P in keyof T]: T[P];
|
|
51
|
+
} & {};
|
|
38
52
|
|
|
39
53
|
type Exponent = -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
40
54
|
type UnitFull<T extends Record<string, UnitValue>> = number & Readonly<BrandUnit<T>>;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
type BrandUnit<T extends Record<string, UnitValue>> = {
|
|
2
|
-
__exactKeys: keyof T
|
|
3
|
-
__uom_types: {
|
|
4
|
-
[U in keyof T]: BrandUnitConfigValue<T[U]
|
|
5
|
-
}
|
|
2
|
+
__exactKeys: keyof ExcludeZeroExponentsKeys<T>;
|
|
3
|
+
__uom_types: RemoveNever<{
|
|
4
|
+
[U in keyof T]: U extends keyof ExcludeZeroExponentsKeys<T> ? BrandUnitConfigValue<T[U]> : never;
|
|
5
|
+
}>;
|
|
6
|
+
};
|
|
7
|
+
type RemoveNever<T> = {
|
|
8
|
+
[K in keyof T as [T[K]] extends [never] ? never : K]: T[K];
|
|
6
9
|
};
|
|
7
10
|
type BrandUnitConfigValue<T extends UnitValue> = ExcludeZeroExponents<T> & {
|
|
8
11
|
__exactKeys: keyof ExcludeZeroExponents<T>;
|
|
9
12
|
};
|
|
10
13
|
type GetUnitConfig<T> = T extends UnitFull<infer C> ? C : never;
|
|
14
|
+
type ExcludeZeroExponentsKeys<T extends Record<string, UnitValue>> = {
|
|
15
|
+
[K in keyof T as T[K] extends {
|
|
16
|
+
exponent: 0;
|
|
17
|
+
} ? never : K]: T[K];
|
|
18
|
+
};
|
|
11
19
|
type ExcludeZeroExponents<T extends UnitValue> = {
|
|
12
20
|
[K in keyof T as T[K] extends 0 ? never : K]: T[K];
|
|
13
21
|
};
|
|
@@ -16,7 +24,7 @@ type GetUnitValues<T extends Record<string, UnitValue>, U> = U extends keyof T ?
|
|
|
16
24
|
};
|
|
17
25
|
type KeysOfTwoObjects<A, B> = keyof A | keyof B;
|
|
18
26
|
type KeysOfKeyOfTwoObjects<A, B, K> = K extends keyof A ? K extends keyof B ? keyof A[K] | keyof B[K] : keyof A[K] : K extends keyof B ? keyof B[K] : never;
|
|
19
|
-
type Inverse<T extends number> = T extends
|
|
27
|
+
type Inverse<T extends number> = T extends UnknownUnit ? InverseUnit<T> : number;
|
|
20
28
|
type NegativeExponent<T extends Exponent> = T extends -6 ? 6 : T extends -5 ? 5 : T extends -4 ? 4 : T extends -3 ? 3 : T extends -2 ? 2 : T extends -1 ? 0 : T extends 0 ? 1 : T extends 1 ? -1 : T extends 2 ? -2 : T extends 3 ? -3 : T extends 4 ? -4 : T extends 5 ? -5 : T extends 6 ? -6 : never;
|
|
21
29
|
type InverseUnit<T extends UnknownUnit> = InverseUnitCore<T> extends Record<string, UnitValue> ? Unit<InverseUnitCore<T>> : never;
|
|
22
30
|
type InverseUnitCore<T extends UnknownUnit> = {
|
|
@@ -24,17 +32,23 @@ type InverseUnitCore<T extends UnknownUnit> = {
|
|
|
24
32
|
[E in keyof GetUnitConfig<T>[U]]: NegativeExponent<GetUnitConfig<T>[U][E]>;
|
|
25
33
|
};
|
|
26
34
|
};
|
|
27
|
-
type Multiply<A extends number, B extends number> = A extends
|
|
28
|
-
type MultiplyUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnitsCore<A, B> extends Record<string, UnitValue> ? Unit<MultiplyUnitsCore<A, B
|
|
35
|
+
type Multiply<A extends number, B extends number> = A extends UnknownUnit ? B extends UnknownUnit ? MultiplyUnits<A, B> : A : B extends UnknownUnit ? B : number;
|
|
36
|
+
type MultiplyUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnitsCore<A, B> extends Record<string, UnitValue> ? Unit<FlatternAlias<MultiplyUnitsCore<A, B>>> : never;
|
|
29
37
|
type MultiplyUnitsCore<A extends UnknownUnit, B extends UnknownUnit> = {
|
|
30
38
|
[U in KeysOfTwoObjects<GetUnitConfig<A>, GetUnitConfig<B>>]: {
|
|
31
39
|
[E in KeysOfKeyOfTwoObjects<GetUnitConfig<A>, GetUnitConfig<B>, U>]: SumExponents<E extends keyof GetUnitValues<GetUnitConfig<A>, U> ? GetUnitValues<GetUnitConfig<A>, U>[E] extends Exponent ? GetUnitValues<GetUnitConfig<A>, U>[E] : 0 : 0, E extends keyof GetUnitValues<GetUnitConfig<B>, U> ? GetUnitValues<GetUnitConfig<B>, U>[E] extends Exponent ? GetUnitValues<GetUnitConfig<B>, U>[E] : 0 : 0>;
|
|
32
40
|
};
|
|
33
41
|
};
|
|
34
|
-
type Divide<A extends number, B extends number> = A extends
|
|
42
|
+
type Divide<A extends number, B extends number> = A extends UnknownUnit ? B extends UnknownUnit ? DivideUnits<A, B> : A : B extends UnknownUnit ? Inverse<B> : number;
|
|
35
43
|
type DivideUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnits<A, InverseUnit<B> extends UnknownUnit ? InverseUnit<B> : never>;
|
|
36
|
-
type SubExponents<A extends Exponent, B extends Exponent> = SumExponents<A, NegativeExponent<B>>;
|
|
37
44
|
type SumExponents<A extends Exponent, B extends Exponent> = A extends -6 ? B extends 0 ? A : B extends 1 ? -5 : B extends 2 ? -4 : B extends 3 ? -3 : B extends 4 ? -2 : B extends 5 ? -1 : B extends 6 ? 0 : never : A extends -5 ? B extends -1 ? -6 : B extends 0 ? A : B extends 1 ? -4 : B extends 2 ? -3 : B extends 3 ? -2 : B extends 4 ? -1 : B extends 5 ? 0 : B extends 6 ? 1 : never : A extends -4 ? B extends -2 ? -6 : B extends -1 ? -5 : B extends 0 ? A : B extends 1 ? -3 : B extends 2 ? -2 : B extends 3 ? -1 : B extends 4 ? 0 : B extends 5 ? 1 : B extends 6 ? 2 : never : A extends -3 ? B extends -3 ? -6 : B extends -2 ? -5 : B extends -1 ? -4 : B extends 0 ? A : B extends 1 ? -2 : B extends 2 ? -1 : B extends 3 ? 0 : B extends 4 ? 1 : B extends 5 ? 2 : B extends 6 ? 3 : never : A extends -2 ? B extends -4 ? -6 : B extends -3 ? -5 : B extends -2 ? -4 : B extends -1 ? -3 : B extends 0 ? A : B extends 1 ? -1 : B extends 2 ? 0 : B extends 3 ? 1 : B extends 4 ? 2 : B extends 5 ? 3 : B extends 6 ? 4 : never : A extends -1 ? B extends -5 ? -6 : B extends -4 ? -5 : B extends -3 ? -4 : B extends -2 ? -3 : B extends -1 ? -2 : B extends 0 ? A : B extends 1 ? 0 : B extends 2 ? 1 : B extends 3 ? 2 : B extends 4 ? 3 : B extends 5 ? 4 : B extends 6 ? 5 : never : A extends 0 ? B : A extends 1 ? B extends -6 ? -5 : B extends -5 ? -4 : B extends -4 ? -3 : B extends -3 ? -2 : B extends -2 ? -1 : B extends -1 ? 0 : B extends 0 ? A : B extends 1 ? 2 : B extends 2 ? 3 : B extends 3 ? 4 : B extends 4 ? 5 : B extends 5 ? 6 : never : A extends 2 ? B extends -6 ? -4 : B extends -5 ? -3 : B extends -4 ? -2 : B extends -3 ? -1 : B extends -2 ? 0 : B extends -1 ? 1 : B extends 0 ? A : B extends 1 ? 3 : B extends 2 ? 4 : B extends 3 ? 5 : B extends 4 ? 6 : never : A extends 3 ? B extends -6 ? -3 : B extends -5 ? -2 : B extends -4 ? -1 : B extends -3 ? 0 : B extends -2 ? 1 : B extends -1 ? 2 : B extends 0 ? A : B extends 1 ? 4 : B extends 2 ? 5 : B extends 3 ? 6 : never : A extends 4 ? B extends -6 ? -2 : B extends -5 ? -1 : B extends -4 ? 0 : B extends -3 ? 1 : B extends -2 ? 2 : B extends -1 ? 3 : B extends 0 ? A : B extends 1 ? 5 : B extends 2 ? 6 : never : A extends 5 ? B extends -6 ? -1 : B extends -5 ? 0 : B extends -4 ? 1 : B extends -3 ? 2 : B extends -2 ? 3 : B extends -1 ? 4 : B extends 0 ? A : B extends 1 ? 6 : never : A extends 6 ? B extends -6 ? 0 : B extends -5 ? 1 : B extends -4 ? 2 : B extends -3 ? 3 : B extends -2 ? 4 : B extends -1 ? 5 : B extends 0 ? A : never : never;
|
|
45
|
+
/**
|
|
46
|
+
* Flatten a complex type such as a union or intersection of objects into a
|
|
47
|
+
* single object.
|
|
48
|
+
*/
|
|
49
|
+
type FlatternAlias<T> = {
|
|
50
|
+
[P in keyof T]: T[P];
|
|
51
|
+
} & {};
|
|
38
52
|
|
|
39
53
|
type Exponent = -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
40
54
|
type UnitFull<T extends Record<string, UnitValue>> = number & Readonly<BrandUnit<T>>;
|
package/dist/si-units.d.cts
CHANGED
package/dist/si-units.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uom-types",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Typesafe units with no runtime overhead.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"uom",
|
|
@@ -22,6 +22,13 @@
|
|
|
22
22
|
"email": "rebecca.stevens@outlook.co.nz"
|
|
23
23
|
},
|
|
24
24
|
"type": "module",
|
|
25
|
+
"imports": {
|
|
26
|
+
"#uom-types": "./src/base/index.ts",
|
|
27
|
+
"#uom-types/functions": "./src/functions/index.ts",
|
|
28
|
+
"#uom-types/functions/higher-order": "./src/functions-ho/index.ts",
|
|
29
|
+
"#uom-types/si-units": "./src/si-units/index.ts",
|
|
30
|
+
"#uom-types/si-units/converters": "./src/si-units-converters/index.ts"
|
|
31
|
+
},
|
|
25
32
|
"exports": {
|
|
26
33
|
".": {
|
|
27
34
|
"types": {
|
|
@@ -81,8 +88,7 @@
|
|
|
81
88
|
"lint:prettier-fix": "prettier \"**/*\" --ignore-unknown --write",
|
|
82
89
|
"lint:spelling": "cspell \"**\" \".github/**/*\"",
|
|
83
90
|
"prepare": "husky install",
|
|
84
|
-
"release": "pnpm run release:
|
|
85
|
-
"release:pre": "node --no-warnings=ExperimentalWarning --loader=ts-paths-esm-loader/transpile-only --experimental-specifier-resolution=node ./scripts/pre-release.ts",
|
|
91
|
+
"release": "pnpm run release:semantic",
|
|
86
92
|
"release:semantic": "semantic-release",
|
|
87
93
|
"test": "pnpm run test:js",
|
|
88
94
|
"test:js": "c8 ava",
|
|
@@ -93,7 +99,6 @@
|
|
|
93
99
|
"@commitlint/config-conventional": "17.6.6",
|
|
94
100
|
"@cspell/dict-cryptocurrencies": "3.0.1",
|
|
95
101
|
"@rebeccastevens/eslint-config": "1.7.8",
|
|
96
|
-
"@rollup/plugin-node-resolve": "15.1.0",
|
|
97
102
|
"@rollup/plugin-typescript": "11.1.2",
|
|
98
103
|
"@semantic-release/changelog": "6.0.3",
|
|
99
104
|
"@semantic-release/commit-analyzer": "10.0.1",
|