-3

I get a string that is dynamic depending on the environment :

const dynamicString = environnement-dev.com

and I would transform this string into an url automatically starting with https: //

I am currently using this method :

 const dynamicString = environnement-dev.com;
 const url = 'https://'+ dynamicString

that does the job, but I would like to know if there is another more conventional method.

Greg-A
  • 772
  • 1
  • 19
  • 41

2 Answers2

3

You could use URL() this will throw an error if the url does not conform to the current format

 const dynamicString = 'environnement-dev.com';
 const url = new URL(`https://${dynamicString}`);

And since you receive an object, with url.href you can get the full url

Kris
  • 135
  • 7
1

const dynamicString = 'environnement-dev.com';
const getUrl = (addres) => `https://${addres}`
console.log(getUrl(dynamicString))
Jacek Rojek
  • 1,082
  • 8
  • 16