-
-
Save distractdiverge/7ad77a9497f172b74ddc to your computer and use it in GitHub Desktop.
Object to QueryString
This file contains hidden or 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
var _objectToQueryString = function(object) { | |
if(!object) { | |
return ''; | |
} | |
if(_.isEmpty(object)) { | |
return ''; | |
} | |
var queryString = '?'; | |
_.each(errorParameters, function(value, key) { | |
if(_.isString(value)){ | |
value = value.trim(); | |
} | |
if(value) { | |
queryString += key + '=' + encodeURIComponent(value) + '&'; | |
} | |
}); | |
if (queryString[queryString.length - 1] === '&') { | |
queryString = queryString.slice(0, queryString.length - 1); | |
} | |
return queryString; | |
} | |
//var source ="TEST SOURCE"; | |
var source; | |
//var message = "TEST MESSSAGE"; | |
var message = ' '; | |
var statusCode = 1234; | |
var statusCode; | |
var errorParameters = {}; | |
if (source) { | |
errorParameters['source'] = source; | |
} | |
if (message) { | |
errorParameters['message'] = message; | |
} | |
if (typeof(statusCode) === 'number') { | |
errorParameters['status-code'] = statusCode; | |
} | |
var queryString = _objectToQueryString(errorParameters); | |
console.log(queryString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment