Last active
June 27, 2022 20:37
-
-
Save organom/88ee2dbf9c7070cab732c35d6597c254 to your computer and use it in GitHub Desktop.
util code for nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* adds an object as query to the given url | |
* @param url | |
* @param {object} query | |
* @returns {string} url with the extra query parameters | |
*/ | |
function addQueryToUrl(url, query) { | |
if(!url) return ''; | |
if(!query) return url; | |
const parsedUrl = require('url').parse(url, true); | |
Object.assign(parsedUrl.query, query); | |
// needed in order for the URL to parse the query field instead of the search field | |
// Documentation => on url.format: query (object; see querystring) will only be used if search is absent. | |
delete parsedUrl.search; | |
return parsedUrl.format(); | |
} | |
module.exports = { | |
addQueryToUrl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment