-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (47 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict'
var qs = require('qs')
var querystring = require('querystring')
exports.parse = function (str, options) {
options = init(options)
if (options.parse.querystring) {
var o = options.parse.querystring
return querystring.parse(str, o.sep || null, o.eq || null, o.options)
}
else {
return qs.parse(str, options.parse.qs)
}
}
exports.stringify = function (obj, options) {
options = init(options)
if (options.stringify.querystring) {
var o = options.stringify.querystring
return rfc3986(
querystring.stringify(obj, o.sep || null, o.eq || null, o.options))
}
else {
return qs.stringify(obj, options.stringify.qs)
}
}
function rfc3986 (str) {
return str.replace(/[!'()*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
}
function init (options) {
if (!options) {
return {parse: {qs: {}}, stringify: {qs: {}}}
}
if (!options.parse) {
options.parse = {qs: {}}
}
if (!options.stringify) {
options.stringify = {qs: {}}
}
if (!options.parse.qs) {
options.parse.qs = {}
}
if (!options.stringify.qs) {
options.stringify.qs = {}
}
return options
}