Here is the code
converter =
{
'SIToImperial' :
{
'cm' : function(value) {return value * convertRatioImperial[0];},
'm' : function(value) {return value * convertRatioImperial[1];},
'km' : function(value) {return value * convertRatioImperial[2];},
'g' : function(value) {return value * convertRatioImperial[3];},
'kg' : function(value) {return value * convertRatioImperial[4];},
't' : function(value) {return value * convertRatioImperial[5];},
'mL' : function(value) {return value * convertRatioImperial[6];},
'L' : function(value) {return value * convertRatioImperial[7];},
'm3' : function(value) {return value * convertRatioImperial[8];},
'kWh' : function(value) {return value;},
'nb' : function(value) {return value;},
'undefined': function(value) {return 'Not Found';}
}
}
It is clear that my line 'undefined'.... does not work like I want it to.
I would like that when converter["SIToImperial"][units] is called with an 'units' not listed in the properties. Exemple : converter["SIToImperial"]['oz']. It should return the undefined line IE 'Not Found'.
Could someone help me out I have tried various ways but I'm still not enough familiar with dictionnaries to get it working properly.
Update :
//Generalized conversion function
function convert(value,valueUnit,system, toSI)
{
var result;
//From SI to Imp/U
if(!toSI)
{
result = converter.guardian('SITo'+system,valueUnit,value);
}
else if(toSI)//To SI from Imp/US
{
result = converter.guardian(system+'ToSI',valueUnit,value);
}
return result;
};
and
converter =
{
guardian : function (system,units,value) {var label = this[system][units]; if(typeof(label) === 'undefined') {return "Not Found";} else {return label(value);}},
'SIToImperial' :
{
'cm' : function(value) {return value * convertRatioImperial[0];},
'm' : function(value) {return value * convertRatioImperial[1];},
'km' : function(value) {return value * convertRatioImperial[2];},
'g' : function(value) {return value * convertRatioImperial[3];},
'kg' : function(value) {return value * convertRatioImperial[4];},
't' : function(value) {return value * convertRatioImperial[5];},
'mL' : function(value) {return value * convertRatioImperial[6];},
'L' : function(value) {return value * convertRatioImperial[7];},
'm3' : function(value) {return value * convertRatioImperial[8];},
'kWh' : function(value) {return value;},
'nb' : function(value) {return value;}
}
}