maegor

Unit conversion library


Keywords
unit-conversion, measurement, javascript, measurements
License
MIT
Install
npm install maegor@0.3.0

Documentation

maegor CircleCI Build Status Coverage Status

Unit conversion library

Install

$ npm install --save maegor

Examples

Converting Distances

const {Measurement, Length} = require('maegor');

let distance = new Measurement(58.7, Length.kilometers);

distance.convertedTo(Length.meters);  //=> '58700 m'
distance.convertedTo(Length.miles);   //=> '36.47457964134366 mi'
distance.convertedTo(Length.yards);   //=> '64195.10061242345 yd'

Converting Temperature

const {Measurement, Temperature} = require('maegor');

let temp = new Measurement(23.12, Temperature.celsius);
temp.convertedTo(Temperature.fahrenheit);  //=> '73.61599999999567 °F'

API

Measurement

  • Measurement#canBeConvertedTo(unit): Check if a measurement can be converted to the specified unit
  • Measurement#convertedTo(unit): Returns a new Measurement created by converting to the specified unit
  • Measurement#convertTo(unit): Converts the measurement to the specified unit (In-Place)

Unit

Unit is the baseclass all dimensions (like Length or Mass) extend. You can define custom units either by extending a dimension's class or by using the API:

  1. Defining custom units via the API:
// earth's radius is approx. 6371 meters
Length.addUnit('earthRadius', 'er', 6371);

let marathon = new Measurement(42.195, Length.kilometers);
marathon.convertTo(Length.earthRadius);  //=> '6.622979124156333 er'
  1. Defining custom units via subclassing:
class LengthAdditions extends Length {
  static get earthRadius() {
    return new Length('er', 6371);
  }
}

let marathon = new Measurement(42.195, Length.kilometers);
marathon.convertTo(LengthAdditions.earthRadius); // //=> '6.622979124156333 er'

Unit.addUnit(name, symbol, converterOrCoefficient) -> Unit

Register a custom unit.

Parameter Description
name The name of the custom unit
symbol Unit symbol. This will be used when converting values of this unit to strings
converterOrCoefficient Either the coefficient for converting the dimensions base unit to the custom unit or a converter object
Returns A Unit object that can be passed to Measurement#convert{ed}To

Note: This will throw an error if an unit with name already exists

Units

Dimension Units Base Unit
Acceleration metersPerSecondSquared (m/s²)
gravity (g)
metersPerSecondSquared (m/s²)
Angle degrees (°)
arcMinutes (ʹ)
arcSeconds (ʹʹ)
radians (rad)
gradians (grad)
revolutions (rev)
degrees (°)
Area squareMegameters (Mm²)
squareKilometers (km²)
squareMeters (m²)
squareCentimeters (cm²)
squareMillimeters (mm²)
squareMicrometers (µm²)
squareNanometers (nm²)
squareInches (in²)
squareFeet (ft²)
squareYards (yd²)
squareMiles (mi²)
acres (ac)
ares (a)
hectares (ha)
squareMeters (m²)
Concentration Mass gramsPerLiter (g/L)
milligramsPerDeciliter (mg/dL)
millimolesPerLiter (mmol/L)
gramsPerLiter (g/L)
Dispersion partsPerMillion (ppm) partsPerMillion (ppm)
Duration seconds (s)
minutes (m)
hours (h)
seconds (s)
Electric Charge coulombs (C)
megaampereHours (MAh)
kiloampereHours (kAh)
ampereHours (Ah)
milliampereHour (mAh)
microampereHour (µAh)
coulombs (C)
Electric Current megaamperes (MA)
kiloamperes (kA)
amperes (A)
milliamperes (mA)
microamperes (µA)
amperes (A)
Electric Potential Difference megavolts (MV)
kilovolts (kV)
volts (V)
millivolts (mV)
microvolts (µV)
volts (V)
Electric Resistance megaohms (MΩ)
kiloohms (kΩ)
ohms (Ω)
milliohms (mΩ)
microohms (µΩ)
ohms (Ω)
Energy kilojoules (kJ)
joules (J)
kilocalories (kCal)
calories (cal)
kilowattHours (kWh)
joules (J)
Frequency terahertz (THz)
gigahertz (GHz)
megahertz (MHz)
kilohertz (kHz)
hertz (Hz)
millihertz (mHz)
microhertz (µHz)
nanohertz (nHz)
hertz (Hz)
Fuel Efficency litersPer100Kilometers (L/100km)
milesPerImperialGallon (mpg)
milesPerGallon (mpg)
litersPer100Kilometers (L/100km)
Illuminance lux (lx) lux (lx)
Length megameters (Mm)
kilometers (km)
hectometers (hm)
decameters (dam)
meters (m)
decimeters (dm)
centimeters (cm)
millimeters (mm)
micrometers (µm)
nanometers (nm)
picometers (pm)
inches (in)
feet (ft)
yards (yd)
miles (mi)
scandinavianMiles (smi)
lightyears (ly)
nauticalMiles (NM)
fathoms (ftm)
furlongs (fur)
astronomicalUnits (ua)
parsecs (pc)
meters (m)
Mass kilograms (kg)
grams (g)
decigrams (dg)
centigrams (cg)
milligrams (mg)
micrograms (µg)
nanograms (ng)
picograms (pg)
ounces (oz)
pounds (lb)
stones (st)
metricTons (t)
shortTons (ton)
carats (ct)
ouncesTroy (oz t)
slugs (slug)
kilograms (kg)
Power terawatts (TW)
gigawatts (GW)
megawatts (MW)
kilowatts (kW)
watts (W)
milliwatts (mW)
microwatts (µW)
nanowatts (nW)
picowatts (nW)
femtowatts (nHz)
horsepower (hp)
watts (W)
Pressure newtonsPerMetersSquared (N/m²)
gigapascals (GPa)
megapascals (MPa)
kilopascals (kPa)
hectopascals (hPa)
inchesOfMercury (inHg)
bars (bar)
millibars (mbar)
millimetersOfMercury (mmHg)
poundsForcePerSquareInch (psi)
newtonsPerMetersSquared (N/m²)
Speed metersPerSecond (m/s)
kilometersPerHour (km/h)
milesPerHour (mph)
knots (kn)
metersPerSecond (m/s)
Temperature kelvin (K)
celsius (°C)
fahrenheit (°F)
kelvin (K)
Volume megaliters (ML)
kiloliters (kL)
liters (L)
deciliters (dl)
centiliters (cL)
milliliters (mL)
cubicKilometers (km³)
cubicMeters (m³)
cubicDecimeters (dm³)
cubicCentimeters (cm³)
cubicMillimeters (mm³)
cubicInches (in³)
cubicFeet (ft³)
cubicYards (yd³)
cubicMiles (mi³)
acreFeet (af)
bushels (bsh)
teaspoons (tsp)
tablespoons (tbsp)
fluidOunces (fl oz)
cups (cup)
pints (pt)
quarts (qt)
gallons (gal)
imperialTeaspoons (tsp Imperial)
imperialTablespoons (tbsp Imperial)
imperialFluidOunces (fl oz Imperial)
imperialPints (pt Imperial)
imperialQuarts (qt Imperial)
imperialGallons (gal Imperial)
metricCups (metric cup Imperial)
liters (L)

TODO

  • Add methods to add, subtract, multiply and divide Measurement objects of the same Unit

License

MIT © Lukas Kollmer