Length.js – JavaScript Library for Length Units Conversion

Length.js is a very useful & great JavaScript library for length units conversion.

The length.js was designed to work both in the browser and in Node.js.

How to install Length.js?

As mentioned above, length.js was designed to work both in the browser and in Node.js.

Browser

<script src="length.js"></script>

Length is available on unpkg CDN in compressed and uncompressed version.

Node.js

npm install length.js
var length = require('length.js');
// or using ES6 import
import length from 'length.js';

How to use Length.js?

Length creates an object which contains valueunit, 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:
    • pm: picometre,
    • nm: nanometre,
    • um: micrometre,
    • mm: milimeter,
    • cm: centimeter,
    • dm: decimeter,
    • m: meter,
    • km: kilometer,
    • ft: foot,
    • in: inch,
    • yd: yard,
    • mi: mile.

Returns

  • (Object): New Length object.

Example

length(12, 'cm');

Methods

.to(unit)

Arguments

Returns

  • (Object): New Length object with value converted to the 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()

Returns

  • (String): String containing value and unit type.

Example

length(100, 'cm').getString();
// => 100cm
length(30, 'cm').to('ft').getString();
// => 0.984251968503937ft

Website: https://github.com/appalaszynski/length.js