uom-types 1.0.3 → 3.0.0

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.
@@ -1,95 +1,42 @@
1
- type BrandUnit<T extends Record<string, UnitValue>> = {
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];
9
- };
10
- type BrandUnitConfigValue<T extends UnitValue> = ExcludeZeroExponents<T> & {
11
- __exactKeys: keyof ExcludeZeroExponents<T>;
12
- };
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
- };
19
- type ExcludeZeroExponents<T extends UnitValue> = {
20
- [K in keyof T as T[K] extends 0 ? never : K]: T[K];
21
- };
22
- type GetUnitValues<T extends Record<string, UnitValue>, U> = U extends keyof T ? T[U] : {
23
- exponent: 0;
24
- };
25
- type KeysOfTwoObjects<A, B> = keyof A | keyof B;
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;
27
- type Inverse<T extends number> = T extends UnknownUnit ? InverseUnit<T> : number;
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;
29
- type InverseUnit<T extends UnknownUnit> = InverseUnitCore<T> extends Record<string, UnitValue> ? Unit<InverseUnitCore<T>> : never;
30
- type InverseUnitCore<T extends UnknownUnit> = {
31
- [U in keyof GetUnitConfig<T>]: {
32
- [E in keyof GetUnitConfig<T>[U]]: NegativeExponent<GetUnitConfig<T>[U][E]>;
33
- };
34
- };
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;
37
- type MultiplyUnitsCore<A extends UnknownUnit, B extends UnknownUnit> = {
38
- [U in KeysOfTwoObjects<GetUnitConfig<A>, GetUnitConfig<B>>]: {
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>;
40
- };
41
- };
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;
43
- type DivideUnits<A extends UnknownUnit, B extends UnknownUnit> = MultiplyUnits<A, InverseUnit<B> extends UnknownUnit ? InverseUnit<B> : never>;
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
- } & {};
1
+ import { M as Multiply, D as Divide, I as Inverse, U as UnknownUnit, a as Unit, b as UnknownAbstractUnit, A as AbstractUnit, c as DivideUnitExponents } from './_index.cjs';
2
+ import { R as Radian } from './_angle-plane.cjs';
3
+ import { U as Unitless } from './_identity.cjs';
52
4
 
53
- type Exponent = -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;
54
- type UnitFull<T extends Record<string, UnitValue>> = number & Readonly<BrandUnit<T>>;
55
- type UnitValue = {
56
- exponent: Exponent;
57
- } & Record<string, Exponent>;
58
- /**
59
- * A unit of measurement.
60
- */
61
- type Unit<T extends Record<string, Exponent | UnitValue>> = UnitFull<{
62
- [K in keyof T]: T[K] extends Exponent ? {
63
- exponent: T[K];
64
- } : T[K] extends UnitValue ? T[K] : never;
65
- }>;
66
- type UnknownUnit = number & Readonly<{
67
- __exactKeys: string;
68
- __uom_types: Record<string, {}>;
69
- }>;
70
-
71
- type Decimal = Unit<{}>;
5
+ /**
6
+ * @module uom-types/functions
7
+ */
72
8
 
73
- type OperationIO<T extends number> = T extends UnknownUnit ? T : number;
9
+ type OperationIO<T extends number> = [T] extends [
10
+ UnknownUnit | UnknownAbstractUnit
11
+ ] ? T : number;
74
12
  /**
75
- * Add two values together.
13
+ * Add two values with the same units together.
14
+ *
15
+ * @category Math
76
16
  */
77
17
  declare function add<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
78
18
  /**
79
- * Subtract one value from another.
19
+ * Subtract one value from another with the same units.
20
+ *
21
+ * @category Math
80
22
  */
81
23
  declare function sub<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
82
24
  /**
83
25
  * Multiple two values together.
26
+ *
27
+ * @category Math
84
28
  */
85
- declare function mul<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Multiply<A, B>>;
29
+ declare function mul<A extends number, B extends number>(a: A, b: B): Multiply<A, B>;
86
30
  /**
87
31
  * Divide one value by another.
32
+ *
33
+ * @category Math
88
34
  */
89
- declare function div<A extends number, B extends number>(a: OperationIO<A>, b: OperationIO<B>): OperationIO<Divide<A, B>>;
35
+ declare function div<A extends number, B extends number>(a: A, b: B): Divide<A, B>;
90
36
  /**
91
37
  * Modulo operator.
92
38
  *
39
+ * @category Math
93
40
  * @param a - Must be an integer.
94
41
  * @param b - Must be an integer.
95
42
  * @returns `a % b`
@@ -98,6 +45,7 @@ declare function mod<T extends number>(a: OperationIO<T>, b: OperationIO<T>): Op
98
45
  /**
99
46
  * Perform mathematic modular arithmetic.
100
47
  *
48
+ * @category Math
101
49
  * @param a - Must be an integer.
102
50
  * @param b - Must be a positive integer.
103
51
  * @returns An integer between zero (inclusive) and `b` (exclusive).
@@ -105,99 +53,221 @@ declare function mod<T extends number>(a: OperationIO<T>, b: OperationIO<T>): Op
105
53
  declare function modSafe<T extends number>(a: OperationIO<T>, b: OperationIO<T>): OperationIO<T>;
106
54
  /**
107
55
  * Put a number to the power of -1.
56
+ *
57
+ * @category Math
108
58
  */
109
- declare function pow<B extends number>(base: OperationIO<B>, exponent: -1): OperationIO<Inverse<B>>;
59
+ declare function pow<B extends number>(base: B, exponent: -1): Inverse<B>;
110
60
  /**
111
61
  * Put a number to the power of 0.
62
+ *
63
+ * @category Math
112
64
  */
113
- declare function pow<B extends number>(base: OperationIO<B>, exponent: 0): OperationIO<B> extends UnknownUnit ? Decimal : 1;
65
+ declare function pow<B extends number>(base: B, exponent: 0): B extends UnknownUnit ? Unit<{}> : B extends UnknownAbstractUnit ? AbstractUnit<{}> : 1;
114
66
  /**
115
67
  * Put a number to the power of 1/2.
68
+ *
69
+ * @category Math
116
70
  */
117
- declare function pow<B extends number>(base: OperationIO<B>, exponent: 0.5): OperationIO<Divide<B, 2>>;
71
+ declare function pow<B extends number>(base: B, exponent: 0.5): DivideUnitExponents<B, 2>;
118
72
  /**
119
73
  * Put a number to the power of 1.
74
+ *
75
+ * @category Math
120
76
  */
121
- declare function pow<E extends number>(base: OperationIO<E>, exponent: 1): E;
77
+ declare function pow<E extends number>(base: E, exponent: 1): E;
122
78
  /**
123
79
  * Put a number to the power of 2.
80
+ *
81
+ * @category Math
124
82
  */
125
- declare function pow<B extends number>(base: OperationIO<B>, exponent: 2): OperationIO<Multiply<B, B>>;
83
+ declare function pow<B extends number>(base: B, exponent: 2): Multiply<B, B>;
126
84
  /**
127
85
  * Put a number to the power of 3.
86
+ *
87
+ * @category Math
128
88
  */
129
- declare function pow<B extends number>(base: OperationIO<B>, exponent: 3): OperationIO<Multiply<B, Multiply<B, B>>>;
89
+ declare function pow<B extends number>(base: B, exponent: 3): Multiply<B, Multiply<B, B>>;
130
90
  /**
131
91
  * Put a number to the power of 4.
92
+ *
93
+ * @category Math
132
94
  */
133
- declare function pow<B extends number>(base: OperationIO<B>, exponent: 4): OperationIO<Multiply<B, Multiply<B, Multiply<B, B>>>>;
95
+ declare function pow<B extends number>(base: B, exponent: 4): Multiply<B, Multiply<B, Multiply<B, B>>>;
134
96
  /**
135
97
  * Put one number to the power of the other.
98
+ *
99
+ * @category Math
136
100
  */
137
- declare function pow<B extends number, E extends number>(base: OperationIO<B>, exponent: E extends UnknownUnit ? never : E): number;
101
+ declare function pow<B extends number, E extends number>(base: B, exponent: E extends UnknownUnit ? never : E extends UnknownAbstractUnit ? never : E): number;
138
102
  /**
139
103
  * Take the square root of the given value.
104
+ *
105
+ * @category Math
140
106
  */
141
- declare function sqrt<T extends number>(value: OperationIO<T>): OperationIO<Divide<T, 2>>;
107
+ declare function sqrt<T extends number>(value: T): DivideUnitExponents<T, 2>;
142
108
  /**
143
109
  * Inverse the given value.
110
+ *
111
+ * @category Math
144
112
  */
145
- declare function inverse<T extends number>(value: OperationIO<T>): OperationIO<Inverse<T>>;
113
+ declare function inverse<T extends number>(value: T): Inverse<T>;
146
114
  /**
147
- * Make the given value negative.
115
+ * Returns the negative of the given value.
116
+ *
117
+ * @category Math
148
118
  */
149
119
  declare function negate<T extends number>(value: OperationIO<T>): OperationIO<T>;
150
120
  /**
151
- * Returns the absolute value of a number.
121
+ * Returns the absolute value of the given value.
122
+ *
123
+ * @category Math
152
124
  */
153
125
  declare function abs<T extends number>(value: OperationIO<T>): OperationIO<T>;
154
126
  /**
155
127
  * Returns the greatest integer less than or equal to the given value.
128
+ *
129
+ * @category Math
156
130
  */
157
131
  declare function floor<T extends number>(value: OperationIO<T>): OperationIO<T>;
158
132
  /**
159
133
  * Returns the smallest integer greater than or equal the given value.
134
+ *
135
+ * @category Math
160
136
  */
161
137
  declare function ceil<T extends number>(value: OperationIO<T>): OperationIO<T>;
162
138
  /**
163
139
  * Returns the given value rounded to the nearest integer.
140
+ *
141
+ * @category Math
164
142
  */
165
143
  declare function round<T extends number>(value: OperationIO<T>): OperationIO<T>;
166
144
  /**
167
145
  * Returns the larger value in the given collection.
146
+ *
147
+ * @category Math
168
148
  */
169
149
  declare function max<T extends number>(values: Iterable<T>): OperationIO<T>;
170
150
  /**
171
151
  * Returns the smallest value in the given collection.
152
+ *
153
+ * @category Math
172
154
  */
173
155
  declare function min<T extends number>(values: Iterable<T>): OperationIO<T>;
174
156
  /**
175
157
  * Takes the sum of all the values in the given collection.
158
+ *
159
+ * @category Math
176
160
  */
177
- declare function sum<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
178
- /**
179
- * Takes the product of all the values in the given collection.
180
- */
181
- declare function product<T extends number>(values: ReadonlyArray<T>): OperationIO<T>;
161
+ declare function sum<T extends number>(values: Iterable<T>): OperationIO<T>;
182
162
  /**
183
163
  * Equal: Compare if two values are equal.
164
+ *
165
+ * @category Math
184
166
  */
185
- declare function eq<A extends number>(a: OperationIO<A>, b: A): boolean;
167
+ declare function eq<T extends number>(a: OperationIO<T>, b: OperationIO<T>): boolean;
186
168
  /**
187
169
  * Greater Than: Compare if the first value is greater than the second.
170
+ *
171
+ * @category Math
188
172
  */
189
- declare function gt<A extends number>(a: OperationIO<A>, b: A): boolean;
173
+ declare function gt<T extends number>(a: OperationIO<T>, b: OperationIO<T>): boolean;
190
174
  /**
191
175
  * Greater Than or Equal: Compare if the first value is greater than or equal to the second.
176
+ *
177
+ * @category Math
192
178
  */
193
- declare function gte<A extends number>(a: OperationIO<A>, b: A): boolean;
179
+ declare function gte<T extends number>(a: OperationIO<T>, b: OperationIO<T>): boolean;
194
180
  /**
195
181
  * Less Than: Compare if the first value is less than the second.
182
+ *
183
+ * @category Math
196
184
  */
197
- declare function lt<A extends number>(a: OperationIO<A>, b: A): boolean;
185
+ declare function lt<T extends number>(a: OperationIO<T>, b: OperationIO<T>): boolean;
198
186
  /**
199
187
  * Less Than or Equal: Compare if the first value is less than or equal to the second.
188
+ *
189
+ * @category Math
190
+ */
191
+ declare function lte<T extends number>(a: OperationIO<T>, b: OperationIO<T>): boolean;
192
+ /**
193
+ * Returns the sine of a number.
194
+ *
195
+ * @category Math
196
+ */
197
+ declare function sin(angle: Radian): Unitless;
198
+ /**
199
+ * Returns the cosine of a number.
200
+ *
201
+ * @category Math
202
+ */
203
+ declare function cos(angle: Radian): Unitless;
204
+ /**
205
+ * Returns the tangent of a number.
206
+ *
207
+ * @category Math
208
+ */
209
+ declare function tan(angle: Radian): Unitless;
210
+ /**
211
+ * Returns the arcsine of a number.
212
+ *
213
+ * @category Math
214
+ */
215
+ declare function asin(value: Unitless): Radian;
216
+ /**
217
+ * Returns the arc cosine (or inverse cosine) of a number.
218
+ *
219
+ * @category Math
220
+ */
221
+ declare function acos(value: Unitless): Radian;
222
+ /**
223
+ * Returns the arctangent of a number.
224
+ *
225
+ * @category Math
226
+ */
227
+ declare function atan(value: Unitless): Radian;
228
+ /**
229
+ * Returns the angle (in radians) from the X axis to a point.
230
+ *
231
+ * @category Math
232
+ * @param x - A number representing the cartesian x-coordinate.
233
+ * @param y - A number representing the cartesian y-coordinate.
234
+ */
235
+ declare function atan2<T extends number>(x: T, y: T): Radian;
236
+ /**
237
+ * Returns the hyperbolic sine of a number.
238
+ *
239
+ * @category Math
240
+ */
241
+ declare function sinh(angle: Radian): Unitless;
242
+ /**
243
+ * Returns the hyperbolic cosine of a number.
244
+ *
245
+ * @category Math
246
+ */
247
+ declare function cosh(angle: Radian): Unitless;
248
+ /**
249
+ * Returns the hyperbolic tangent of a number.
250
+ *
251
+ * @category Math
252
+ */
253
+ declare function tanh(angle: Radian): Unitless;
254
+ /**
255
+ * Returns the inverse hyperbolic sine of a number.
256
+ *
257
+ * @category Math
258
+ */
259
+ declare function asinh(value: Unitless): Radian;
260
+ /**
261
+ * Returns the inverse hyperbolic cosine of a number.
262
+ *
263
+ * @category Math
264
+ */
265
+ declare function acosh(value: Unitless): Radian;
266
+ /**
267
+ * Returns the inverse hyperbolic tangent of a number.
268
+ *
269
+ * @category Math
200
270
  */
201
- declare function lte<A extends number>(a: OperationIO<A>, b: A): boolean;
271
+ declare function atanh(value: Unitless): Radian;
202
272
 
203
- export { abs, add, ceil, div, eq, floor, gt, gte, inverse, lt, lte, max, min, mod, modSafe, mul, negate, pow, product, round, sqrt, sub, sum };
273
+ export { abs, acos, acosh, add, asin, asinh, atan, atan2, atanh, ceil, cos, cosh, div, eq, floor, gt, gte, inverse, lt, lte, max, min, mod, modSafe, mul, negate, pow, round, sin, sinh, sqrt, sub, sum, tan, tanh };