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

130
Visualizações
Better way of searching specific keywords in strings in JavaScript

I am working with an array of urls and for each url i wanna find a image corresponding to the site domain. my first attempt was

  const url = new URL(props.url);
  const platform = url.hostname.split(".")[1];
  console.log(platform)

  const platform_logos = {
    "codechef": "images/chef.png",
    "withgoogle": "images/google.png",
    .
    .
    .
    "codeforces": "images/codeforces.png",
  }

  let platform_logo = platform_logos[platform];

but it doesnt work with url of type 'https://momo2022fr.hackerearth.com' so i had to resort to

  let platform_logo = "images/code.png"

  if (url.includes("hackerearth")) {
    platform_logo = "images/hackerearth.png"
  }
  else if (url.includes("hackerrank")) {
    platform_logo = "images/hackerrank.png"
  }
  else if (url.includes("codeforces")) {
    platform_logo = "images/codeforces.png"
  }
  else if (url.includes("codechef")) {
    platform_logo = "images/chef.png"
  }
  else if (url.includes("atcoder")) {
    platform_logo = "images/atcoder.png"
  }
  else if (url.includes("leetcode")) {
    platform_logo = "images/leetcode.png"
  }
  else if (props.url.includes("withgoogle")) {
    platform_logo = "images/google.png"
  }

Is there any better way of writing the code below, it just feels like it violates DRY

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

0

You could just do the same thing as in your first solution and store the mapping from the substring to the image path in an ocject:

const platform_logos = {
  "hackerearth": "images/hackerearth.png",
  "hackerrank": "images/hackerrank.png",
  "codeforces": "images/codeforces.png",
  "codechef": "images/chef.png",
  "atcoder": "images/atcoder.png",
  "leetcode": "images/leetcode.png",
  "withgoogle": "images/google.png"
};

Then you could iterate over the key-value pairs in your object to find the key that is part of the URL and return it once it matches:

function getLogo(url) {
  for(const [key, value] of Object.entries(platform_logos)) {
    if(url.contains(key)) {
      return value;
    }
  }
}

let platform_logo = getLogo(url);
about 4 years ago · Juan Pablo Isaza Relatório

0

You could change how you're reading the url to only get the root domain.

location.hostname.split('.').reverse().splice(0,2).reverse().join('.').split('.')[0]

This code would give hackerearth for https://momo2022fr.hackerearth.com/.

about 4 years ago · Juan Pablo Isaza Relatório

0

So there are several ways of achieving this. These are just two from the top of my head.

Parsing the url and using a switch() to determine the outcome, with fallback if none is found.

const url = new URL("https://www.withgoogle.com/search?q=test");
const sites = [
  "hackerearth",
  "hackerrank",
  "codeforces",
  "codechef",
  "atcoder",
  "leetcode",
  "withgoogle",
];

console.info(url.hostname);

const site = url.hostname.match(new RegExp(`${sites.join("|")}`));

let logo = "";

switch (site[0]) {
  case "hackerearth":
    logo = "images/hackerearth.png";
    break;
  case "hackerrank":
    logo = "images/hackerrank.png";
    break;
  case "codeforces":
    logo = "images/codeforces.png";
    break;
  case "codechef":
    logo = "images/chef.png";
    break;
  case "atcoder":
    logo = "images/atcoder.png";
    break;
  case "leetcode":
    logo = "images/leetcode.png";
    break;
  case "withgoogle":
    logo = "images/google.png";
    break;
  default:
    logo = "images/code.png";
    break;
}

console.info(logo);

Then there is the modern way, with less code and programming the fallback.

// const url = new URL("https://eee.com/test");
const url = new URL("https://www.withgoogle.com/search?q=test");
const sites = {
  hackerearth: "images/hackerearth.png",
  hackerrank: "images/hackerrank.png",
  codeforces: "images/codeforces.png",
  codechef: "images/chef.png",
  atcoder: "images/atcoder.png",
  leetcode: "images/leetcode.png",
  withgoogle: "images/google.png",
  default: "images/code.png",
};

let site = url.hostname.match(new RegExp(`${Object.keys(sites).join("|")}`));

if (site === null) {
  site = "default";
}

console.info(site, sites[site]);

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