index.js (745B)
1 /** 2 * Compiles a querystring 3 * Returns string representation of the object 4 * 5 * @param {Object} 6 * @api private 7 */ 8 9 exports.encode = function (obj) { 10 var str = ''; 11 12 for (var i in obj) { 13 if (obj.hasOwnProperty(i)) { 14 if (str.length) str += '&'; 15 str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]); 16 } 17 } 18 19 return str; 20 }; 21 22 /** 23 * Parses a simple querystring into an object 24 * 25 * @param {String} qs 26 * @api private 27 */ 28 29 exports.decode = function(qs){ 30 var qry = {}; 31 var pairs = qs.split('&'); 32 for (var i = 0, l = pairs.length; i < l; i++) { 33 var pair = pairs[i].split('='); 34 qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); 35 } 36 return qry; 37 };