Tengo una función llamada 'func':
const otherFunc = (arg1, arg2) => {...} const func = (condition1, condition2) => { condition1 || condition2 ? otherFunc(value, true) : otherFunc(false) }La forma anterior funciona, pero me pregunto si hay alguna forma de evitar el uso de dos llamadas diferentes a otherFunc. Intenté esto pero no es la sintaxis correcta:
const func = (condition1, condition2) => { otherFunc((condition1 || condition2) && ...[value, true]) }Editar: es una función simple para encadenar if y elses:
function is(value, done) { return { else: (v) => is(done ? value : v, done), if: (condition) => (done || condition ? is(value, true) : is(null)), get: value, }; } const n = 50000; console.log( is('small') .if(n < 1000) .else('big') .if(n > 10000) .else('medium-big') .if(n > 5000) .else('medium-small').get, );Si puede colocar los argumentos en una matriz, puede alternar entre las matrices para distribuirlos en la lista de argumentos.
otherFunc( ...((condition1 || condition2) ? [value, true] : [false]) );Dicho esto, no es muy legible. realmente preferiría
if (condition1 || condition2) { otherFunc(value, true); } else { otherFunc(false); }Un buen código mantenible no es una competencia de golf: estar lo más SECO posible no siempre es el mejor enfoque.