Skip to content

Commit 9650df1

Browse files
committed
Update builtin units docs to be more complete
1 parent 98b7ca4 commit 9650df1

File tree

6 files changed

+262
-23
lines changed

6 files changed

+262
-23
lines changed

docs/builtin.md

Lines changed: 223 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ While users can [define their own unit system](unit-systems.html), Safe Units al
44

55
All quantities provided are generic and of the form `type Quantity<N = number>` so all quantity types will default to using `number` as the numeric type but may be passed another numeric type.
66

7-
## Base Units
7+
## Dimensions
88

99
The built-in units include the standard set of SI base dimension and corresponding units:
1010

11-
* `Length` / `meters`
12-
* `Mass` / `kilograms`
13-
* `Time` / `seconds`
14-
* `ElectricCurrent` / `amperes`
15-
* `Temperature` / `kelvin`
16-
* `AmountOfSubstance` / `moles`
17-
* `LuminousIntensity` / `candelas`
11+
* `Length` / `meters` (m)
12+
* `Mass` / `kilograms` (kg)
13+
* `Time` / `seconds` (s)
14+
* `ElectricCurrent` / `amperes` (A)
15+
* `Temperature` / `kelvin` (K)
16+
* `AmountOfSubstance` / `moles` (mol)
17+
* `LuminousIntensity` / `candelas` (cd)
1818

1919
In addition, several extra dimensions are defined:
2020

21-
* `PlaneAngle` / `radians`
22-
* `SolidAngle` / `steradians`
23-
* `Memory` / `bits`
21+
* `PlaneAngle` / `radians` (rd)
22+
* `SolidAngle` / `steradians` (sr)
23+
* `Memory` / `bits` (b)
2424

2525
While plane and solid angles are defined as dimensionless in the SI specification, they are defined here in case users want to be more rigorous with their angles. If not, then these can be safely ignored.
2626

27-
## Generic Base Units
27+
### Generic Base Units
2828

2929
The base units are only provided for `number` measures, but users can create the base units for a given measure type through:
3030

@@ -34,13 +34,220 @@ builtinBaseUnits.ts
3434

3535
These versions of `meters`, `kilograms`, etc. will all operate on measures whose numeric types are `BigNumber`.
3636

37-
## Provided Units
37+
## Derived Units
3838

39-
A large number of built in quantities (e.g. distance, velocity, force and magnetic flux density) are provided. See [here](https://github.com/jscheiny/safe-units/blob/master/src/unit/quantities.ts) for a full list.
39+
The following is a list of the provided quantities and measures given by the library. Units are grouped by their type (also referred to as a quantity). Some quanties do not have a corresponding unit. US customary units and imperial units are placed into `US` and `Imperial` namespaces respectively to avoid name clashes.
4040

41-
Many units are provided from the SI, Imperial and U.S. customary unit systems. See [here](https://github.com/jscheiny/safe-units/tree/master/src/unit) for all of the units.
41+
- `Length` - m
42+
- `meters` (m) _[base unit]_
43+
- `inches` (in)
44+
- `thous` (th)
45+
- `feet` (ft)
46+
- `yards` (yd)
47+
- `chains` (ch)
48+
- `furlongs` (fur)
49+
- `miles` (mi)
50+
- `leagues` (lea)
51+
- `fathoms` (ftm)
52+
- `cables` (cable)
53+
- `nauticalMiles` (nmi)
54+
- `links` (li)
55+
- `rods` (rd)
56+
- `angstroms` (Å)
57+
- `US.points` (p)
58+
- `US.picas` (pica)
59+
- `Mass` - kg
60+
- `kilograms` (kg) _[base unit]_
61+
- `grams` (g)
62+
- `pounds` (lb)
63+
- `grains` (gr)
64+
- `ounces` (oz)
65+
- `carats` (ct)
66+
- `Imperial.drachms` (dr)
67+
- `Imperial.stone` (st)
68+
- `Imperial.quarters` (qtr)
69+
- `Imperial.hundredweights` (cwd)
70+
- `Imperial.tons` (t)
71+
- `US.drams` (dr)
72+
- `US.pennyweights` (dwt)
73+
- `US.hundredweights` (cwd)
74+
- `US.tons` (ton)
75+
- `Time` - s
76+
- `seconds` (s) _[base unit]_
77+
- `minutes` (min)
78+
- `hours` (hr)
79+
- `days` (d)
80+
- `ElectricCurrent` - A
81+
- `amperes` (A) _[base unit]_
82+
- `Temperature` - K
83+
- `kelvin` (K) _[base unit]_
84+
- `AmountOfSubstance` - mol
85+
- `moles` (mol) _[base unit]_
86+
- `LuminousIntesity` - cd
87+
- `candelas` (cd) _[base unit]_
88+
- `PlaneAngle` - rad
89+
- `radians` (rad) _[base unit]_
90+
- `piRadians` (pi rad)
91+
- `tauRadians` (tau rad)
92+
- `arcSeconds` (arcsec)
93+
- `arcMinutes` (arcmin)
94+
- `degrees` (deg)
95+
- `SolidAngle` - sr
96+
- `steradians` (sr) _[base unit]_
97+
- `Memory` - b
98+
- `bits` (b) _[base unit]_
99+
- `bytes` (B)
100+
- `Frequency` - 1 / s
101+
- `hertz` (Hz)
102+
- `FrequencyDrift` - 1 / s²
103+
- `FuelEfficiency` - 1 / m²
104+
- `Wavenumber` - 1 / m
105+
- `Area` - m²
106+
- `perches` (perch)
107+
- `roods` (rood)
108+
- `acres` (acre)
109+
- `ares` (a)
110+
- `hectares` (ha)
111+
- `Volume` - m³
112+
- `liters` (L)
113+
- `Imperial.fluidOunces` (fl oz);
114+
- `Imperial.gills` (gi)
115+
- `Imperial.pints` (pt)
116+
- `Imperial.quarts` (qt)
117+
- `Imperial.gallons` (gal)
118+
- `US.minims` (min)
119+
- `US.fluidDrams` (fl dr)
120+
- `US.teaspoons` (tsp)
121+
- `US.tablespoons` (Tbsp)
122+
- `US.fluidOunces` (fl oz)
123+
- `US.shots` (jig)
124+
- `US.gills` (gi)
125+
- `US.cups` (cp)
126+
- `US.pints` (pt)
127+
- `US.quarts` (qt)
128+
- `US.gallons` (gal)
129+
- `US.barrels` (liq bbl)
130+
- `US.oilBarrels` (bbl)
131+
- `US.hogsheads` (hogshead)
132+
- `US.dryPints` (dry pt)
133+
- `US.dryQuarts` (dry qt)
134+
- `US.dryGallons` (dry gal)
135+
- `US.pecks` (pk)
136+
- `US.bushels` (bu)
137+
- `US.dryBarrels` (dry bbl)
138+
- `Absement` - m ⋅ s
139+
- `Velocity` - m / s
140+
- `speedOfLight` (C)
141+
- `Acceleration` - m / s²
142+
- `Jerk` - m / s³
143+
- `Jounce` - m / s⁴
144+
- `Crackle` - m / s⁵
145+
- `Pop` - m / s⁶
146+
- `VolumetricFlow` - m³ / s
147+
- `MassFlowRate` - kg / s
148+
- `LinearDensity` - kg / m
149+
- `AreaDensity` - kg / m²
150+
- `VolumeDensity` - kg / m³
151+
- `Force` - kg ⋅ m / s²
152+
- `newtons` (N)
153+
- `Yank` - km ⋅ m / s³
154+
- `Pressure` - kg / (m ⋅ s²)
155+
- `pascals` (Pa)
156+
- `bars` (bar)
157+
- `atmospheres` (atm)
158+
- `torrs` (Torr)
159+
- `Compressibility` - m ⋅ s² / kg
160+
- `DynamicViscosity` - kg / (m ⋅ s)
161+
- `SurfaceTension` - kg / s²
162+
- `Momentum` - kg ⋅ m / s
163+
- `MomentOfInertia` - kg ⋅ m²
164+
- `Energy` - kg ⋅ m² / s²
165+
- `joules` (J)
166+
- `Power` - kg ⋅ m² / s³
167+
- `watts` (W)
168+
- `PowerDensity` - kg / (m ⋅ s³)
169+
- `Voltage` - kg ⋅ m² / (s³ ⋅ A)
170+
- `volts` (V)
171+
- `ElectricCharge` - s ⋅ A
172+
- `coulombs` (C)
173+
- `ElectricChargeDensity` - s ⋅ A / m³
174+
- `ElectricCurrentDensity` - A / m²
175+
- `ElectricDisplacement` - s ⋅ A / m²
176+
- `EletricFieldStrength` - kg ⋅ m / (s³ ⋅ A)
177+
- `ElectricalCapacitance` - s⁴ ⋅ A² / (kg ⋅ m²)
178+
- `farads` (F)
179+
- `ElectricalConductance` - s³ ⋅ A / (kg ⋅ m²)
180+
- `siemens` (S)
181+
- `ElectricalConductivity` - s³ ⋅ A² / (kg ⋅ m³)
182+
- `ElectricalResistance` - kg ⋅ m² / (s³ ⋅ A²)
183+
- `ohms` (Ω)
184+
- `ElectricalResistivity` - kg ⋅ m³ / (s³ ⋅ A²)
185+
- `ElectricalInductance` - kg ⋅ m² / (s² ⋅ A²)
186+
- `henrys` (H)
187+
- `LinearChargeDensity` - s ⋅ A / m
188+
- `Permittivity` - s⁴ ⋅ A² / (kg ⋅ m³)
189+
- `MagneticFlux` - kg ⋅ m² / (s² ⋅ A)
190+
- `webers` (Wb)
191+
- `MagneticFluxDensity` - kg / (s² ⋅ A)
192+
- `teslas` (T)
193+
- `MagneticPermeability` - kg ⋅ m / (s² ⋅ A²)
194+
- `Magnetization` - A / m
195+
- `MagneticReluctance` - s² ⋅ A² / (kg ⋅ m²)
196+
- `MagneticMoment` - kg ⋅ m³ / (s² ⋅ A)
197+
- `MagneticRigidity` - kg ⋅ m / (s² ⋅ A)
198+
- `MagneticDipoleMoment` - m² ⋅ A
199+
- `MagneticSusceptibility` - s² ⋅ A² / (kg ⋅ m)
200+
- `Irradiance` - kg / s³
201+
- `Entropy` - kg ⋅ m / (s² ⋅ K)
202+
- `SpecificHeat` - m² / (s² ⋅ K)
203+
- `SpecificVolume` - m³ / kg
204+
- `ThermalConductivity` - kg ⋅ m / (s³ ⋅ K)
205+
- `ThermalResistance` - s³ ⋅ K / (kg ⋅ m²)
206+
- `ThermalExpansionCoefficient` - 1 / K
207+
- `ThermalGradient` - K / m
208+
- `MolarEntropy` - kg ⋅ m² / (s² ⋅ K ⋅ mol)
209+
- `MolarEnergy` - kg ⋅ m² / (s² ⋅ mol)
210+
- `Molarity` - mol / m³
211+
- `MolarVolume` - m³ / mol
212+
- `Molality` - mol / kg
213+
- `MolarMass` - kg / mol
214+
- `MolarConductivity` - s³ ⋅ A² / (kg ⋅ mol)
215+
- `CatalyticActivity` - mol / s
216+
- `katals` (kat)
217+
- `CatalyticEfficiency` - m³ / (s ⋅ mol)
218+
- `ReactionRate` - mol / (m³ ⋅ s)
219+
- `RadiationDose` - m² / s²
220+
- `sieverts` (Sv)
221+
- `RadiationDoseRate` - m² / s³
222+
- `ElectronMobility` - s² ⋅ A / kg
223+
- `AngularMomentum` - kg ⋅ m² / s
224+
- `SpecificAngularMomentum` - m² / s
225+
- `Luminance` - cd / m²
226+
- `LuminousFlux` - cd ⋅ sr
227+
- `lumens` (lm)
228+
- `Illuminance` - cd ⋅ sr / m²
229+
- `luxes` (lx)
230+
- `LuminousEnergy` - s ⋅ cd ⋅ sr
231+
- `LuminousExposure` - s ⋅ cd ⋅ sr / m²
232+
- `LuminousEfficiency` - s³ ⋅ cd ⋅ sr / (kg ⋅ m²)
233+
- `RadiantIntensity` - kg ⋅ m² / (s³ ⋅ sr)
234+
- `SpectralIntensity` - kg ⋅ m / (s³ ⋅ sr)
235+
- `Radiance` - kg / (s³ ⋅ sr)
236+
- `SpectralRadiance` - kg / (m ⋅ s³ ⋅ sr)
237+
- `AngularVelocity` - rad / s
238+
- `AngularAcceleration` - rad / s²
42239

43-
Prefix functions are provided for both standard [SI prefixes](https://github.com/jscheiny/safe-units/blob/master/src/unit/metric.ts) and for [memory prefixes](https://github.com/jscheiny/safe-units/blob/master/src/unit/memory.ts).
240+
## Prefixes
241+
242+
You may notice that some commonly used units, such as kilometers, are absent from the list above. Instead of creating all the different combinations of prefix and unit pairs, Safe Units instead provides prefix functions. These are functions which operate on measures to construct a new measure with a corresponding symbol. For example:
243+
244+
```example
245+
builtinPrefixes.ts
246+
```
247+
248+
Prefix functions are provided for all of the [SI prefixes](https://en.wikipedia.org/wiki/Metric_prefix) and all of the [binary prefixes](https://en.wikipedia.org/wiki/Binary_prefix) for memory.
249+
250+
## Trigonometry
44251

45252
Trigonometric functions are provided in the `Trig` namespace for converting between plane angles and dimensionless values, the signatures are as follows:
46253

docs/changelog.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
# Changelog
22

3+
## v2.0.0
4+
5+
**Breaking changes**
6+
- The `Measure.dimension` and `Measure.dimensionless` functions now require a unit system as their first parameter. The `Measure.dimension` function can no longer arbitrarily produce new dimensions. To add new dimensions, create a new `UnitSystem`.
7+
- Changed the way in which unit exponents are represented. Exponents are now numbers instead of strings and units contain every dimension in their unit system even if the exponent is zero.
8+
- Removed the ability to arbitrarily perform exponentiation on measures. The `measure.toThe` and `Measure.pow` functions have been removed.
9+
- Removed the ability to perform roots of measures. The `Measure.sqrt` and `Measure.cbrt` functions have been removed.
10+
11+
**New features**
12+
- Introduced the concept of a [unit system](unit-systems.html). Unit systems define a fixed set of dimensions and their corresponding base units. Measures of a given unit system are not assignable to measures of other unit systems. Safe units ships with a default implementation of the SI unit system as `SIUnitSystem`.
13+
- Removed the limitations on exponents. Previously, exponents of dimensions had to be between -5 and +5. Now the limit on exponents is much larger and should no longer present any issues.
14+
- Added a new `valueIn` method to the `Measure` class. Calling `measure.valueIn(unit)` is syntactic sugar for `measure.div(unit).value`.
15+
16+
**Fixes**
17+
- Fixed an issue where measures could be assigned to measures of different unit types if they contained a superset of the other measure's dimensions. In order to fix this, the concept of unit systems was introduced.
18+
- Fixed incorrect symbols for the `joules` and `watts` units.
19+
320
## v1.1.1
421

22+
**Fixes**
523
- Fixed an issue where `wrapRootFn` would allow non-positive values as the root parameter.
624

725
## v1.1.0
826

27+
**Improvements**
928
- Added a custom formatter argument that can be passed into the `Measure.in` and `Measure.toString` methods.
1029

1130
## v1.0.0
1231

32+
**Breaking changes**
1333
- Changed all interface names to no longer be I- prefixed.
1434
- Fixed the definition of the `bar` unit.

docs/examples/builtinPrefixes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { bits, kibi, kilo, Measure, meters } from "safe-units";
2+
3+
const distance = Measure.of(30, kilo(meters)); // 30000 m
4+
distance.in(kilo(meters)); // "30 km"
5+
const size = Measure.of(2, kibi(bits)); // 2048 b
6+
size.in(kibi(bits)); // "2 KiB"

src/measure/genericMeasureFactory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ interface GenericMeasureFactory<N> {
1111

1212
/**
1313
* Creates a new dimension base unit.
14-
* @param dim a unique string literal which names this dimension (e.g. "length")
14+
* @param unitSystem the unit system for this dimension
15+
* @param dimension the basis of the unit system for this dimension
1516
* @param symbol the symbol of the base unit of the dimension (e.g. "m")
1617
* @returns A measure representing 1 base unit of the dimension (1 m)
1718
*/
@@ -23,6 +24,7 @@ interface GenericMeasureFactory<N> {
2324

2425
/**
2526
* Creates a dimensionless measure.
27+
* @param unitSystem the unit system for this measure
2628
* @param value the value of the measure
2729
* @returns a measure with no dimensions
2830
*/
@@ -61,7 +63,7 @@ export type GenericMeasureType<N, StaticMethods extends {}> = GenericMeasureComm
6163
* useful for attaching static math operations to the type.
6264
* @returns a factory for constructing measures of the given numeric type
6365
* @example
64-
* type MyMeasure<U extends Unit> = GenericMeasure<MyNumberType, U>;
66+
* type MyMeasure<B, U extends Unit<B>> = GenericMeasure<MyNumberType, B, U>;
6567
* const MyMeasure = createMeasureType({ ... });
6668
*/
6769
export function createMeasureType<N, S extends {} = {}>(

src/unit/metric.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import * as Quantity from "./quantities";
66
export const hertz: Quantity.Frequency = seconds.inverse().withSymbol("Hz");
77
export const newtons: Quantity.Force = kilograms.times(meters.per(seconds.squared())).withSymbol("N");
88
export const pascals: Quantity.Pressure = newtons.per(meters.squared()).withSymbol("Pa");
9-
export const joules: Quantity.Energy = newtons.times(meters).withSymbol("N");
10-
export const watts: Quantity.Power = joules.per(seconds).withSymbol("J");
9+
export const joules: Quantity.Energy = newtons.times(meters).withSymbol("J");
10+
export const watts: Quantity.Power = joules.per(seconds).withSymbol("W");
1111
export const volts: Quantity.Voltage = watts.per(amperes).withSymbol("V");
1212
export const coulombs: Quantity.ElectricCharge = amperes.times(seconds).withSymbol("C");
1313
export const farads: Quantity.ElectricalCapacitance = coulombs.per(volts).withSymbol("F");

src/unit/quantities.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export const Jounce = Jerk.over(Time);
115115
export type Crackle<N = number> = LiftMeasure<typeof Crackle, N>;
116116
export const Crackle = Jounce.over(Time);
117117

118+
/** m / s⁶ */
119+
export type Pop<N = number> = LiftMeasure<typeof Pop, N>;
120+
export const Pop = Crackle.over(Time);
121+
118122
/** m³ / s */
119123
export type VolumetricFlow<N = number> = LiftMeasure<typeof VolumetricFlow, N>;
120124
export const VolumetricFlow = Volume.over(Time);
@@ -339,7 +343,7 @@ export const CatalyticActivity = AmountOfSubstance.over(Time);
339343
export type CatalyticEfficiency<N = number> = LiftMeasure<typeof CatalyticEfficiency, N>;
340344
export const CatalyticEfficiency = Volume.over(AmountOfSubstance.times(Time));
341345

342-
/** mol / (m³ ⋅ s) */
346+
/** mol / (m³ ⋅ s) */
343347
export type ReactionRate<N = number> = LiftMeasure<typeof ReactionRate, N>;
344348
export const ReactionRate = CatalyticActivity.over(Volume);
345349

@@ -359,7 +363,7 @@ export const ElectronMobility = Area.over(Voltage.times(Time));
359363
export type AngularMomentum<N = number> = LiftMeasure<typeof AngularMomentum, N>;
360364
export const AngularMomentum = Force.times(Length).times(Time);
361365

362-
/** m² /s */
366+
/** m² / s */
363367
export type SpecificAngularMomentum<N = number> = LiftMeasure<typeof SpecificAngularMomentum, N>;
364368
export const SpecificAngularMomentum = AngularMomentum.over(Mass);
365369

@@ -393,7 +397,7 @@ export const LuminousEfficiency = LuminousFlux.over(Power);
393397
export type RadiantIntensity<N = number> = LiftMeasure<typeof RadiantIntensity, N>;
394398
export const RadiantIntensity = Power.over(SolidAngle);
395399

396-
/** kg ⋅ m / (s³ ⋅ sr) */
400+
/** kg ⋅ m / (s³ ⋅ sr) */
397401
export type SpectralIntensity<N = number> = LiftMeasure<typeof SpectralIntensity, N>;
398402
export const SpectralIntensity = RadiantIntensity.over(Length);
399403

0 commit comments

Comments
 (0)