query.js 878 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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;