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

144
Visualizações
Is there a way for my manual special case if statement look more pretty and cleaner?

I have a website where some of the Titles have been translated to my country's language.

Because of that, when I match with worldwide data API, it'll not match (just some cases) due to API title is using in English name, so I tried the if statement to translate some "specific case" back into English, but it looks really long and doesn't feel organized well, I think I should put it into 1 other .JS file and just add more special case to that file only for the scalable purpose. But how can I do that and make sure to classify all the cases correctly?

Pardon me if this is an already answered question somewhere, but I don't know the keyword of it.

This is an example of my code:

static async titleInfo() {

//- Some codes here
//- ...

//- Make URL slug becomes a Title
let slugMod = animeSlug.replace(/-/g, " ").toString()

// Special case that doesn't correct due to country language, have to do manually.
        if (slugMod == "vua hai tac") {
            slugMod = "One Piece"
        }
        if (slugMod == "su mang than chet") {
            slugMod = "Bleach"
        }
        if (slugMod == "nanatsu no taizai that dai toi") {
            slugMod = "Nanatsu no Taizai"
        }
        if (slugMod == "hoi phap su") {
            slugMod = "Fairy Tail"
        }
        if (slugMod == "vua phap thuat 2021") {
            slugMod = "SHAMAN KING (2021)"
        }
        if (slugMod == "hunter x hunter") {
            slugMod = "HUNTER×HUNTER (2011)"
        }
        //
//- Ending code

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could try something similar to Jump table , It can be implemented like this

const handleSpecialCase = 
{
    'vua hai tac'                    : 'One Piece',
    'su mang than chet'              : 'Bleach',
    'nanatsu no taizai that dai toi' : 'Nanatsu no Taizai'
}

handleSpecialCase[specialCase]();

You could also store this in an another file and then import it, will make your code cleaner! Hope it helps

about 4 years ago · Juan Pablo Isaza Relatório

0

An object indexed by search string whose values are the string to replace it with would work.

const titles = {
    'vua hai tac': 'One Piece',
    'su mang than chet': 'Bleach',
    'nanatsu no taizai that dai toi': 'Nanatsu no Taizai',
    // etc
};
const slugWithSpaces = animeSlug.replace(/-/g, " ");
const correctedTitle = titles[slugWithSpaces] || slugWithSpaces;

If you have a lot of these, yes, consider a separate file.

If you have a lot and you see the need to add even more exceptions quite frequently, and manually editing the source code seems too error-prone, you could make yourself a little app (like with Electron) that would let you copy-paste titles into a page, which then updates the data that your script uses with a button.

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