Skip to content

Commit 865644a

Browse files
jasnellevanlucas
authored andcommitted
doc: general improvements to querystring.md copy
PR-URL: #7023 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent dd4c607 commit 865644a

File tree

1 file changed

+84
-35
lines changed

1 file changed

+84
-35
lines changed

doc/api/querystring.md

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,130 @@
44

55
<!--name=querystring-->
66

7-
This module provides utilities for dealing with query strings.
8-
It provides the following methods:
7+
The `querystring` module provides utilities for parsing and formatting URL
8+
query strings. It can be accessed using:
99

10-
## querystring.escape
10+
```js
11+
const querystring = require('querystring');
12+
```
13+
14+
## querystring.escape(str)
1115
<!-- YAML
1216
added: v0.1.25
1317
-->
1418

15-
The escape function used by `querystring.stringify`,
16-
provided so that it could be overridden if necessary.
19+
* `str` {String}
20+
21+
The `querystring.escape()` method performs URL percent-encoding on the given
22+
`str` in a manner that is optimized for the specific requirements of URL
23+
query strings.
1724

18-
## querystring.parse(str[, sep][, eq][, options])
25+
The `querystring.escape()` method is used by `querystring.stringify()` and is
26+
generally not expected to be used directly. It is exported primarily to allow
27+
application code to provide a replacement percent-encoding implementation if
28+
necessary by assigning `querystring.escape` to an alternative function.
29+
30+
## querystring.parse(str[, sep[, eq[, options]]])
1931
<!-- YAML
2032
added: v0.1.25
2133
-->
2234

23-
Deserialize a query string to an object.
24-
Optionally override the default separator (`'&'`) and assignment (`'='`)
25-
characters.
35+
* `str` {String} The URL query string to parse
36+
* `sep` {String} The substring used to delimit key and value pairs in the
37+
query string. Defaults to `'&'`.
38+
* `eq` {String}. The substring used to delimit keys and values in the
39+
query string. Defaults to `'='`.
40+
* `options` {Object}
41+
* `decodeURIComponent` {Function} The function to use when decoding
42+
percent-encoded characters in the query string. Defaults to
43+
`querystring.unescape()`.
44+
* `maxKeys` {number} Specifies the maximum number of keys to parse.
45+
Defaults to `1000`. Specify `0` to remove key counting limitations.
46+
47+
The `querystring.parse()` method parses a URL query string (`str`) into a
48+
collection of key and value pairs.
2649

27-
Options object may contain `maxKeys` property (equal to 1000 by default), it'll
28-
be used to limit processed keys. Set it to 0 to remove key count limitation.
50+
For example, the query string `'foo=bar&abc=xyz&abc=123'` is parsed into:
2951

30-
Options object may contain `decodeURIComponent` property (`querystring.unescape` by default),
31-
it can be used to decode a `non-utf8` encoding string if necessary.
52+
```js
53+
{
54+
foo: 'bar',
55+
abc: ['xyz', '123']
56+
}
57+
```
3258

33-
Example:
59+
*Note*: The object returned by the `querystring.parse()` method _does not_
60+
prototypically extend from the JavaScript `Object`. This means that the
61+
typical `Object` methods such as `obj.toString()`, `obj.hashOwnProperty()`,
62+
and others are not defined and *will not work*.
63+
64+
By default, percent-encoded characters within the query string will be assumed
65+
to use UTF-8 encoding. If an alternative character encoding is used, then an
66+
alternative `decodeURIComponent` option will need to be specified as illustrated
67+
in the following example:
3468

3569
```js
36-
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
37-
// returns { foo: 'bar', baz: ['qux', 'quux'], corge: '' }
70+
// Assuming gbkDecodeURIComponent function already exists...
3871

39-
// Suppose gbkDecodeURIComponent function already exists,
40-
// it can decode `gbk` encoding string
4172
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
4273
{ decodeURIComponent: gbkDecodeURIComponent })
43-
// returns { w: '中文', foo: 'bar' }
4474
```
4575

46-
## querystring.stringify(obj[, sep][, eq][, options])
76+
## querystring.stringify(obj[, sep[, eq[, options]]])
4777
<!-- YAML
4878
added: v0.1.25
4979
-->
5080

51-
Serialize an object to a query string.
52-
Optionally override the default separator (`'&'`) and assignment (`'='`)
53-
characters.
81+
* `obj` {Object} The object to serialize into a URL query string
82+
* `sep` {String} The substring used to delimit key and value pairs in the
83+
query string. Defaults to `'&'`.
84+
* `eq` {String}. The substring used to delimit keys and values in the
85+
query string. Defaults to `'='`.
86+
* `options`
87+
* `encodeURIComponent` {Function} The function to use when converting
88+
URL-unsafe characters to percent-encoding in the query string. Defaults to
89+
`querystring.escape()`.
5490

55-
Options object may contain `encodeURIComponent` property (`querystring.escape` by default),
56-
it can be used to encode string with `non-utf8` encoding if necessary.
91+
The `querystring.stringify()` method produces a URL query string from a
92+
given `obj` by iterating through the object's "own properties".
5793

58-
Example:
94+
For example:
5995

6096
```js
6197
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
6298
// returns 'foo=bar&baz=qux&baz=quux&corge='
6399

64100
querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
65101
// returns 'foo:bar;baz:qux'
102+
```
103+
104+
By default, characters requiring percent-encoding within the query string will
105+
be encoded as UTF-8. If an alternative encoding is required, then an alternative
106+
`encodeURIComponent` option will need to be specified as illustrated in the
107+
following example:
108+
109+
```js
110+
// Assuming gbkEncodeURIComponent function already exists,
66111

67-
// Suppose gbkEncodeURIComponent function already exists,
68-
// it can encode string with `gbk` encoding
69112
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
70113
{ encodeURIComponent: gbkEncodeURIComponent })
71-
// returns 'w=%D6%D0%CE%C4&foo=bar'
72114
```
73115

74-
## querystring.unescape
116+
## querystring.unescape(str)
75117
<!-- YAML
76118
added: v0.1.25
77119
-->
120+
* `str` {String}
121+
122+
123+
The `querystring.unescape()` method performs decoding of URL percent-encoded
124+
characters on the given `str`.
78125

79-
The unescape function used by `querystring.parse`,
80-
provided so that it could be overridden if necessary.
126+
The `querystring.unescape()` method is used by `querystring.parse()` and is
127+
generally not expected to be used directly. It is exported primarily to allow
128+
application code to provide a replacement decoding implementation if
129+
necessary by assigning `querystring.unescape` to an alternative function.
81130

82-
It will try to use `decodeURIComponent` in the first place,
83-
but if that fails it falls back to a safer equivalent that
84-
doesn't throw on malformed URLs.
131+
By default, the `querystring.unescape()` method will attempt to use the
132+
JavaScript built-in `decodeURIComponent()` method to decode. If that fails,
133+
a safer equivalent that does not throw on malformed URLs will be used.

0 commit comments

Comments
 (0)