parseqs.js 811 B

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