Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!