Re: [whatwg/url] Consider adding a constructor to make a URL from parts (#354)

Hello! Thank you for your hard work.

However, please also consider a classic [builder pattern](https://en.wikipedia.org/wiki/Builder_pattern).

The use case is pretty straightforward: what if you want to create a URL string from individual parts and you don't want to concatenate strings yourself, because `URL` class already does this efficiently and correctly?

Right now, you can't even create an instance of URL class without specifying at least a minimal correct URL string. So in order to build URL progressively from scratch you have to create an instance using some placeholder URL like `new URL('https://example.com')` and then to add/replace it's parts using the provided API. I believe this is a design limitation.

I think it would be practical to allow creation of an empty URL instance and then to allow adding individual parts to it. The error (in case of missing important fields, like protocol) could be thrown when URL instance is actually being cast to string. However, such approach would require us to allow URL instance to represent invalid URL at times and I'm not sure if it's viable.

— [Related question on StackOverflow](https://stackoverflow.com/q/55867415/1056679)

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/354#issuecomment-487049841

Received on Friday, 26 April 2019 13:07:03 UTC