npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

measurement

v0.2.1

Published

A flexible library for handling unit conversions

Downloads

399

Readme

Measurement Build Status Analytics

A flexible library for handling unit conversions

Installation

npm install measurement

or include it on your page.

<script src="dist/measurement.min.js" type="text/javascript"></script>

Basic Usage

To start initialize a new instance of the measurement factory.

var measurement = require('measurement');
var mf = new measurement.Factory();

// Get a new measurement
var measurement = mf.km(25);

// from a string
measurement = mf.measure('25km');

// or
measurement = mf.measurement('25km');


// as a combination of a scalar value and a unit
measurement = mf.measure(25, 'km');
measurement = mf.measure(25, mf.units.km);

// or
measurement = mf.measurement(25, 'km');
measurement = mf.measurement(25, mf.units.km);


// Get the scalar value that represent the measurement
var value = measurement.getValue();

// Get the value in terms of another unit
var valueInMeters = measurement.getValue('m');  // or .getValue(mf.units.m);

Converting Units

Measurements can be converted from one unit to another by calling a unit method or by using the generic to() and as() methods.

measurement = mf.km(8);

// convert to miles using the unit method
var miles = measurement.mi();

// using the as() method returns a copy with the converted unit
var miles = measurement.as('mi');
var miles = measurement.as(mf.units.mi);

// using to() converts the object itself to the given unit
measurement.to('mi');
measurement.to(mf.units.mi);

Compound Units

Compound units can be built up from the base units of TIME, LENGTH, VOLUME, MEMORY, and MASS to represent values such as speed or transfer rate.

var distance = mf.km(25);

// The per() method adds another unit to the current, compounding it

var speed = distance.per('hr');  // or per(mf.units.hr)

// There isn't a hard limit to the amount of units you can compound on

// 25km/hr/s, acceleration at a pace of 25km/hr every second
var acceleration = speed.per('s'); // mf.km(25).per('hr').per('s')

Converting Compound Units

Measurements with compound units are special and act as an array of different units bunched together. The unit methods act on separate pieces of compound units, while the to() and as() methods act differently whether another compound unit or a base unit is supplied.

var speed = mf.km(25).per('hr'); // => 25km/h

// Using the unit method to convert to mi/h
var mph = speed.mi();

// Unit methods default to changing the first unit in the list,
// but they can also be supplied a zero-based index
var metersPerSecond = speed.m().s(1);  // => 6.9444m/s

// All of the functions that deal with units can take an index parameter to
// deal with the individual parts of compound units
var valueInKilometersPerSecond = speed.getValue('s', 1);