queryStringify.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // 出处:https://github.com/unshiftio/querystringify
  2. 'use strict';
  3. var has = Object.prototype.hasOwnProperty,
  4. undef;
  5. /**
  6. * Decode a URI encoded string.
  7. *
  8. * @param {String} input The URI encoded string.
  9. * @returns {String|Null} The decoded string.
  10. * @api private
  11. */
  12. function decode(input) {
  13. try {
  14. return decodeURIComponent(input.replace(/\+/g, ' '));
  15. } catch (e) {
  16. return null;
  17. }
  18. }
  19. /**
  20. * Attempts to encode a given input.
  21. *
  22. * @param {String} input The string that needs to be encoded.
  23. * @returns {String|Null} The encoded string.
  24. * @api private
  25. */
  26. function encode(input) {
  27. try {
  28. return encodeURIComponent(input);
  29. } catch (e) {
  30. return null;
  31. }
  32. }
  33. /**
  34. * Simple query string parser.
  35. *
  36. * @param {String} query The query string that needs to be parsed.
  37. * @returns {Object}
  38. * @api public
  39. */
  40. function querystring(query) {
  41. var parser = /([^=?#&]+)=?([^&]*)/g,
  42. result = {},
  43. part;
  44. while (part = parser.exec(query)) {
  45. var key = decode(part[1]),
  46. value = decode(part[2]);
  47. //
  48. // Prevent overriding of existing properties. This ensures that build-in
  49. // methods like `toString` or __proto__ are not overriden by malicious
  50. // querystrings.
  51. //
  52. // In the case if failed decoding, we want to omit the key/value pairs
  53. // from the result.
  54. //
  55. if (key === null || value === null || key in result) continue;
  56. result[key] = value;
  57. }
  58. return result;
  59. }
  60. /**
  61. * Transform a query string to an object.
  62. *
  63. * @param {Object} obj Object that should be transformed.
  64. * @param {String} prefix Optional prefix.
  65. * @returns {String}
  66. * @api public
  67. */
  68. function querystringify(obj, prefix) {
  69. prefix = prefix || '';
  70. var pairs = [],
  71. value, 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 = encode(key);
  87. value = encode(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;
  103. export default { stringify: querystringify, parse: querystring }