Parse, manipulate, format and validate physical dimensions.
Install
Browser
$ bower install --save qty
Include Qty.js and its dependencies in your html:
<!-- or <script src="components/lodash/lodash.js"></script> -->
nodejs or iojs
$ npm install --save qty
var Qty = ;// ...
AngularJS support (optional)
- Add reference to
angular-qty.js
<!-- or <script src="components/lodash/lodash.js"></script> -->
- Include
qty
into application's dependencies
angular.module('app', [..., 'qty']);
Basic usage
Parse
value; // 2.54value; // 1value; // 100value; // 1
Manipulate
Conversions
value; // 1value; // 0.0283... value; // 160.934...value; // 8388608 (=== 8 x 2²⁰)
Note:
Qty#toSI
function refers to International System of Units.- Both quantities must have the same dimensions
Additions
// 1001m
Subtractions
// -1999m
Multiplications
// 4m^2 // true
Divisions
// 1m/s // true
Format
; // 3.14159; // 0.25; // 25%; // 25 percents; // 1"; // 0.03kg; // 160.93km/h; // 161 kilometers per hour; // 1,099,511,627,776B
Note: default format is 0,0[.]00u
Validate
; // true; // false; // true; // true; // true; // 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:
...
Note: Use 'Qty.locale(LOCALE_KEY)' to enable the language.
You can also setup a new language using:
Qty;
Note: see locale/fr.js
for an example.
Formats
General syntax: Qty().format(__FORMAT_TEMPLATE__)
Examples:
// value only; // 10,000.0000; // 31,416; // +31,416; // 31,415.9; // 31415.927; // 123.45600; // (123.4560); // -123.456 // value with unit; // 123mph; // 123 mph; // 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
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: usegrunt 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.