Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined and null handling fixes #1 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions measurement.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@
/**
* Really strange rounding error:
* (100 - 273.15) gives -173.14999999999998 (tested in Chrome 26.0.1410.63)
*
*
* Following workarounds:
*/
if (reverse) {
return parseFloat((value + 273 + 0.15).toFixed(10));
}

return (value - 273) - 0.15;
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@
if (DEFINITIONS[unitType]) {
var inputDef = DEFINITIONS[unitType][inputUnit],
outputDef = DEFINITIONS[unitType][outputUnit];

if (inputDef && outputDef) {

if (inputDef.base === outputUnit) {
Expand All @@ -295,25 +295,25 @@

/**
* TODO use direct reconversion factors, while trading off the higher accuracy / performance
* vs. larger configuration array/file size
* vs. larger configuration array/file size
*/
var baseType = inputDef.base || outputDef.base, baseValue;
if (typeof baseType === 'undefined') {
return false;
}

if (baseType === inputDef.base) {
baseValue = mjs(unitType).convert(value).from(inputDef.key).to(inputDef.base);
inputUnit = inputDef.base;
} else if (baseType === outputDef.base) {
baseValue = mjs(unitType).convert(value).from(outputDef.key).to(outputDef.base);
inputUnit = outputDef.base;
}

if (baseType === UNIT.Temperature.CELSIUS) {
return parseFloat(self.convert(baseValue).toFixed(10));
}

return self.convert(baseValue);
}
}
Expand Down Expand Up @@ -342,14 +342,18 @@
function MeasurementJs(UnitType) {
var self = this;
/**
*
*
* @param {type} value
* @returns {MeasurementConverter}
*/
var convert = function(value) {
var valueToConvert = value,
converter = new MeasurementConverter(UnitType);

if(typeof(value) == 'undefined' || value == null) {
throw new Error('convert() argument is null or undefined');
}

function readyToConvert() {
return converter.inputUnit !== null && converter.outputUnit !== null;
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mJs.ApiSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ define([ 'measurement' ], function( measurement ) {
expect(typeof measurement().convert).toBe('function');
});

describe("measurement().convert(null/undefined)", function() {
it("throws an error", function() {
var ex = new Error("convert() argument is null or undefined");
expect(function() { measurement().convert(null) }).toThrow(ex);
expect(function() { measurement().convert(undefined) }).toThrow(ex);
})
});

describe("measurement().convert(someValue)", function() {
var testUnitType = 'Distance',
testValue = 42,
Expand Down