Amounts
Represent amounts of stuff with units and conversions. Supports lenient parsing of amounts, normalization and basic formatting.
const length duration = ; // Supports parsing from strings:const value = ;// Convert into another unit (returns a number)console;console; // Parse durationsconsole; // Supports precision on the numbers
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:
;;;
But this will not work:
;
Or fetch units from factories and use convert
:
// To convert to mmHgpressure;// Or back to kPapressure;
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:
meterscm oz
Generic amounts
The generic amount does not have any units, but supports SI-prefixes.
const generic = ; // Without any unit, returns an amount with value 20console; // With a generic SI-unit, returns an amount with value 20000console; // Full length names are supportedconsole
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 Name | Short name | Factor | Factor (expanded) |
---|---|---|---|
yocto |
y |
10-24 | 0.000 000 000 000 000 000 000 001 |
zepto |
z |
10-21 | 0.000 000 000 000 000 000 001 |
atto |
a |
10-18 | 0.000 000 000 000 000 001 |
femto |
f |
10-15 | 0.000 000 000 000 001 |
pico |
p |
10-12 | 0.000 000 000 001 |
nano |
n |
10-9 | 0.000 000 001 |
micro |
u , mc , µ |
10-6 | 0.000 001 |
milli |
m |
10-3 | 0.001 |
centi |
c |
10-2 | 0.01 |
deci |
d |
10-1 | 0.1 |
deca , deka |
da |
101 | 10 |
hecto |
h |
102 | 100 |
kilo |
k |
103 | 1 000 |
mega |
M |
106 | 1 000 000 |
giga |
G |
109 | 1 000 000 000 |
tera |
T |
1012 | 1 000 000 000 000 |
peta |
P |
1015 | 1 000 000 000 000 000 |
exa |
E |
1018 | 1 000 000 000 000 000 000 |
zetta |
Z |
1021 | 1 000 000 000 000 000 000 000 |
yotta |
Y |
1024 | 1 000 000 000 000 000 000 000 000 |
Angle
const angle = ; console;console;
Unit | SI | Names |
---|---|---|
Degree | No | deg , degree , degrees |
Radian | Yes | rad , radian , radians |
Area
const area = ; console;console;
Unit | SI | Names |
---|---|---|
Square Meter | Yes | m² , m^2 , m2 , square metre , square metres , square meter , square meters |
Square Inch | No | sq in , square inch , square inches |
Square Foot | No | sq ft , square foot , square feet |
Square Yard | No | sq yd , square yard , square yards |
Square Mile | No | sq mi , square mile , square miles |
Hectare | No | ha , hectare , hectares |
Acre | No | acre , acres |
Duration
Durations represent a number of milliseconds something takes. Multiple units can be combined into a value.
const duration = ; console; // Defaults to millisecondsconsole;console;console;
Unit | SI | Names |
---|---|---|
Milliseconds | No | ms , millisecond , milliseconds |
Seconds | No | s , second , seconds |
Minutes | No | m , minute , minutes |
Hours | No | h , hour , hours |
Days | No | d , day , days |
Energy
const energy = ; console; // Joulesconsole;
Unit | SI | Names |
---|---|---|
Joules | Yes | J , j , joule , joules |
Watt hours | True | Wh , wh , watt hour , watt hours |
Illuminance
const illuminance = ; console;console;
Unit | SI | Names |
---|---|---|
Lux | Yes | lx , lux |
Phot | No | ph , phot |
Nox | No | nx , nox |
Foot-candle | No | fc , lm/ft² , ft-c , foot-candle , foot-candles , foot candle , foot candles |
Length
const length = ; console; // Metersconsole;console;
Unit | SI | Names |
---|---|---|
Metre | Yes | m , meter , meters , metre , metres |
Inch | No | in , inch , inches |
Feet | No | ft , foot , feet |
Yard | No | yd , yard , yards |
Mile | No | mi , mile , miles |
Mass
const mass = ; console; // Gramsconsole;console
Unit | SI | Names |
---|---|---|
Gram | Yes | g , gram , grams , gramme , grammes |
Pound | No | lb , lbs , pound , pounds , # |
Ounce | No | oz , ounce , ounces |
Stone | No | st , stone , stones |
Power
const power = ; console; // Wattsconsole;console;
Unit | SI | Names |
---|---|---|
Watt | Yes | w , W , watt |
Horsepower | No | hp , horsepower |
Pressure
const pressure = ; console; // Pascalconsole;console;
Unit | SI | Names |
---|---|---|
Pascal | Yes | pa , Pa , pascal , pascals |
Atmosphere | No | atm , atmosphere , atmospheres |
Bar | No | bar , bars |
PSI | No | psi , pounds per square inch , pound per square inch |
Torr | No | torr |
mmHg | No | mmHg , 'millimetre of mercury', millimetres of mercury , millimeter of mercury , millimetres of mercury |
Sound Pressure Level
const soundPressureLevel = ; console; // dbconsole; // db
Unit | SI | Names |
---|---|---|
Decibels | No | dB , db , dbs , decibel , decibels |
Speed
const speed = ; console; // m/sconsole;console;
Unit | SI | Names |
---|---|---|
Metres/Second | Yes | m/s , mps , metre per second , metres per second , meter per second , meters per second , metre/second , metres/second , meter/second , meters/second |
Kilometre/Hour | No | km/h , kph , kilometre per hour , kilometres per hour , kilometer per hour kilometers per hour , kilometers/hour , kilometre/hour |
Miles/Hour | No | mph , mile per hour , miles per hour , mile/hour , miles/hour |
Feet/Second | No | ft/s , fps , foot per second , feet per second , foot/second , feet/second |
Knot | No | kt , knot , knots |
Temperature
const temperature = ; console; // Celsiusconsole;console;
Unit | SI | Names |
---|---|---|
Celsius | No | C , c , celsius |
Kelvin | Yes | K , kelvin , kelvins |
Fahrenheit | No | F , f , fahrenheit , fahrenheits |
Voltage
const voltage = ; console; // Voltsconsole;
Unit | SI | Names |
---|---|---|
Volt | Yes | V , v , volt , volts |
Volume
const volume = ; console; // Litersconsole;console;
Unit | SI | Names |
---|---|---|
Liter | Yes | l , L , liter , litre , litre , litres |
Gallon | No | gal , gallon , gallons |
Quart | No | qt , quart , quarts |
Pint | No | pt , pint , pints |
Cup | No | cu , cup , cups |
Fluid ounce | No | floz , oz , fluid ounce , ounce , fluid ounces , ounces |
Tablespoon | No | tb , tbsp , tbs , tablesppon , tablespoons |
Teaspoon | No | tsp , teaspoon , teaspoons |