index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. var has = Object.prototype.hasOwnProperty
  3. , undef;
  4. /**
  5. * Decode a URI encoded string.
  6. *
  7. * @param {String} input The URI encoded string.
  8. * @returns {String|Null} The decoded string.
  9. * @api private
  10. */
  11. function decode(input) {
  12. try {
  13. return decodeURIComponent(input.replace(/\+/g, ' '));
  14. } catch (e) {
  15. return null;
  16. }
  17. }
  18. /**
  19. * Attempts to encode a given input.
  20. *
  21. * @param {String} input The string that needs to be encoded.
  22. * @returns {String|Null} The encoded string.
  23. * @api private
  24. */
  25. function encode(input) {
  26. try {
  27. return encodeURIComponent(input);
  28. } catch (e) {
  29. return null;
  30. }
  31. }
  32. /**
  33. * Simple query string parser.
  34. *
  35. * @param {String} query The query string that needs to be parsed.
  36. * @returns {Object}
  37. * @api public
  38. */
  39. function querystring(query) {
  40. var parser = /([^=?&]+)=?([^&]*)/g
  41. , result = {}
  42. , part;
  43. while (part = parser.exec(query)) {
  44. var key = decode(part[1])
  45. , value = decode(part[2]);
  46. //
  47. // Prevent overriding of existing properties. This ensures that build-in
  48. // methods like `toString` or __proto__ are not overriden by malicious
  49. // querystrings.
  50. //
  51. // In the case if failed decoding, we want to omit the key/value pairs
  52. // from the result.
  53. //
  54. if (key === null || value === null || key in result) continue;
  55. result[key] = value;
  56. }
  57. return result;
  58. }
  59. /**
  60. * Transform a query string to an object.
  61. *
  62. * @param {Object} obj Object that should be transformed.
  63. * @param {String} prefix Optional prefix.
  64. * @returns {String}
  65. * @api public
  66. */
  67. function querystringify(obj, prefix) {
  68. prefix = prefix || '';
  69. var pairs = []
  70. , value
  71. , key;
  72. //
  73. // Optionally prefix with a '?' if needed
  74. //
  75. if ('string' !== typeof prefix) prefix = '?';
  76. for (key in obj) {
  77. if (has.call(obj, key)) {
  78. value = obj[key];
  79. //
  80. // Edge cases where we actually want to encode the value to an empty
  81. // string instead of the stringified value.
  82. //
  83. if (!value && (value === null || value === undef || isNaN(value))) {
  84. value = '';
  85. }
  86. key = encodeURIComponent(key);
  87. value = encodeURIComponent(value);
  88. //
  89. // If we failed to encode the strings, we should bail out as we don't
  90. // want to add invalid strings to the query.
  91. //
  92. if (key === null || value === null) continue;
  93. pairs.push(key +'='+ value);
  94. }
  95. }
  96. return pairs.length ? prefix + pairs.join('&') : '';
  97. }
  98. //
  99. // Expose the module.
  100. //
  101. exports.stringify = querystringify;
  102. exports.parse = querystring;