@thi.ng/units 0.1.1 → 0.3.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.
- package/CHANGELOG.md +23 -1
- package/README.md +131 -25
- package/api.d.ts +4 -0
- package/api.js +9 -0
- package/constants/densities.d.ts +25 -0
- package/constants/densities.js +28 -0
- package/constants/din-sizes.d.ts +10 -0
- package/constants/din-sizes.js +12 -0
- package/constants/din.d.ts +10 -0
- package/constants/din.js +11 -0
- package/constants/earth.d.ts +33 -0
- package/constants/earth.js +35 -0
- package/constants/velocities.d.ts +13 -0
- package/constants/velocities.js +14 -0
- package/index.d.ts +24 -19
- package/index.js +24 -19
- package/package.json +70 -45
- package/unit.d.ts +103 -21
- package/unit.js +113 -58
- package/units/accel.d.ts +5 -0
- package/{accel.js → units/accel.js} +1 -1
- package/units/angle.d.ts +8 -0
- package/{angle.js → units/angle.js} +1 -1
- package/units/area.d.ts +10 -0
- package/{area.js → units/area.js} +1 -1
- package/units/data.d.ts +25 -0
- package/{data.js → units/data.js} +1 -1
- package/units/density.d.ts +3 -0
- package/units/density.js +6 -0
- package/units/electric.d.ts +24 -0
- package/{electric.js → units/electric.js} +1 -1
- package/units/energy.d.ts +7 -0
- package/{energy.js → units/energy.js} +1 -1
- package/units/force.d.ts +2 -0
- package/{force.js → units/force.js} +1 -1
- package/units/frequency.d.ts +9 -0
- package/{frequency.js → units/frequency.js} +1 -1
- package/units/length.d.ts +20 -0
- package/{length.js → units/length.js} +1 -1
- package/units/luminous.d.ts +4 -0
- package/{luminous.js → units/luminous.js} +1 -1
- package/units/mass.d.ts +11 -0
- package/{mass.js → units/mass.js} +1 -1
- package/units/parts.d.ts +8 -0
- package/{parts.js → units/parts.js} +1 -1
- package/units/power.d.ts +9 -0
- package/{power.js → units/power.js} +1 -1
- package/units/pressure.d.ts +9 -0
- package/{pressure.js → units/pressure.js} +1 -1
- package/units/speed.d.ts +6 -0
- package/{speed.js → units/speed.js} +1 -1
- package/units/substance.d.ts +2 -0
- package/{substance.js → units/substance.js} +1 -1
- package/units/temperature.d.ts +4 -0
- package/{temperature.js → units/temperature.js} +1 -1
- package/units/time.d.ts +11 -0
- package/{time.js → units/time.js} +1 -1
- package/units/velocity.d.ts +6 -0
- package/units/velocity.js +8 -0
- package/units/volume.d.ts +15 -0
- package/{volume.js → units/volume.js} +1 -1
- package/accel.d.ts +0 -5
- package/angle.d.ts +0 -8
- package/area.d.ts +0 -10
- package/data.d.ts +0 -25
- package/electric.d.ts +0 -24
- package/energy.d.ts +0 -7
- package/force.d.ts +0 -2
- package/frequency.d.ts +0 -9
- package/length.d.ts +0 -20
- package/luminous.d.ts +0 -4
- package/mass.d.ts +0 -11
- package/parts.d.ts +0 -8
- package/power.d.ts +0 -9
- package/pressure.d.ts +0 -9
- package/speed.d.ts +0 -6
- package/substance.d.ts +0 -2
- package/temperature.d.ts +0 -4
- package/time.d.ts +0 -11
- package/volume.d.ts +0 -15
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/units",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Extensible SI unit creation, conversions & calculations (~
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Extensible SI unit creation, conversions, quantities & calculations (incl. ~170 predefined units & constants)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
7
7
|
"typings": "./index.d.ts",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@thi.ng/api": "^8.7.4",
|
|
37
38
|
"@thi.ng/checks": "^3.3.10",
|
|
38
39
|
"@thi.ng/equiv": "^2.1.20",
|
|
39
40
|
"@thi.ng/errors": "^2.2.13"
|
|
@@ -52,23 +53,27 @@
|
|
|
52
53
|
"area",
|
|
53
54
|
"bits",
|
|
54
55
|
"bytes",
|
|
56
|
+
"calculation",
|
|
55
57
|
"capacitance",
|
|
56
58
|
"current",
|
|
57
59
|
"voltage",
|
|
58
60
|
"resistance",
|
|
59
|
-
"
|
|
61
|
+
"conversion",
|
|
60
62
|
"energy",
|
|
61
63
|
"force",
|
|
62
64
|
"frequency",
|
|
63
65
|
"length",
|
|
64
66
|
"mass",
|
|
67
|
+
"math",
|
|
65
68
|
"power",
|
|
66
69
|
"si",
|
|
67
70
|
"speed",
|
|
71
|
+
"symbolic",
|
|
68
72
|
"temperature",
|
|
69
73
|
"time",
|
|
70
74
|
"typescript",
|
|
71
75
|
"units",
|
|
76
|
+
"velocity",
|
|
72
77
|
"volume"
|
|
73
78
|
],
|
|
74
79
|
"publishConfig": {
|
|
@@ -83,76 +88,96 @@
|
|
|
83
88
|
},
|
|
84
89
|
"files": [
|
|
85
90
|
"./*.js",
|
|
86
|
-
"./*.d.ts"
|
|
91
|
+
"./*.d.ts",
|
|
92
|
+
"constants",
|
|
93
|
+
"units"
|
|
87
94
|
],
|
|
88
95
|
"exports": {
|
|
89
96
|
".": {
|
|
90
97
|
"default": "./index.js"
|
|
91
98
|
},
|
|
92
|
-
"./accel": {
|
|
93
|
-
"default": "./accel.js"
|
|
94
|
-
},
|
|
95
|
-
"./angle": {
|
|
96
|
-
"default": "./angle.js"
|
|
97
|
-
},
|
|
98
99
|
"./api": {
|
|
99
100
|
"default": "./api.js"
|
|
100
101
|
},
|
|
101
|
-
"./
|
|
102
|
-
"default": "./
|
|
102
|
+
"./constants/densities": {
|
|
103
|
+
"default": "./constants/densities.js"
|
|
103
104
|
},
|
|
104
|
-
"./
|
|
105
|
-
"default": "./
|
|
105
|
+
"./constants/din-sizes": {
|
|
106
|
+
"default": "./constants/din-sizes.js"
|
|
106
107
|
},
|
|
107
|
-
"./
|
|
108
|
-
"default": "./
|
|
108
|
+
"./constants/earth": {
|
|
109
|
+
"default": "./constants/earth.js"
|
|
109
110
|
},
|
|
110
|
-
"./
|
|
111
|
-
"default": "./
|
|
111
|
+
"./constants/velocities": {
|
|
112
|
+
"default": "./constants/velocities.js"
|
|
112
113
|
},
|
|
113
|
-
"./
|
|
114
|
-
"default": "./
|
|
114
|
+
"./unit": {
|
|
115
|
+
"default": "./unit.js"
|
|
115
116
|
},
|
|
116
|
-
"./
|
|
117
|
-
"default": "./
|
|
117
|
+
"./units/accel": {
|
|
118
|
+
"default": "./units/accel.js"
|
|
118
119
|
},
|
|
119
|
-
"./
|
|
120
|
-
"default": "./
|
|
120
|
+
"./units/angle": {
|
|
121
|
+
"default": "./units/angle.js"
|
|
121
122
|
},
|
|
122
|
-
"./
|
|
123
|
-
"default": "./
|
|
123
|
+
"./units/area": {
|
|
124
|
+
"default": "./units/area.js"
|
|
124
125
|
},
|
|
125
|
-
"./
|
|
126
|
-
"default": "./
|
|
126
|
+
"./units/data": {
|
|
127
|
+
"default": "./units/data.js"
|
|
127
128
|
},
|
|
128
|
-
"./
|
|
129
|
-
"default": "./
|
|
129
|
+
"./units/density": {
|
|
130
|
+
"default": "./units/density.js"
|
|
130
131
|
},
|
|
131
|
-
"./
|
|
132
|
-
"default": "./
|
|
132
|
+
"./units/electric": {
|
|
133
|
+
"default": "./units/electric.js"
|
|
133
134
|
},
|
|
134
|
-
"./
|
|
135
|
-
"default": "./
|
|
135
|
+
"./units/energy": {
|
|
136
|
+
"default": "./units/energy.js"
|
|
136
137
|
},
|
|
137
|
-
"./
|
|
138
|
-
"default": "./
|
|
138
|
+
"./units/force": {
|
|
139
|
+
"default": "./units/force.js"
|
|
139
140
|
},
|
|
140
|
-
"./
|
|
141
|
-
"default": "./
|
|
141
|
+
"./units/frequency": {
|
|
142
|
+
"default": "./units/frequency.js"
|
|
142
143
|
},
|
|
143
|
-
"./
|
|
144
|
-
"default": "./
|
|
144
|
+
"./units/length": {
|
|
145
|
+
"default": "./units/length.js"
|
|
145
146
|
},
|
|
146
|
-
"./
|
|
147
|
-
"default": "./
|
|
147
|
+
"./units/luminous": {
|
|
148
|
+
"default": "./units/luminous.js"
|
|
149
|
+
},
|
|
150
|
+
"./units/mass": {
|
|
151
|
+
"default": "./units/mass.js"
|
|
152
|
+
},
|
|
153
|
+
"./units/parts": {
|
|
154
|
+
"default": "./units/parts.js"
|
|
155
|
+
},
|
|
156
|
+
"./units/power": {
|
|
157
|
+
"default": "./units/power.js"
|
|
158
|
+
},
|
|
159
|
+
"./units/pressure": {
|
|
160
|
+
"default": "./units/pressure.js"
|
|
161
|
+
},
|
|
162
|
+
"./units/substance": {
|
|
163
|
+
"default": "./units/substance.js"
|
|
164
|
+
},
|
|
165
|
+
"./units/temperature": {
|
|
166
|
+
"default": "./units/temperature.js"
|
|
167
|
+
},
|
|
168
|
+
"./units/time": {
|
|
169
|
+
"default": "./units/time.js"
|
|
170
|
+
},
|
|
171
|
+
"./units/velocity": {
|
|
172
|
+
"default": "./units/velocity.js"
|
|
148
173
|
},
|
|
149
|
-
"./volume": {
|
|
150
|
-
"default": "./volume.js"
|
|
174
|
+
"./units/volume": {
|
|
175
|
+
"default": "./units/volume.js"
|
|
151
176
|
}
|
|
152
177
|
},
|
|
153
178
|
"thi.ng": {
|
|
154
179
|
"status": "beta",
|
|
155
180
|
"year": 2021
|
|
156
181
|
},
|
|
157
|
-
"gitHead": "
|
|
182
|
+
"gitHead": "226659c410d47899bdc702e8cd871e2b66548bb8\n"
|
|
158
183
|
}
|
package/unit.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IDeref } from "@thi.ng/api";
|
|
1
2
|
import { Dimensions, MaybeUnit, NamedUnit, Prefix, Unit } from "./api.js";
|
|
2
3
|
/**
|
|
3
4
|
* Cache/registry for all units defined via {@link defUnit}.
|
|
@@ -68,31 +69,99 @@ export declare const asUnit: (id: string) => Unit;
|
|
|
68
69
|
* @param coherent
|
|
69
70
|
*/
|
|
70
71
|
export declare const prefix: (id: Prefix, unit: MaybeUnit, coherent?: boolean) => Unit;
|
|
72
|
+
type QUnit<T extends number | number[]> = T extends number ? Unit : Unit[];
|
|
71
73
|
/**
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
* Wrapper for scalar or vector quantities. See {@link quantity}.
|
|
75
|
+
*/
|
|
76
|
+
export declare class Quantity<T extends number | number[]> implements IDeref<T> {
|
|
77
|
+
readonly value: QUnit<T>;
|
|
78
|
+
constructor(value: QUnit<T>);
|
|
79
|
+
deref(): T;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new {@link Quantity}, i.e. a certain finite amount of a given unit.
|
|
83
|
+
* `value` can be a number or vector.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* The quantities can then be used for calculations & conversions using the
|
|
87
|
+
* polymorphic functions: {@link div}, {@link mul}, {@link reciprocal} and
|
|
88
|
+
* {@link convert}.
|
|
89
|
+
*
|
|
90
|
+
* The {@link Quantity} class also implements the standard [`IDeref`]()
|
|
91
|
+
* interface to obtain unwrapped amount (though only should be used for
|
|
92
|
+
* dimensionless quantities). Use {@link convert} otherwise!
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const speedOfLight = quantity(299792458, "m/s");
|
|
97
|
+
*
|
|
98
|
+
* // compute wavelength of a WiFi signal in millimeters
|
|
99
|
+
* convert(div(speedOfLight, quantity(2.4,"GHz")), "mm");
|
|
100
|
+
* // 124.9135
|
|
101
|
+
*
|
|
102
|
+
* // DIN A4 paper size
|
|
103
|
+
* const A4 = quantity([210, 297], "mm");
|
|
104
|
+
*
|
|
105
|
+
* // convert paper size to inches
|
|
106
|
+
* convert(A4, "in");
|
|
107
|
+
* // [ 8.2677, 11.6929 ]
|
|
108
|
+
*
|
|
109
|
+
* // or calculate pixel dimensions @ 300 dpi
|
|
110
|
+
* // the result of the product is dimensionless so we use NONE as target unit
|
|
111
|
+
* convert(mul(A4, quantity(300, "dpi")), NONE)
|
|
112
|
+
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
113
|
+
*
|
|
114
|
+
* // alternatively dimensionless units can be deref'd directly
|
|
115
|
+
* mul(A4, quantity(300, "dpi")).deref()
|
|
116
|
+
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @param value
|
|
120
|
+
* @param unit
|
|
121
|
+
*/
|
|
122
|
+
export declare const quantity: <T extends number | number[]>(value: T, unit: MaybeUnit) => Quantity<T>;
|
|
123
|
+
/**
|
|
124
|
+
* Polymorphic function. Derives a new quantity or unit as the product of the
|
|
125
|
+
* given quantities/units.
|
|
126
|
+
*
|
|
127
|
+
* @remarks
|
|
128
|
+
* If given units and if `coherent` is true (default: false), the new unit
|
|
129
|
+
* itself is considered coherent and can be prefixed later.
|
|
75
130
|
*
|
|
76
131
|
* @param a
|
|
77
132
|
* @param b
|
|
78
133
|
* @param coherent
|
|
79
134
|
*/
|
|
80
|
-
export declare
|
|
135
|
+
export declare function mul(a: Quantity<number>, b: Quantity<number>): Quantity<number>;
|
|
136
|
+
export declare function mul(a: Quantity<number>, b: Quantity<number[]>): Quantity<number[]>;
|
|
137
|
+
export declare function mul(a: Quantity<number[]>, b: Quantity<number>): Quantity<number[]>;
|
|
138
|
+
export declare function mul(a: Quantity<number[]>, b: Quantity<number[]>): Quantity<number[]>;
|
|
139
|
+
export declare function mul(a: MaybeUnit, b: MaybeUnit | number, coherent?: boolean): Unit;
|
|
81
140
|
/**
|
|
82
|
-
* Derives a new unit via the division of the
|
|
83
|
-
*
|
|
84
|
-
*
|
|
141
|
+
* Polymorphic function. Derives a new quantity or unit via the division of the
|
|
142
|
+
* given quantities/units.
|
|
143
|
+
*
|
|
144
|
+
* @remarks
|
|
145
|
+
* If given units and if `coherent` is true (default: false), the new unit
|
|
146
|
+
* itself is considered coherent and can be prefixed later.
|
|
85
147
|
*
|
|
86
148
|
* @param a
|
|
87
149
|
* @param b
|
|
88
150
|
* @param coherent
|
|
89
151
|
*/
|
|
90
|
-
export declare
|
|
152
|
+
export declare function div(a: Quantity<number>, b: Quantity<number>): Quantity<number>;
|
|
153
|
+
export declare function div(a: Quantity<number>, b: Quantity<number[]>): Quantity<number[]>;
|
|
154
|
+
export declare function div(a: Quantity<number[]>, b: Quantity<number>): Quantity<number[]>;
|
|
155
|
+
export declare function div(a: Quantity<number[]>, b: Quantity<number[]>): Quantity<number[]>;
|
|
156
|
+
export declare function div(a: MaybeUnit, b: MaybeUnit | number, coherent?: boolean): Unit;
|
|
91
157
|
/**
|
|
92
|
-
* Creates the reciprocal version of given
|
|
93
|
-
* flip sign) and the scale factor of the new
|
|
94
|
-
*
|
|
95
|
-
*
|
|
158
|
+
* Polymorphic function. Creates the reciprocal version of given quantity or
|
|
159
|
+
* unit (i.e. all SI dimensions will flip sign) and the scale factor of the new
|
|
160
|
+
* unit will be `1/scale`.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* If given a unit and if `coherent` is true (default: false), the new unit
|
|
164
|
+
* itself is considered coherent and can be prefixed later.
|
|
96
165
|
*
|
|
97
166
|
* @example
|
|
98
167
|
* ```ts
|
|
@@ -102,7 +171,9 @@ export declare const div: (a: MaybeUnit, b: MaybeUnit | number, coherent?: boole
|
|
|
102
171
|
* @param u
|
|
103
172
|
* @param coherent
|
|
104
173
|
*/
|
|
105
|
-
export declare
|
|
174
|
+
export declare function reciprocal(u: Quantity<number>): Quantity<number>;
|
|
175
|
+
export declare function reciprocal(u: Quantity<number[]>): Quantity<number[]>;
|
|
176
|
+
export declare function reciprocal(u: MaybeUnit, coherent?: boolean): Unit;
|
|
106
177
|
/**
|
|
107
178
|
* Raises given unit to power `k`. If `coherent` is true (default: false), the
|
|
108
179
|
* new unit itself is considered coherent and can be prefixed later.
|
|
@@ -121,8 +192,11 @@ export declare const reciprocal: (u: MaybeUnit, coherent?: boolean) => Unit;
|
|
|
121
192
|
*/
|
|
122
193
|
export declare const pow: (u: MaybeUnit, k: number, coherent?: boolean) => Unit;
|
|
123
194
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
195
|
+
* Polymorphic function. If given a {@link Quantity}, attempts to convert it to
|
|
196
|
+
* given `dest` unit and returns result as raw/unwrapped value (or vector).
|
|
197
|
+
* Otherwise, attempts to convert `x` amount from `src` unit into `dest` unit
|
|
198
|
+
* and returns result. In all cases an error is thrown if units are
|
|
199
|
+
* incompatible.
|
|
126
200
|
*
|
|
127
201
|
* @remarks
|
|
128
202
|
* Units can only be converted if their SI dimensions are compatible. See
|
|
@@ -132,20 +206,21 @@ export declare const pow: (u: MaybeUnit, k: number, coherent?: boolean) => Unit;
|
|
|
132
206
|
* @param src
|
|
133
207
|
* @param dest
|
|
134
208
|
*/
|
|
135
|
-
export declare
|
|
209
|
+
export declare function convert<T extends number | number[]>(x: Quantity<T>, dest: MaybeUnit): T;
|
|
210
|
+
export declare function convert(x: number, src: MaybeUnit, dest: MaybeUnit): number;
|
|
136
211
|
/**
|
|
137
|
-
* Returns true if `src` unit is convertible to `dest
|
|
212
|
+
* Returns true if `src` quantity or unit is convertible to `dest` unit.
|
|
138
213
|
*
|
|
139
214
|
* @param src
|
|
140
215
|
* @param dest
|
|
141
216
|
*/
|
|
142
|
-
export declare const isConvertible: (src: MaybeUnit, dest: MaybeUnit) => boolean;
|
|
217
|
+
export declare const isConvertible: (src: Quantity<any> | MaybeUnit, dest: MaybeUnit) => boolean;
|
|
143
218
|
/**
|
|
144
|
-
* Returns true, if `u` is a dimensionless unit.
|
|
219
|
+
* Returns true, if `u` is a dimensionless quantity or unit.
|
|
145
220
|
*
|
|
146
221
|
* @param u
|
|
147
222
|
*/
|
|
148
|
-
export declare const isDimensionless: (u: MaybeUnit) => boolean;
|
|
223
|
+
export declare const isDimensionless: (u: Quantity<any> | MaybeUnit) => boolean;
|
|
149
224
|
/**
|
|
150
225
|
* Returns true if the two given units are reciprocal to each other (and
|
|
151
226
|
* therefore can be used for conversion).
|
|
@@ -154,5 +229,12 @@ export declare const isDimensionless: (u: MaybeUnit) => boolean;
|
|
|
154
229
|
* @param b
|
|
155
230
|
*/
|
|
156
231
|
export declare const isReciprocal: (a: MaybeUnit, b: MaybeUnit) => boolean;
|
|
157
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Polymorphic function. Returns formatted version of given quantity's or unit's
|
|
234
|
+
* SI dimension vector.
|
|
235
|
+
*
|
|
236
|
+
* @param u
|
|
237
|
+
*/
|
|
238
|
+
export declare const formatSI: (u: Quantity<any> | MaybeUnit) => string;
|
|
239
|
+
export {};
|
|
158
240
|
//# sourceMappingURL=unit.d.ts.map
|
package/unit.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { isArray } from "@thi.ng/checks";
|
|
1
2
|
import { isNumber } from "@thi.ng/checks/is-number";
|
|
2
3
|
import { isString } from "@thi.ng/checks/is-string";
|
|
3
4
|
import { equivArrayLike } from "@thi.ng/equiv";
|
|
4
5
|
import { assert } from "@thi.ng/errors/assert";
|
|
5
6
|
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
6
|
-
import { PREFIXES, } from "./api.js";
|
|
7
|
+
import { NONE, PREFIXES, } from "./api.js";
|
|
7
8
|
/**
|
|
8
9
|
* Cache/registry for all units defined via {@link defUnit}.
|
|
9
10
|
*/
|
|
@@ -39,7 +40,7 @@ export const coherent = (dim) => unit(dim, 1, 0, true);
|
|
|
39
40
|
* @param offset
|
|
40
41
|
* @param coherent
|
|
41
42
|
*/
|
|
42
|
-
export const dimensionless = (scale, offset = 0, coherent = false) => unit(
|
|
43
|
+
export const dimensionless = (scale, offset = 0, coherent = false) => unit(NONE.dim, scale, offset, coherent);
|
|
43
44
|
/**
|
|
44
45
|
* Takes a unit symbol, full unit name and pre-defined {@link Unit} impl and
|
|
45
46
|
* registers it in the {@link UNITS} cache for further lookups by symbol name.
|
|
@@ -71,7 +72,9 @@ export const asUnit = (id) => {
|
|
|
71
72
|
if (unit) {
|
|
72
73
|
return PREFIXES[pre] !== undefined
|
|
73
74
|
? prefix(pre, unit)
|
|
74
|
-
:
|
|
75
|
+
: !pre
|
|
76
|
+
? unit
|
|
77
|
+
: illegalArgs(`unknown unit: ${id}`);
|
|
75
78
|
}
|
|
76
79
|
}
|
|
77
80
|
for (let u in UNITS) {
|
|
@@ -103,54 +106,88 @@ export const prefix = (id, unit, coherent = false) => {
|
|
|
103
106
|
: illegalArgs("unit isn't coherent");
|
|
104
107
|
};
|
|
105
108
|
/**
|
|
106
|
-
*
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
* Wrapper for scalar or vector quantities. See {@link quantity}.
|
|
110
|
+
*/
|
|
111
|
+
export class Quantity {
|
|
112
|
+
constructor(value) {
|
|
113
|
+
this.value = value;
|
|
114
|
+
}
|
|
115
|
+
deref() {
|
|
116
|
+
return ((isArray(this.value)
|
|
117
|
+
? this.value.map((x) => x.scale)
|
|
118
|
+
: this.value.scale));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new {@link Quantity}, i.e. a certain finite amount of a given unit.
|
|
123
|
+
* `value` can be a number or vector.
|
|
109
124
|
*
|
|
110
|
-
* @
|
|
111
|
-
*
|
|
112
|
-
* @
|
|
125
|
+
* @remarks
|
|
126
|
+
* The quantities can then be used for calculations & conversions using the
|
|
127
|
+
* polymorphic functions: {@link div}, {@link mul}, {@link reciprocal} and
|
|
128
|
+
* {@link convert}.
|
|
129
|
+
*
|
|
130
|
+
* The {@link Quantity} class also implements the standard [`IDeref`]()
|
|
131
|
+
* interface to obtain unwrapped amount (though only should be used for
|
|
132
|
+
* dimensionless quantities). Use {@link convert} otherwise!
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const speedOfLight = quantity(299792458, "m/s");
|
|
137
|
+
*
|
|
138
|
+
* // compute wavelength of a WiFi signal in millimeters
|
|
139
|
+
* convert(div(speedOfLight, quantity(2.4,"GHz")), "mm");
|
|
140
|
+
* // 124.9135
|
|
141
|
+
*
|
|
142
|
+
* // DIN A4 paper size
|
|
143
|
+
* const A4 = quantity([210, 297], "mm");
|
|
144
|
+
*
|
|
145
|
+
* // convert paper size to inches
|
|
146
|
+
* convert(A4, "in");
|
|
147
|
+
* // [ 8.2677, 11.6929 ]
|
|
148
|
+
*
|
|
149
|
+
* // or calculate pixel dimensions @ 300 dpi
|
|
150
|
+
* // the result of the product is dimensionless so we use NONE as target unit
|
|
151
|
+
* convert(mul(A4, quantity(300, "dpi")), NONE)
|
|
152
|
+
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
153
|
+
*
|
|
154
|
+
* // alternatively dimensionless units can be deref'd directly
|
|
155
|
+
* mul(A4, quantity(300, "dpi")).deref()
|
|
156
|
+
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @param value
|
|
160
|
+
* @param unit
|
|
113
161
|
*/
|
|
114
|
-
export const
|
|
162
|
+
export const quantity = (value, unit) => new Quantity(((isNumber(value)
|
|
163
|
+
? mul(unit, value)
|
|
164
|
+
: value.map((x) => mul(unit, x)))));
|
|
165
|
+
export function mul(a, b, coherent = false) {
|
|
166
|
+
if (a instanceof Quantity)
|
|
167
|
+
return __combineQ(mul, a, b);
|
|
115
168
|
const $a = __ensureUnit(a);
|
|
116
|
-
if (isNumber(b))
|
|
169
|
+
if (isNumber(b))
|
|
117
170
|
return unit($a.dim, $a.scale * b, $a.offset, coherent);
|
|
118
|
-
}
|
|
119
171
|
const $b = __ensureUnit(b);
|
|
120
172
|
return unit($a.dim.map((x, i) => x + $b.dim[i]), $a.scale * $b.scale, 0, coherent);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
* prefixed later.
|
|
126
|
-
*
|
|
127
|
-
* @param a
|
|
128
|
-
* @param b
|
|
129
|
-
* @param coherent
|
|
130
|
-
*/
|
|
131
|
-
export const div = (a, b, coherent = false) => {
|
|
173
|
+
}
|
|
174
|
+
export function div(a, b, coherent = false) {
|
|
175
|
+
if (a instanceof Quantity)
|
|
176
|
+
return __combineQ(div, a, b);
|
|
132
177
|
const $a = __ensureUnit(a);
|
|
133
178
|
if (isNumber(b)) {
|
|
134
179
|
return unit($a.dim, $a.scale / b, $a.offset, coherent);
|
|
135
180
|
}
|
|
136
181
|
const $b = __ensureUnit(b);
|
|
137
182
|
return unit($a.dim.map((x, i) => x - $b.dim[i]), $a.scale / $b.scale, 0, coherent);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
* ```ts
|
|
147
|
-
* const HZ = reciprocal(S, true);
|
|
148
|
-
* ```
|
|
149
|
-
*
|
|
150
|
-
* @param u
|
|
151
|
-
* @param coherent
|
|
152
|
-
*/
|
|
153
|
-
export const reciprocal = (u, coherent = false) => div(dimensionless(1), u, coherent);
|
|
183
|
+
}
|
|
184
|
+
export function reciprocal(u, coherent = false) {
|
|
185
|
+
return u instanceof Quantity
|
|
186
|
+
? new Quantity(isArray(u.value)
|
|
187
|
+
? u.value.map((x) => div(NONE, x))
|
|
188
|
+
: div(NONE, u.value))
|
|
189
|
+
: div(NONE, u, coherent);
|
|
190
|
+
}
|
|
154
191
|
/**
|
|
155
192
|
* Raises given unit to power `k`. If `coherent` is true (default: false), the
|
|
156
193
|
* new unit itself is considered coherent and can be prefixed later.
|
|
@@ -171,44 +208,41 @@ export const pow = (u, k, coherent = false) => {
|
|
|
171
208
|
const $u = __ensureUnit(u);
|
|
172
209
|
return unit($u.dim.map((x) => x * k), $u.scale ** k, 0, coherent);
|
|
173
210
|
};
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
* @param x
|
|
183
|
-
* @param src
|
|
184
|
-
* @param dest
|
|
185
|
-
*/
|
|
186
|
-
export const convert = (x, src, dest) => {
|
|
187
|
-
const $src = __ensureUnit(src);
|
|
188
|
-
const $dest = __ensureUnit(dest);
|
|
211
|
+
export function convert(x, a, b) {
|
|
212
|
+
const $src = __ensureUnit(a);
|
|
213
|
+
if (x instanceof Quantity) {
|
|
214
|
+
return isArray(x.value)
|
|
215
|
+
? x.value.map((y) => convert(1, y, $src))
|
|
216
|
+
: convert(1, x.value, $src);
|
|
217
|
+
}
|
|
218
|
+
const $dest = __ensureUnit(b);
|
|
189
219
|
const xnorm = x * $src.scale + $src.offset;
|
|
190
220
|
if (isReciprocal($src, $dest))
|
|
191
221
|
return (1 / xnorm - $dest.offset) / $dest.scale;
|
|
192
222
|
assert(equivArrayLike($src.dim, $dest.dim), "incompatible dimensions");
|
|
193
223
|
return (xnorm - $dest.offset) / $dest.scale;
|
|
194
|
-
}
|
|
224
|
+
}
|
|
195
225
|
/**
|
|
196
|
-
* Returns true if `src` unit is convertible to `dest
|
|
226
|
+
* Returns true if `src` quantity or unit is convertible to `dest` unit.
|
|
197
227
|
*
|
|
198
228
|
* @param src
|
|
199
229
|
* @param dest
|
|
200
230
|
*/
|
|
201
231
|
export const isConvertible = (src, dest) => {
|
|
232
|
+
if (src instanceof Quantity)
|
|
233
|
+
return isConvertible(__qunit(src), dest);
|
|
202
234
|
const $src = __ensureUnit(src);
|
|
203
235
|
const $dest = __ensureUnit(dest);
|
|
204
236
|
return isReciprocal($src, $dest) || equivArrayLike($src.dim, $dest.dim);
|
|
205
237
|
};
|
|
206
238
|
/**
|
|
207
|
-
* Returns true, if `u` is a dimensionless unit.
|
|
239
|
+
* Returns true, if `u` is a dimensionless quantity or unit.
|
|
208
240
|
*
|
|
209
241
|
* @param u
|
|
210
242
|
*/
|
|
211
|
-
export const isDimensionless = (u) =>
|
|
243
|
+
export const isDimensionless = (u) => u instanceof Quantity
|
|
244
|
+
? isDimensionless(__qunit(u))
|
|
245
|
+
: __ensureUnit(u).dim.every((x) => x === 0);
|
|
212
246
|
/**
|
|
213
247
|
* Returns true if the two given units are reciprocal to each other (and
|
|
214
248
|
* therefore can be used for conversion).
|
|
@@ -231,7 +265,15 @@ export const isReciprocal = (a, b) => {
|
|
|
231
265
|
}
|
|
232
266
|
return ok;
|
|
233
267
|
};
|
|
268
|
+
/**
|
|
269
|
+
* Polymorphic function. Returns formatted version of given quantity's or unit's
|
|
270
|
+
* SI dimension vector.
|
|
271
|
+
*
|
|
272
|
+
* @param u
|
|
273
|
+
*/
|
|
234
274
|
export const formatSI = (u) => {
|
|
275
|
+
if (u instanceof Quantity)
|
|
276
|
+
return formatSI(__qunit(u));
|
|
235
277
|
const { dim } = __ensureUnit(u);
|
|
236
278
|
const SI = ["kg", "m", "s", "A", "K", "mol", "cd"];
|
|
237
279
|
const acc = [];
|
|
@@ -250,3 +292,16 @@ const __oneHot = (x) => {
|
|
|
250
292
|
dims[x] = 1;
|
|
251
293
|
return dims;
|
|
252
294
|
};
|
|
295
|
+
const __qunit = (q) => (isArray(q.value) ? q.value[0] : q.value);
|
|
296
|
+
const __combineQ = (op, a, b) => {
|
|
297
|
+
const $b = b;
|
|
298
|
+
const vecA = isArray(a.value);
|
|
299
|
+
const vecB = isArray($b.value);
|
|
300
|
+
return new Quantity(vecA
|
|
301
|
+
? vecB
|
|
302
|
+
? a.value.map((x, i) => op(x, $b.value[i]))
|
|
303
|
+
: a.value.map((x) => op(x, $b.value))
|
|
304
|
+
: vecB
|
|
305
|
+
? $b.value.map((x) => op(a.value, x))
|
|
306
|
+
: op(a.value, $b.value));
|
|
307
|
+
};
|
package/units/accel.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const m_s2: import("../api.js").NamedUnit;
|
|
2
|
+
export declare const ft_s2: import("../api.js").NamedUnit;
|
|
3
|
+
export declare const rad_s2: import("../api.js").NamedUnit;
|
|
4
|
+
export declare const g0: import("../api.js").NamedUnit;
|
|
5
|
+
//# sourceMappingURL=accel.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { rad } from "./angle.js";
|
|
2
2
|
import { ft, m } from "./length.js";
|
|
3
3
|
import { s } from "./time.js";
|
|
4
|
-
import { defUnit, div, mul, pow } from "
|
|
4
|
+
import { defUnit, div, mul, pow } from "../unit.js";
|
|
5
5
|
const s2 = pow(s, 2);
|
|
6
6
|
export const m_s2 = defUnit("m/s2", "meter per second squared", div(m, s2));
|
|
7
7
|
export const ft_s2 = defUnit("ft/s2", "foot per second squared", div(ft, s2));
|
package/units/angle.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const rad: import("../api.js").NamedUnit;
|
|
2
|
+
export declare const deg: import("../api.js").NamedUnit;
|
|
3
|
+
export declare const gon: import("../api.js").NamedUnit;
|
|
4
|
+
export declare const turn: import("../api.js").NamedUnit;
|
|
5
|
+
export declare const arcmin: import("../api.js").NamedUnit;
|
|
6
|
+
export declare const arcsec: import("../api.js").NamedUnit;
|
|
7
|
+
export declare const sr: import("../api.js").NamedUnit;
|
|
8
|
+
//# sourceMappingURL=angle.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defUnit, dimensionless, mul } from "
|
|
1
|
+
import { defUnit, dimensionless, mul } from "../unit.js";
|
|
2
2
|
const PI = Math.PI;
|
|
3
3
|
export const rad = defUnit("rad", "radian", dimensionless(1, 0, true));
|
|
4
4
|
export const deg = defUnit("deg", "degree", mul(rad, PI / 180));
|
package/units/area.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const m2: import("../api.js").NamedUnit;
|
|
2
|
+
export declare const mm2: import("../api.js").NamedUnit;
|
|
3
|
+
export declare const cm2: import("../api.js").NamedUnit;
|
|
4
|
+
export declare const km2: import("../api.js").NamedUnit;
|
|
5
|
+
export declare const ha: import("../api.js").NamedUnit;
|
|
6
|
+
export declare const ac: import("../api.js").NamedUnit;
|
|
7
|
+
export declare const sqin: import("../api.js").NamedUnit;
|
|
8
|
+
export declare const sqft: import("../api.js").NamedUnit;
|
|
9
|
+
export declare const sqmi: import("../api.js").NamedUnit;
|
|
10
|
+
//# sourceMappingURL=area.d.ts.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { cm, ft, inch, km, m, mi, mm } from "./length.js";
|
|
2
|
-
import { defUnit, mul, pow } from "
|
|
2
|
+
import { defUnit, mul, pow } from "../unit.js";
|
|
3
3
|
export const m2 = defUnit("m2", "square meter", pow(m, 2));
|
|
4
4
|
export const mm2 = defUnit("mm2", "square millimeter", pow(mm, 2));
|
|
5
5
|
export const cm2 = defUnit("cm2", "square centimeter", pow(cm, 2));
|