You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
878 B

4 years ago
  1. "use strict";
  2. var hasOwnProperty = Object.prototype.hasOwnProperty;
  3. function parseQuery(urlObj, options)
  4. {
  5. urlObj.query.string.full = stringify(urlObj.query.object, false);
  6. // TWEAK :: condition only for speed optimization
  7. if (options.removeEmptyQueries)
  8. {
  9. urlObj.query.string.stripped = stringify(urlObj.query.object, true);
  10. }
  11. }
  12. function stringify(queryObj, removeEmptyQueries)
  13. {
  14. var count = 0;
  15. var str = "";
  16. for (var i in queryObj)
  17. {
  18. if ( i!=="" && hasOwnProperty.call(queryObj, i)===true )
  19. {
  20. var value = queryObj[i];
  21. if (value !== "" || !removeEmptyQueries)
  22. {
  23. str += (++count===1) ? "?" : "&";
  24. i = encodeURIComponent(i);
  25. if (value !== "")
  26. {
  27. str += i +"="+ encodeURIComponent(value).replace(/%20/g,"+");
  28. }
  29. else
  30. {
  31. str += i;
  32. }
  33. }
  34. }
  35. }
  36. return str;
  37. }
  38. module.exports = parseQuery;