Lookup Maps in transforms

#1

I have to map units coming from files send by customer to c3 units. currently I use a custom transform like this :slight_smile:

function findUnits(xxxUnit){
  var unit,index;
  var YYYY =['kVArh','kg','h','kWh','l','°C','%','m/s2','kg/h','A','V','s','mbar','bar','kW','Hz','mdeg','cm','rpm','ltr/h','kV','kVAr','cSt','l/min','g/kg','m³','MW','MVAr','mm','g/kWh','hPa','MJ/kg'];
  var c3units  =['kvarh','kilogram','hour','kilowatt_hour','litre','degrees_celcius','percent','kg-per-m2','kg_per_hr','ampere','volt','second','bar','bar','kilowatt','hertz','dimensionless','centimeter','rpm','l_per_hr','kilovolt','kvar','dimensionless','litre-per-min','g-per-kg','dimensionless','megawatt','dimensionless','milimeter','g_per_kwh','hectopascal','MJ_per_kg'];    
  index = YYYY.indexOf(xxxUnit);
  return index > 0 ? c3units[index] : 'dimensionless';
} 

I am wondering if a) there is a better way of doing this and b) if this can be done in standard transform by declaring an enum type before

0 Likes

#2

The ideal solution would be to use enum types to encompass this mapping and use them in a standard transform. Im not sure if this is a feature that currnetly works.

0 Likes

#3

This should be a default expression engine function to find unit. Can you add a ticket for the team ?

I don’t think declaring an enum type is the right approach here since Units are more like seed data and it would be nice to have the flexibility to add / edit units without redeploying the whole application.

Instead of having the huge string up there, all you can do is

function findUnits(xxxUnit){
  var unit = Unit.get(xxxUnit);
  return unit != null ? unit : Unit.make({id:"dimensionless"});
}
0 Likes

#4

Do you mean I would their symbols to our Unit type and then look it up there ? Issue is that their symbols are different then our symbols, hence the lookup.

0 Likes