joyent/node — pull request #7525 by fengmk2 (2014-04-29 01:54:23+00:00)
Title: querystring: support custom encode on stringify and decode on parse

Not all querystring are utf-8 encoding, make `querystring` module can be used
to encode / decode `non-utf8` encoding string if necessary.

Using [iconv-lite](https://github.com/ashtuchkin/iconv-lite) or [iconv](https://github.com/bnoordhuis/node-iconv), now can easy to encode or decode no-utf8 string:

```js
var iconv = require('iconv-lite');

function gbkEncodeURIComponent(str) {
var buf = iconv.encode(str, 'gbk');
var encodeStr = '';
for (var i = 0; i < buf.length; i++) {
encodeStr += '%' + buf[i].toString('16');
}
encodeStr = encodeStr.toUpperCase();
return encodeStr;
}

function gbkDecodeURIComponent(str) {
var bytes = [];
for (var i = 0; i < str.length; ) {
if (str[i] === '%') {
i++;
bytes.push(parseInt(str.substring(i, i + 2), 16));
i += 2;
} else {
bytes.push(str.charCodeAt(i));
i++;
}
}
var buf = new Buffer(bytes);
return iconv.decode(buf, 'gbk');
}

querystring.stringify({ w: '中文' }, null, null,
{ encode: gbkEncodeURIComponent })
// returns
'w=%D6%D0%CE%C4'

querystring.parse('w=%D6%D0%CE%C4', null, null,
{ decode: gbkDecodeURIComponent })
// returns
{ w: '中文' }
```