Skip to content

Commit

Permalink
Merge pull request #12 from Thorsten71/master
Browse files Browse the repository at this point in the history
Add unit family 'Power'
  • Loading branch information
danielwippermann committed Apr 8, 2016
2 parents 474e52c + 1260ee6 commit df2e865
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/specification-data.js
Expand Up @@ -178,7 +178,7 @@ var createVBusSpecificationData = function() {
Kilowatts: {
unitId: 'Kilowatts',
unitCode: 'Kilowatts',
unitFamily: null,
unitFamily: 'Power',
unitText: ' kW',
},

Expand Down Expand Up @@ -325,7 +325,7 @@ var createVBusSpecificationData = function() {
Watts: {
unitId: 'Watts',
unitCode: 'Watts',
unitFamily: null,
unitFamily: 'Power',
unitText: ' W',
},

Expand Down
28 changes: 28 additions & 0 deletions src/specification.js
Expand Up @@ -747,6 +747,8 @@ var Specification = extend(null, /** @lends Specification# */ {
rawValue = that._convertPressureRawValue(rawValue, sourceUnit.unitCode, targetUnit.unitCode);
} else if (unitFamily === 'Energy') {
rawValue = that._convertEnergyRawValue(rawValue, sourceUnit.unitCode, targetUnit.unitCode);
} else if (unitFamily === 'Power') {
rawValue = that._convertPowerRawValue(rawValue, sourceUnit.unitCode, targetUnit.unitCode);
} else if (unitFamily === 'Time') {
rawValue = that._convertTimeRawValue(rawValue, sourceUnit.unitCode, targetUnit.unitCode);
} else {
Expand Down Expand Up @@ -979,6 +981,32 @@ var Specification = extend(null, /** @lends Specification# */ {
return rawValue;
},

_convertPowerRawValue: function(rawValue, sourceUnitCode, targetUnitCode) {
switch (sourceUnitCode) {
case 'Watts':
// nop
break;
case 'Kilowatts':
rawValue = rawValue * 1000;
break;
default:
throw new Error('Unsupported source unit ' + JSON.stringify(sourceUnitCode));
}

switch (targetUnitCode) {
case 'Watts':
// nop
break;
case 'Kilowatts':
rawValue = rawValue / 1000;
break;
default:
throw new Error('Unsupported target unit ' + JSON.stringify(targetUnitCode));
}

return rawValue;
},

_convertTimeRawValue: function(rawValue, sourceUnitCode, targetUnitCode) {
switch (sourceUnitCode) {
case 'Seconds':
Expand Down
14 changes: 14 additions & 0 deletions test/specs/specification.spec.js
Expand Up @@ -1206,6 +1206,20 @@ describe('Specification', function() {

});

describe('Unit Family "Power"', function() {

it('should convert from "Kilowatts" to "Watts"', function() {
expectConversion(0, 'Kilowatts', 'Watts').closeTo(0, delta);
expectConversion(1000000, 'Kilowatts', 'Watts').closeTo(1000000000, delta);
});

it('should convert from "Watts" to "Kilowatts"', function() {
expectConversion(0, 'Watts', 'Kilowatts').closeTo(0, delta);
expectConversion(1000000, 'Watts', 'Kilowatts').closeTo(1000, delta);
});

});

describe('Unit Family "Pressure"', function() {

it('should convert from "Bars" to "PoundsForcePerSquareInch"', function() {
Expand Down
1 change: 1 addition & 0 deletions tools/specification-importer/utils.js
Expand Up @@ -10,6 +10,7 @@ var _ = require('lodash');
var unitCodesByUnitFamily = {
'Energy': [ 'WattHours', 'KilowattHours', 'MegawattHours', 'Btus', 'KiloBtus', 'MegaBtus', 'GramsCO2Gas', 'KilogramsCO2Gas', 'TonsCO2Gas', 'GramsCO2Oil', 'KilogramsCO2Oil', 'TonsCO2Oil' ],
'Pressure': [ 'Bars', 'PoundsForcePerSquareInch' ],
'Power': [ 'Watts', 'Kilowatts' ],
'Temperature': [ 'DegreesCelsius', 'DegreesFahrenheit' ],
'Time': [ 'Seconds', 'Minutes', 'Hours', 'Days' ],
'Volume': [ 'Liters', 'CubicMeters', 'Gallons' ],
Expand Down

0 comments on commit df2e865

Please sign in to comment.