Git Product home page Git Product logo

length.js's Introduction

Length.js

JavaScript library for length units conversion




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

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

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,
    • 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): New Length object.

Example

length(12, 'cm');
// => { value: 12, unit: 'cm }

Methods

.to(unit)

Arguments

  • unit (String): Unit type.

Returns

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

Example

length(100, 'cm').to('m');
// => { 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): New Length object with incremented value.

Example

length(100, 'cm').add(2);
// => { value: 102, unit: 'cm' }
length(100, 'cm').add(2, 'dm');
// => { value: 120, unit: 'cm' }

.toPrecision([digits])

Arguments

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

Returns

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

Example

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

.getValue()

Returns

  • (Number): Current value.

Example

length(100, 'cm').getValue();
// => 100

.getUnit()

Returns

  • (String): Current unit type.

Example

length(100, 'cm').getUnit();
// => cm

.getString(), .toString()

Returns

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

Example

length(100, 'cm').getString();
length(100, 'cm').toString();
// => 100cm
console.log(length(10, 'm') + '');
// => 10m

length.js's People

Contributors

tarekis avatar nchudleigh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.