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

220
Visualizações
Extract domain from URL in Typescript app using new URL() API

I've got a URL containing a lot of params, and I need to extract just the domain name in order to perform a check on that domain name only. The app I'm coding in is a typescript application, it treats URL as a type instead of a string.

Originally I was writing code that was along these lines:

const hostnameExample = "http://www.foo.bar.com?search=www.google.com" 
//example of URL I'm trying to get just a domain of

const cleanupParams2 = (url: string) => {
  let domain = new URL(url);
  return domain.hostname;
}
export const isExternalLink = (hostname?: string, goodDomains: string[] = []) => {
  return goodDomains.every((domain) => {
    if (hostname && !hostname.startsWith("#")) {
      return cleanupParams2(hostname).indexOf(domain) < 0;
    }
  });
};

I'm getting an error message in my unit test scripts, I really don't even understand the error messages I'm getting or why I'm getting them to be honest.

    expect(jest.fn()).not.toHaveBeenCalled()

    Expected number of calls: 0
    Received number of calls: 14

    1: "Invalid URL: www.foo.bar.com·

What would be wrong with the source code that it would be throwing an error, and/or what in the world is that error? It's being thrown from basically any file that appears to directly or indirectly be using the isExternalLink function. I know I'm not showing all of the code here, but this is the problematic code basically.

about 4 years ago · Juan Pablo Isaza
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