-
Notifications
You must be signed in to change notification settings - Fork 0
/
querystring.js
146 lines (127 loc) · 3.54 KB
/
querystring.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* querystring
*
* This module provides utilities for dealing with query strings.
*/
define(function() {
function stringifyPrimitive(v) {
switch (typeof v) {
case 'string':
return v;
case 'boolean':
return v ? 'true' : 'false';
case 'number':
return isFinite(v) ? v : '';
default:
return '';
}
}
return {
/**
* Serialize an object to a query string.
*
* Optionally override the default separator ('&') and assignment ('=')
* characters.
*
* Examples:
*
* querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
* // returns
* 'foo=bar&baz=qux&baz=quux&corge='
*
* querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
* // returns
* 'foo:bar;baz:qux'
*
* @param {Object} obj
* @param {String} sep
* @param {String} eq
* @api public
*/
stringify: function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
obj = undefined;
}
if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
}
}).join(sep);
}
if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
},
/**
* Deserialize a query string to an object.
*
* Optionally override the default separator ('&') and assignment ('=')
* characters.
*
* Options object may contain maxKeys property (equal to 1000 by default),
* it'll be used to limit processed keys. Set it to 0 to remove key count
* limitation.
*
* Examples:
*
* querystring.parse('foo=bar&baz=qux&baz=quux&corge')
* // returns
* { foo: 'bar', baz: ['qux', 'quux'], corge: '' }
*
* @param {String} qs
* @param {String} sep
* @param {String} eq
* @param {Object} options
* @api public
*/
parse: function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var obj = {};
if (typeof qs !== 'string' || qs.length === 0) {
return obj;
}
var regexp = /\+/g;
qs = qs.split(sep);
var maxKeys = 1000;
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}
var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0 && len > maxKeys) {
len = maxKeys;
}
for (var i = 0; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
kstr, vstr, k, v;
if (idx >= 0) {
kstr = x.substr(0, idx);
vstr = x.substr(idx + 1);
} else {
kstr = x;
vstr = '';
}
k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
if (!obj.hasOwnProperty(k)) {
obj[k] = v;
} else if (Array.isArray(obj[k])) {
obj[k].push(v);
} else {
obj[k] = [obj[k], v];
}
}
return obj;
}
};
});