Installation
Length was designed to work both in the browser and in Node.js.
Browser
<script src="length.js"></script>Length is available on unpgk CDN in compressed and uncompressed version.
Node.js
npm install length.jsvar length = require('length.js');
// or using ES6 import
import length from 'length.js';Usage
Length creates an object which contains value, unit, and conversion methods.
To get this object, simply call length() with two supported arguments. Then you can convert passed value by calling one of available method.
The Length prototype is exposed through length.fn (if you want to add your own functions).
length(value, unit)
Creates an object which contains value, unit, and conversion methods.
Arguments
-
value
(Number): Number of units. -
unit
(String): Unit type.Available unit types:
cm: centimeter,dm: decimeter,m: meter,km: kilometer,ft: foot,in: inch,yd: yard,mi: mile.
Returns
(Object): Returns new Length object.
Example
length(12, 'cm');Methods
.to(unit)
Arguments
- unit
(String): Unit type. Available unit types.
Returns
(Object): Length object with value converted to passed unit.
Example
length(100, 'cm').to('m');
// => { value: 1, unit: 'm' }.getValue()
Returns
(Number): Current value.
Example
length(100, 'cm').getValue();
// => 100.getUnit()
Returns
(String): Current unit type.
Example
length(100, 'cm').getUnit();
// => cm.getString(digits)
Arguments
- digits
(Number): The number of digits to appear after the decimal point.
Returns
(String): String containing value and unit type.
Example
length(100, 'cm').getString();
// => 100cm
length(100, 'cm').getString(2);
// => 100.00cm
length(30, 'cm').to('ft').getString();
// => 0.984251968503937ft
length(30, 'cm').to('ft').getString(2);
// => 0.98ft.add(value)
Arguments
- value
(Number): The number to increment value.
Returns
(Object): Length object with incremented value.
Example
length(100, 'cm').add(2);
// => { value: 102, unit: 'cm' }