Length.js was designed to work both in the browser and in Node.js.
<script src="length.js"></script>
Length.js is available on unpkg CDN in compressed and uncompressed version.
$ npm install length.js
var length = require('length.js');
// or using ES6 import
import length from 'length.js';
Length.js creates an object which contains value and unit.
To get this object, simply call length()
with two supported arguments. Then you can call available methods.
The Length
prototype is exposed through length.fn
(if you want to add your own functions).
Creates an object which contains value and unit.
-
value
(Number)
: Number of units. -
unit
(String)
: Unit type.Available unit types:
pm
: picometre: (1 / 1 000 000 000 000) m,nm
: nanometre: (1 / 1 000 000 000) m,um
: micrometre: (1 / 1 000 000) m,mm
: millimetre: (1 / 1 000) m,cm
: centimetre: (1 / 100) m,dm
: decimetre: (1 / 10) m,m
: metre,dam
: decametre: 10 m,hm
: hectoametre: 100 m,km
: kilometre: 1 000 m,nmi
: nautical mile: 1 852 m,ft
: foot: 0.3048 m,in
: inch: 0.0254 m,yd
: yard: 0.9144 m,fm
: fathom: 1.8288 m,mi
: mile: 1 609.344 m,au
: astronomical unit: 149 597 870 700 m,ly
: light year: 9 460 730 472 580 800 m,pc
: parsec: ((648 000 / ฯ) * 149 597 870 700) m.
(Object)
: NewLength
object.
length(12, 'cm');
// => { value: 12, unit: 'cm }
- unit
(String)
: Unit type.
(Object)
: NewLength
object with value converted to passed unit.
length(100, 'cm').to('m');
// => { value: 1, unit: 'm' }
- value
(Number)
: The number to increment value. - [unit]
(String)
: Unit type in which the value was given.
(Object)
: NewLength
object with incremented value.
length(100, 'cm').add(2);
// => { value: 102, unit: 'cm' }
length(100, 'cm').add(2, 'dm');
// => { value: 120, unit: 'cm' }
- [digits]
(Number)
: The number of digits to appear after the decimal point.
(Object)
: NewLength
object with fixed value.
length(100, 'cm').toPrecision();
// => { value: 100, unit: 'cm' }
length(100, 'cm').toPrecision(2);
// => { value: 100, unit: 'cm' }
length(0.97982, 'cm').toPrecision(2);
// => { value: 0.98, unit: 'cm' }
(Number)
: Current value.
length(100, 'cm').getValue();
// => 100
(String)
: Current unit type.
length(100, 'cm').getUnit();
// => cm
(String)
: String containing value and unit type.
length(100, 'cm').getString();
length(100, 'cm').toString();
// => 100cm
console.log(length(10, 'm') + '');
// => 10m