@isentropic/dim-si 0.4.2 → 0.4.3
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/README.md +167 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# @isentropic/dim-si
|
|
2
|
+
|
|
3
|
+
Ready-to-use SI units with compile-time dimensional analysis.
|
|
4
|
+
|
|
5
|
+
Provides the [SI](https://en.wikipedia.org/wiki/International_System_of_Units)
|
|
6
|
+
unit system — unit conversions, SI prefixes, and affine units like temperature
|
|
7
|
+
scales, all with dimensions tracked at the type level. Wrap raw numbers in typed
|
|
8
|
+
quantities and let the compiler catch dimension mismatches before your code
|
|
9
|
+
runs.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Create and compute
|
|
14
|
+
|
|
15
|
+
Unit functions wrap raw numbers into typed quantities. The fluent `q()` API
|
|
16
|
+
chains arithmetic while tracking dimensions at compile time:
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { kilometer, meter } from "@isentropic/dim-si/length";
|
|
20
|
+
import { hour } from "@isentropic/dim-si/time";
|
|
21
|
+
import { q } from "@isentropic/dim-si/ops";
|
|
22
|
+
|
|
23
|
+
const distance = kilometer(100);
|
|
24
|
+
const duration = hour(2);
|
|
25
|
+
|
|
26
|
+
const speed = q(distance).div(duration);
|
|
27
|
+
const total = q(kilometer(5)).plus(meter(500));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Temperature conversions handle offsets correctly. Absolute temperatures use
|
|
31
|
+
affine conversion (applying the zero-point offset), while deltas stay linear:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { celsius, kelvin } from "@isentropic/dim-si/temperature";
|
|
35
|
+
|
|
36
|
+
q(celsius(100)).in(kelvin); // 373.15
|
|
37
|
+
q(celsius(100)).minus(celsius(0)).in(kelvin); // 100 (linear delta)
|
|
38
|
+
q(celsius(20)).plus(celsius.delta(5)).in(celsius); // 25
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Extract values
|
|
42
|
+
|
|
43
|
+
Use `.in(unit)` to get a plain number back — for example, to serialize or
|
|
44
|
+
display:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { meterPerSecond } from "@isentropic/dim-si/velocity";
|
|
48
|
+
|
|
49
|
+
speed.in(meterPerSecond); // ~13.89
|
|
50
|
+
total.in(meter); // 5500
|
|
51
|
+
total.in(kilometer); // 5.5
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The free function `valueIn(quantity, unit)` does the same thing outside a chain.
|
|
55
|
+
Free functions `add`, `subtract`, `multiply`, `divide`, and `scale` are also
|
|
56
|
+
available from `@isentropic/dim-si/ops`.
|
|
57
|
+
|
|
58
|
+
### Type safety
|
|
59
|
+
|
|
60
|
+
Dimension mismatches are caught at compile time:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
q(kilometer(5)).plus(hour(1)); // Error: can't add length and time
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Use the exported quantity types for function signatures:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import type { Length } from "@isentropic/dim-si/length";
|
|
70
|
+
import type { Time } from "@isentropic/dim-si/time";
|
|
71
|
+
import type { Velocity } from "@isentropic/dim-si/velocity";
|
|
72
|
+
|
|
73
|
+
function speed(d: Length, t: Time): Velocity {
|
|
74
|
+
return q(d).div(t);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Custom units
|
|
79
|
+
|
|
80
|
+
Use SI prefixes to create units not provided out of the box:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { meter } from "@isentropic/dim-si/length";
|
|
84
|
+
import { gram } from "@isentropic/dim-si/mass";
|
|
85
|
+
import { MEGA, PICO } from "@isentropic/dim-si/prefixes";
|
|
86
|
+
|
|
87
|
+
const megameter = meter.scaled(MEGA);
|
|
88
|
+
const picogram = gram.scaled(PICO);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
See
|
|
92
|
+
[prefixes.ts](https://github.com/isentropic-dev/dim/blob/main/packages/dim-si/src/prefixes.ts)
|
|
93
|
+
for all available SI prefixes.
|
|
94
|
+
|
|
95
|
+
You can also compose units from other unit scales:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { joule } from "@isentropic/dim-si/energy";
|
|
99
|
+
import { kilowatt } from "@isentropic/dim-si/power";
|
|
100
|
+
import { hour } from "@isentropic/dim-si/time";
|
|
101
|
+
|
|
102
|
+
const kilowattHour = joule.scaled(kilowatt.scale * hour.scale);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
If you find yourself using a custom unit frequently, consider
|
|
106
|
+
[contributing it](https://github.com/isentropic-dev/dim/blob/main/CONTRIBUTING.md#adding-a-new-si-unit)
|
|
107
|
+
to the package.
|
|
108
|
+
|
|
109
|
+
## Installation
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Deno
|
|
113
|
+
deno add jsr:@isentropic/dim-si
|
|
114
|
+
# npm
|
|
115
|
+
npm install @isentropic/dim-si
|
|
116
|
+
# Bun
|
|
117
|
+
bun add @isentropic/dim-si
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Units
|
|
121
|
+
|
|
122
|
+
### Base
|
|
123
|
+
|
|
124
|
+
| Quantity | Units |
|
|
125
|
+
| ------------------------ | ------------------------------------------------------------------------------------------- |
|
|
126
|
+
| Length | `meter`, `kilometer`, `centimeter`, `millimeter`, `micrometer`, `nanometer`, `picometer` |
|
|
127
|
+
| Mass | `kilogram`, `gram`, `milligram`, `microgram`, `nanogram`, `tonne` |
|
|
128
|
+
| Time | `second`, `millisecond`, `microsecond`, `nanosecond`, `picosecond`, `minute`, `hour`, `day` |
|
|
129
|
+
| Temperature [*](#affine) | `kelvin`, `celsius`, `fahrenheit` |
|
|
130
|
+
| Electric Current | `ampere`, `milliampere`, `microampere` |
|
|
131
|
+
| Amount of Substance | `mole`, `millimole`, `micromole` |
|
|
132
|
+
| Luminous Intensity | `candela` |
|
|
133
|
+
|
|
134
|
+
### Derived
|
|
135
|
+
|
|
136
|
+
| Quantity | Units |
|
|
137
|
+
| --------------------- | ------------------------------------------------------------------- |
|
|
138
|
+
| Area | `squareMeter`, `hectare` |
|
|
139
|
+
| Volume | `cubicMeter`, `liter`, `milliliter`, `microliter` |
|
|
140
|
+
| Velocity | `meterPerSecond` |
|
|
141
|
+
| Acceleration | `meterPerSecondSquared` |
|
|
142
|
+
| Force | `newton` |
|
|
143
|
+
| Pressure | `pascal`, `bar`, `millibar` |
|
|
144
|
+
| Energy | `joule`, `kilojoule`, `megajoule`, `kilowattHour` |
|
|
145
|
+
| Power | `watt`, `milliwatt`, `kilowatt`, `megawatt`, `gigawatt`, `terawatt` |
|
|
146
|
+
| Frequency | `hertz`, `kilohertz`, `megahertz`, `gigahertz`, `becquerel` |
|
|
147
|
+
| Voltage | `volt`, `millivolt`, `kilovolt` |
|
|
148
|
+
| Resistance | `ohm`, `milliohm`, `kilohm`, `megohm` |
|
|
149
|
+
| Capacitance | `farad`, `microfarad`, `nanofarad`, `picofarad` |
|
|
150
|
+
| Inductance | `henry`, `millihenry`, `microhenry` |
|
|
151
|
+
| Charge | `coulomb` |
|
|
152
|
+
| Magnetic Flux | `weber` |
|
|
153
|
+
| Magnetic Flux Density | `tesla` |
|
|
154
|
+
| Conductance | `siemens` |
|
|
155
|
+
| Illuminance | `lux` |
|
|
156
|
+
| Luminous Flux | `lumen` |
|
|
157
|
+
| Catalytic Activity | `katal` |
|
|
158
|
+
| Thermal Conductance | `wattPerKelvin`, `milliwattPerKelvin`, `kilowattPerKelvin` |
|
|
159
|
+
| Absorbed Dose | `gray`, `sievert` |
|
|
160
|
+
|
|
161
|
+
_<a id="affine">\*</a>
|
|
162
|
+
[Affine quantity](https://github.com/isentropic-dev/dim/blob/main/packages/dim-unit/README.md#affine-units)
|
|
163
|
+
— zero point is arbitrary, which restricts valid operations._
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isentropic/dim-si",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Ready-to-use SI units with compile-time dimensional analysis",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -113,8 +113,8 @@
|
|
|
113
113
|
"test": "node test_runner.js"
|
|
114
114
|
},
|
|
115
115
|
"dependencies": {
|
|
116
|
-
"@isentropic/dim-isq": "^0.4.
|
|
117
|
-
"@isentropic/dim-unit": "^0.4.
|
|
116
|
+
"@isentropic/dim-isq": "^0.4.3",
|
|
117
|
+
"@isentropic/dim-unit": "^0.4.3"
|
|
118
118
|
},
|
|
119
119
|
"devDependencies": {
|
|
120
120
|
"@types/node": "^20.9.0",
|