Git Product home page Git Product logo

convert-units's Introduction

convert-units

Build Status

A handy utility for converting between quantities in different units.

Usage

convert-units has a simple chained API that is easy to read.

Here's how you move between the metric units for volume:

var convert = require('convert-units')

convert(1).from('l').to('ml')
// 1000

Jump from imperial to metric units the same way:

convert(1).from('lb').to('kg')
// 0.4536... (tested to 4 significant figures)

Just be careful not to ask for an impossible conversion:

convert(1).from('oz').to('fl-oz')
// throws -- you can't go from mass to volume!

You can ask convert-units to select the best unit for you:

convert(1200).from('mm').toBest()
// 1.2 Meters (the smallest unit with a value above 1)

You can get a list of the measurement types supported with .measures

convert().measures()
// [ 'length', 'mass', 'volume' ]

If you ever want to know the possible conversions for a unit, just use .possibilities

convert().from('l').possibilities()
// [ 'ml', 'l', 'tsp', 'tbsp', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]

convert().from('kg').possibilities()
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]

You can also get the possible conversions for a measure:

convert().possibilities('mass')
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]

You can also get the all the available units:

convert().possibilities()
// [ 'mm', 'cm', 'm', 'in', 'ft', 'mi', 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal', 'ea' ];

To get a detailed description of a unit, use describe

convert().describe('kg')
/*
  {
    abbr: 'kg'
  , measure: 'mass'
  , system: 'metric'
  , singular: 'Kilogram'
  , plural: 'Kilograms'
  }
*/

To get detailed descriptions of all units, use list.

convert().list()
/*
  [{
    abbr: 'kg'
  , measure: 'mass'
  , system: 'metric'
  , singular: 'Kilogram'
  , plural: 'Kilograms'
  }, ...]
*/

You can also get detailed descriptions of all units for a measure:

convert().list('mass')
/*
  [{
    abbr: 'kg'
  , measure: 'mass'
  , system: 'metric'
  , singular: 'Kilogram'
  , plural: 'Kilograms'
  }, ...]
*/

Supported Units

Length

  • mm
  • cm
  • m
  • in
  • ft
  • mi

Mass

  • mcg
  • mg
  • g
  • kg
  • oz
  • lb

Volume

  • mm3
  • cm3
  • ml
  • l
  • m3
  • km3
  • tsp
  • tbsp
  • in3
  • fl-oz
  • cup
  • pnt
  • qt
  • gal
  • ft3
  • yd3

Want More?

Adding new measurement sets is easy. Take a look at lib/definitions to see how it's done.

License

Copyright (c) 2013 Ben Ng, http://benng.me

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

convert-units's People

Contributors

jeffstieler avatar jkudish avatar maddijoyce avatar mgtitimoli avatar protometa avatar toddself 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.