convert-units
Advanced tools
Comparing version 2.3.4 to 3.0.0-beta.0
{ | ||
"name": "convert-units", | ||
"version": "2.3.4", | ||
"version": "3.0.0-beta.0", | ||
"description": "Convert between quantities in different units", | ||
"main": "lib", | ||
"dependencies": { | ||
"lodash.keys": "2.3.x", | ||
"lodash.foreach": "2.3.x" | ||
}, | ||
"main": "./lib/cjs/index.js", | ||
"module": "./lib/esm/index.js", | ||
"files": [ | ||
"lib/" | ||
], | ||
"devDependencies": { | ||
"assert-diff": "^1.1.0", | ||
"jake": "8.0.12" | ||
"@types/jest": "^26.0.23", | ||
"@typescript-eslint/eslint-plugin": "^4.24.0", | ||
"@typescript-eslint/parser": "^4.23.0", | ||
"eslint": "^7.19.0", | ||
"eslint-config-prettier": "^4.3.0", | ||
"eslint-plugin-jest": "^24.3.6", | ||
"eslint-plugin-ordered-imports": "^0.5.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"jest": "^26.6.3", | ||
"prettier": "2.2.1", | ||
"prettier-plugin-organize-imports": "^2.1.0", | ||
"ts-jest": "^26.5.6", | ||
"typescript": "^4.2.4" | ||
}, | ||
"scripts": { | ||
"test": "jake test --trace" | ||
"test": "jest --coverage", | ||
"test:watch": "jest --watch", | ||
"lint": "eslint src", | ||
"format": "prettier --write src", | ||
"compile": "tsc -p ./tsconfig.json && tsc -p ./tsconfig-cjs.json && tsc -p ./tsconfig-umd.json" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/ben-ng/convert-units.git" | ||
"url": "git://github.com/convert-units/convert-units.git" | ||
}, | ||
@@ -21,0 +36,0 @@ "keywords": [ |
253
README.md
convert-units | ||
============= | ||
[![Build Status](https://travis-ci.org/ben-ng/convert-units.png)](https://travis-ci.org/ben-ng/convert-units) [![Downloads](https://img.shields.io/npm/dm/convert-units.svg)](https://www.npmjs.com/package/convert-units) | ||
[![Downloads](https://img.shields.io/npm/dm/convert-units.svg)](https://www.npmjs.com/package/convert-units) | ||
@@ -11,17 +11,51 @@ A handy utility for converting between quantities in different units. | ||
``` | ||
```bash | ||
npm install convert-units --save | ||
``` | ||
```bash | ||
# beta builds are also available with | ||
npm install convert-units@next --save | ||
``` | ||
Usage | ||
----- | ||
`convert-units` has a simple chained API that is easy to read. | ||
`convert-units` has a simple chained API that is easy to read. It can also be configured with the measures that are packaged with it or custom measures. | ||
The code snippet below shows everything needed to get going: | ||
```js | ||
import configureMeasurements from 'convert-units'; | ||
// `allMeasures` includes all the measures packaged with this library | ||
import allMeasures from 'convert-units/definitions'; | ||
const convert = configureMeasurements(allMeausres); | ||
``` | ||
It's also possible to limit the measures configured to only the ones your application needs: | ||
```js | ||
import configureMeasurements from 'convert-units'; | ||
import volume from 'convert-units/definitions/volume'; | ||
import mass from 'convert-units/definitions/mass'; | ||
import length from 'convert-units/definitions/length'; | ||
/* | ||
`configureMeasurements` is a closure that accepts a directory | ||
of measures and returns a factory function (`convert`) that uses | ||
only those measures. | ||
*/ | ||
const convert = configureMeasurements({ | ||
volume, | ||
mass, | ||
length, | ||
}); | ||
``` | ||
Here's how you move between the metric units for volume: | ||
```js | ||
var convert = require('convert-units') | ||
convert(1).from('l').to('ml') | ||
convert(1).from('l').to('ml'); | ||
// 1000 | ||
@@ -33,3 +67,3 @@ ``` | ||
```js | ||
convert(1).from('lb').to('kg') | ||
convert(1).from('lb').to('kg'); | ||
// 0.4536... (tested to 4 significant figures) | ||
@@ -41,3 +75,3 @@ ``` | ||
```js | ||
convert(1).from('oz').to('fl-oz') | ||
convert(1).from('oz').to('fl-oz'); | ||
// throws -- you can't go from mass to volume! | ||
@@ -48,20 +82,29 @@ ``` | ||
```js | ||
convert(12000).from('mm').toBest() | ||
// 12 Meters (the smallest unit with a value above 1) | ||
convert(12000).from('mm').toBest(); | ||
// { val: 12, unit: 'm', plural: 'Meters' } (the smallest unit with a value above 1) | ||
convert(12000).from('mm').toBest({ exclude: ['m'] }) | ||
// 1200 Centimeters (the smallest unit excluding meters) | ||
convert(12000).from('mm').toBest({ exclude: ['m'] }); | ||
// { val: 1200, unit: 'cm', plural: 'Centimeters' } (the smallest unit excluding meters) | ||
convert(900).from('mm').toBest({ cutOffNumber: 10 }); | ||
// 900 Centimeters (the smallest unit with a value equal to or above 10) | ||
// { val: 90, unit: 'cm', plural: 'Centimeters' } (the smallest unit with a value equal to or above 10) | ||
convert(1000).from('mm').toBest({ cutOffNumber: 10 }) | ||
// 10 Meters (the smallest unit with a value equal to or above 10) | ||
convert(1000).from('mm').toBest({ cutOffNumber: 10 }); | ||
// { val: 100, unit: 'cm', plural: 'Centimeters' } (the smallest unit with a value equal to or above 10) | ||
``` | ||
You can get a list of the measurement types supported with `.measures` | ||
You can get a list of the measures available to the current instance with `.measures` | ||
```js | ||
convert().measures() | ||
// [ 'length', 'mass', 'volume' ] | ||
convert().measures(); | ||
// [ 'length', 'mass', 'volume', ... ] | ||
const differentConvert = configureMeasurements({ | ||
volume, | ||
mass, | ||
length, | ||
area, | ||
}); | ||
differentConvert().measures(); | ||
// [ 'length', 'mass', 'volume', 'area' ] | ||
``` | ||
@@ -72,6 +115,6 @@ | ||
```js | ||
convert().from('l').possibilities() | ||
convert().from('l').possibilities(); | ||
// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ] | ||
convert().from('kg').possibilities() | ||
convert().from('kg').possibilities(); | ||
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ] | ||
@@ -82,3 +125,3 @@ ``` | ||
```js | ||
convert().possibilities('mass') | ||
convert().possibilities('mass'); | ||
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ] | ||
@@ -89,3 +132,3 @@ ``` | ||
```js | ||
convert().possibilities() | ||
convert().possibilities(); | ||
// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't', 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal', 'ea', 'dz' ]; | ||
@@ -97,10 +140,10 @@ ``` | ||
```js | ||
convert().describe('kg') | ||
convert().describe('kg'); | ||
/* | ||
{ | ||
abbr: 'kg' | ||
, measure: 'mass' | ||
, system: 'metric' | ||
, singular: 'Kilogram' | ||
, plural: 'Kilograms' | ||
abbr: 'kg', | ||
measure: 'mass', | ||
system: 'metric', | ||
singular: 'Kilogram', | ||
plural: 'Kilograms', | ||
} | ||
@@ -113,10 +156,10 @@ */ | ||
```js | ||
convert().list() | ||
convert().list(); | ||
/* | ||
[{ | ||
abbr: 'kg' | ||
, measure: 'mass' | ||
, system: 'metric' | ||
, singular: 'Kilogram' | ||
, plural: 'Kilograms' | ||
abbr: 'kg', | ||
measure: 'mass', | ||
system: 'metric', | ||
singular: 'Kilogram', | ||
plural: 'Kilograms', | ||
}, ...] | ||
@@ -129,10 +172,10 @@ */ | ||
```js | ||
convert().list('mass') | ||
convert().list('mass'); | ||
/* | ||
[{ | ||
abbr: 'kg' | ||
, measure: 'mass' | ||
, system: 'metric' | ||
, singular: 'Kilogram' | ||
, plural: 'Kilograms' | ||
abbr: 'kg', | ||
measure: 'mass', | ||
system: 'metric', | ||
singular: 'Kilogram', | ||
plural: 'Kilograms', | ||
}, ...] | ||
@@ -142,12 +185,99 @@ */ | ||
Supported Units | ||
Custom Measures | ||
--------------- | ||
```js | ||
import configureMeasurements from 'convert-units'; | ||
import length from 'convert-units/definitions/length'; | ||
import area from 'convert-units/definitions/area'; | ||
const customEach = { | ||
systems: { | ||
metric: { | ||
ea: { | ||
name: { | ||
singular: 'Each', | ||
plural: 'Each', | ||
}, | ||
to_anchor: 1, | ||
}, | ||
dz: { | ||
name: { | ||
singular: 'Dozen', | ||
plural: 'Dozens', | ||
}, | ||
to_anchor: 12, | ||
}, | ||
hdz: { | ||
name: { | ||
singular: 'Half Dozen', | ||
plural: 'Half Dozens', | ||
}, | ||
to_anchor: 6, | ||
}, | ||
}, | ||
}, | ||
anchors: { | ||
metric: { | ||
unit: 'ea', | ||
ratio: 1, | ||
}, | ||
}, | ||
}; | ||
export default configureMeasurements({ length, area, each: customEach }); | ||
``` | ||
Migrating from Old API | ||
--------------------- | ||
This only applies if moving from `<=2.3.4` to `>=3.x`. | ||
`index.js` | ||
```js | ||
import convert from 'convert-units'; | ||
convert(1).from('m').to('mm'); | ||
convert(1).from('m').to('ft'); | ||
``` | ||
The code above could be changed to match the following: | ||
`index.js` | ||
```js | ||
import convert from './convert'; // defined below | ||
convert(1).from('m').to('mm'); | ||
convert(1).from('m').to('ft'); | ||
``` | ||
`convert.js` | ||
```js | ||
import configureMeasurements from 'convert-units'; | ||
import allMeasures from 'convert-units/definitions'; | ||
export default configureMeasurements(allMeasures); | ||
``` | ||
Request Measures & Units | ||
----------------------- | ||
All new measures and additional units are welcome! Take a look at [`src/definitions`](https://github.com/convert-units/convert-units/tree/main/src/definitions) to see some examples. | ||
Packaged Units | ||
-------------- | ||
### Length | ||
* nm | ||
* μm | ||
* mm | ||
* cm | ||
* m | ||
* km | ||
* in | ||
* yd | ||
* ft-us | ||
* ft | ||
* fathom | ||
* mi | ||
* nMi | ||
@@ -265,3 +395,3 @@ ### Area | ||
* km/h | ||
* m/h | ||
* mph | ||
* knot | ||
@@ -274,3 +404,3 @@ * ft/s | ||
* s/ft | ||
* min/km | ||
* min/mi | ||
@@ -325,2 +455,6 @@ ### Pressure | ||
* GW | ||
* PS | ||
* Btu/s | ||
* ft-lb/s | ||
* hp | ||
@@ -364,6 +498,34 @@ ### Apparent Power | ||
### Want More? | ||
### Charge | ||
* c | ||
* mC | ||
* μC | ||
* nC | ||
* pC | ||
Adding new measurement sets is easy. Take a look at [`lib/definitions`](https://github.com/ben-ng/convert-units/tree/master/lib/definitions) to see how it's done. | ||
### Force | ||
* N | ||
* kN | ||
* lbf | ||
### Acceleration | ||
* g (g-force) | ||
* m/s2 | ||
### Pieces | ||
* pcs | ||
* bk-doz | ||
* cp | ||
* doz-doz | ||
* doz | ||
* gr-gr | ||
* gros | ||
* half-dozen | ||
* long-hundred | ||
* ream | ||
* scores | ||
* sm-gr | ||
* trio | ||
License | ||
@@ -393,1 +555,2 @@ ------- | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
504018
0
177
19129
541
13
2
2
- Removedlodash.foreach@2.3.x
- Removedlodash.keys@2.3.x
- Removedlodash._basebind@2.3.0(transitive)
- Removedlodash._basecreate@2.3.0(transitive)
- Removedlodash._basecreatecallback@2.3.0(transitive)
- Removedlodash._basecreatewrapper@2.3.0(transitive)
- Removedlodash._createwrapper@2.3.0(transitive)
- Removedlodash._objecttypes@2.3.0(transitive)
- Removedlodash._renative@2.3.0(transitive)
- Removedlodash._setbinddata@2.3.0(transitive)
- Removedlodash._shimkeys@2.3.0(transitive)
- Removedlodash._slice@2.3.0(transitive)
- Removedlodash.bind@2.3.0(transitive)
- Removedlodash.foreach@2.3.0(transitive)
- Removedlodash.forown@2.3.0(transitive)
- Removedlodash.identity@2.3.0(transitive)
- Removedlodash.isfunction@2.3.0(transitive)
- Removedlodash.isobject@2.3.0(transitive)
- Removedlodash.keys@2.3.0(transitive)
- Removedlodash.noop@2.3.0(transitive)
- Removedlodash.support@2.3.0(transitive)