GeezerVison is showing github.com — querystring: custom encode and decode · lightsofapollo/node@4729202
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/querystring.markdown +21-1Lines changed: 21 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | This module provides utilities for dealing with query strings. | 8 | 8 | It provides the following methods: | 9 | 9 |
| 10 | | -## querystring.stringify(obj, [sep], [eq]) | | 10 | +## querystring.stringify(obj, [sep], [eq], [options]) | 11 | 11 |
| 12 | 12 | Serialize an object to a query string. | 13 | 13 | Optionally override the default separator (`'&'`) and assignment (`'='`) | 14 | 14 | characters. | 15 | 15 |
| | 16 | +Options object may contain `encodeURIComponent` property (`querystring.escape` by default), | | 17 | +it can be used to encode string with `non-utf8` encoding if necessary. | | 18 | + | 16 | 19 | Example: | 17 | 20 |
| 18 | 21 | querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' }) | @@ -23,6 +26,13 @@ Example: | 23 | 26 | // returns | 24 | 27 | 'foo:bar;baz:qux' | 25 | 28 |
| | 29 | + // Suppose gbkEncodeURIComponent function already exists, | | 30 | + // it can encode string with `gbk` encoding | | 31 | + querystring.stringify({ w: '中文', foo: 'bar' }, null, null, | | 32 | + { encodeURIComponent: gbkEncodeURIComponent }) | | 33 | + // returns | | 34 | + 'w=%D6%D0%CE%C4&foo=bar' | | 35 | + | 26 | 36 | ## querystring.parse(str, [sep], [eq], [options]) | 27 | 37 |
| 28 | 38 | Deserialize a query string to an object. | @@ -32,12 +42,22 @@ characters. | 32 | 42 | Options object may contain `maxKeys` property (equal to 1000 by default), it'll | 33 | 43 | be used to limit processed keys. Set it to 0 to remove key count limitation. | 34 | 44 |
| | 45 | +Options object may contain `decodeURIComponent` property (`decodeURIComponent` by default), | | 46 | +it can be used to decode `non-utf8` encoding string if necessary. | | 47 | + | 35 | 48 | Example: | 36 | 49 |
| 37 | 50 | querystring.parse('foo=bar&baz=qux&baz=quux&corge') | 38 | 51 | // returns | 39 | 52 | { foo: 'bar', baz: ['qux', 'quux'], corge: '' } | 40 | 53 |
| | 54 | + // Suppose gbkDecodeURIComponent function already exists, | | 55 | + // it can decode `gbk` encoding string | | 56 | + querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null, | | 57 | + { decodeURIComponent: gbkDecodeURIComponent }) | | 58 | + // returns | | 59 | + { w: '中文', foo: 'bar' } | | 60 | + | 41 | 61 | ## querystring.escape | 42 | 62 |
| 43 | 63 | The escape function used by `querystring.stringify`, | |
Collapse file Copy file name to clipboardExpand all lines: lib/querystring.js +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,25 +125,29 @@ var stringifyPrimitive = function(v) { | 125 | 125 | }; | 126 | 126 |
| 127 | 127 |
| 128 | | -QueryString.stringify = QueryString.encode = function(obj, sep, eq) { | | 128 | +QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) { | 129 | 129 | sep = sep || '&'; | 130 | 130 | eq = eq || '='; | 131 | 131 | if (util.isNull(obj)) { | 132 | 132 | obj = undefined; | 133 | 133 | } | 134 | 134 |
| | 135 | + var encode = QueryString.escape; | | 136 | + if (options && typeof options.encodeURIComponent === 'function') { | | 137 | + encode = options.encodeURIComponent; | | 138 | + } | | 139 | + | 135 | 140 | if (util.isObject(obj)) { | 136 | 141 | return Object.keys(obj).map(function(k) { | 137 | | - var ks = QueryString.escape(stringifyPrimitive(k)) + eq; | | 142 | + var ks = encode(stringifyPrimitive(k)) + eq; | 138 | 143 | if (util.isArray(obj[k])) { | 139 | 144 | return obj[k].map(function(v) { | 140 | | - return ks + QueryString.escape(stringifyPrimitive(v)); | | 145 | + return ks + encode(stringifyPrimitive(v)); | 141 | 146 | }).join(sep); | 142 | 147 | } else { | 143 | | - return ks + QueryString.escape(stringifyPrimitive(obj[k])); | | 148 | + return ks + encode(stringifyPrimitive(obj[k])); | 144 | 149 | } | 145 | 150 | }).join(sep); | 146 | | - | 147 | 151 | } | 148 | 152 | return ''; | 149 | 153 | }; | @@ -172,6 +176,11 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { | 172 | 176 | len = maxKeys; | 173 | 177 | } | 174 | 178 |
| | 179 | + var decode = decodeURIComponent; | | 180 | + if (options && typeof options.decodeURIComponent === 'function') { | | 181 | + decode = options.decodeURIComponent; | | 182 | + } | | 183 | + | 175 | 184 | for (var i = 0; i < len; ++i) { | 176 | 185 | var x = qs[i].replace(regexp, '%20'), | 177 | 186 | idx = x.indexOf(eq), | @@ -186,8 +195,8 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) { | 186 | 195 | } | 187 | 196 |
| 188 | 197 | try { | 189 | | - k = decodeURIComponent(kstr); | 190 | | - v = decodeURIComponent(vstr); | | 198 | + k = decode(kstr); | | 199 | + v = decode(vstr); | 191 | 200 | } catch (e) { | 192 | 201 | k = QueryString.unescape(kstr, true); | 193 | 202 | v = QueryString.unescape(vstr, true); | |
Collapse file Copy file name to clipboardExpand all lines: test/simple/test-querystring.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,3 +228,22 @@ assert.equal(0xeb, b[16]); | 228 | 228 | assert.equal(0xd8, b[17]); | 229 | 229 | assert.equal(0xa2, b[18]); | 230 | 230 | assert.equal(0xe6, b[19]); | | 231 | + | | 232 | + | | 233 | +// Test custom decode | | 234 | +function demoDecode(str) { | | 235 | + return str + str; | | 236 | +} | | 237 | +assert.deepEqual( | | 238 | + qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }), | | 239 | + { aa: 'aa', bb: 'bb', cc: 'cc' }); | | 240 | + | | 241 | + | | 242 | +// Test custom encode | | 243 | +function demoEncode(str) { | | 244 | + return str[0]; | | 245 | +} | | 246 | +var obj = { aa: 'aa', bb: 'bb', cc: 'cc' }; | | 247 | +assert.equal( | | 248 | + qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }), | | 249 | + 'a=a&b=b&c=c'); |
|