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

234
Vistas
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 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