Created
June 2, 2018 19:38
-
-
Save TravisMullen/9b771986c782dc153cf3aad6842742d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var service = {}, | |
// unit model | |
// 'singular': 'USD 1 Millon', | |
// 'plural': 'USD 1 Millon', | |
// 'abbr': '$1M', | |
// // also can be true and template would assume to be `abbr` | |
// // (for sbackwards compat) | |
// 'prefix': '$', | |
// 'postfix': 'M' | |
// LABEL for mapping | |
unitTypes = { | |
'electricDemand': [{ | |
'singular' : 'Kilowatt', | |
'plural' : 'Kilowatts', | |
'abbr' : 'kW', | |
'LABEL': 'KILOWATTS' | |
}, { | |
'singular': 'Megawatt', | |
'plural': 'Megawatts', | |
'abbr': 'MW', | |
'LABEL': 'MEGAWATTS' | |
}, { | |
'singular': 'Gigawatt', | |
'plural': 'Gigawatts', | |
'abbr': 'GW', | |
'LABEL': 'GIGAWATTS' | |
}, { | |
'singular': 'Terawatt', | |
'plural': 'Terawatts', | |
'abbr': 'TW', | |
'LABEL': 'TERAWATTS' | |
}, | |
{ | |
'singular' : 'Petawatt', | |
'plural' : 'Petawatts', | |
'abbr' : 'PW', | |
'LABEL': 'PETAWATT' | |
}], | |
'steamDemand': [{ | |
'singular': 'Pounds per hour', | |
'plural': 'Pounds per hour', | |
'abbr': 'lbs/hr', | |
'LABEL': 'POUNDS_PER_HOUR' | |
}], | |
'usage': [{ | |
'singular': 'Kilowatt hour', | |
'plural': 'Kilowatt hours', | |
'abbr': 'kWh', | |
'LABEL': 'KILOWATT_HOURS' | |
}, { | |
'singular': 'Megawatts hour', | |
'plural': 'Megawatt hours', | |
'abbr': 'MWh', | |
'LABEL': 'MEGAWATT_HOURS' | |
}, { | |
'singular': 'Gigawatt hour', | |
'plural': 'Gigawatt hours', | |
'abbr': 'GWh', | |
'LABEL': 'GIGAWATT_HOURS' | |
}, { | |
'singular' : 'Terawatt hour', | |
'plural' : 'Terawatt hours', | |
'abbr' : 'TWh', | |
'LABEL': 'TERAWATT_HOURS' | |
}, | |
{ | |
'singular' : 'Petawatt hour', | |
'plural' : 'Petawatt hours', | |
'abbr' : 'PWh', | |
'LABEL': 'PETAWATT_HOURS' | |
}], | |
'price': [{ | |
'singular': 'Dollar per MWh', | |
'plural': 'Dollars per MWh', | |
'abbr': '$/MWh', | |
'prefix': true, | |
'LABEL': 'DOLLAR_PER_MWH' | |
}, { | |
'singular': 'Dollar per GWh', | |
'plural': 'Dollars per GWh', | |
'abbr': '$/GWh', | |
'prefix': true, | |
'LABEL': 'DOLLAR_PER_GWH' | |
}, { | |
'singular': 'Dollar per TWh', | |
'plural': 'Dollars per TWh', | |
'abbr': '$/TWh', | |
'prefix': true, | |
'LABEL': 'DOLLAR_PER_TWH' | |
}], | |
'cost': [{ | |
'singular': 'USD', | |
'plural': 'USD', | |
'abbr': '$', | |
'prefix': true, | |
'LABEL': 'USD' | |
}, | |
null, | |
{ | |
'singular': 'USD 1 Millon', | |
'plural': 'USD 1 Millon', | |
'abbr': '$1M', | |
'prefix': '$', | |
'postfix': 'M', | |
'LABEL': 'USD' | |
}] | |
}, | |
labels = { | |
legend: { | |
'demand': 'Load in', | |
'usage': 'Load in', | |
'price': 'LMP in', | |
'cost': 'Cost in' | |
} | |
}; | |
// cause electric is default | |
unitTypes.demand = unitTypes.electricDemand; | |
// use param.type to get units | |
function getUnitsByType( type, all ){ | |
// todo: if already augmented then pass in returned type? | |
return (all) ? unitTypes[type] : unitTypes[type][0]; | |
} | |
function getLabelsByType( labelType, type ){ | |
// todo: if already augmented then pass in returned type? | |
return labels[labelType][type]; | |
} | |
// use param.type to get units, | |
// check again value to augment units | |
// for better display | |
function factory( baseValue, type, forcePower ){ | |
var negative = 1, | |
power, | |
divisor, | |
unitType; | |
if ( angular.isString(baseValue) ){ | |
baseValue = parseFloat(baseValue); | |
} | |
negative = Math.sign( baseValue ); | |
if (negative === -1) { | |
// make it a positive for now | |
baseValue = Math.abs( baseValue ); | |
} | |
if (forcePower !== undefined) { | |
power = forcePower; | |
} else { | |
power = Math.floor( Math.log10(baseValue) ); | |
} | |
if (type && unitTypes[type]) { | |
if (power >= 12 && unitTypes[type][4]) { | |
// peta | |
unitType = unitTypes[type][4]; | |
divisor = Math.pow(10,12); | |
} else if (power >= 9 && unitTypes[type][3]) { | |
// tera | |
unitType = unitTypes[type][3]; | |
divisor = Math.pow(10,9); | |
} else if (power >= 6 && unitTypes[type][2]) { | |
// giga | |
unitType = unitTypes[type][2]; | |
divisor = Math.pow(10,6); | |
} else if (power >= 3 && unitTypes[type][1]) { | |
// mega | |
unitType = unitTypes[type][1]; | |
divisor = Math.pow(10,3); | |
} else { | |
// kilo | |
unitType = unitTypes[type][0] || false; | |
divisor = 1; | |
} | |
} | |
var t = { | |
units: unitType, | |
divisor: divisor, | |
negative: negative, | |
power: power, | |
eval: function( value ){ | |
var v = value / divisor; | |
if ( v === 0 && type !== 'cost' ) { // 0.00 === 0 | |
return v.toFixed( 0 ); | |
}else{ | |
return v.toFixed( 2 ); | |
} | |
} | |
}; | |
return t; | |
} | |
function translateValue( value, type, forcePower ){ | |
var t = factory( value, type, forcePower ); | |
t.display = t.eval( value ); | |
return t; | |
} | |
service.factory = factory; | |
service.labels = labels; | |
service.getUnitsByType = getUnitsByType; | |
service.translateValue = translateValue; | |
service.getLabelsByType = getLabelsByType; | |
return service; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment