query-string 5.1.0 → 6.2.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.js +103 -93
- package/package.json +9 -8
- package/readme.md +8 -1
package/index.js
CHANGED
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var decodeComponent = require('decode-uri-component');
|
|
2
|
+
const strictUriEncode = require('strict-uri-encode');
|
|
3
|
+
const decodeComponent = require('decode-uri-component');
|
|
5
4
|
|
|
6
|
-
function encoderForArrayFormat(
|
|
7
|
-
switch (
|
|
5
|
+
function encoderForArrayFormat(options) {
|
|
6
|
+
switch (options.arrayFormat) {
|
|
8
7
|
case 'index':
|
|
9
|
-
return
|
|
8
|
+
return (key, value, index) => {
|
|
10
9
|
return value === null ? [
|
|
11
|
-
encode(key,
|
|
10
|
+
encode(key, options),
|
|
12
11
|
'[',
|
|
13
12
|
index,
|
|
14
13
|
']'
|
|
15
14
|
].join('') : [
|
|
16
|
-
encode(key,
|
|
15
|
+
encode(key, options),
|
|
17
16
|
'[',
|
|
18
|
-
encode(index,
|
|
17
|
+
encode(index, options),
|
|
19
18
|
']=',
|
|
20
|
-
encode(value,
|
|
19
|
+
encode(value, options)
|
|
21
20
|
].join('');
|
|
22
21
|
};
|
|
23
|
-
|
|
24
22
|
case 'bracket':
|
|
25
|
-
return
|
|
26
|
-
return value === null ? encode(key,
|
|
27
|
-
encode(key,
|
|
23
|
+
return (key, value) => {
|
|
24
|
+
return value === null ? [encode(key, options), '[]'].join('') : [
|
|
25
|
+
encode(key, options),
|
|
28
26
|
'[]=',
|
|
29
|
-
encode(value,
|
|
27
|
+
encode(value, options)
|
|
30
28
|
].join('');
|
|
31
29
|
};
|
|
32
|
-
|
|
33
30
|
default:
|
|
34
|
-
return
|
|
35
|
-
return value === null ? encode(key,
|
|
36
|
-
encode(key,
|
|
31
|
+
return (key, value) => {
|
|
32
|
+
return value === null ? encode(key, options) : [
|
|
33
|
+
encode(key, options),
|
|
37
34
|
'=',
|
|
38
|
-
encode(value,
|
|
35
|
+
encode(value, options)
|
|
39
36
|
].join('');
|
|
40
37
|
};
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
function parserForArrayFormat(
|
|
45
|
-
|
|
41
|
+
function parserForArrayFormat(options) {
|
|
42
|
+
let result;
|
|
46
43
|
|
|
47
|
-
switch (
|
|
44
|
+
switch (options.arrayFormat) {
|
|
48
45
|
case 'index':
|
|
49
|
-
return
|
|
46
|
+
return (key, value, accumulator) => {
|
|
50
47
|
result = /\[(\d*)\]$/.exec(key);
|
|
51
48
|
|
|
52
49
|
key = key.replace(/\[\d*\]$/, '');
|
|
@@ -62,25 +59,25 @@ function parserForArrayFormat(opts) {
|
|
|
62
59
|
|
|
63
60
|
accumulator[key][result[1]] = value;
|
|
64
61
|
};
|
|
65
|
-
|
|
66
62
|
case 'bracket':
|
|
67
|
-
return
|
|
63
|
+
return (key, value, accumulator) => {
|
|
68
64
|
result = /(\[\])$/.exec(key);
|
|
69
65
|
key = key.replace(/\[\]$/, '');
|
|
70
66
|
|
|
71
67
|
if (!result) {
|
|
72
68
|
accumulator[key] = value;
|
|
73
69
|
return;
|
|
74
|
-
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (accumulator[key] === undefined) {
|
|
75
73
|
accumulator[key] = [value];
|
|
76
74
|
return;
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
accumulator[key] = [].concat(accumulator[key], value);
|
|
80
78
|
};
|
|
81
|
-
|
|
82
79
|
default:
|
|
83
|
-
return
|
|
80
|
+
return (key, value, accumulator) => {
|
|
84
81
|
if (accumulator[key] === undefined) {
|
|
85
82
|
accumulator[key] = value;
|
|
86
83
|
return;
|
|
@@ -91,9 +88,17 @@ function parserForArrayFormat(opts) {
|
|
|
91
88
|
}
|
|
92
89
|
}
|
|
93
90
|
|
|
94
|
-
function encode(value,
|
|
95
|
-
if (
|
|
96
|
-
return
|
|
91
|
+
function encode(value, options) {
|
|
92
|
+
if (options.encode) {
|
|
93
|
+
return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function decode(value, options) {
|
|
100
|
+
if (options.decode) {
|
|
101
|
+
return decodeComponent(value);
|
|
97
102
|
}
|
|
98
103
|
|
|
99
104
|
return value;
|
|
@@ -102,120 +107,125 @@ function encode(value, opts) {
|
|
|
102
107
|
function keysSorter(input) {
|
|
103
108
|
if (Array.isArray(input)) {
|
|
104
109
|
return input.sort();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (typeof input === 'object') {
|
|
113
|
+
return keysSorter(Object.keys(input))
|
|
114
|
+
.sort((a, b) => Number(a) - Number(b))
|
|
115
|
+
.map(key => input[key]);
|
|
111
116
|
}
|
|
112
117
|
|
|
113
118
|
return input;
|
|
114
119
|
}
|
|
115
120
|
|
|
116
|
-
|
|
117
|
-
|
|
121
|
+
function extract(input) {
|
|
122
|
+
const queryStart = input.indexOf('?');
|
|
118
123
|
if (queryStart === -1) {
|
|
119
124
|
return '';
|
|
120
125
|
}
|
|
121
|
-
return str.slice(queryStart + 1);
|
|
122
|
-
};
|
|
123
126
|
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
return input.slice(queryStart + 1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function parse(input, options) {
|
|
131
|
+
options = Object.assign({decode: true, arrayFormat: 'none'}, options);
|
|
126
132
|
|
|
127
|
-
|
|
133
|
+
const formatter = parserForArrayFormat(options);
|
|
128
134
|
|
|
129
135
|
// Create an object with no prototype
|
|
130
|
-
|
|
131
|
-
var ret = Object.create(null);
|
|
136
|
+
const ret = Object.create(null);
|
|
132
137
|
|
|
133
|
-
if (typeof
|
|
138
|
+
if (typeof input !== 'string') {
|
|
134
139
|
return ret;
|
|
135
140
|
}
|
|
136
141
|
|
|
137
|
-
|
|
142
|
+
input = input.trim().replace(/^[?#&]/, '');
|
|
138
143
|
|
|
139
|
-
if (!
|
|
144
|
+
if (!input) {
|
|
140
145
|
return ret;
|
|
141
146
|
}
|
|
142
147
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// Firefox (pre 40) decodes `%3D` to `=`
|
|
146
|
-
// https://github.com/sindresorhus/query-string/pull/37
|
|
147
|
-
var key = parts.shift();
|
|
148
|
-
var val = parts.length > 0 ? parts.join('=') : undefined;
|
|
148
|
+
for (const param of input.split('&')) {
|
|
149
|
+
let [key, value] = param.replace(/\+/g, ' ').split('=');
|
|
149
150
|
|
|
150
|
-
//
|
|
151
|
+
// Missing `=` should be `null`:
|
|
151
152
|
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
152
|
-
|
|
153
|
+
value = value === undefined ? null : decode(value, options);
|
|
153
154
|
|
|
154
|
-
formatter(
|
|
155
|
-
}
|
|
155
|
+
formatter(decode(key, options), value, ret);
|
|
156
|
+
}
|
|
156
157
|
|
|
157
|
-
return Object.keys(ret).sort().reduce(
|
|
158
|
-
|
|
159
|
-
if (Boolean(
|
|
158
|
+
return Object.keys(ret).sort().reduce((result, key) => {
|
|
159
|
+
const value = ret[key];
|
|
160
|
+
if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
|
|
160
161
|
// Sort object keys, not values
|
|
161
|
-
result[key] = keysSorter(
|
|
162
|
+
result[key] = keysSorter(value);
|
|
162
163
|
} else {
|
|
163
|
-
result[key] =
|
|
164
|
+
result[key] = value;
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
return result;
|
|
167
168
|
}, Object.create(null));
|
|
168
|
-
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
exports.extract = extract;
|
|
172
|
+
exports.parse = parse;
|
|
173
|
+
|
|
174
|
+
exports.stringify = (obj, options) => {
|
|
175
|
+
if (!obj) {
|
|
176
|
+
return '';
|
|
177
|
+
}
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
var defaults = {
|
|
179
|
+
options = Object.assign({
|
|
172
180
|
encode: true,
|
|
173
181
|
strict: true,
|
|
174
182
|
arrayFormat: 'none'
|
|
175
|
-
};
|
|
183
|
+
}, options);
|
|
176
184
|
|
|
177
|
-
|
|
185
|
+
const formatter = encoderForArrayFormat(options);
|
|
186
|
+
const keys = Object.keys(obj);
|
|
178
187
|
|
|
179
|
-
if (
|
|
180
|
-
|
|
188
|
+
if (options.sort !== false) {
|
|
189
|
+
keys.sort(options.sort);
|
|
181
190
|
}
|
|
182
191
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return obj ? Object.keys(obj).sort(opts.sort).map(function (key) {
|
|
186
|
-
var val = obj[key];
|
|
192
|
+
return keys.map(key => {
|
|
193
|
+
const value = obj[key];
|
|
187
194
|
|
|
188
|
-
if (
|
|
195
|
+
if (value === undefined) {
|
|
189
196
|
return '';
|
|
190
197
|
}
|
|
191
198
|
|
|
192
|
-
if (
|
|
193
|
-
return encode(key,
|
|
199
|
+
if (value === null) {
|
|
200
|
+
return encode(key, options);
|
|
194
201
|
}
|
|
195
202
|
|
|
196
|
-
if (Array.isArray(
|
|
197
|
-
|
|
203
|
+
if (Array.isArray(value)) {
|
|
204
|
+
const result = [];
|
|
198
205
|
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
|
|
206
|
+
for (const value2 of value.slice()) {
|
|
207
|
+
if (value2 === undefined) {
|
|
208
|
+
continue;
|
|
202
209
|
}
|
|
203
210
|
|
|
204
|
-
result.push(formatter(key,
|
|
205
|
-
}
|
|
211
|
+
result.push(formatter(key, value2, result.length));
|
|
212
|
+
}
|
|
206
213
|
|
|
207
214
|
return result.join('&');
|
|
208
215
|
}
|
|
209
216
|
|
|
210
|
-
return encode(key,
|
|
211
|
-
}).filter(
|
|
212
|
-
return x.length > 0;
|
|
213
|
-
}).join('&') : '';
|
|
217
|
+
return encode(key, options) + '=' + encode(value, options);
|
|
218
|
+
}).filter(x => x.length > 0).join('&');
|
|
214
219
|
};
|
|
215
220
|
|
|
216
|
-
exports.parseUrl =
|
|
221
|
+
exports.parseUrl = (input, options) => {
|
|
222
|
+
const hashStart = input.indexOf('#');
|
|
223
|
+
if (hashStart !== -1) {
|
|
224
|
+
input = input.slice(0, hashStart);
|
|
225
|
+
}
|
|
226
|
+
|
|
217
227
|
return {
|
|
218
|
-
url:
|
|
219
|
-
query:
|
|
228
|
+
url: input.split('?')[0] || '',
|
|
229
|
+
query: parse(extract(input), options)
|
|
220
230
|
};
|
|
221
231
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-string",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "Parse and stringify URL query strings",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/query-string",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "sindresorhus.com"
|
|
11
11
|
},
|
|
12
12
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
13
|
+
"node": ">=6"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "xo && ava"
|
|
@@ -27,19 +27,20 @@
|
|
|
27
27
|
"param",
|
|
28
28
|
"parameter",
|
|
29
29
|
"url",
|
|
30
|
-
"uri",
|
|
31
30
|
"parse",
|
|
32
31
|
"stringify",
|
|
33
32
|
"encode",
|
|
34
|
-
"decode"
|
|
33
|
+
"decode",
|
|
34
|
+
"searchparams"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"decode-uri-component": "^0.2.0",
|
|
38
|
-
"
|
|
39
|
-
"strict-uri-encode": "^1.0.0"
|
|
38
|
+
"strict-uri-encode": "^2.0.0"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
|
-
"ava": "^0.
|
|
43
|
-
"
|
|
41
|
+
"ava": "^0.25.0",
|
|
42
|
+
"deep-equal": "^1.0.1",
|
|
43
|
+
"fast-check": "^1.5.0",
|
|
44
|
+
"xo": "^0.23.0"
|
|
44
45
|
}
|
|
45
46
|
}
|
package/readme.md
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
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`.
|
|
19
|
+
|
|
18
20
|
<a href="https://www.patreon.com/sindresorhus">
|
|
19
21
|
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
|
|
20
22
|
</a>
|
|
@@ -60,7 +62,12 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
|
|
|
60
62
|
|
|
61
63
|
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`.
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
#### decode
|
|
66
|
+
|
|
67
|
+
Type: `boolean`<br>
|
|
68
|
+
Default: `true`
|
|
69
|
+
|
|
70
|
+
Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
|
|
64
71
|
|
|
65
72
|
#### arrayFormat
|
|
66
73
|
|