0.3.0 • Published 7 years ago

maegor v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

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);
len.convertTo(LengthAdditions.earthRadius); // //=> '6.622979124156333 er'

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

Register a custom unit.

ParameterDescription
nameThe name of the custom unit
symbolUnit symbol. This will be used when converting values of this unit to strings
converterOrCoefficientEither the coefficient for converting the dimensions base unit to the custom unit or a converter object
ReturnsA 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

DimensionUnitsBase Unit
AccelerationmetersPerSecondSquared (m/s²) gravity (g)metersPerSecondSquared (m/s²)
Angledegrees (°) arcMinutes (ʹ) arcSeconds (ʹʹ) radians (rad) gradians (grad) revolutions (rev)degrees (°)
AreasquareMegameters (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 MassgramsPerLiter (g/L) milligramsPerDeciliter (mg/dL) millimolesPerLiter (mmol/L)gramsPerLiter (g/L)
DispersionpartsPerMillion (ppm)partsPerMillion (ppm)
Durationseconds (s) minutes (m) hours (h)seconds (s)
Electric Chargecoulombs (C) megaampereHours (MAh) kiloampereHours (kAh) ampereHours (Ah) milliampereHour (mAh) microampereHour (µAh)coulombs (C)
Electric Currentmegaamperes (MA) kiloamperes (kA) amperes (A) milliamperes (mA) microamperes (µA)amperes (A)
Electric Potential Differencemegavolts (MV) kilovolts (kV) volts (V) millivolts (mV) microvolts (µV)volts (V)
Electric Resistancemegaohms (MΩ) kiloohms (kΩ) ohms (Ω) milliohms (mΩ) microohms (µΩ)ohms (Ω)
Energykilojoules (kJ) joules (J) kilocalories (kCal) calories (cal) kilowattHours (kWh)joules (J)
Frequencyterahertz (THz) gigahertz (GHz) megahertz (MHz) kilohertz (kHz) hertz (Hz) millihertz (mHz) microhertz (µHz) nanohertz (nHz)hertz (Hz)
Fuel EfficencylitersPer100Kilometers (L/100km) milesPerImperialGallon (mpg) milesPerGallon (mpg)litersPer100Kilometers (L/100km)
Illuminancelux (lx)lux (lx)
Lengthmegameters (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)
Masskilograms (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)
Powerterawatts (TW) gigawatts (GW) megawatts (MW) kilowatts (kW) watts (W) milliwatts (mW) microwatts (µW) nanowatts (nW) picowatts (nW) femtowatts (nHz) horsepower (hp)watts (W)
PressurenewtonsPerMetersSquared (N/m²) gigapascals (GPa) megapascals (MPa) kilopascals (kPa) hectopascals (hPa) inchesOfMercury (inHg) bars (bar) millibars (mbar) millimetersOfMercury (mmHg) poundsForcePerSquareInch (psi)newtonsPerMetersSquared (N/m²)
SpeedmetersPerSecond (m/s) kilometersPerHour (km/h) milesPerHour (mph) knots (kn)metersPerSecond (m/s)
Temperaturekelvin (K) celsius (°C) fahrenheit (°F)kelvin (K)
Volumemegaliters (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
  • Add an API to extends existing Units as well as defining new ones
  • Find a workaround to avoid floating point percision problems

License

MIT © Lukas Kollmer

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago