Comparing version 0.2.0 to 0.3.0
29
index.js
'use strict'; | ||
const Amount = require('./lib/amount'); | ||
const Angle = require('./lib/angle'); | ||
const Duration = require('./lib/duration'); | ||
const Energy = require('./lib/energy'); | ||
const Illuminance = require('./lib/illuminance'); | ||
const Length = require('./lib/length'); | ||
const Mass = require('./lib/mass'); | ||
const Power = require('./lib/power'); | ||
const Pressure = require('./lib/pressure'); | ||
const Speed = require('./lib/speed'); | ||
const Temperature = require('./lib/temperature'); | ||
const Volume = require('./lib/volume'); | ||
const amount = require('./lib/amount'); | ||
const angle = require('./lib/angle'); | ||
const duration = require('./lib/duration'); | ||
const energy = require('./lib/energy'); | ||
const illuminance = require('./lib/illuminance'); | ||
const length = require('./lib/length'); | ||
const mass = require('./lib/mass'); | ||
const power = require('./lib/power'); | ||
const pressure = require('./lib/pressure'); | ||
const speed = require('./lib/speed'); | ||
const temperature = require('./lib/temperature'); | ||
const volume = require('./lib/volume'); | ||
module.exports = { | ||
Amount, Angle, Duration, Energy, Illuminance, Length, Mass, Power, | ||
Pressure, Speed, Temperature, Volume | ||
amount, generic: amount, | ||
angle, duration, energy, illuminance, length, mass, power, pressure, speed, | ||
temperature, volume | ||
}; |
@@ -9,4 +9,5 @@ 'use strict'; | ||
names: [ 'rad', 'radian', 'radians' ], | ||
scale: 360 / (2 * Math.PI) | ||
scale: 360 / (2 * Math.PI), | ||
prefix: true | ||
}) | ||
.build(); |
@@ -6,3 +6,4 @@ 'use strict'; | ||
names: [ 'J', 'j', 'joule', 'joules' ], | ||
prefix: true | ||
prefix: true, | ||
exposePrefixes: [ 'kilo', 'mega' ] | ||
}) | ||
@@ -13,4 +14,4 @@ .add('wh', { | ||
prefix: true, | ||
exposePrefixes: [ 'kilo' ] | ||
exposePrefixes: [ 'kilo', 'mega' ] | ||
}) | ||
.build(); |
@@ -6,8 +6,5 @@ 'use strict'; | ||
names: [ 'g', 'gram', 'grams', 'gramme', 'grammes' ], | ||
prefix: true | ||
prefix: true, | ||
exposePrefixes: [ 'kilo' ] | ||
}) | ||
.add('kg', { | ||
names: [ 'kg', 'kilogram', 'kilograms' ], | ||
scale: 1000 | ||
}) | ||
.add('lbs', { | ||
@@ -14,0 +11,0 @@ names: [ 'lb', 'lbs', 'pound', 'pounds', '#' ], |
@@ -10,3 +10,3 @@ 'use strict'; | ||
.add('atm', { | ||
names: [ 'atm', 'atmosphere' ], | ||
names: [ 'atm', 'atmosphere', 'atmospheres' ], | ||
scale: 101235 | ||
@@ -13,0 +13,0 @@ }) |
@@ -79,2 +79,10 @@ 'use strict'; | ||
scale: 1e18 | ||
}, | ||
{ | ||
names: [ 'Z', 'zetta' ], | ||
scale: 1e21 | ||
}, | ||
{ | ||
names: [ 'Y', 'yotta' ], | ||
scale: 1e24 | ||
} | ||
@@ -159,3 +167,3 @@ ]; | ||
// Create the instance factory | ||
this.Value = function(value, unit) { | ||
const Value = this.Value = function(value, unit) { | ||
this.value = value; | ||
@@ -178,10 +186,17 @@ this.unit = unit; | ||
this.Value.prototype.to = function(unit) { | ||
if(this.unit === unit) { | ||
return this; | ||
} | ||
const v = self.convert(this.value, this.unit, unit); | ||
return new Value(v, unit); | ||
} | ||
for(let key of Object.keys(this.conversions)) { | ||
const conversion = this.conversions[key]; | ||
for(let cName of conversion.names) { | ||
if(cName.length > 1) { | ||
Object.defineProperty(this.Value.prototype, cName, { | ||
get: createAs(cName) | ||
}); | ||
} | ||
Object.defineProperty(this.Value.prototype, cName, { | ||
get: createAs(cName) | ||
}); | ||
} | ||
@@ -188,0 +203,0 @@ |
@@ -5,21 +5,21 @@ 'use strict'; | ||
.base('mps', { | ||
names: [ 'm/s', 'mps', 'metersPerSecond', 'metresPerSecond' ], | ||
names: [ 'm/s', 'mps', 'metersPerSecond', 'metresPerSecond', 'metres/second', 'meters/second' ], | ||
prefix: true | ||
}) | ||
.add('kmh', { | ||
names: [ 'km/h', 'kph', 'kilometersPerHour', 'kilometresPerHour' ], | ||
names: [ 'km/h', 'kph', 'kilometersPerHour', 'kilometresPerHour', 'kilometres/hour', 'kilometers/hour' ], | ||
scale: 1000 / 3600 | ||
}) | ||
.add('mph', { | ||
names: [ 'mph', 'milesPerHour' ], | ||
names: [ 'mph', 'milesPerHour', 'miles/hour' ], | ||
scale: 0.44704 | ||
}) | ||
.add('fps', { | ||
names: [ 'ft/s', 'fps', 'footPerSecond' ], | ||
names: [ 'ft/s', 'fps', 'footPerSecond', 'feetPerSecond', 'foot/second', 'feet/second' ], | ||
scale: 0.3048 | ||
}) | ||
.add('knot', { | ||
names: [ 'kt', 'knot' ], | ||
names: [ 'kt', 'knot', 'knots' ], | ||
scale: 0.514444 | ||
}) | ||
.build(); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
.add('K', { | ||
names: [ 'K', 'kelvin' ], | ||
names: [ 'K', 'kelvin', 'kelvins' ], | ||
prefix: true, | ||
@@ -19,3 +19,3 @@ toBase: function(value) { | ||
.add('F', { | ||
names: [ 'F', 'f', 'fahrenheit' ], | ||
names: [ 'F', 'f', 'fahrenheit', 'fahrenheits' ], | ||
toBase: function(value) { | ||
@@ -22,0 +22,0 @@ return (value - 32) * (5/9); |
@@ -26,7 +26,7 @@ 'use strict'; | ||
.add('fluidOunce', { | ||
names: [ 'floz', 'oz', 'fluid-ounce', 'ounze', 'fluid-ounces', 'ounzes' ], | ||
names: [ 'floz', 'oz', 'fluid-ounce', 'ounce', 'fluid-ounces', 'ounces' ], | ||
scale: 0.0295735 | ||
}) | ||
.add('tablespoon', { | ||
names: [ 'tb', 'tbsp', 'tbs', 'tablesppon', 'tablespoons' ], | ||
names: [ 'tb', 'tbsp', 'tbs', 'tablespoon', 'tablespoons' ], | ||
scale: 0.0147868 | ||
@@ -33,0 +33,0 @@ }) |
{ | ||
"name": "amounts", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Measurements and unit conversions", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
288
README.md
# Amounts | ||
Measurements and unit conversions for JavaScript. | ||
Measurements and unit conversions for JavaScript. Supports angle, duration, | ||
energy, illuminance, length, mass, power, pressure, speed, temperature, volume | ||
and generic amounts. | ||
```javascript | ||
const { Length } = require('amounts'); | ||
const { length, duration } = require('amounts'); | ||
const length = new Length('30 m'); // or new Length(30) or Length('12 ft') | ||
const value = length('30 m'); | ||
console.log(length.as('ft')); | ||
console.log(length('98 ft').as('cm')); | ||
console.log(duration('20 m, 4 s')); | ||
``` | ||
## 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. | ||
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: | ||
```javascript | ||
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. | ||
```javascript | ||
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 Name | Short name | Factor | ||
---------------|----------------|--------------------- | ||
`yocto` | `y` | 10<sup>-24</sup> | ||
`zepto` | `z` | 10<sup>-21</sup> | ||
`atto` | `a` | 10<sup>-18</sup> | ||
`femto` | `f` | 10<sup>-15</sup> | ||
`pico` | `p` | 10<sup>-12</sup> | ||
`nano` | `n` | 10<sup>-9</sup> | ||
`micro` | `u`, `mc`, `µ` | 10<sup>-6</sup> | ||
`milli` | `m` | 10<sup>-3</sup> | ||
`centi` | `c` | 10<sup>-2</sup> | ||
`deci` | `d` | 10<sup>-1</sup> | ||
`deca`, `deka` | `da` | 10<sup>1</sup> | ||
`hecto` | `h` | 10<sup>2</sup> | ||
`kilo` | `k` | 10<sup>3</sup> | ||
`mega` | `M` | 10<sup>6</sup> | ||
`giga` | `G` | 10<sup>9</sup> | ||
`tera` | `T` | 10<sup>12</sup> | ||
`peta` | `P` | 10<sup>15</sup> | ||
`exa` | `E` | 10<sup>18</sup> | ||
`zetta` | `Z` | 10<sup>21</sup> | ||
`yotta` | `Y` | 10<sup>24</sup> | ||
## Angle | ||
```javascript | ||
const { angle } = require('amounts'); | ||
console.log(angle(2, 'rad')); | ||
console.log(angle('5 degrees').as('radians')); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Degree | No | `deg`, `degree`, `degrees` | ||
Radian | Yes | `rad`, `radian`, `radians` | ||
## Duration | ||
Durations represent a number of milliseconds something takes. Multiple units | ||
can be combined into a value. | ||
```javascript | ||
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')); | ||
``` | ||
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 | ||
```javascript | ||
const { energy } = require('amounts'); | ||
console.log(energy(10)); // Joules | ||
console.log(energy('3.5 kJ').kWh); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Joules | Yes | `J`, `j`, `joule`, `joules` | ||
Watt hours | True | `Wh`, `wh`, `wattHour`, `wattHours` | ||
## Illuminance | ||
```javascript | ||
const { illuminance } = require('amounts'); | ||
console.log(illuminance(2, 'lux')); | ||
console.log(angle('8000 lux')); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Lux | No | `lux` | ||
## Length | ||
```javascript | ||
const { length } = require('amounts'); | ||
console.log(length(2)); // Meters | ||
console.log(length('5 ft').as('micrometer')); | ||
console.log(length('2 ft ').cm); | ||
``` | ||
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 | ||
```javascript | ||
const { mass } = require('amounts'); | ||
console.log(mass(210)); // Grams | ||
console.log(mass('78 kg').lbs); | ||
console.log(mass('2 stone').kg) | ||
``` | ||
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 | ||
```javascript | ||
const { power } = require('amounts'); | ||
console.log(power(500)); // Watts | ||
console.log(power('6000 kW').mW); | ||
console.log(power('6 hp').as('microwatts')); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Watt | Yes | `w`, `W`, `watt` | ||
Horsepower | No | `hp`, `horsepower` | ||
## Pressure | ||
```javascript | ||
const { pressure } = require('amounts'); | ||
console.log(pressure(500)); // Pascal | ||
console.log(power('700 hPa').atm); | ||
console.log(power('7 bar').kPa); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Pascal | Yes | `pa`, `Pa`, `pascal` | ||
Atmosphere | No | `atm`, `atmosphere`, `athmospheres` | ||
Bar | No | `bar` | ||
PSI | No | `psi`, `poundsPerSquareInch` | ||
Torr | No | `torr` | ||
## Speed | ||
```javascript | ||
const { speed } = require('amounts'); | ||
console.log(speed(500)); // m/s | ||
console.log(speed('5 km/s').kph); | ||
console.log(speed('10 mph').mps); | ||
``` | ||
Unit | SI | Names | ||
---------------|------|-------------------- | ||
Metres/Second | Yes | `m/s`, `mps`, `metersPerSecond`, `metresPerSecond`, `metres/second`, `meters/second` | ||
Kilometre/Hour | No | `km/h`, `kph`, `kilometersPerHour`, `kilometresPerHour`, `kilometers/hour`, `kilometre/hour` | ||
Miles/Hour | No | `mph`, `milesPerHour`, `miles/hour` | ||
Feet/Second | No | `ft/s`, `fps`, `footPerSecond`, `feetPerSecond`, `foot/second`, `feet/second` | ||
Knot | No | `kt`, `knot`, `knots` | ||
## Temperature | ||
```javascript | ||
const { temperature } = require('amounts'); | ||
console.log(temperature(22)); // Celsius | ||
console.log(temperature('200 k').celsius); | ||
console.log(temperature(80, 'f').kelvin); | ||
``` | ||
Unit | SI | Names | ||
-------------|------|-------------------- | ||
Celsius | No | `C`, `c`, `celsius` | ||
Kelvin | Yes | `K`, `kelvin`, `kelvins` | ||
Fahrenheit | No | `F`, `f`, `fahrenheit`, `fahrenheits` | ||
## Volume | ||
```javascript | ||
const { volume } = require('amounts'); | ||
console.log(volume(2)); // Liters | ||
console.log(volume(2, 'quarts').dl); | ||
console.log(volume('20 ml').tbsp); | ||
``` | ||
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` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28248
624
293