Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Function calls with the arguments amount based on condition

I have a function called 'func':

const otherFunc = (arg1, arg2) => {...}
const func = (condition1, condition2) => {
 condition1 || condition2 ? otherFunc(value, true) : otherFunc(false)
}

The previous way works, but i'm wondering if is there is a way to avoid using two different calls to otherFunc. I tried this but is not correct syntax:

const func = (condition1, condition2) => {
 otherFunc((condition1 || condition2) && ...[value, true])
}

Edit: It is a simple function to chain if and 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,
);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you can put the arguments into an array, you can then alternate between the arrays to spread into the argument list.

otherFunc(
  ...((condition1 || condition2) ? [value, true] : [false])
);

That said, it isn't very readable. I'd really prefer

if (condition1 || condition2) {
  otherFunc(value, true);
} else {
  otherFunc(false);
}

Good maintainable code isn't a golfing competition - being as DRY as absolutely possible isn't always the best approach.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda