corva-convert-units
Advanced tools
Comparing version 1.15.1 to 1.16.0
# Changelog | ||
## [1.16.0](https://github.com/corva-ai/corva-convert-units/compare/v1.15.1...v1.16.0) (2024-05-31) | ||
### Features | ||
* **dri-1031:** add the ability to support measure (unit group) ([#42](https://github.com/corva-ai/corva-convert-units/issues/42)) ([fe33f34](https://github.com/corva-ai/corva-convert-units/commit/fe33f342aaa70d817c0700e77f5a19a6123696aa)) | ||
## [1.15.1](https://github.com/corva-ai/corva-convert-units/compare/v1.15.0...v1.15.1) (2024-05-27) | ||
@@ -4,0 +11,0 @@ |
@@ -51,9 +51,15 @@ const unitBucketMapping = require('./definitions/unitBucketMapping'); | ||
Converter = function(numerator, denominator) { | ||
Converter = function(numerator, measure) { | ||
this.unitBucketMapping = unitBucketMapping; | ||
this.measure = measure; | ||
if (denominator) | ||
this.val = numerator / denominator; | ||
else | ||
this.val = numerator; | ||
if (this.measure) { | ||
const isMeasureValid = this.measures().includes(this.measure); | ||
if (!isMeasureValid) { | ||
this.throwUnsupportedMeasureError(this.measure); | ||
} | ||
} | ||
this.val = numerator; | ||
}; | ||
@@ -70,3 +76,3 @@ | ||
this.origin = this.getUnit(from); | ||
this.origin = this.getUnit(from, this.measure); | ||
@@ -87,3 +93,3 @@ if (!this.origin) { | ||
this.destination = this.getUnit(to); | ||
this.destination = this.getUnit(to, this.measure); | ||
@@ -101,13 +107,2 @@ var result, transform; | ||
// You can't go from liquid to mass, for example | ||
if (this.destination.measure != this.origin.measure) { | ||
var pair = this.getUnitForPair(this.destination.abbr, this.origin.abbr); | ||
if (!pair) { | ||
throw new Error('Cannot convert incompatible measures of ' + | ||
this.destination.measure + ' and ' + this.origin.measure); | ||
} | ||
this.destination = pair[0]; | ||
this.origin = pair[1]; | ||
} | ||
/** | ||
@@ -131,3 +126,2 @@ * Convert from the source value to its anchor inside the system | ||
/** | ||
@@ -195,6 +189,8 @@ * Convert from one system to another through the anchor ratio. Some conversions | ||
*/ | ||
Converter.prototype.getUnit = function(abbr) { | ||
Converter.prototype.getUnit = function(abbr, measure) { | ||
var found; | ||
each(measures, function(systems, measure) { | ||
if (measure) { | ||
const systems = measures[measure]; | ||
each(systems, function(units, system) { | ||
@@ -220,14 +216,10 @@ if (system === '_anchors') | ||
if (found) | ||
return false; | ||
}); | ||
if (!found) { | ||
this.throwUnsupportedCompatibilityError(abbr, measure); | ||
} | ||
return found; | ||
}; | ||
return found; | ||
} | ||
Converter.prototype.getUnitForPair = function(abbrOne, abbrTwo) { | ||
var foundOne; | ||
var foundTwo; | ||
// if measure is not provided, it will try to find the first matching one from the whole measures list | ||
each(measures, function(systems, measure) { | ||
@@ -239,5 +231,5 @@ each(systems, function(units, system) { | ||
each(units, function(unit, testAbbr) { | ||
if (testAbbr === abbrOne) { | ||
foundOne = { | ||
abbr: abbrOne, | ||
if (testAbbr === abbr) { | ||
found = { | ||
abbr: abbr, | ||
measure: measure, | ||
@@ -247,12 +239,2 @@ system: system, | ||
}; | ||
} else if (testAbbr === abbrTwo) { | ||
foundTwo = { | ||
abbr: abbrTwo, | ||
measure: measure, | ||
system: system, | ||
unit: unit | ||
}; | ||
} | ||
if (foundOne && foundTwo) { | ||
return false; | ||
@@ -262,16 +244,11 @@ } | ||
if (foundOne && foundTwo) { | ||
if (found) | ||
return false; | ||
} | ||
}); | ||
if (foundOne && foundTwo) { | ||
if (found) | ||
return false; | ||
} else { | ||
foundOne = null; | ||
foundTwo = null; | ||
} | ||
}); | ||
return foundOne && foundTwo ? [foundOne, foundTwo] : null; | ||
return found; | ||
}; | ||
@@ -294,3 +271,3 @@ | ||
Converter.prototype.describe = function(abbr) { | ||
var resp = Converter.prototype.getUnit(abbr); | ||
var resp = Converter.prototype.getUnit(abbr, this.measure); | ||
@@ -344,2 +321,10 @@ return describe(resp); | ||
Converter.prototype.throwUnsupportedMeasureError = function(what) { | ||
throw new Error('Unsupported measure ' + what + ', use one of: ' + this.measures()); | ||
}; | ||
Converter.prototype.throwUnsupportedCompatibilityError = function(unit, measure) { | ||
throw new Error('Unsupported compatibility for unit ' + unit + ' and measure ' + measure); | ||
}; | ||
/** | ||
@@ -385,6 +370,6 @@ * Returns the abbreviated measures that the value can be | ||
convert = function(value) { | ||
return new Converter(value); | ||
convert = function(value, measure = undefined) { | ||
return new Converter(value, measure); | ||
}; | ||
module.exports = convert; |
{ | ||
"name": "corva-convert-units", | ||
"version": "1.15.1", | ||
"version": "1.16.0", | ||
"description": "Convert between quantities in different units", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
84779
53
4212