|
4 | 4 |
|
5 | 5 | <!--name=querystring-->
|
6 | 6 |
|
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: |
9 | 9 |
|
10 |
| -## querystring.escape |
| 10 | +```js |
| 11 | +const querystring = require('querystring'); |
| 12 | +``` |
| 13 | + |
| 14 | +## querystring.escape(str) |
11 | 15 | <!-- YAML
|
12 | 16 | added: v0.1.25
|
13 | 17 | -->
|
14 | 18 |
|
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. |
17 | 24 |
|
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]]]) |
19 | 31 | <!-- YAML
|
20 | 32 | added: v0.1.25
|
21 | 33 | -->
|
22 | 34 |
|
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. |
26 | 49 |
|
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: |
29 | 51 |
|
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 | +``` |
32 | 58 |
|
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: |
34 | 68 |
|
35 | 69 | ```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... |
38 | 71 |
|
39 |
| -// Suppose gbkDecodeURIComponent function already exists, |
40 |
| -// it can decode `gbk` encoding string |
41 | 72 | querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
|
42 | 73 | { decodeURIComponent: gbkDecodeURIComponent })
|
43 |
| -// returns { w: '中文', foo: 'bar' } |
44 | 74 | ```
|
45 | 75 |
|
46 |
| -## querystring.stringify(obj[, sep][, eq][, options]) |
| 76 | +## querystring.stringify(obj[, sep[, eq[, options]]]) |
47 | 77 | <!-- YAML
|
48 | 78 | added: v0.1.25
|
49 | 79 | -->
|
50 | 80 |
|
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()`. |
54 | 90 |
|
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". |
57 | 93 |
|
58 |
| -Example: |
| 94 | +For example: |
59 | 95 |
|
60 | 96 | ```js
|
61 | 97 | querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
|
62 | 98 | // returns 'foo=bar&baz=qux&baz=quux&corge='
|
63 | 99 |
|
64 | 100 | querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
|
65 | 101 | // 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, |
66 | 111 |
|
67 |
| -// Suppose gbkEncodeURIComponent function already exists, |
68 |
| -// it can encode string with `gbk` encoding |
69 | 112 | querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
|
70 | 113 | { encodeURIComponent: gbkEncodeURIComponent })
|
71 |
| -// returns 'w=%D6%D0%CE%C4&foo=bar' |
72 | 114 | ```
|
73 | 115 |
|
74 |
| -## querystring.unescape |
| 116 | +## querystring.unescape(str) |
75 | 117 | <!-- YAML
|
76 | 118 | added: v0.1.25
|
77 | 119 | -->
|
| 120 | +* `str` {String} |
| 121 | + |
| 122 | + |
| 123 | +The `querystring.unescape()` method performs decoding of URL percent-encoded |
| 124 | +characters on the given `str`. |
78 | 125 |
|
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. |
81 | 130 |
|
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