UNPKG

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