Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Optimising conditional check in typescript

I have the following JS code were i am displaying different status based on the response key from API. is there any better approach to optimise this code so that i don’t have to check each case with IF, in case if the number of status increases

if (data.shippingStatus) {
  let shippingStatus = data.shippingStatus.toString();
  if (shippingStatus === "AWAITING_SHIPMENT") {
    shippingStatus = "Awaiting Shipment";
  } else if (shippingStatus === "SHIPPED") {
    shippingStatus = "Shipped";
  } else if (shippingStatus === "DELIVERED") {
    shippingStatus = "Delivered";
  } else if (shippingStatus === "CANCELLED") {
    shippingStatus = "Cancelled";
  }
  resData.push(setData(data.shippingStatus ? shippingStatus : ""));
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try object mapper:

const statusMapper: {[key:string]: string} = {
  AWAITING_SHIPMENT: "Awaiting Shipment",
  SHIPPED: "Shipped",
  DELIVERED: "Delivered",
  CANCELLED: "Cancelled"
};

if (data.shippingStatus) {
  let shippingStatus = data.shippingStatus.toString();
  resData.push(setData(data.shippingStatus ? statusMapper[shippingStatus] : ""));
}

EDIT: Added type to the mapper

about 4 years ago · Juan Pablo Isaza Relatório

0

There are different approaches to your question. If you're just looking for a solid solution for the current problem aka mapping values, you can either create an object mapper as the other answers suggest or just a simple function that formats your string e.g.:

var text = "AWAITING_SHIPMENT";
text = text.toLowerCase()
    .split('_')
    .map((s) => s.charAt(0).toUpperCase() + s.substring(1))
    .join(' ');
console.log(text);

But if you're looking into the subject in a broader sense, you can use dynamic dispatch via polymorphism. This is an example that uses polymorphism to change behavior based on a type.

about 4 years ago · Juan Pablo Isaza Relatório

0

How my implementation works, it splits the status by _ and capitalize it, and finally return a new status, as required

// Dummy Data

const data = {
  shippingStatus:"AWAITING_SHIPMENT"
}


// New Proposal

if(data.shippingStatus){
  const { shippingStatus } = data;
  
  const status = 
  shippingStatus.split('_') 
  .map(string => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase())
  .join(" ");
  
  console.log(status)
  
  // no need to check for data.shippingStatus twice
  // while you're still within if condition, just do the following :
  // resData.push(setDate(status))
  
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda