diff --git a/measurement.js b/measurement.js index 2c59664..e126b6b 100644 --- a/measurement.js +++ b/measurement.js @@ -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; } } @@ -274,7 +274,7 @@ if (DEFINITIONS[unitType]) { var inputDef = DEFINITIONS[unitType][inputUnit], outputDef = DEFINITIONS[unitType][outputUnit]; - + if (inputDef && outputDef) { if (inputDef.base === outputUnit) { @@ -295,13 +295,13 @@ /** * 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; @@ -309,11 +309,11 @@ 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); } } @@ -342,7 +342,7 @@ function MeasurementJs(UnitType) { var self = this; /** - * + * * @param {type} value * @returns {MeasurementConverter} */ @@ -350,6 +350,10 @@ 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; } diff --git a/test/unit/mJs.ApiSpec.js b/test/unit/mJs.ApiSpec.js index 96b6bf8..b86ee37 100644 --- a/test/unit/mJs.ApiSpec.js +++ b/test/unit/mJs.ApiSpec.js @@ -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,