Skip to content

Instantly share code, notes, and snippets.

@jonathan-fulton
Last active May 14, 2018 00:59
Show Gist options
  • Save jonathan-fulton/53b03db644353af05279f8fe3eea1a09 to your computer and use it in GitHub Desktop.
Save jonathan-fulton/53b03db644353af05279f8fe3eea1a09 to your computer and use it in GitHub Desktop.
build-url: the bad way
function buildUrl(url, options) {
var queryString = [];
var key;
var builtUrl;
if (url === null) {
builtUrl = '';
} else if (typeof(url) === 'object') {
builtUrl = '';
options = url;
} else {
builtUrl = url;
}
if (options) {
if (options.path) {
builtUrl += '/' + options.path;
}
if (options.queryParams) {
for (key in options.queryParams) {
if (options.queryParams.hasOwnProperty(key)) {
queryString.push(key + '=' + options.queryParams[key]);
}
}
builtUrl += '?' + queryString.join('&');
}
if (options.hash) {
builtUrl += '#' + options.hash;
}
}
return builtUrl;
};
@jonathan-fulton
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment