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

Added volume #9

Open
wants to merge 1 commit into
base: 0-1-stable
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
86 changes: 75 additions & 11 deletions measurement.js
Original file line number Diff line number Diff line change
@@ -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}
*/
Expand Down Expand Up @@ -50,6 +50,13 @@
MILLIGRAM: 'mg',
OUNCE: 'oz',
POUND: 'lb'
},
Volume: {
LITER: 'l',
MILLILITER: 'ml',
CUP: 'c',
TABLESPOON: 'tbsp',
TEASPOON: 'tsp'
}
};

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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'
}
}
}
};

Expand All @@ -274,7 +338,7 @@
if (DEFINITIONS[unitType]) {
var inputDef = DEFINITIONS[unitType][inputUnit],
outputDef = DEFINITIONS[unitType][outputUnit];

if (inputDef && outputDef) {

if (inputDef.base === outputUnit) {
Expand All @@ -295,25 +359,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,7 +406,7 @@
function MeasurementJs(UnitType) {
var self = this;
/**
*
*
* @param {type} value
* @returns {MeasurementConverter}
*/
Expand Down
8 changes: 4 additions & 4 deletions test/browserRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -51,7 +51,7 @@ require([ 'jasmine-html' ], function() {
console.log('Executing Tests...');
execJasmine(specs);
}

if (document.readyState === "complete")
return onLoad();

Expand Down
39 changes: 39 additions & 0 deletions test/unit/mJs.convert.VolumeSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});


});
});
});