2012-02-28 03:09:34 +08:00
|
|
|
# Query String
|
|
|
|
|
2012-03-03 07:14:03 +08:00
|
|
|
Stability: 3 - Stable
|
|
|
|
|
2012-02-28 03:09:34 +08:00
|
|
|
<!--name=querystring-->
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
This module provides utilities for dealing with query strings.
|
|
|
|
It provides the following methods:
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-09-30 07:32:34 +08:00
|
|
|
## querystring.stringify(obj[, sep][, eq][, options])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
Serialize an object to a query string.
|
2011-12-27 16:43:58 +08:00
|
|
|
Optionally override the default separator (`'&'`) and assignment (`'='`)
|
|
|
|
characters.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-04-29 09:40:40 +08:00
|
|
|
Options object may contain `encodeURIComponent` property (`querystring.escape` by default),
|
|
|
|
it can be used to encode string with `non-utf8` encoding if necessary.
|
|
|
|
|
2010-10-28 20:18:16 +08:00
|
|
|
Example:
|
|
|
|
|
2011-08-30 06:21:37 +08:00
|
|
|
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
|
2010-10-28 20:18:16 +08:00
|
|
|
// returns
|
2011-08-30 06:21:37 +08:00
|
|
|
'foo=bar&baz=qux&baz=quux&corge='
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2011-08-30 06:21:37 +08:00
|
|
|
querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
|
2010-10-28 20:18:16 +08:00
|
|
|
// returns
|
2011-08-30 06:21:37 +08:00
|
|
|
'foo:bar;baz:qux'
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-04-29 09:40:40 +08:00
|
|
|
// Suppose gbkEncodeURIComponent function already exists,
|
|
|
|
// it can encode string with `gbk` encoding
|
|
|
|
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
|
|
|
|
{ encodeURIComponent: gbkEncodeURIComponent })
|
|
|
|
// returns
|
|
|
|
'w=%D6%D0%CE%C4&foo=bar'
|
|
|
|
|
2014-09-30 07:32:34 +08:00
|
|
|
## querystring.parse(str[, sep][, eq][, options])
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
Deserialize a query string to an object.
|
2011-12-27 16:43:58 +08:00
|
|
|
Optionally override the default separator (`'&'`) and assignment (`'='`)
|
|
|
|
characters.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2012-01-16 03:45:31 +08:00
|
|
|
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.
|
|
|
|
|
2014-04-29 09:40:40 +08:00
|
|
|
Options object may contain `decodeURIComponent` property (`decodeURIComponent` by default),
|
|
|
|
it can be used to decode `non-utf8` encoding string if necessary.
|
|
|
|
|
2010-10-28 20:18:16 +08:00
|
|
|
Example:
|
|
|
|
|
2011-08-30 06:21:37 +08:00
|
|
|
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
|
2010-10-28 20:18:16 +08:00
|
|
|
// returns
|
2011-08-30 06:21:37 +08:00
|
|
|
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2014-04-29 09:40:40 +08:00
|
|
|
// Suppose gbkDecodeURIComponent function already exists,
|
|
|
|
// it can decode `gbk` encoding string
|
|
|
|
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
|
|
|
|
{ decodeURIComponent: gbkDecodeURIComponent })
|
|
|
|
// returns
|
|
|
|
{ w: '中文', foo: 'bar' }
|
|
|
|
|
2012-02-28 03:09:34 +08:00
|
|
|
## querystring.escape
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
The escape function used by `querystring.stringify`,
|
|
|
|
provided so that it could be overridden if necessary.
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2012-02-28 03:09:34 +08:00
|
|
|
## querystring.unescape
|
2010-10-28 20:18:16 +08:00
|
|
|
|
2010-11-18 20:00:24 +08:00
|
|
|
The unescape function used by `querystring.parse`,
|
|
|
|
provided so that it could be overridden if necessary.
|