query-string 6.2.0 → 6.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +135 -0
- package/index.js +4 -0
- package/package.json +7 -5
- package/readme.md +3 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export interface ParseOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
|
|
4
|
+
*
|
|
5
|
+
* @default true
|
|
6
|
+
*/
|
|
7
|
+
readonly decode?: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
|
|
11
|
+
*
|
|
12
|
+
* @default 'none'
|
|
13
|
+
*
|
|
14
|
+
* - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
|
|
15
|
+
*
|
|
16
|
+
*
|
|
17
|
+
* queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
18
|
+
* //=> foo: [1,2,3]
|
|
19
|
+
*
|
|
20
|
+
* - `index`: stands for parsing taking the index into account, such as:
|
|
21
|
+
*
|
|
22
|
+
*
|
|
23
|
+
* queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
24
|
+
* //=> foo: [1,2,3]
|
|
25
|
+
*
|
|
26
|
+
* - `none`: is the **default** option and removes any bracket representation, such as:
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* queryString.parse('foo=1&foo=2&foo=3');
|
|
30
|
+
* //=> foo: [1,2,3]
|
|
31
|
+
*/
|
|
32
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'none';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ParsedQuery {
|
|
36
|
+
readonly [key: string]: string | string[] | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
|
|
41
|
+
*
|
|
42
|
+
* The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
|
|
43
|
+
*
|
|
44
|
+
* @param query - The query string to parse.
|
|
45
|
+
*/
|
|
46
|
+
export function parse(query: string, options?: ParseOptions): ParsedQuery;
|
|
47
|
+
|
|
48
|
+
export interface ParsedUrl {
|
|
49
|
+
readonly url: string;
|
|
50
|
+
readonly query: ParsedQuery;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Extract the URL and the query string as an object.
|
|
55
|
+
*
|
|
56
|
+
* @param url - The URL to parse.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* queryString.parseUrl('https://foo.bar?foo=bar');
|
|
61
|
+
* //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
|
|
62
|
+
*/
|
|
63
|
+
export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
|
|
64
|
+
|
|
65
|
+
export interface StringifyOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Strictly encode URI components with [`strict-uri-encode`](https://github.com/kevva/strict-uri-encode). It uses [`encodeURIComponent`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
|
|
68
|
+
*
|
|
69
|
+
* @default true
|
|
70
|
+
*/
|
|
71
|
+
readonly strict?: boolean;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
|
|
75
|
+
*
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
readonly encode?: boolean;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
|
|
82
|
+
*
|
|
83
|
+
* @default 'none'
|
|
84
|
+
*
|
|
85
|
+
* - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
|
|
86
|
+
*
|
|
87
|
+
*
|
|
88
|
+
* queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'bracket'});
|
|
89
|
+
* // => foo[]=1&foo[]=2&foo[]=3
|
|
90
|
+
*
|
|
91
|
+
* - `index`: stands for parsing taking the index into account, such as:
|
|
92
|
+
*
|
|
93
|
+
*
|
|
94
|
+
* queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'index'});
|
|
95
|
+
* // => foo[0]=1&foo[1]=2&foo[3]=3
|
|
96
|
+
*
|
|
97
|
+
* - `none`: is the **default** option and removes any bracket representation, such as:
|
|
98
|
+
*
|
|
99
|
+
*
|
|
100
|
+
* queryString.stringify({foo: [1,2,3]});
|
|
101
|
+
* // => foo=1&foo=2&foo=3
|
|
102
|
+
*/
|
|
103
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'none';
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
|
107
|
+
*
|
|
108
|
+
* If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
*
|
|
112
|
+
* const order = ['c', 'a', 'b'];
|
|
113
|
+
* queryString.stringify({ a: 1, b: 2, c: 3}, {
|
|
114
|
+
* sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
|
|
115
|
+
* });
|
|
116
|
+
* // => 'c=3&a=1&b=2'
|
|
117
|
+
*
|
|
118
|
+
* queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false});
|
|
119
|
+
* // => 'b=1&c=2&a=3'
|
|
120
|
+
*/
|
|
121
|
+
readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Stringify an object into a query string, sorting the keys.
|
|
126
|
+
*/
|
|
127
|
+
export function stringify(
|
|
128
|
+
object: {[key: string]: unknown},
|
|
129
|
+
options?: StringifyOptions
|
|
130
|
+
): string;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Extract a query string from a URL that can be passed into `.parse()`.
|
|
134
|
+
*/
|
|
135
|
+
export function extract(url: string): string;
|
package/index.js
CHANGED
|
@@ -19,6 +19,7 @@ function encoderForArrayFormat(options) {
|
|
|
19
19
|
encode(value, options)
|
|
20
20
|
].join('');
|
|
21
21
|
};
|
|
22
|
+
|
|
22
23
|
case 'bracket':
|
|
23
24
|
return (key, value) => {
|
|
24
25
|
return value === null ? [encode(key, options), '[]'].join('') : [
|
|
@@ -27,6 +28,7 @@ function encoderForArrayFormat(options) {
|
|
|
27
28
|
encode(value, options)
|
|
28
29
|
].join('');
|
|
29
30
|
};
|
|
31
|
+
|
|
30
32
|
default:
|
|
31
33
|
return (key, value) => {
|
|
32
34
|
return value === null ? encode(key, options) : [
|
|
@@ -59,6 +61,7 @@ function parserForArrayFormat(options) {
|
|
|
59
61
|
|
|
60
62
|
accumulator[key][result[1]] = value;
|
|
61
63
|
};
|
|
64
|
+
|
|
62
65
|
case 'bracket':
|
|
63
66
|
return (key, value, accumulator) => {
|
|
64
67
|
result = /(\[\])$/.exec(key);
|
|
@@ -76,6 +79,7 @@ function parserForArrayFormat(options) {
|
|
|
76
79
|
|
|
77
80
|
accumulator[key] = [].concat(accumulator[key], value);
|
|
78
81
|
};
|
|
82
|
+
|
|
79
83
|
default:
|
|
80
84
|
return (key, value, accumulator) => {
|
|
81
85
|
if (accumulator[key] === undefined) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-string",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.0",
|
|
4
4
|
"description": "Parse and stringify URL query strings",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/query-string",
|
|
@@ -13,10 +13,11 @@
|
|
|
13
13
|
"node": ">=6"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"test": "xo && ava"
|
|
16
|
+
"test": "xo && ava && tsd-check"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"index.js"
|
|
19
|
+
"index.js",
|
|
20
|
+
"index.d.ts"
|
|
20
21
|
],
|
|
21
22
|
"keywords": [
|
|
22
23
|
"browser",
|
|
@@ -38,9 +39,10 @@
|
|
|
38
39
|
"strict-uri-encode": "^2.0.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"ava": "^
|
|
42
|
+
"ava": "^1.2.1",
|
|
42
43
|
"deep-equal": "^1.0.1",
|
|
43
44
|
"fast-check": "^1.5.0",
|
|
44
|
-
"
|
|
45
|
+
"tsd-check": "^0.3.0",
|
|
46
|
+
"xo": "^0.24.0"
|
|
45
47
|
}
|
|
46
48
|
}
|
package/readme.md
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
$ npm install query-string
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`.
|
|
18
|
+
This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app v1](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`.
|
|
19
19
|
|
|
20
20
|
<a href="https://www.patreon.com/sindresorhus">
|
|
21
21
|
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
|
|
@@ -153,14 +153,14 @@ Supports both `Function` as a custom sorting function or `false` to disable sort
|
|
|
153
153
|
```js
|
|
154
154
|
const order = ['c', 'a', 'b'];
|
|
155
155
|
queryString.stringify({ a: 1, b: 2, c: 3}, {
|
|
156
|
-
sort: (m, n) => order.indexOf(m)
|
|
156
|
+
sort: (m, n) => order.indexOf(m) - order.indexOf(n)
|
|
157
157
|
});
|
|
158
158
|
// => 'c=3&a=1&b=2'
|
|
159
159
|
```
|
|
160
160
|
|
|
161
161
|
```js
|
|
162
162
|
queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false});
|
|
163
|
-
// => 'c=
|
|
163
|
+
// => 'b=1&c=2&a=3'
|
|
164
164
|
```
|
|
165
165
|
|
|
166
166
|
If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
|