fix question mark stripped from url when no params are given (#776)#777
fix question mark stripped from url when no params are given (#776)#777dzek69 wants to merge 2 commits intonode-fetch:masterfrom dzek69:fix-776
Conversation
| @@ -0,0 +1,12 @@ | |||
| export function getSearch(parsedURL) { | |||
There was a problem hiding this comment.
I'm not sure a helper function is needed for this. It can be written concisely without any function calls or allocations like so:
u.search || u.href[u.href.length - u.hash.length - 1] === '?' ? '?' : ''There was a problem hiding this comment.
I can do that, but I usually avoid "magic looking code" (or just unreadable at first sight) even if I personally like that. Decision is up to node-fetch authors.
There was a problem hiding this comment.
Oh, and your code will fail, it should look for ? with includes
There was a problem hiding this comment.
Hmm... What input will cause it to fail? Using includes will perform a linear search from the beginning of the string. Given that we know the input must have been valid to have been parsed by URL, we should be able to safely check the end of the href, minus the hash.
There was a problem hiding this comment.
It will fail for http://example.com/?# but if you include some hash check too it should be okay to test only the last character I think
There was a problem hiding this comment.
Ah, good spotting 😄 I suppose unit tests would be useful for covering such edge cases. How about this?
function getSearch(u) {
if (u.search) {
return u.search;
}
const lastOffset = u.href.length - 1;
const hash = u.hash || u.href[lastOffset] === '#' ? '#' : '';
return u.href[lastOffset - hash.length] === '?' ? '?' : '';
}There was a problem hiding this comment.
Seems good to me. I have added an unit test for the search string already, so updating my solution with yours should pass the tests
I don't know if just the hash alone causes issues anywhere, as it is not send to the server anyway
There was a problem hiding this comment.
I have pushed an update with your suggestion (and fixed missing parentheses). I have replaced u with parsedURL for readability (build job should minify the code, seems like builds are kind of work in progress so I guess this will come later).
Thank you for your suggestions
|
See #782 |
note:
new URL()will return bothsearchandhashwith their starting character (?or#) but not when these parameters are empty, including when only their starting characters are given in urlhowever
hrefkeeps them both and this fix relies on thathrefWithoutHashis technically incorrect when on that edge case with hash, but that doesn't matter here so I haven't wrote a code that doesn't matter in the final resultFixes #776