querystring.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict'
  2. var qs = require('qs')
  3. var querystring = require('querystring')
  4. function Querystring (request) {
  5. this.request = request
  6. this.lib = null
  7. this.useQuerystring = null
  8. this.parseOptions = null
  9. this.stringifyOptions = null
  10. }
  11. Querystring.prototype.init = function (options) {
  12. if (this.lib) { return }
  13. this.useQuerystring = options.useQuerystring
  14. this.lib = (this.useQuerystring ? querystring : qs)
  15. this.parseOptions = options.qsParseOptions || {}
  16. this.stringifyOptions = options.qsStringifyOptions || {}
  17. }
  18. Querystring.prototype.stringify = function (obj) {
  19. return (this.useQuerystring)
  20. ? this.rfc3986(this.lib.stringify(obj,
  21. this.stringifyOptions.sep || null,
  22. this.stringifyOptions.eq || null,
  23. this.stringifyOptions))
  24. : this.lib.stringify(obj, this.stringifyOptions)
  25. }
  26. Querystring.prototype.parse = function (str) {
  27. return (this.useQuerystring)
  28. ? this.lib.parse(str,
  29. this.parseOptions.sep || null,
  30. this.parseOptions.eq || null,
  31. this.parseOptions)
  32. : this.lib.parse(str, this.parseOptions)
  33. }
  34. Querystring.prototype.rfc3986 = function (str) {
  35. return str.replace(/[!'()*]/g, function (c) {
  36. return '%' + c.charCodeAt(0).toString(16).toUpperCase()
  37. })
  38. }
  39. Querystring.prototype.unescape = querystring.unescape
  40. exports.Querystring = Querystring