convert-units
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -99,2 +99,33 @@ var convert | ||
/** | ||
* Converts the unit to the best available unit. | ||
*/ | ||
Converter.prototype.toBest = function() { | ||
if(!this.origin) | ||
throw new Error('.toBest must be called after .from'); | ||
var best; | ||
/** | ||
Looks through every possibility for the 'best' available unit. | ||
i.e. Where the value has the fewest numbers before the decimal point, | ||
but is still higher than 1. | ||
*/ | ||
each(this.possibilities(), function(possibility) { | ||
var unit = this.describe(possibility); | ||
if (unit.system === this.origin.system) { | ||
var result = this.to(possibility); | ||
if (!best || (result >= 1 && result < best.val)) { | ||
best = { | ||
val: result, | ||
unit: possibility, | ||
singular: unit.singular, | ||
plural: unit.plural | ||
}; | ||
} | ||
} | ||
}.bind(this)); | ||
return best; | ||
} | ||
/** | ||
* Finds the unit | ||
@@ -101,0 +132,0 @@ */ |
{ | ||
"name": "convert-units", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Convert between quantities in different units", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -36,2 +36,8 @@ convert-units | ||
You can ask `convert-units` to select the best unit for you: | ||
```js | ||
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` | ||
@@ -38,0 +44,0 @@ |
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
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
30047
27
1012
147