Expression Library

Hello All,

If I have an expression library, having content:

{
FindObjectID:x => (
(x == ‘OBJ1’ ? ‘OBJ2’ :
(x == ‘OBJ3’ ? ‘OBJ4’ :
(x == ‘OBJ5’ ? ‘OBJ6’ :
(x == ‘OBJ7’ ? ‘OBJ8’ :
(x == ‘OBJ9’ ? ‘OBJ10’ :
(x == ‘OBJ11’ ? ‘OBJ12’ :
(x == ‘OBJ13’ ? ‘OBJ14’ :
(x == ‘OBJ15’ ? ‘OBJ16’ :
(x == ‘OBJ17’ ? ‘OBJ18’ :
‘UNKNOWN’))))))))))
}

Is there any way to simplify this huge expression?

What are these OBJ1 … OBJ8 etc?

Are you more looking to have dynamically evaluate the number of ternary operations here?

Since your conditions are simple string comparisons, I would suggest using an Object Literal to store the values for each key and then using the get() method to do the lookup:

{
    objs: {
        OBJ1: 'OBJ2',
        OBJ3: 'OBJ4',
        OBJ5: 'OBJ6',
        OBJ7: 'OBJ8',
    },
    FindObjectID: x => this.objs.get(x, 'UNKNOWN')
}

you can also consider using the conditional snap which would make this easier to maintain / understand.