While users can define their own unit system, Safe Units also comes with a large collection of built-in quantities and units based on the SI unit system.
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.
The built-in units include the standard set of SI base dimension and corresponding units:
Length
/ meters
(m)Mass
/ kilograms
(kg)Time
/ seconds
(s)ElectricCurrent
/ amperes
(A)Temperature
/ kelvin
(K)AmountOfSubstance
/ moles
(mol)LuminousIntensity
/ candelas
(cd)In addition, several extra dimensions are defined:
PlaneAngle
/ radians
(rd)SolidAngle
/ steradians
(sr)Memory
/ bits
(b)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.
The base units are only provided for number
measures, but users can create the base units for a given measure type through:
const BigNumberMeasure = createMeasureType<BigNumber>(numericOps);
const { meters, kilograms, seconds, ...rest } = createSIBaseUnits(BigNumberMeasure);
const bigValue = new BigNumber("3.14");
const bigLength: Length<BigNumber> = BigNumberMeasure.of(bigValue, meters);
These versions of meters
, kilograms
, etc. will all operate on measures whose numeric types are BigNumber
.
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.
Length
- mmeters
(m) [base unit]inches
(in)thous
(th)feet
(ft)yards
(yd)chains
(ch)furlongs
(fur)miles
(mi)leagues
(lea)fathoms
(ftm)cables
(cable)nauticalMiles
(nmi)links
(li)rods
(rd)angstroms
(Å)US.points
(p)US.picas
(pica)Mass
- kgkilograms
(kg) [base unit]grams
(g)pounds
(lb)grains
(gr)ounces
(oz)carats
(ct)Imperial.drachms
(dr)Imperial.stone
(st)Imperial.quarters
(qtr)Imperial.hundredweights
(cwd)Imperial.tons
(t)US.drams
(dr)US.pennyweights
(dwt)US.hundredweights
(cwd)US.tons
(ton)Time
- sseconds
(s) [base unit]minutes
(min)hours
(hr)days
(d)ElectricCurrent
- Aamperes
(A) [base unit]Temperature
- Kkelvin
(K) [base unit]AmountOfSubstance
- molmoles
(mol) [base unit]LuminousIntesity
- cdcandelas
(cd) [base unit]PlaneAngle
- radradians
(rad) [base unit]piRadians
(pi rad)tauRadians
(tau rad)arcSeconds
(arcsec)arcMinutes
(arcmin)degrees
(deg)SolidAngle
- srsteradians
(sr) [base unit]Memory
- bbits
(b) [base unit]bytes
(B)Frequency
- 1 / shertz
(Hz)FrequencyDrift
- 1 / s²FuelEfficiency
- 1 / m²Wavenumber
- 1 / mArea
- m²perches
(perch)roods
(rood)acres
(acre)ares
(a)hectares
(ha)Volume
- m³liters
(L)Imperial.fluidOunces
(fl oz);Imperial.gills
(gi)Imperial.pints
(pt)Imperial.quarts
(qt)Imperial.gallons
(gal)US.minims
(min)US.fluidDrams
(fl dr)US.teaspoons
(tsp)US.tablespoons
(Tbsp)US.fluidOunces
(fl oz)US.shots
(jig)US.gills
(gi)US.cups
(cp)US.pints
(pt)US.quarts
(qt)US.gallons
(gal)US.barrels
(liq bbl)US.oilBarrels
(bbl)US.hogsheads
(hogshead)US.dryPints
(dry pt)US.dryQuarts
(dry qt)US.dryGallons
(dry gal)US.pecks
(pk)US.bushels
(bu)US.dryBarrels
(dry bbl)Absement
- m ⋅ sVelocity
- m / sspeedOfLight
(C)Acceleration
- m / s²Jerk
- m / s³Jounce
- m / s⁴Crackle
- m / s⁵Pop
- m / s⁶VolumetricFlow
- m³ / sMassFlowRate
- kg / sLinearDensity
- kg / mAreaDensity
- kg / m²VolumeDensity
- kg / m³Force
- kg ⋅ m / s²newtons
(N)Yank
- km ⋅ m / s³Pressure
- kg / (m ⋅ s²)pascals
(Pa)bars
(bar)atmospheres
(atm)torrs
(Torr)Compressibility
- m ⋅ s² / kgDynamicViscosity
- kg / (m ⋅ s)SurfaceTension
- kg / s²Momentum
- kg ⋅ m / sMomentOfInertia
- kg ⋅ m²Energy
- kg ⋅ m² / s²joules
(J)Power
- kg ⋅ m² / s³watts
(W)PowerDensity
- kg / (m ⋅ s³)Voltage
- kg ⋅ m² / (s³ ⋅ A)volts
(V)ElectricCharge
- s ⋅ Acoulombs
(C)ElectricChargeDensity
- s ⋅ A / m³ElectricCurrentDensity
- A / m²ElectricDisplacement
- s ⋅ A / m²EletricFieldStrength
- kg ⋅ m / (s³ ⋅ A)ElectricalCapacitance
- s⁴ ⋅ A² / (kg ⋅ m²)farads
(F)ElectricalConductance
- s³ ⋅ A / (kg ⋅ m²)siemens
(S)ElectricalConductivity
- s³ ⋅ A² / (kg ⋅ m³)ElectricalResistance
- kg ⋅ m² / (s³ ⋅ A²)ohms
(Ω)ElectricalResistivity
- kg ⋅ m³ / (s³ ⋅ A²)ElectricalInductance
- kg ⋅ m² / (s² ⋅ A²)henrys
(H)LinearChargeDensity
- s ⋅ A / mPermittivity
- s⁴ ⋅ A² / (kg ⋅ m³)MagneticFlux
- kg ⋅ m² / (s² ⋅ A)webers
(Wb)MagneticFluxDensity
- kg / (s² ⋅ A)teslas
(T)MagneticPermeability
- kg ⋅ m / (s² ⋅ A²)Magnetization
- A / mMagneticReluctance
- s² ⋅ A² / (kg ⋅ m²)MagneticMoment
- kg ⋅ m³ / (s² ⋅ A)MagneticRigidity
- kg ⋅ m / (s² ⋅ A)MagneticDipoleMoment
- m² ⋅ AMagneticSusceptibility
- s² ⋅ A² / (kg ⋅ m)Irradiance
- kg / s³Entropy
- kg ⋅ m / (s² ⋅ K)SpecificHeat
- m² / (s² ⋅ K)SpecificVolume
- m³ / kgThermalConductivity
- kg ⋅ m / (s³ ⋅ K)ThermalResistance
- s³ ⋅ K / (kg ⋅ m²)ThermalExpansionCoefficient
- 1 / KThermalGradient
- K / mMolarEntropy
- kg ⋅ m² / (s² ⋅ K ⋅ mol)MolarEnergy
- kg ⋅ m² / (s² ⋅ mol)Molarity
- mol / m³MolarVolume
- m³ / molMolality
- mol / kgMolarMass
- kg / molMolarConductivity
- s³ ⋅ A² / (kg ⋅ mol)CatalyticActivity
- mol / skatals
(kat)CatalyticEfficiency
- m³ / (s ⋅ mol)ReactionRate
- mol / (m³ ⋅ s)RadiationDose
- m² / s²sieverts
(Sv)RadiationDoseRate
- m² / s³ElectronMobility
- s² ⋅ A / kgAngularMomentum
- kg ⋅ m² / sSpecificAngularMomentum
- m² / sLuminance
- cd / m²LuminousFlux
- cd ⋅ srlumens
(lm)Illuminance
- cd ⋅ sr / m²luxes
(lx)LuminousEnergy
- s ⋅ cd ⋅ srLuminousExposure
- s ⋅ cd ⋅ sr / m²LuminousEfficiency
- s³ ⋅ cd ⋅ sr / (kg ⋅ m²)RadiantIntensity
- kg ⋅ m² / (s³ ⋅ sr)SpectralIntensity
- kg ⋅ m / (s³ ⋅ sr)Radiance
- kg / (s³ ⋅ sr)SpectralRadiance
- kg / (m ⋅ s³ ⋅ sr)AngularVelocity
- rad / sAngularAcceleration
- rad / s²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:
import { bits, kibi, kilo, Measure, meters } from "safe-units";
const distance = Measure.of(30, kilo(meters)); // 30000 m
distance.in(kilo(meters)); // "30 km"
const size = Measure.of(2, kibi(bits)); // 2048 b
size.in(kibi(bits)); // "2 KiB"
Prefix functions are provided for all of the SI prefixes and all of the binary prefixes for memory.
Trigonometric functions are provided in the Trig
namespace for converting between plane angles and dimensionless values, the signatures are as follows:
namespace Trig {
function cos(x: PlaneAngle): Dimensionless;
function sin(x: PlaneAngle): Dimensionless;
function tan(x: PlaneAngle): Dimensionless;
function acos(x: Dimensionless): PlaneAngle;
function asin(x: Dimensionless): PlaneAngle;
function atan(x: Dimensionless): PlaneAngle;
function atan2(y: Length, x: Length): PlaneAngle;
}