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

205
Vistas
How would a switch statement look like which has to express a complex condition that contains both logical operators (|| and &&)?

I have to build a condition based on one or two values.

const getLabel = (type?: string, typeXy?: string) => {
  let label;

  switch (type || typeXy) {
    case 'Value A':
      label = 'Label A';
      break;
    case 'Value B':
      label = 'Label B';
      break;
    case 'Value C':
    case 'Value X':
      label = 'Label C + X';
      break;
    case 'Value Y':
      label = 'Label C + Y';
      break;
    default:
      break;
  }
  return label;
};

I need a dubbel case on two places. When I add case 'Value C': above case 'Value Y': I get:

Duplicate case label.eslintno-duplicate-case

1) I add // eslint-disable-next-line no-duplicate-case but my code isn't working yet. Or is there a cleaner way anyway?

2) I can change my switch from the or to the and: (type || typeXy) but then the first case isn't working? How do I support both or and and?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

How about using an object literal as lookup table where one can precisely express with, which result for which case one does prefer ?..

const getLabel = (type/*?: string*/, typeXy/*?: string*/) => {
  return ({

    'Value A': 'Label A',
    'Value B': 'Label B',
    'Value C': 'Label C',
    'Value X': 'Label X',
    'Value Y': 'Label Y',
    'Value CValue X': 'Label C + X',
    'Value XValue C': 'Label C + X',
    'Value CValue Y': 'Label C + Y',
    'Value YValue C': 'Label C + Y',

  })[(type ?? '') + (typeXy ?? '')];
};

console.log("getLabel('Value A') ...", getLabel('Value A'));

console.log("getLabel('Value B', null) ...", getLabel('Value B', null));
console.log("getLabel(null, 'Value C') ...", getLabel(null, 'Value C'));
console.log("getLabel(undefined, 'Value X') ...", getLabel(undefined, 'Value X'));

console.log("getLabel('Value Y') ...", getLabel('Value Y'));

console.log("getLabel('Value C', 'Value X') ...", getLabel('Value C', 'Value X'));
console.log("getLabel('Value X', 'Value C') ...", getLabel('Value X', 'Value C'));

console.log("getLabel('Value C', 'Value Y') ...", getLabel('Value C', 'Value Y'));
console.log("getLabel('Value Y', 'Value C') ...", getLabel('Value Y', 'Value C'));
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand your question you want to conditionally check if both arguments are passed and if so, use a logical AND comparison, otherwise if only one or the other is passed revert to a logical OR comparison.

Use an outer switch to branch on the logical AND/OR condition, and then use nested switches to evaluate the specific AND or OR combinations.

const getLabel = (type /*?: string*/, typeXy /*?: string*/) => {
  switch (true) {
    case !!(type && typeXy):
      switch (type) {
        case "Value C": {
          let label = "Label C";
          switch (typeXy) {
            case "Value X":
              return label + " + X";
            case "Value Y":
              return label + " + Y";
          }
        }
      }
      break;

    case !!(type || typeXy):
      switch (type || typeXy) {
        case "Value A":
          return "Label A";
        case "Value B":
          return "Label B";
      }
  }
};

console.log(getLabel("Value A"));            // Label A
console.log(getLabel("Value B"));            // Label B
console.log(getLabel(undefined, "Value A")); // Label A
console.log(getLabel(undefined, "Value B")); // Label B
console.log(getLabel("Value C", "Value X")); // Label C + X
console.log(getLabel("Value C", "Value Y")); // Label C + Y

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