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

qty

v1.0.1

Published

Parse, validate, manipulate, and display physical dimensions.

Downloads

150

Readme

Parse, manipulate, format and validate physical dimensions.

NPM Build Status Coverage Status

Install

Browser

$ bower install --save qty

Include Qty.js and its dependencies in your html:

<script src="components/underscore/underscore.js"></script> 
<!-- or <script src="components/lodash/lodash.js"></script> --> 
<script src="components/qty/min/qty.min.js"></script>

nodejs or iojs

$ npm install --save qty
var Qty = require('qty');
// ...

AngularJS support (optional)

  • Add reference to angular-qty.js
<script src="components/underscore/underscore.js"></script> 
<!-- or <script src="components/lodash/lodash.js"></script> --> 
<script src="components/angular/angular.js"></script>
<script src="components/qty/min/qty.min.js"></script>
<script src="components/qty/min/angular-qty.min.js"></script>
  • Include qty into application's dependencies
angular.module('app', [..., 'qty']);

Basic usage

Parse

Qty('2.54cm').value();              // 2.54
Qty('1 oz').value();                // 1
Qty('100 mph').value();             // 100
Qty('1MB').value();                 // 1

Manipulate

Conversions

Qty('2.54cm').to('inch').value();   // 1
Qty('1 oz').toSI().value();         // 0.0283... 
Qty('100 mph').to('km/h').value();  // 160.934...
Qty('1MiB').to('bit').value();      // 8388608 (=== 8 x 2²⁰)

Note:

Additions

Qty('1m').add(1, 'km').format('0u') // 1001m

Subtractions

Qty('1m').subtract('2km').format('0u') // -1999m

Multiplications

Qty('2m').times('2m').format('0u') // 4m^2
Qty('2m').times('2m').isArea()     // true

Divisions

Qty('1m').by('1s').format('0u') // 1m/s
Qty('1m').by('1s').isSpeed()    // true

Format

Qty(Math.PI).format('0.00000');             // 3.14159
Qty(1/4).format('0.[000]');                 // 0.25
Qty(1/4).to('%').format('0u');              // 25%
Qty(1/4).to('%').format('0 U');             // 25 percents
Qty('2.54cm').to('inch').format();          // 1"
Qty('1 oz').toSI().format();                // 0.03kg
Qty('100 mph').to('km/h').format();         // 160.93km/h
Qty('100 mph').to('km/h').format('0 U');    // 161 kilometers per hour
Qty('1TiB').toSI().format();                // 1,099,511,627,776B

Note: default format is 0,0[.]00u

Validate

Qty(Math.PI).isScalar();    // true
Qty('2.54cm').isScalar();   // false
Qty('2.54cm').isLength();   // true
Qty('1 oz').isMass();       // true
Qty('100 mph').isSpeed();   // true
Qty('1024KiB').isMemory();  // true
...

Note: all possible dimensions are listed in Doc/Dimensions

Doc

Locale

Default locale is 'en' (US-English).

You can import existing languages by referencing locale file:

...   
<script src="components/qty/locale/__LOCALE_KEY__.js"></script>

Note: Use 'Qty.locale(LOCALE_KEY)' to enable the language.

You can also setup a new language using:

Qty.defineLocale(__LOCALE_KEY__, {
    units: {
       __KEY_0__: [__VAL_0__, __VAL_1__, ...],
       __KEY_1__: [__VAL_0__, __VAL_1__, ...],
       ...
    },
    format: {
        thousands: ',',
        decimal: '.',
        unit: '',
        divideShort:'/',
        divideLong: ' per ',
        multiplyShort: '*',
        multiplyLong: ' '
    }
});

Note: see locale/fr.js for an example.

Formats

General syntax: Qty().format(__FORMAT_TEMPLATE__)

Examples:

// value only
Qty(1e4).format('0,0.0000');        //  10,000.0000
Qty(1e4 * Math.PI).format('0,0');   //  31,416
Qty(1e4 * Math.PI).format('+0,0');  // +31,416
Qty(1e4 * Math.PI).format('0,0.0'); //  31,415.9
Qty(1e4 * Math.PI).format('0.000'); //  31415.927
Qty(123.456).format('0[.]00000');   //  123.45600
Qty(-123.456).format('(0,0.0000)'); // (123.4560)
Qty(-123.456).format('0.0[0000]');  // -123.456

// value with unit
Qty(123.123, 'mph').format('0u');   // 123mph
Qty(123.123, 'mph').format('0 u');  // 123 mph
Qty(123.123, 'mph').format('0 U');  // 123 miles per hour

Note: default format is 0,0[.]00u

Dimensions

Supported dimensions are :

  • Scalar (no unit),
  • Length (L),
  • Area (L²),
  • Volume (L³),
  • Duration: (T),
  • Speed : (L/T),
  • Acceleration: (L / T²),
  • Temperature: (K),
  • Mass: (M),
  • Pressure: (M / L / T²),
  • Memory: (bytes)
  • Current: (I)
  • Substance: (N)
  • Capacitance: (T⁴ * I² / L² / M),
  • Charge: (I * T),
  • Conductance (T³ * I² / L² / M),
  • Inductance (L² * M / T² / I²),
  • Potential (L² * M / T³ / I),
  • Resistance (L² * M / T³ / I²)
  • MagneticFlux (M * L² / T² / I)
  • MagneticFluxDensity (M / T² / I)
  • Energy: (L² * M / T²)
  • Force: M² / T²
  • Frequency: (1 / T),
  • Angle (angle),
  • AngularVelocity: (angle / T),
  • Luminosity (J),
  • Power: (M * L² / T³)

AngularJS

qtyBind directive

The qty-bind attribute tells angular to replace the text content of the specified HTML element with the formatted value of the qty

Usage

<ANY qty-bind="qtyExpr"></ANY>
<ANY qty-bind="qtyExpr" qty-convert-to="unitExpression"></ANY>
<ANY qty-bind="qtyExpr" qty-format="formatValue"></ANY>
<ANY qty-bind="qtyExpr" qty-unit="unitValue"></ANY>

Arguments

Param | type | Details -------------|-------------|--------------- qtyBind | expression | Can be a Qty, a String or a Number qtyConvertTo | expression | Must resolve to a String qtyFormat | expression | Must resolve to a String. See Doc / formats for formatting options qtyUnit | expression | Must resolve to a String

qtyConvertTo & qtyConvertToSI filters

Usage

{{ qty | qtyConvertTo: unit }}
{{ qty | qtyConvertToSI: format }}

Arguments

Param | type | Details -------|-------------|--------------------- qty | expression | Can be a Qty, a String or a Number unit | expression | Must resolve to a String

qtyFormat filter

Usage

{{ qty | qtyFormat: format }}

Arguments

Param | type | Details -------|-------------|--------------------- qty | expression | Can be a Qty, a String or a Number format | string | See Doc / formats

Tests

grunt test

Note: use grunt exec:coveralls for coverage report

Credits

Qty.js was inspired by (and heavily borrowed from) :

License

The MIT License

Copyright (c) 2015 SEN.SE Copyright (c) 2015 Nicolas Panel

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.