@thi.ng/units 0.1.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 +24 -0
- package/LICENSE +201 -0
- package/README.md +525 -0
- package/accel.d.ts +5 -0
- package/accel.js +9 -0
- package/angle.d.ts +8 -0
- package/angle.js +9 -0
- package/api.d.ts +97 -0
- package/api.js +30 -0
- package/area.d.ts +10 -0
- package/area.js +11 -0
- package/data.d.ts +25 -0
- package/data.js +26 -0
- package/electric.d.ts +24 -0
- package/electric.js +28 -0
- package/energy.d.ts +7 -0
- package/energy.js +9 -0
- package/force.d.ts +2 -0
- package/force.js +4 -0
- package/frequency.d.ts +9 -0
- package/frequency.js +10 -0
- package/index.d.ts +22 -0
- package/index.js +21 -0
- package/length.d.ts +20 -0
- package/length.js +20 -0
- package/luminous.d.ts +4 -0
- package/luminous.js +6 -0
- package/mass.d.ts +11 -0
- package/mass.js +11 -0
- package/package.json +158 -0
- package/parts.d.ts +8 -0
- package/parts.js +8 -0
- package/power.d.ts +9 -0
- package/power.js +11 -0
- package/pressure.d.ts +9 -0
- package/pressure.js +11 -0
- package/speed.d.ts +6 -0
- package/speed.js +8 -0
- package/substance.d.ts +2 -0
- package/substance.js +2 -0
- package/temperature.d.ts +4 -0
- package/temperature.js +4 -0
- package/time.d.ts +11 -0
- package/time.js +11 -0
- package/unit.d.ts +158 -0
- package/unit.js +252 -0
- package/volume.d.ts +15 -0
- package/volume.js +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
|
+
|
|
3
|
+
# 
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@thi.ng/units)
|
|
6
|
+

|
|
7
|
+
[](https://mastodon.thi.ng/@toxi)
|
|
8
|
+
|
|
9
|
+
This project is part of the
|
|
10
|
+
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
|
|
11
|
+
|
|
12
|
+
- [About](#about)
|
|
13
|
+
- [Unit definitions](#unit-definitions)
|
|
14
|
+
- [Predefined units](#predefined-units)
|
|
15
|
+
- [Acceleration](#acceleration)
|
|
16
|
+
- [Angle](#angle)
|
|
17
|
+
- [Area](#area)
|
|
18
|
+
- [Data](#data)
|
|
19
|
+
- [Electric current](#electric-current)
|
|
20
|
+
- [Energy](#energy)
|
|
21
|
+
- [Force](#force)
|
|
22
|
+
- [Frequency](#frequency)
|
|
23
|
+
- [Length](#length)
|
|
24
|
+
- [Luminous intensity](#luminous-intensity)
|
|
25
|
+
- [Mass](#mass)
|
|
26
|
+
- [Parts per notation](#parts-per-notation)
|
|
27
|
+
- [Power](#power)
|
|
28
|
+
- [Pressure](#pressure)
|
|
29
|
+
- [Speed](#speed)
|
|
30
|
+
- [Substance](#substance)
|
|
31
|
+
- [Temperature](#temperature)
|
|
32
|
+
- [Time](#time)
|
|
33
|
+
- [Volume](#volume)
|
|
34
|
+
- [Creating & deriving units](#creating--deriving-units)
|
|
35
|
+
- [Using standard metric prefixes](#using-standard-metric-prefixes)
|
|
36
|
+
- [Unit combinators](#unit-combinators)
|
|
37
|
+
- [Unit conversions](#unit-conversions)
|
|
38
|
+
- [Status](#status)
|
|
39
|
+
- [Installation](#installation)
|
|
40
|
+
- [Dependencies](#dependencies)
|
|
41
|
+
- [API](#api)
|
|
42
|
+
- [Authors](#authors)
|
|
43
|
+
- [License](#license)
|
|
44
|
+
|
|
45
|
+
## About
|
|
46
|
+
|
|
47
|
+
Extensible SI unit creation, conversions & calculations (~150 units predefined).
|
|
48
|
+
|
|
49
|
+
All unit definitions & conversions are based on the SI unit system & concepts
|
|
50
|
+
described here:
|
|
51
|
+
|
|
52
|
+
- https://en.wikipedia.org/wiki/International_System_of_Units
|
|
53
|
+
- https://en.wikipedia.org/wiki/SI_base_unit
|
|
54
|
+
- https://en.wikipedia.org/wiki/SI_derived_unit
|
|
55
|
+
- https://en.wikipedia.org/wiki/Coherence_(units_of_measurement)
|
|
56
|
+
- https://en.wikipedia.org/wiki/Metric_prefix
|
|
57
|
+
|
|
58
|
+
### Unit definitions
|
|
59
|
+
|
|
60
|
+
Each unit is defined via a 7-dimensional vector representing individual
|
|
61
|
+
exponents for each of the [SI base unit
|
|
62
|
+
dimensions](https://en.wikipedia.org/wiki/SI_base_unit), in order:
|
|
63
|
+
|
|
64
|
+
| id | SI dimension | Base unit | Base unit symbol |
|
|
65
|
+
|----|---------------------|-----------|------------------|
|
|
66
|
+
| 0 | mass | kilogram | kg |
|
|
67
|
+
| 1 | length | meter | m |
|
|
68
|
+
| 2 | time | second | s |
|
|
69
|
+
| 3 | current | ampere | A |
|
|
70
|
+
| 4 | temperature | kelvin | K |
|
|
71
|
+
| 5 | amount of substance | mole | mol |
|
|
72
|
+
| 6 | luminous intensity | candela | cd |
|
|
73
|
+
|
|
74
|
+
Dimensionless units (e.g. radian, byte) are supported too and represented by a
|
|
75
|
+
vector with all dimensions set to zero.
|
|
76
|
+
|
|
77
|
+
Additionally, we also define a scale factor and zero offset for each unit, with
|
|
78
|
+
most dimensions' base units usually using a factor of 1 and no offset.
|
|
79
|
+
|
|
80
|
+
For example, here's how we can define kilograms and meters:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// kilogram, SI dimension 0
|
|
84
|
+
const KG = coherent(1);
|
|
85
|
+
// { dim: [ 1, 0, 0, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
86
|
+
|
|
87
|
+
// meters, SI dimension 1
|
|
88
|
+
const M = coherent(1);
|
|
89
|
+
// { dim: [ 0, 1, 0, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
90
|
+
|
|
91
|
+
// kelvin, SI dimension 4 (here without syntax sugar)
|
|
92
|
+
const K = unit(4, 1, 0, true);
|
|
93
|
+
// { dim: [ 0, 0, 0, 0, 1, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
94
|
+
|
|
95
|
+
// fahrenheit, SI dim 4 with custom scale factor and zero offset
|
|
96
|
+
const F = unit(4, 1 / 1.8, 459.67 / 1.8);
|
|
97
|
+
// { dim: [ 0, 0, 0, 0, 1, 0, 0 ], scale: 0.5555, offset: 255.3722, coherent: false }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
More complex units like electrical resistance (e.g. ohm) are based on more than
|
|
101
|
+
a single dimension:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// ohm = volt / ampere
|
|
105
|
+
div(V, A)
|
|
106
|
+
// { dim: [ 1, 2, -3, -2, 0, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This dimension vector represents the unit definition for (see [SI derived
|
|
110
|
+
units](https://en.wikipedia.org/wiki/SI_derived_unit)):
|
|
111
|
+
|
|
112
|
+
> Ω = kg⋅m<sup>2</sup>⋅s<sup>−3</sup>⋅A<sup>−2</sup>
|
|
113
|
+
|
|
114
|
+
Btw. The [`formatSI()`]() function can be used to format a unit's dimension
|
|
115
|
+
vector:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
formatSI(div(V, A));
|
|
119
|
+
// "kg·m2·s-3·A-2"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Predefined units
|
|
123
|
+
|
|
124
|
+
The following units are provided as "builtins", here grouped by dimension:
|
|
125
|
+
|
|
126
|
+
#### Acceleration
|
|
127
|
+
|
|
128
|
+
| Unit name | Variable name | Description |
|
|
129
|
+
|-----------|---------------|---------------------------|
|
|
130
|
+
| `m/s2` | `m_s2` | meter per second squared |
|
|
131
|
+
| `ft/s2` | `ft_s2` | foot per second squared |
|
|
132
|
+
| `rad/s2` | `rad_s2` | radian per second squared |
|
|
133
|
+
| `g0` | `g0` | standard gravity |
|
|
134
|
+
|
|
135
|
+
#### Angle
|
|
136
|
+
|
|
137
|
+
| Unit name | Variable name | Description |
|
|
138
|
+
|-----------|---------------|-------------|
|
|
139
|
+
| `arcmin` | `arcmin` | arc minute |
|
|
140
|
+
| `arcsec` | `arcsec` | arc second |
|
|
141
|
+
| `deg` | `deg` | degree |
|
|
142
|
+
| `gon` | `gon` | gradian |
|
|
143
|
+
| `rad` | `rad` | radian |
|
|
144
|
+
| `sr` | `sr` | steradian |
|
|
145
|
+
| `turn` | `turn` | turn |
|
|
146
|
+
|
|
147
|
+
#### Area
|
|
148
|
+
|
|
149
|
+
| Unit name | Variable name | Description |
|
|
150
|
+
|-----------|---------------|-------------------|
|
|
151
|
+
| `m2` | `m2` | square meter |
|
|
152
|
+
| `cm2` | `cm2` | square centimeter |
|
|
153
|
+
| `mm2` | `mm2` | square millimeter |
|
|
154
|
+
| `km2` | `km2` | square kilometer |
|
|
155
|
+
| `ha` | `ha` | hectar |
|
|
156
|
+
| `ac` | `ac` | acre |
|
|
157
|
+
| `sqin` | `sqin` | square inch |
|
|
158
|
+
| `sqft` | `sqft` | square foot |
|
|
159
|
+
| `sqmi` | `sqmi` | square mile |
|
|
160
|
+
|
|
161
|
+
#### Data
|
|
162
|
+
|
|
163
|
+
| Unit name | Variable name | Description |
|
|
164
|
+
|-----------|---------------|-------------------|
|
|
165
|
+
| `bit` | `bit` | bit |
|
|
166
|
+
| `kbit` | `kbit` | kilobit |
|
|
167
|
+
| `Mbit` | `Mbit` | megabit |
|
|
168
|
+
| `Gbit` | `Gbit` | gigabit |
|
|
169
|
+
| `Tbit` | `Tbit` | terabit |
|
|
170
|
+
| `kibit` | `kibit` | kibibit (1024) |
|
|
171
|
+
| `Mibit` | `Mibit` | mebibit (1024) |
|
|
172
|
+
| `Gibit` | `Gibit` | gibibit (1024) |
|
|
173
|
+
| `Tibit` | `Tibit` | tebibit (1024) |
|
|
174
|
+
| `B` | `B` | byte (8 bit) |
|
|
175
|
+
| `kB` | `kB` | kilobyte (metric) |
|
|
176
|
+
| `MB` | `MB` | megabyte (metric) |
|
|
177
|
+
| `GB` | `GB` | gigabyte (metric) |
|
|
178
|
+
| `TB` | `TB` | terabyte (metric) |
|
|
179
|
+
| `PB` | `PB` | petabyte (metric) |
|
|
180
|
+
| `EB` | `EB` | exabyte (metric) |
|
|
181
|
+
| `KiB` | `KiB` | kibibyte (1024) |
|
|
182
|
+
| `MiB` | `MiB` | mebibyte (1024) |
|
|
183
|
+
| `GiB` | `GiB` | gibibyte (1024) |
|
|
184
|
+
| `TiB` | `TiB` | tebibyte (1024) |
|
|
185
|
+
| `PiB` | `PiB` | pebibyte (1024) |
|
|
186
|
+
| `EiB` | `EiB` | exbibyte (1024) |
|
|
187
|
+
|
|
188
|
+
#### Electric current
|
|
189
|
+
|
|
190
|
+
| Unit | Variable name | Description |
|
|
191
|
+
|-------|---------------|-------------------|
|
|
192
|
+
| `A` | `A` | ampere |
|
|
193
|
+
| `mA` | `mA` | milliampere |
|
|
194
|
+
| `mAh` | `mAh` | milliampere-hours |
|
|
195
|
+
| `C` | `C` | coulomb |
|
|
196
|
+
| `V` | `V` | volt |
|
|
197
|
+
| `mV` | `mV` | millivolt |
|
|
198
|
+
| `kV` | `kV` | kilovolt |
|
|
199
|
+
| `MV` | `MV` | megavolt |
|
|
200
|
+
| `F` | `F` | farad |
|
|
201
|
+
| `pF` | `pF` | picofarad |
|
|
202
|
+
| `µF` | `µF` | microfarad |
|
|
203
|
+
| `Ω` | `Ω` / `ohm` | ohm |
|
|
204
|
+
| `kΩ` | `kΩ` / `kohm` | kiloohm |
|
|
205
|
+
| `MΩ` | `MΩ` / `Mohm` | megaohm |
|
|
206
|
+
| `GΩ` | `GΩ` / `Gohm` | gigaohm |
|
|
207
|
+
| `S` | `S` | siemens |
|
|
208
|
+
| `Wb` | `Wb` | weber |
|
|
209
|
+
| `T` | `T` | tesla |
|
|
210
|
+
| `H` | `H` | henry |
|
|
211
|
+
|
|
212
|
+
#### Energy
|
|
213
|
+
|
|
214
|
+
| Unit | Variable name | Description |
|
|
215
|
+
|--------|---------------|-------------|
|
|
216
|
+
| `J` | `J` | joule |
|
|
217
|
+
| `kJ` | `kJ` | kilojoule |
|
|
218
|
+
| `MJ` | `MJ` | megajoule |
|
|
219
|
+
| `GJ` | `GJ` | gigajoule |
|
|
220
|
+
| `cal` | `cal` | calorie |
|
|
221
|
+
| `kcal` | `kcal` | kilocalorie |
|
|
222
|
+
|
|
223
|
+
#### Force
|
|
224
|
+
|
|
225
|
+
| Unit | Variable name | Description |
|
|
226
|
+
|------|---------------|-------------|
|
|
227
|
+
| `N` | `N` | newton |
|
|
228
|
+
|
|
229
|
+
#### Frequency
|
|
230
|
+
|
|
231
|
+
| Unit | Variable name | Description |
|
|
232
|
+
|-------|---------------|---------------------|
|
|
233
|
+
| `Hz` | `Hz` | hertz |
|
|
234
|
+
| `kHz` | `KHz` | kilohertz |
|
|
235
|
+
| `MHz` | `MHz` | megahertz |
|
|
236
|
+
| `GHz` | `GHz` | gigahertz |
|
|
237
|
+
| `THz` | `THz` | terahertz |
|
|
238
|
+
| `rpm` | `rpm` | rotation per minute |
|
|
239
|
+
| `ω` | `ω` / `omega` | radian per second |
|
|
240
|
+
|
|
241
|
+
#### Length
|
|
242
|
+
|
|
243
|
+
| Unit name | Variable name | Description |
|
|
244
|
+
|-----------|----------------|-------------------|
|
|
245
|
+
| `m` | `m` | meter |
|
|
246
|
+
| `Å` | `angstrom` | angstrom |
|
|
247
|
+
| `nm` | `nm` | nanometer |
|
|
248
|
+
| `µm` | `µm` | micrometer |
|
|
249
|
+
| `mm` | `mm` | millimeter |
|
|
250
|
+
| `cm` | `cm` | centimeter |
|
|
251
|
+
| `km` | `km` | kilometer |
|
|
252
|
+
| `au` | `au` | astronomical unit |
|
|
253
|
+
| `pc` | `pc` | parsec |
|
|
254
|
+
| `ly` | `ly` | light year |
|
|
255
|
+
| `in` | `in` | inch |
|
|
256
|
+
| `mil` | `mil` / `thou` | 1/1000th inch |
|
|
257
|
+
| `ft` | `ft` | foot |
|
|
258
|
+
| `yd` | `yd` | yard |
|
|
259
|
+
| `mi` | `mi` | mile |
|
|
260
|
+
| `nmi` | `nmi` | nautical mile |
|
|
261
|
+
| `pica` | `pica` | pica |
|
|
262
|
+
| `point` | `point` | point |
|
|
263
|
+
|
|
264
|
+
#### Luminous intensity
|
|
265
|
+
|
|
266
|
+
| Unit | Variable name | Description |
|
|
267
|
+
|------|---------------|-------------|
|
|
268
|
+
| `cd` | `cd` | candela |
|
|
269
|
+
| `lm` | `lm` | lumen |
|
|
270
|
+
| `lx` | `lx` | lux |
|
|
271
|
+
|
|
272
|
+
#### Mass
|
|
273
|
+
|
|
274
|
+
| Unit name | Variable name | Description |
|
|
275
|
+
|-----------|---------------|----------------|
|
|
276
|
+
| `µg` | `µg` | microgram |
|
|
277
|
+
| `mg` | `mg` | milligram |
|
|
278
|
+
| `g` | `g` | gram |
|
|
279
|
+
| `kg` | `kg` | kilogram |
|
|
280
|
+
| `t` | `t` | tonne |
|
|
281
|
+
| `kt` | `kt` | kilotonne |
|
|
282
|
+
| `Mt` | `Mt` | megatonne |
|
|
283
|
+
| `Gt` | `Gt` | gigatonne |
|
|
284
|
+
| `lb` | `lb` | imperial pound |
|
|
285
|
+
| `st` | `st` | stone |
|
|
286
|
+
|
|
287
|
+
#### Parts per notation
|
|
288
|
+
|
|
289
|
+
https://en.wikipedia.org/wiki/Parts-per_notation
|
|
290
|
+
|
|
291
|
+
| Unit name | Variable name | Description |
|
|
292
|
+
|-----------|---------------|--------------------------------|
|
|
293
|
+
| `%` | `percent` | part per hundred |
|
|
294
|
+
| `‰` | `permille` | part per thousand |
|
|
295
|
+
| `‱` | `permyriad` | part per ten thousand |
|
|
296
|
+
| `pcm` | `pcm` | part per cent hundred thousand |
|
|
297
|
+
| `ppm` | `ppm` | part per million |
|
|
298
|
+
| `ppb` | `ppb` | part per billion |
|
|
299
|
+
| `ppt` | `ppt` | part per trillion |
|
|
300
|
+
|
|
301
|
+
#### Power
|
|
302
|
+
|
|
303
|
+
| Unit name | Variable name | Description |
|
|
304
|
+
|-----------|---------------|---------------|
|
|
305
|
+
| `W` | `W` | watt |
|
|
306
|
+
| `mW` | `mW` | milliwatt |
|
|
307
|
+
| `kW` | `kW` | kilowatt |
|
|
308
|
+
| `MW` | `MW` | megawatt |
|
|
309
|
+
| `GW` | `GW` | gigawatt |
|
|
310
|
+
| `TW` | `TW` | terawatt |
|
|
311
|
+
| `Wh` | `Wh` | watt-hour |
|
|
312
|
+
| `kWh` | `kWh` | kilowatt-hour |
|
|
313
|
+
|
|
314
|
+
#### Pressure
|
|
315
|
+
|
|
316
|
+
| Unit name | Variable name | Description |
|
|
317
|
+
|-----------|---------------|-----------------------|
|
|
318
|
+
| `Pa` | `Pa` | pascal |
|
|
319
|
+
| `kPa` | `KPa` | kilopascal |
|
|
320
|
+
| `MPa` | `MPa` | megapascal |
|
|
321
|
+
| `GPa` | `GPa` | gigapascal |
|
|
322
|
+
| `at` | `at` | technical atmosphere |
|
|
323
|
+
| `atm` | `atm` | atmosphere |
|
|
324
|
+
| `bar` | `bar` | bar |
|
|
325
|
+
| `psi` | `psi` | pound per square inch |
|
|
326
|
+
|
|
327
|
+
#### Speed
|
|
328
|
+
|
|
329
|
+
| Unit | Variable name | Description |
|
|
330
|
+
|--------|---------------|--------------------|
|
|
331
|
+
| `m/s` | `m_s` | meter per second |
|
|
332
|
+
| `km/h` | `km_h` | kilometer per hour |
|
|
333
|
+
| `mph` | `mph` | mile per hour |
|
|
334
|
+
| `kn` | `kn` | knot |
|
|
335
|
+
|
|
336
|
+
#### Substance
|
|
337
|
+
|
|
338
|
+
| Unit | Variable name | Description |
|
|
339
|
+
|-------|---------------|-------------|
|
|
340
|
+
| `mol` | `mol` | mole |
|
|
341
|
+
|
|
342
|
+
#### Temperature
|
|
343
|
+
|
|
344
|
+
| Unit | Variable name | Description |
|
|
345
|
+
|------|---------------|-------------------|
|
|
346
|
+
| `K` | `K` | kelvin |
|
|
347
|
+
| `℃` | `celsius` | degree celsius |
|
|
348
|
+
| `℉` | `fahrenheit` | degree fahrenheit |
|
|
349
|
+
|
|
350
|
+
#### Time
|
|
351
|
+
|
|
352
|
+
| Unit | Variable name | Description |
|
|
353
|
+
|---------|---------------|--------------------|
|
|
354
|
+
| `s` | `s` | second |
|
|
355
|
+
| `ms` | `ms` | millisecond |
|
|
356
|
+
| `µs` | `µs` | microsecond |
|
|
357
|
+
| `ns` | `ns` | nanosecond |
|
|
358
|
+
| `min` | `min` | minute |
|
|
359
|
+
| `h` | `h` | hour |
|
|
360
|
+
| `d` | `d` | day |
|
|
361
|
+
| `week` | `week` | week |
|
|
362
|
+
| `month` | `month` | month (30 days) |
|
|
363
|
+
| `year` | `year` | year (365.25 days) |
|
|
364
|
+
|
|
365
|
+
#### Volume
|
|
366
|
+
|
|
367
|
+
| Unit | Variable name | Description |
|
|
368
|
+
|------------|---------------|----------------------|
|
|
369
|
+
| `m3` | `m3` | cubic meter |
|
|
370
|
+
| `mm3` | `mm3` | cubic millimeter |
|
|
371
|
+
| `cm3` | `cm3` | cubic centimeter |
|
|
372
|
+
| `km3` | `km3` | cubic kilometer |
|
|
373
|
+
| `l` | `l` | liter |
|
|
374
|
+
| `cl` | `cl` | centiliter |
|
|
375
|
+
| `ml` | `ml` | milliliter |
|
|
376
|
+
| `gal` | `gal` | imperial gallon |
|
|
377
|
+
| `pt` | `pt` | imperial pint |
|
|
378
|
+
| `fl oz` | `floz` | imperial fluid ounce |
|
|
379
|
+
| `us gal` | `us_gal` | US gallon |
|
|
380
|
+
| `us pt` | `us_pt` | US pint |
|
|
381
|
+
| `us cup` | `us_cup` | US cup |
|
|
382
|
+
| `us fl oz` | `us_floz` | US fluid ounce |
|
|
383
|
+
|
|
384
|
+
### Creating & deriving units
|
|
385
|
+
|
|
386
|
+
#### Using standard metric prefixes
|
|
387
|
+
|
|
388
|
+
Existing [coherent units]() can be
|
|
389
|
+
[prefixed](https://docs.thi.ng/umbrella/units/functions/prefix-1.html) to produce
|
|
390
|
+
derived versions:
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
// define micrometer (also available as preset)
|
|
394
|
+
prefix("µ", "m")
|
|
395
|
+
// { dim: [ 0, 1, 0, 0, 0, 0, 0 ], scale: 0.000001, offset: 0, coherent: false }
|
|
396
|
+
|
|
397
|
+
// define kKhz
|
|
398
|
+
prefix("k", Hz);
|
|
399
|
+
// { dim: [ 0, 0, -1, 0, 0, 0, 0 ], scale: 1000, offset: 0, coherent: false }
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
#### Unit combinators
|
|
403
|
+
|
|
404
|
+
The following combinators can be used to derive scaled and/or more complex units
|
|
405
|
+
in multiple SI dimensions:
|
|
406
|
+
|
|
407
|
+
- [`div(a, b)`](https://docs.thi.ng/umbrella/units/functions/div.html): derives
|
|
408
|
+
a new unit via the division of the given units
|
|
409
|
+
- [`mul(a, b)`](https://docs.thi.ng/umbrella/units/functions/mul.html): derives
|
|
410
|
+
a new unit as the product of the given units
|
|
411
|
+
- [`pow(u, k)`](https://docs.thi.ng/umbrella/units/functions/pow.html): raises
|
|
412
|
+
given unit to power `k` (e.g. meter ⇒ square meter)
|
|
413
|
+
- [`reciprocal(u)`](https://docs.thi.ng/umbrella/units/functions/reciprocal.html):
|
|
414
|
+
Creates reciprocal of given unit (e.g. Hz ⇒ 1/second)
|
|
415
|
+
|
|
416
|
+
```ts
|
|
417
|
+
// acceleration (meter per second squared)
|
|
418
|
+
const m_s2 = div(m, pow(s, 2));
|
|
419
|
+
// { dim: [ 0, 1, -2, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: false }
|
|
420
|
+
|
|
421
|
+
// define kilowatt-hour (also available as preset)
|
|
422
|
+
const kWh = mul(prefix("k","W"), "h");
|
|
423
|
+
// { dim: [ 1, 2, -2, 0, 0, 0, 0 ], scale: 3600000, offset: 0, coherent: false }
|
|
424
|
+
|
|
425
|
+
// define `word` as 16 bits
|
|
426
|
+
const word = mul(bit, 16);
|
|
427
|
+
// { dim: [ 0, 0, 0, 0, 0, 0, 0 ], scale: 16, offset: 0, coherent: false }
|
|
428
|
+
|
|
429
|
+
// Hz = 1/s
|
|
430
|
+
const Hz = reciprocal(s);
|
|
431
|
+
// { dim: [ 0, 0, -1, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: false }
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Unit conversions
|
|
435
|
+
|
|
436
|
+
Only units with compatible (incl. reciprocal) dimensions can be converted,
|
|
437
|
+
otherwise an error will be thrown. All dimensionless units can be converted
|
|
438
|
+
to other dimensionless units (even if it would be semantic nonsense).
|
|
439
|
+
|
|
440
|
+
Units can be specified in various ways:
|
|
441
|
+
|
|
442
|
+
```ts
|
|
443
|
+
// convert from km/h to mph using unit names
|
|
444
|
+
convert(100, "km/h", "mph");
|
|
445
|
+
// 62.13711922373341
|
|
446
|
+
|
|
447
|
+
// or using predefined unit constants directly
|
|
448
|
+
convert(60, mph, km_h);
|
|
449
|
+
// 96.56063999999998
|
|
450
|
+
|
|
451
|
+
// or using anonymous units (meter/second ⇒ yard/hour)
|
|
452
|
+
convert(1, "m/s", div(yd, h));
|
|
453
|
+
// 3937.007874015749
|
|
454
|
+
|
|
455
|
+
// convert into opposite direction (meter/second ⇒ second/meter)
|
|
456
|
+
convert(10, "m/s", reciprocal("m/s"));
|
|
457
|
+
// 0.1
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
Another example using dimensionless units (here angles, arc second ⇒ radian) to
|
|
461
|
+
compute the distance of 10 arcsec on the earth surface (in meters):
|
|
462
|
+
|
|
463
|
+
```ts
|
|
464
|
+
const R = 6371000; // earth radius in meters
|
|
465
|
+
|
|
466
|
+
convert(10, "arcsec", "rad") * R;
|
|
467
|
+
// 308.87479623488537
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
## Status
|
|
471
|
+
|
|
472
|
+
**BETA** - possibly breaking changes forthcoming
|
|
473
|
+
|
|
474
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bunits%5D+in%3Atitle)
|
|
475
|
+
|
|
476
|
+
## Installation
|
|
477
|
+
|
|
478
|
+
```bash
|
|
479
|
+
yarn add @thi.ng/units
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
ES module import:
|
|
483
|
+
|
|
484
|
+
```html
|
|
485
|
+
<script type="module" src="https://cdn.skypack.dev/@thi.ng/units"></script>
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
[Skypack documentation](https://docs.skypack.dev/)
|
|
489
|
+
|
|
490
|
+
For Node.js REPL:
|
|
491
|
+
|
|
492
|
+
```js
|
|
493
|
+
const units = await import("@thi.ng/units");
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 3.57 KB
|
|
497
|
+
|
|
498
|
+
## Dependencies
|
|
499
|
+
|
|
500
|
+
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
501
|
+
- [@thi.ng/equiv](https://github.com/thi-ng/umbrella/tree/develop/packages/equiv)
|
|
502
|
+
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
503
|
+
|
|
504
|
+
## API
|
|
505
|
+
|
|
506
|
+
[Generated API docs](https://docs.thi.ng/umbrella/units/)
|
|
507
|
+
|
|
508
|
+
## Authors
|
|
509
|
+
|
|
510
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
511
|
+
|
|
512
|
+
If this project contributes to an academic publication, please cite it as:
|
|
513
|
+
|
|
514
|
+
```bibtex
|
|
515
|
+
@misc{thing-units,
|
|
516
|
+
title = "@thi.ng/units",
|
|
517
|
+
author = "Karsten Schmidt",
|
|
518
|
+
note = "https://thi.ng/units",
|
|
519
|
+
year = 2021
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
## License
|
|
524
|
+
|
|
525
|
+
© 2021 - 2023 Karsten Schmidt // Apache License 2.0
|
package/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
|
package/accel.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { rad } from "./angle.js";
|
|
2
|
+
import { ft, m } from "./length.js";
|
|
3
|
+
import { s } from "./time.js";
|
|
4
|
+
import { defUnit, div, mul, pow } from "./unit.js";
|
|
5
|
+
const s2 = pow(s, 2);
|
|
6
|
+
export const m_s2 = defUnit("m/s2", "meter per second squared", div(m, s2));
|
|
7
|
+
export const ft_s2 = defUnit("ft/s2", "foot per second squared", div(ft, s2));
|
|
8
|
+
export const rad_s2 = defUnit("rad/s2", "radian per second squared", div(rad, s2));
|
|
9
|
+
export const g0 = defUnit("g0", "standard gravity", mul(m_s2, 9.80665));
|
package/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
|
package/angle.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defUnit, dimensionless, mul } from "./unit.js";
|
|
2
|
+
const PI = Math.PI;
|
|
3
|
+
export const rad = defUnit("rad", "radian", dimensionless(1, 0, true));
|
|
4
|
+
export const deg = defUnit("deg", "degree", mul(rad, PI / 180));
|
|
5
|
+
export const gon = defUnit("gon", "gradian", mul(rad, PI / 200));
|
|
6
|
+
export const turn = defUnit("turn", "turn", mul(rad, 2 * PI));
|
|
7
|
+
export const arcmin = defUnit("arcmin", "arc minute", mul(rad, PI / 10800));
|
|
8
|
+
export const arcsec = defUnit("arcsec", "arc second", mul(rad, PI / 648000));
|
|
9
|
+
export const sr = defUnit("sr", "steradian", dimensionless(1, 0, true));
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export interface Unit {
|
|
2
|
+
/**
|
|
3
|
+
* SI base dimension vector ({@link Dimensions})
|
|
4
|
+
*/
|
|
5
|
+
dim: Dimensions;
|
|
6
|
+
/**
|
|
7
|
+
* Scaling factor relative to the coherent unit
|
|
8
|
+
*/
|
|
9
|
+
scale: number;
|
|
10
|
+
/**
|
|
11
|
+
* Zero offset value
|
|
12
|
+
*/
|
|
13
|
+
offset: number;
|
|
14
|
+
/**
|
|
15
|
+
* True, if this unit is coherent.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Reference:
|
|
19
|
+
* - https://en.wikipedia.org/wiki/Coherence_(units_of_measurement)
|
|
20
|
+
*/
|
|
21
|
+
coherent: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface NamedUnit extends Unit {
|
|
24
|
+
/**
|
|
25
|
+
* Symbol under which this unit can be looked up with via {@link asUnit} and
|
|
26
|
+
* others.
|
|
27
|
+
*/
|
|
28
|
+
sym: string;
|
|
29
|
+
/**
|
|
30
|
+
* This unit's human readable description/name.
|
|
31
|
+
*/
|
|
32
|
+
name: string;
|
|
33
|
+
}
|
|
34
|
+
export type MaybeUnit = Unit | string;
|
|
35
|
+
/**
|
|
36
|
+
* Vector of the 7 basic SI unit dimensions.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* In order:
|
|
40
|
+
*
|
|
41
|
+
* - 0 = mass
|
|
42
|
+
* - 1 = length
|
|
43
|
+
* - 2 = time
|
|
44
|
+
* - 3 = current
|
|
45
|
+
* - 4 = temperature
|
|
46
|
+
* - 5 = amount of substance
|
|
47
|
+
* - 6 = luminous intensity
|
|
48
|
+
*
|
|
49
|
+
* Note: For dimensionless units, all dimensions are zero.
|
|
50
|
+
*
|
|
51
|
+
* Reference:
|
|
52
|
+
* - https://en.wikipedia.org/wiki/SI_base_unit
|
|
53
|
+
*/
|
|
54
|
+
export type Dimensions = [
|
|
55
|
+
number,
|
|
56
|
+
number,
|
|
57
|
+
number,
|
|
58
|
+
number,
|
|
59
|
+
number,
|
|
60
|
+
number,
|
|
61
|
+
number
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
* A known metric prefix.
|
|
65
|
+
*/
|
|
66
|
+
export type Prefix = keyof typeof PREFIXES;
|
|
67
|
+
/**
|
|
68
|
+
* @remarks
|
|
69
|
+
* Reference:
|
|
70
|
+
* - https://en.wikipedia.org/wiki/Metric_prefix
|
|
71
|
+
*/
|
|
72
|
+
export declare const PREFIXES: {
|
|
73
|
+
Q: number;
|
|
74
|
+
R: number;
|
|
75
|
+
Y: number;
|
|
76
|
+
Z: number;
|
|
77
|
+
E: number;
|
|
78
|
+
P: number;
|
|
79
|
+
T: number;
|
|
80
|
+
G: number;
|
|
81
|
+
M: number;
|
|
82
|
+
k: number;
|
|
83
|
+
h: number;
|
|
84
|
+
d: number;
|
|
85
|
+
c: number;
|
|
86
|
+
m: number;
|
|
87
|
+
µ: number;
|
|
88
|
+
n: number;
|
|
89
|
+
p: number;
|
|
90
|
+
f: number;
|
|
91
|
+
a: number;
|
|
92
|
+
z: number;
|
|
93
|
+
y: number;
|
|
94
|
+
r: number;
|
|
95
|
+
q: number;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=api.d.ts.map
|
package/api.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remarks
|
|
3
|
+
* Reference:
|
|
4
|
+
* - https://en.wikipedia.org/wiki/Metric_prefix
|
|
5
|
+
*/
|
|
6
|
+
export const PREFIXES = {
|
|
7
|
+
Q: 1e30,
|
|
8
|
+
R: 1e27,
|
|
9
|
+
Y: 1e24,
|
|
10
|
+
Z: 1e21,
|
|
11
|
+
E: 1e18,
|
|
12
|
+
P: 1e15,
|
|
13
|
+
T: 1e12,
|
|
14
|
+
G: 1e9,
|
|
15
|
+
M: 1e6,
|
|
16
|
+
k: 1e3,
|
|
17
|
+
h: 1e2,
|
|
18
|
+
d: 1e-1,
|
|
19
|
+
c: 1e-2,
|
|
20
|
+
m: 1e-3,
|
|
21
|
+
µ: 1e-6,
|
|
22
|
+
n: 1e-9,
|
|
23
|
+
p: 1e-12,
|
|
24
|
+
f: 1e-15,
|
|
25
|
+
a: 1e-18,
|
|
26
|
+
z: 1e-21,
|
|
27
|
+
y: 1e-24,
|
|
28
|
+
r: 1e-27,
|
|
29
|
+
q: 1e-30,
|
|
30
|
+
};
|
package/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
|