The original author of the library is appalaszynski, the library was no longer available so I republished it, all credit goes to him.
Table of Contents
Installation
Length.js was designed to work both in the browser and in Node.js.
Browser
Length.js is available on unpkg CDN in compressed and uncompressed version.
Node.js
$ npm install length.js
var length = // or using ES6 import
Usage
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).
length(value, unit)
Creates an object which contains value and unit.
Arguments
-
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,ld
: lunar distance: 384 402 000 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.
Returns
(Object)
: NewLength
object.
Example
// => { value: 12, unit: 'cm }
Methods
.to(unit)
Arguments
- unit
(String)
: Unit type.
Returns
(Object)
: NewLength
object with value converted to passed unit.
Example
// => { value: 1, unit: 'm' }
.add(value, [unit])
Arguments
- value
(Number)
: The number to increment value. - [unit]
(String)
: Unit type in which the value was given.
Returns
(Object)
: NewLength
object with incremented value.
Example
// => { value: 102, unit: 'cm' }// => { value: 120, unit: 'cm' }
.toPrecision([digits])
Arguments
- [digits]
(Number)
: The number of digits to appear after the decimal point.
Returns
(Object)
: NewLength
object with fixed value.
Example
// => { value: 100, unit: 'cm' }// => { value: 100, unit: 'cm' }// => { value: 0.98, unit: 'cm' }
.getValue()
Returns
(Number)
: Current value.
Example
// => 100
.getUnit()
Returns
(String)
: Current unit type.
Example
// => cm
.getString()
, .toString()
Returns
(String)
: String containing value and unit type.
Example
// => 100cmconsole// => 10m
Contributing
All contributions and suggestions are welcome! For suggested improvements, please create an issue. For direct contributions, please fork the repository, create your feature branch, commit your changes, push commits to the branch and create a new pull request.
License
The code is open source and available under the MIT License.