0.5.0 • Published 6 years ago

amounts v0.5.0

Weekly downloads
4,503
License
MIT
Repository
github
Last release
6 years ago

Amounts

Represent amounts of stuff with units and conversions. Supports lenient parsing of amounts, normalization and basic formatting.

const { length, duration } = require('amounts');

// Supports parsing from strings:
const value = length('30 m');
// Convert into another unit (returns a number)
console.log(length.as('ft'));
console.log(length.feet);

// Parse durations
console.log(duration('20 m, 4 s'));

// Supports precision on the numbers
length('2.8 cm')
length('2e10 m')

Amounts tries to be friendly with handling input, supporting variations of units, such as both short and long names, with longer unit names being case insensitive.

These will all parse to the same unit and value:

mass('750 ug');
mass('750 micrograms');
mass('750 micro gram');
mass('750 MikroGram')

But this will not work:

mass('750 uG');

Or fetch units from factories and use convert:

// To convert to mmHg
pressure.unit('mmHg').convert('200.12 kPa');
// Or back to kPa
pressure.unit('kPa').convert(1501.023, 'mmHg');

API

All amounts that can be represented share a common API. Factories support:

  • factory(string)

    Parse the given string into an amount.

  • factory(value, unit)

    Create an amount with the given value and unit.

  • factory(value)

    Create an amount with the given value and the default unit.

  • factory.unit(unit)

    Get a unit that can be used for conversions for this factory.

Instances have the following API:

  • amount.value: Number

    Get the value of the amount.

  • amount.unit: String

    Get the unit of the amount.

  • amount.as(unit): Number

    Convert the amount to the given unit and return the result as a number.

  • amount.to(unit): Amount

    Convert the amount into another unit and return an object with the value.

Every amount also exposes getters for the most commonly used units:

length('20 cm').meters
length('19 ft').cm

volume(1, 'l').oz

Generic amounts

The generic amount does not have any units, but supports SI-prefixes.

const { generic } = require('amounts');

// Without any unit, returns an amount with value 20
console.log(generic(20));

// With a generic SI-unit, returns an amount with value 20000
console.log(generic('20k'));

// Full length names are supported
console.log(generic('20 micro'))

SI-prefixes

Units in the SI system can be combined with SI-prefixes to create a new unit. SI-prefixes are supported both by their short names and their long names. Examples: cm, milliliters, hPa, MW, kilowatt

Long NameShort nameFactorFactor (expanded)
yoctoy10-240.000 000 000 000 000 000 000 001
zeptoz10-210.000 000 000 000 000 000 001
attoa10-180.000 000 000 000 000 001
femtof10-150.000 000 000 000 001
picop10-120.000 000 000 001
nanon10-90.000 000 001
microu, mc, µ10-60.000 001
millim10-30.001
centic10-20.01
decid10-10.1
deca, dekada10110
hectoh102100
kilok1031 000
megaM1061 000 000
gigaG1091 000 000 000
teraT10121 000 000 000 000
petaP10151 000 000 000 000 000
exaE10181 000 000 000 000 000 000
zettaZ10211 000 000 000 000 000 000 000
yottaY10241 000 000 000 000 000 000 000 000

Angle

const { angle } = require('amounts');

console.log(angle(2, 'rad'));
console.log(angle('5 degrees').as('radians'));
UnitSINames
DegreeNodeg, degree, degrees
RadianYesrad, radian, radians

Area

const { area } = require('amounts');

console.log(area(2, 'm^2'));
console.log(area('10 sq ft').as('m2'));
UnitSINames
Square MeterYes, m^2, m2, square metre, square metres, square meter, square meters
Square InchNosq in, square inch, square inches
Square FootNosq ft, square foot, square feet
Square YardNosq yd, square yard, square yards
Square MileNosq mi, square mile, square miles
HectareNoha, hectare, hectares
AcreNoacre, acres

Duration

Durations represent a number of milliseconds something takes. Multiple units can be combined into a value.

const { duration } = require('amounts');

console.log(duration(2000)); // Defaults to milliseconds
console.log(duration('1d'));
console.log(duration('2h 10m'));
console.log(duration('2 days, 5 hours'));
UnitSINames
MillisecondsNoms, millisecond, milliseconds
SecondsNos, second, seconds
MinutesNom, minute, minutes
HoursNoh, hour, hours
DaysNod, day, days

Energy

const { energy } = require('amounts');

console.log(energy(10)); // Joules
console.log(energy('3.5 kJ').kWh);
UnitSINames
JoulesYesJ, j, joule, joules
Watt hoursTrueWh, wh, watt hour, watt hours

Illuminance

const { illuminance } = require('amounts');

console.log(illuminance(2, 'lx'));
console.log(angle('8000 lux'));
UnitSINames
LuxYeslx, lux
PhotNoph, phot
NoxNonx, nox
Foot-candleNofc, lm/ft², ft-c, foot-candle, foot-candles, foot candle, foot candles

Length

const { length } = require('amounts');

console.log(length(2)); // Meters
console.log(length('5 ft').as('micrometer'));
console.log(length('2 ft ').cm);
UnitSINames
MetreYesm, meter, meters, metre, metres
InchNoin, inch, inches
FeetNoft, foot, feet
YardNoyd, yard, yards
MileNomi, mile, miles

Mass

const { mass } = require('amounts');

console.log(mass(210)); // Grams
console.log(mass('78 kg').lbs);
console.log(mass('2 stone').kg)
UnitSINames
GramYesg, gram, grams, gramme, grammes
PoundNolb, lbs, pound, pounds, #
OunceNooz, ounce, ounces
StoneNost, stone, stones

Power

const { power } = require('amounts');

console.log(power(500)); // Watts
console.log(power('6000 kW').mW);
console.log(power('6 hp').as('microwatts'));
UnitSINames
WattYesw, W, watt
HorsepowerNohp, horsepower

Pressure

const { pressure } = require('amounts');

console.log(pressure(500)); // Pascal
console.log(power('700 hPa').atm);
console.log(power('7 bar').kPa);
UnitSINames
PascalYespa, Pa, pascal, pascals
AtmosphereNoatm, atmosphere, atmospheres
BarNobar, bars
PSINopsi, pounds per square inch, pound per square inch
TorrNotorr
mmHgNommHg, 'millimetre of mercury', millimetres of mercury, millimeter of mercury, millimetres of mercury

Sound Pressure Level

const { soundPressureLevel } = require('amounts');

console.log(soundPressureLevel(30)); // db
console.log(soundPressureLevel(30, 'dB')); // db
UnitSINames
DecibelsNodB, db, dbs, decibel, decibels

Speed

const { speed } = require('amounts');

console.log(speed(500)); // m/s
console.log(speed('5 km/s').kph);
console.log(speed('10 mph').mps);
UnitSINames
Metres/SecondYesm/s, mps, metre per second, metres per second, meter per second, meters per second, metre/second, metres/second, meter/second, meters/second
Kilometre/HourNokm/h, kph, kilometre per hour, kilometres per hour, kilometer per hour kilometers per hour, kilometers/hour, kilometre/hour
Miles/HourNomph, mile per hour, miles per hour, mile/hour, miles/hour
Feet/SecondNoft/s, fps, foot per second, feet per second, foot/second, feet/second
KnotNokt, knot, knots

Temperature

const { temperature } = require('amounts');

console.log(temperature(22)); // Celsius
console.log(temperature('200 K').celsius);
console.log(temperature(80, 'f').kelvin);
UnitSINames
CelsiusNoC, c, celsius
KelvinYesK, kelvin, kelvins
FahrenheitNoF, f, fahrenheit, fahrenheits

Voltage

const { voltage } = require('amounts');

console.log(voltage(22)); // Volts
console.log(voltage('200 V').volts);
UnitSINames
VoltYesV, v, volt, volts

Volume

const { volume } = require('amounts');

console.log(volume(2)); // Liters
console.log(volume(2, 'quarts').dl);
console.log(volume('20 ml').tbsp);
UnitSINames
LiterYesl, L, liter, litre, litre, litres
GallonNogal, gallon, gallons
QuartNoqt, quart, quarts
PintNopt, pint, pints
CupNocu, cup, cups
Fluid ounceNofloz, oz, fluid ounce, ounce, fluid ounces, ounces
TablespoonNotb, tbsp, tbs, tablesppon, tablespoons
TeaspoonNotsp, teaspoon, teaspoons