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

81
Views
Nombre de host a URL válida en javascript

¿Existe una forma elegante de convertir un nombre de host/dominio en una URL http válida de esta manera:
example.com a https://example.com ?

Mi enfoque actual es algo así como:

 const domainToURL = (domain: string) => {
 if (!domain.startsWith('http')) domain= `https://${domain}`;
 return new URL('/', domain).origin;
};

Estoy seguro de que hay algo más robusto por ahí.

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esto es lo que uso: crear un objeto URL ficticio, cambiar la propiedad .hostname y luego leer la propiedad .href . Como esto:

 function hostnameToURL(hostname) {
 // the inital value of the URL object can be anything
 const url = new URL("https://example.com");
 url.hostname = hostname;
 return url.href;
}

console.log(hostnameToURL("google.com"));
console.log(hostnameToURL("a.fun.website.com"));
console.log(hostnameToURL("example.com"));

Si necesita tomar más parámetros (como especificar HTTP o HTTPS), puede modificar fácilmente la función para hacerlo. Encuentro que este enfoque es mejor porque le entregas todo el trabajo al navegador.

almost 4 years ago · Santiago Trujillo Report
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!