parseqs.js 966 B

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