From 95168cab0471ecc20e0898cb66a29895b09a2175 Mon Sep 17 00:00:00 2001 From: Christian Lent Date: Sun, 21 Dec 2014 23:49:06 -0500 Subject: [PATCH] Added volume --- measurement.js | 86 +++++++++++++++++++++++++---- test/browserRunner.js | 8 +-- test/unit/mJs.convert.VolumeSpec.js | 39 +++++++++++++ 3 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 test/unit/mJs.convert.VolumeSpec.js diff --git a/measurement.js b/measurement.js index 2c59664..45189e9 100644 --- a/measurement.js +++ b/measurement.js @@ -1,13 +1,13 @@ /** * Measurement.Js - * + * * Unit-of-Measure conversion made easy. - * + * * @author Philipp Austermann * @version 0.1 * @example text measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); or measurementJs.convert(3.5).from(DISTANCE.KMH).to(DISTANCE.M); - * + * * @param {Object} ns * @returns {undefined} */ @@ -50,6 +50,13 @@ MILLIGRAM: 'mg', OUNCE: 'oz', POUND: 'lb' + }, + Volume: { + LITER: 'l', + MILLILITER: 'ml', + CUP: 'c', + TABLESPOON: 'tbsp', + TEASPOON: 'tsp' } }; @@ -177,13 +184,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; } } @@ -261,6 +268,63 @@ en: 'Pounds' } } + }, + Volume: { + 'l': { + key: UNIT.Volume.LITER, + base: null, + factor: 1, + name: { + en: 'Liter', + }, + plural: { + en: 'Liters' + } + }, + 'ml': { + key: UNIT.Volume.MILLILITER, + base: UNIT.Volume.LITER, + factor: .001, + name: { + en: 'Milliliter', + }, + plural: { + en: 'Milliliters' + } + }, + 'c': { + key: UNIT.Volume.CUP, + base: UNIT.Volume.LITER, + factor: 0.236588, + name: { + en: 'Cup', + }, + plural: { + en: 'Cups' + } + }, + 'tbsp': { + key: UNIT.Volume.TABLESPOON, + base: UNIT.Volume.LITER, + factor: 0.0147868, + name: { + en: 'Tablespoon', + }, + plural: { + en: 'Tablespoons' + } + }, + 'tsp': { + key: UNIT.Volume.TEASPOON, + base: UNIT.Volume.LITER, + factor: 0.00492892, + name: { + en: 'Teaspoon', + }, + plural: { + en: 'Teaspoons' + } + } } }; @@ -274,7 +338,7 @@ if (DEFINITIONS[unitType]) { var inputDef = DEFINITIONS[unitType][inputUnit], outputDef = DEFINITIONS[unitType][outputUnit]; - + if (inputDef && outputDef) { if (inputDef.base === outputUnit) { @@ -295,13 +359,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 +373,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 +406,7 @@ function MeasurementJs(UnitType) { var self = this; /** - * + * * @param {type} value * @returns {MeasurementConverter} */ diff --git a/test/browserRunner.js b/test/browserRunner.js index 8f2c4ed..e165188 100644 --- a/test/browserRunner.js +++ b/test/browserRunner.js @@ -28,14 +28,14 @@ require([ 'jasmine-html' ], function() { jasmineEnv.specFilter = function( spec ) { return htmlReporter.specFilter(spec); }; - var specs = [ + var specs = [ 'mJs.ApiSpec', 'mJs.convert.DistanceSpec', 'mJs.convert.DurationSpec', 'mJs.convert.PressureSpec', 'mJs.convert.SpeedSpec', - 'mJs.convert.TemperatureSpec', + 'mJs.convert.TemperatureSpec', 'mJs.convert.VolumeSpec', 'mJs.CustomNamespaceSpec' ]; - + function execJasmine( specs ) { require(specs, function() { jasmineEnv.execute(); @@ -51,7 +51,7 @@ require([ 'jasmine-html' ], function() { console.log('Executing Tests...'); execJasmine(specs); } - + if (document.readyState === "complete") return onLoad(); diff --git a/test/unit/mJs.convert.VolumeSpec.js b/test/unit/mJs.convert.VolumeSpec.js new file mode 100644 index 0000000..0cf6cf9 --- /dev/null +++ b/test/unit/mJs.convert.VolumeSpec.js @@ -0,0 +1,39 @@ +'use strict'; + +define([ 'measurement' ], function( measurement ) { + describe("measurement.Unit.Volume", function() { + expect(measurement.Unit.Volume).toBeDefined(); + + var volumeUnit = measurement.Unit.Volume; + it('is an object', function() { + expect(typeof volumeUnit).toBe('object'); + }); + + it('which contains string keys that we can use as constants', function() { + for (var i in volumeUnit) { + expect(typeof volumeUnit[i]).toBe('string'); + expect(volumeUnit[i].length).toBeGreaterThan(0); + } + }); + + describe("convert():", function() { + + it('1 Volume.CUP equals 16 Volume.TABLESPOON', function() { + expect(measurement('Volume').convert(1).from(volumeUnit.CUP) + .to(volumeUnit.TABLESPOON)).toBeCloseTo(16); + }); + + it('1 Volume.TABLESPOON equals 3 Volume.TEASPOON', function() { + expect(measurement('Volume').convert(1).from(volumeUnit.TABLESPOON) + .to(volumeUnit.TEASPOON)).toBeCloseTo(3); + }); + + it('1 Volume.LITER equals 1000 Volume.MILLILITER', function() { + expect(measurement('Volume').convert(1).from(volumeUnit.LITER) + .to(volumeUnit.MILLILITER)).toBe(1000); + }); + + + }); + }); +});