Hundreds of javascript libraries exists, some are useful and some are useless to someone. One such useful library I found today is length.js .

What is length.js?

In simple words length.js is a library which you can use to convert one length unit into another one very easily and fast. It comes handy when solving length related problems.

A lot of units are supported. Below is full list.

  • 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.

So now let’s learn how to install and use this library.

Installation

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

Browser

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

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

Node.js


Usage

 

Length.js creates an object which contains valueand 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.

Methods

.to(unit)

Arguments

  • unit (String): Unit type.

Returns

  • (Object): New Length object with value converted to passed unit.

Example

.add(value, [unit])

Arguments

  • value (Number): The number to increment value.
  • [unit] (String): Unit type in which the value was given.

Returns

  • (Object): New Length object with incremented value.

Example

.toPrecision([digits])

Arguments

  • [digits] (Number): The number of digits to appear after the decimal point.

Returns

  • (Object): New Length object with fixed value.

Example

.getValue()

Returns

  • (Number): Current value.

Example

.getUnit()

Returns

  • (String): Current unit type.

Example

.getString().toString()

Returns

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

Example

Last words

I hope this library will help you in solving some problems. And if you enjoyed reading this article then please do share it with your friends. And don’t hesitate to comment if you have any problem. See you in the next post, till then happy solving! 

Posted by Jalaj

A student, a learner, constantly learning new things and have great interest in programming and web development.

2 Comments

  1. Nice intro brother! But i have one problem

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.