updated querystring module
Showing
1 changed file
with
58 additions
and
31 deletions
... | @@ -22,7 +22,6 @@ | ... | @@ -22,7 +22,6 @@ |
22 | // Query String Utilities | 22 | // Query String Utilities |
23 | 23 | ||
24 | var QueryString = exports; | 24 | var QueryString = exports; |
25 | //var urlDecode = process.binding('http_parser').urlDecode; // phantomjs incompatible | ||
26 | 25 | ||
27 | 26 | ||
28 | // If obj.hasOwnProperty has been overridden, then calling | 27 | // If obj.hasOwnProperty has been overridden, then calling |
... | @@ -134,33 +133,31 @@ var stringifyPrimitive = function(v) { | ... | @@ -134,33 +133,31 @@ var stringifyPrimitive = function(v) { |
134 | QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) { | 133 | QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) { |
135 | sep = sep || '&'; | 134 | sep = sep || '&'; |
136 | eq = eq || '='; | 135 | eq = eq || '='; |
137 | obj = (obj === null) ? undefined : obj; | 136 | if (obj === null) { |
138 | 137 | obj = undefined; | |
139 | switch (typeof obj) { | 138 | } |
140 | case 'object': | 139 | |
141 | return Object.keys(obj).map(function(k) { | 140 | if (typeof obj === 'object') { |
142 | if (Array.isArray(obj[k])) { | 141 | return Object.keys(obj).map(function(k) { |
143 | return obj[k].map(function(v) { | 142 | var ks = QueryString.escape(stringifyPrimitive(k)) + eq; |
144 | return QueryString.escape(stringifyPrimitive(k)) + | 143 | if (Array.isArray(obj[k])) { |
145 | eq + | 144 | return obj[k].map(function(v) { |
146 | QueryString.escape(stringifyPrimitive(v)); | 145 | return ks + QueryString.escape(stringifyPrimitive(v)); |
147 | }).join(sep); | 146 | }).join(sep); |
148 | } else { | 147 | } else { |
149 | return QueryString.escape(stringifyPrimitive(k)) + | 148 | return ks + QueryString.escape(stringifyPrimitive(obj[k])); |
150 | eq + | 149 | } |
151 | QueryString.escape(stringifyPrimitive(obj[k])); | 150 | }).join(sep); |
152 | } | ||
153 | }).join(sep); | ||
154 | 151 | ||
155 | default: | ||
156 | if (!name) return ''; | ||
157 | return QueryString.escape(stringifyPrimitive(name)) + eq + | ||
158 | QueryString.escape(stringifyPrimitive(obj)); | ||
159 | } | 152 | } |
153 | |||
154 | if (!name) return ''; | ||
155 | return QueryString.escape(stringifyPrimitive(name)) + eq + | ||
156 | QueryString.escape(stringifyPrimitive(obj)); | ||
160 | }; | 157 | }; |
161 | 158 | ||
162 | // Parse a key=val string. | 159 | // Parse a key=val string. |
163 | QueryString.parse = QueryString.decode = function(qs, sep, eq) { | 160 | QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { |
164 | sep = sep || '&'; | 161 | sep = sep || '&'; |
165 | eq = eq || '='; | 162 | eq = eq || '='; |
166 | var obj = {}; | 163 | var obj = {}; |
... | @@ -169,19 +166,49 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) { | ... | @@ -169,19 +166,49 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) { |
169 | return obj; | 166 | return obj; |
170 | } | 167 | } |
171 | 168 | ||
172 | qs.split(sep).forEach(function(kvp) { | 169 | var regexp = /\+/g; |
173 | var x = kvp.split(eq); | 170 | qs = qs.split(sep); |
174 | var k = QueryString.unescape(x[0], true); | 171 | |
175 | var v = QueryString.unescape(x.slice(1).join(eq), true); | 172 | var maxKeys = 1000; |
173 | if (options && typeof options.maxKeys === 'number') { | ||
174 | maxKeys = options.maxKeys; | ||
175 | } | ||
176 | |||
177 | var len = qs.length; | ||
178 | // maxKeys <= 0 means that we should not limit keys count | ||
179 | if (maxKeys > 0 && len > maxKeys) { | ||
180 | len = maxKeys; | ||
181 | } | ||
182 | |||
183 | for (var i = 0; i < len; ++i) { | ||
184 | var x = qs[i].replace(regexp, '%20'), | ||
185 | idx = x.indexOf(eq), | ||
186 | kstr, vstr, k, v; | ||
187 | |||
188 | if (idx >= 0) { | ||
189 | kstr = x.substr(0, idx); | ||
190 | vstr = x.substr(idx + 1); | ||
191 | } else { | ||
192 | kstr = x; | ||
193 | vstr = ''; | ||
194 | } | ||
195 | |||
196 | try { | ||
197 | k = decodeURIComponent(kstr); | ||
198 | v = decodeURIComponent(vstr); | ||
199 | } catch (e) { | ||
200 | k = QueryString.unescape(kstr, true); | ||
201 | v = QueryString.unescape(vstr, true); | ||
202 | } | ||
176 | 203 | ||
177 | if (!hasOwnProperty(obj, k)) { | 204 | if (!hasOwnProperty(obj, k)) { |
178 | obj[k] = v; | 205 | obj[k] = v; |
179 | } else if (!Array.isArray(obj[k])) { | 206 | } else if (Array.isArray(obj[k])) { |
180 | obj[k] = [obj[k], v]; | ||
181 | } else { | ||
182 | obj[k].push(v); | 207 | obj[k].push(v); |
208 | } else { | ||
209 | obj[k] = [obj[k], v]; | ||
183 | } | 210 | } |
184 | }); | 211 | } |
185 | 212 | ||
186 | return obj; | 213 | return obj; |
187 | }; | 214 | }; | ... | ... |
-
Please register or sign in to post a comment