๐Ÿ“ JavaScript library for length units conversion.
JavaScript
Switch branches/tags
Fetching latest commitโ€ฆ
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gulp Create basic Gulp workflow Apr 13, 2018
min Build 0.0.4 Apr 13, 2018
.gitignore Create .gitignore Apr 12, 2018
LICENSE Create LICENSE Apr 12, 2018
README.md Update README.md Apr 13, 2018
gulpfile.js Create basic Gulp workflow Apr 13, 2018
length.js Build 0.0.4 Apr 13, 2018
package-lock.json Build 0.0.4 Apr 13, 2018
package.json Build 0.0.4 Apr 13, 2018

README.md

Length.js

JavaScript library for length units conversion




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.js
var 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

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' }