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