-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is url.format
broken or am I?
#1176
Comments
url.format is legacy, you should use the whatwg URL api |
👍 to what @devsnek said But also, yes, this is terrible and we should probably fix it. Would you be willing to open an issue @ https://github.com/nodejs/node/, for visibility? |
Lets give that a shot...
¡Dios mííío!
I'm being a bit facetious, it is super easy to build a url with string munging or a template string but I'd very much like to use a lib and be confident I'll never see a double slash. I'm on the hunt for a lib that accepts:
and returns:
Maybe I'm asking for too much. |
Yeah I guess it should be: > const url = require('url')
> url.format({hostname: 'dougbeck.me', pathname: '/womp.html'}); // with a slash
'dougbeck.me/womp.html' but I agree that the slash should not be necessary, that would nice. this does seem problematic... these url parsing things can be a pain in the arse in any language. I also agree that it should throw if there is an obvious formatting problem (in the next Node.js major version). |
@beck I think the intention was not to point to that module, but rather to https://nodejs.org/api/url.html#url_the_whatwg_url_api const url = new URL('https://./'); // Dummy HTTPS URL
url.hostname = 'foo';
url.pathname = 'bar';
url.search = new URLSearchParams({a: 'b'});
console.log(url.href); // => 'https://foo/bar?a=b'
// console.log(url); |
The point being, the WHATWG URL API is now implemented in core and is generally available and should be what is used. |
@addaleax the dummy url is ugly but good. Minor edit would be to document via code: const dummyURL = 'https://./';
const url = new URL(dummyURL); I like this approach. Thank you. |
Using Node@9.9 on OSX.
Problem
Am I crazy for thinking this is broken behavior?
Granted, this seems to be terrible in every other language:
Python
Requiring a 6-item tuple is gross.
Ruby
Java
Node's behavior is worse than all of ☝️because it does not throw an error and returns a result that defies all intuition. 😖
The text was updated successfully, but these errors were encountered: