query-string 2.2.0 → 2.4.2
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 +15 -5
- package/package.json +21 -17
- package/readme.md +18 -14
package/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
var strictUriEncode = require('strict-uri-encode');
|
|
3
|
+
|
|
4
|
+
exports.extract = function (str) {
|
|
5
|
+
return str.split('?')[1] || '';
|
|
6
|
+
};
|
|
2
7
|
|
|
3
8
|
exports.parse = function (str) {
|
|
4
9
|
if (typeof str !== 'string') {
|
|
@@ -11,12 +16,15 @@ exports.parse = function (str) {
|
|
|
11
16
|
return {};
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
return str.
|
|
19
|
+
return str.split('&').reduce(function (ret, param) {
|
|
15
20
|
var parts = param.replace(/\+/g, ' ').split('=');
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
// Firefox (pre 40) decodes `%3D` to `=`
|
|
22
|
+
// https://github.com/sindresorhus/query-string/pull/37
|
|
23
|
+
var key = parts.shift();
|
|
24
|
+
var val = parts.length > 0 ? parts.join('=') : undefined;
|
|
18
25
|
|
|
19
26
|
key = decodeURIComponent(key);
|
|
27
|
+
|
|
20
28
|
// missing `=` should be `null`:
|
|
21
29
|
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
22
30
|
val = val === undefined ? null : decodeURIComponent(val);
|
|
@@ -39,10 +47,12 @@ exports.stringify = function (obj) {
|
|
|
39
47
|
|
|
40
48
|
if (Array.isArray(val)) {
|
|
41
49
|
return val.sort().map(function (val2) {
|
|
42
|
-
return
|
|
50
|
+
return strictUriEncode(key) + '=' + strictUriEncode(val2);
|
|
43
51
|
}).join('&');
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
return
|
|
54
|
+
return strictUriEncode(key) + '=' + strictUriEncode(val);
|
|
55
|
+
}).filter(function (x) {
|
|
56
|
+
return x.length > 0;
|
|
47
57
|
}).join('&') : '';
|
|
48
58
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-string",
|
|
3
|
-
"version": "2.2
|
|
3
|
+
"version": "2.4.2",
|
|
4
4
|
"description": "Parse and stringify URL query strings",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "sindresorhus/query-string",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Sindre Sorhus",
|
|
9
|
+
"email": "sindresorhus@gmail.com",
|
|
10
|
+
"url": "sindresorhus.com"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=0.10.0"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "xo && ava"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"index.js"
|
|
20
|
+
],
|
|
5
21
|
"keywords": [
|
|
6
22
|
"browser",
|
|
7
23
|
"querystring",
|
|
@@ -17,23 +33,11 @@
|
|
|
17
33
|
"encode",
|
|
18
34
|
"decode"
|
|
19
35
|
],
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"name": "Sindre Sorhus",
|
|
23
|
-
"email": "sindresorhus@gmail.com",
|
|
24
|
-
"url": "sindresorhus.com"
|
|
25
|
-
},
|
|
26
|
-
"files": [
|
|
27
|
-
"index.js"
|
|
28
|
-
],
|
|
29
|
-
"repository": "sindresorhus/query-string",
|
|
30
|
-
"scripts": {
|
|
31
|
-
"test": "mocha"
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"strict-uri-encode": "^1.0.0"
|
|
32
38
|
},
|
|
33
39
|
"devDependencies": {
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"engines": {
|
|
37
|
-
"node": ">=0.10.0"
|
|
40
|
+
"ava": "*",
|
|
41
|
+
"xo": "*"
|
|
38
42
|
}
|
|
39
43
|
}
|
package/readme.md
CHANGED
|
@@ -13,19 +13,19 @@ $ npm install --save query-string
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
|
-
|
|
16
|
+
const queryString = require('query-string');
|
|
17
17
|
|
|
18
18
|
console.log(location.search);
|
|
19
|
-
//=> ?foo=bar
|
|
19
|
+
//=> '?foo=bar'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const parsed = queryString.parse(location.search);
|
|
22
22
|
console.log(parsed);
|
|
23
23
|
//=> {foo: 'bar'}
|
|
24
24
|
|
|
25
25
|
console.log(location.hash);
|
|
26
|
-
//=> #token=bada55cafe
|
|
26
|
+
//=> '#token=bada55cafe'
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const parsedHash = queryString.parse(location.hash);
|
|
29
29
|
console.log(parsedHash);
|
|
30
30
|
//=> {token: 'bada55cafe'}
|
|
31
31
|
|
|
@@ -35,35 +35,39 @@ parsed.ilike = 'pizza';
|
|
|
35
35
|
location.search = queryString.stringify(parsed);
|
|
36
36
|
|
|
37
37
|
console.log(location.search);
|
|
38
|
-
//=> ?foo=unicorn&ilike=pizza
|
|
38
|
+
//=> '?foo=unicorn&ilike=pizza'
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
## API
|
|
43
43
|
|
|
44
|
-
###
|
|
44
|
+
### .parse(*string*)
|
|
45
45
|
|
|
46
46
|
Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
|
|
47
47
|
|
|
48
|
-
###
|
|
48
|
+
### .stringify(*object*)
|
|
49
49
|
|
|
50
50
|
Stringify an object into a query string, sorting the keys.
|
|
51
51
|
|
|
52
|
+
### .extract(*string*)
|
|
53
|
+
|
|
54
|
+
Extract a query string from a URL that can be passed into `.parse()`.
|
|
55
|
+
|
|
52
56
|
|
|
53
57
|
## Nesting
|
|
54
58
|
|
|
55
|
-
This module intentionally doesn't support nesting as it's not
|
|
59
|
+
This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of [edge cases](https://github.com/visionmedia/node-querystring/issues).
|
|
56
60
|
|
|
57
61
|
You're much better off just converting the object to a JSON string:
|
|
58
62
|
|
|
59
63
|
```js
|
|
60
64
|
queryString.stringify({
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
foo: 'bar',
|
|
66
|
+
nested: JSON.stringify({
|
|
67
|
+
unicorn: 'cake'
|
|
68
|
+
})
|
|
65
69
|
});
|
|
66
|
-
//=> foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D
|
|
70
|
+
//=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
|
|
67
71
|
```
|
|
68
72
|
|
|
69
73
|
|