query-string 4.3.4 → 5.1.1

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.
Files changed (4) hide show
  1. package/index.js +28 -9
  2. package/license +4 -16
  3. package/package.json +43 -42
  4. package/readme.md +43 -3
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
  var strictUriEncode = require('strict-uri-encode');
3
3
  var objectAssign = require('object-assign');
4
+ var decodeComponent = require('decode-uri-component');
4
5
 
5
6
  function encoderForArrayFormat(opts) {
6
7
  switch (opts.arrayFormat) {
@@ -112,11 +113,15 @@ function keysSorter(input) {
112
113
  return input;
113
114
  }
114
115
 
115
- exports.extract = function (str) {
116
- return str.split('?')[1] || '';
117
- };
116
+ function extract(str) {
117
+ var queryStart = str.indexOf('?');
118
+ if (queryStart === -1) {
119
+ return '';
120
+ }
121
+ return str.slice(queryStart + 1);
122
+ }
118
123
 
119
- exports.parse = function (str, opts) {
124
+ function parse(str, opts) {
120
125
  opts = objectAssign({arrayFormat: 'none'}, opts);
121
126
 
122
127
  var formatter = parserForArrayFormat(opts);
@@ -129,7 +134,7 @@ exports.parse = function (str, opts) {
129
134
  return ret;
130
135
  }
131
136
 
132
- str = str.trim().replace(/^(\?|#|&)/, '');
137
+ str = str.trim().replace(/^[?#&]/, '');
133
138
 
134
139
  if (!str) {
135
140
  return ret;
@@ -144,9 +149,9 @@ exports.parse = function (str, opts) {
144
149
 
145
150
  // missing `=` should be `null`:
146
151
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
147
- val = val === undefined ? null : decodeURIComponent(val);
152
+ val = val === undefined ? null : decodeComponent(val);
148
153
 
149
- formatter(decodeURIComponent(key), val, ret);
154
+ formatter(decodeComponent(key), val, ret);
150
155
  });
151
156
 
152
157
  return Object.keys(ret).sort().reduce(function (result, key) {
@@ -160,7 +165,10 @@ exports.parse = function (str, opts) {
160
165
 
161
166
  return result;
162
167
  }, Object.create(null));
163
- };
168
+ }
169
+
170
+ exports.extract = extract;
171
+ exports.parse = parse;
164
172
 
165
173
  exports.stringify = function (obj, opts) {
166
174
  var defaults = {
@@ -171,9 +179,13 @@ exports.stringify = function (obj, opts) {
171
179
 
172
180
  opts = objectAssign(defaults, opts);
173
181
 
182
+ if (opts.sort === false) {
183
+ opts.sort = function () {};
184
+ }
185
+
174
186
  var formatter = encoderForArrayFormat(opts);
175
187
 
176
- return obj ? Object.keys(obj).sort().map(function (key) {
188
+ return obj ? Object.keys(obj).sort(opts.sort).map(function (key) {
177
189
  var val = obj[key];
178
190
 
179
191
  if (val === undefined) {
@@ -203,3 +215,10 @@ exports.stringify = function (obj, opts) {
203
215
  return x.length > 0;
204
216
  }).join('&') : '';
205
217
  };
218
+
219
+ exports.parseUrl = function (str, opts) {
220
+ return {
221
+ url: str.split('?')[0] || '',
222
+ query: parse(extract(str), opts)
223
+ };
224
+ };
package/license CHANGED
@@ -1,21 +1,9 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
3
  Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
6
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
8
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,44 +1,45 @@
1
1
  {
2
- "name": "query-string",
3
- "version": "4.3.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
- ],
21
- "keywords": [
22
- "browser",
23
- "querystring",
24
- "query",
25
- "string",
26
- "qs",
27
- "param",
28
- "parameter",
29
- "url",
30
- "uri",
31
- "parse",
32
- "stringify",
33
- "encode",
34
- "decode"
35
- ],
36
- "dependencies": {
37
- "object-assign": "^4.1.0",
38
- "strict-uri-encode": "^1.0.0"
39
- },
40
- "devDependencies": {
41
- "ava": "^0.17.0",
42
- "xo": "^0.16.0"
43
- }
2
+ "name": "query-string",
3
+ "version": "5.1.1",
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
+ ],
21
+ "keywords": [
22
+ "browser",
23
+ "querystring",
24
+ "query",
25
+ "string",
26
+ "qs",
27
+ "param",
28
+ "parameter",
29
+ "url",
30
+ "uri",
31
+ "parse",
32
+ "stringify",
33
+ "encode",
34
+ "decode"
35
+ ],
36
+ "dependencies": {
37
+ "decode-uri-component": "^0.2.0",
38
+ "object-assign": "^4.1.0",
39
+ "strict-uri-encode": "^1.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "ava": "^0.17.0",
43
+ "xo": "^0.16.0"
44
+ }
44
45
  }
package/readme.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string)
2
2
 
3
- > Parse and stringify URL [query strings](http://en.wikipedia.org/wiki/Query_string)
3
+ > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
4
4
 
5
5
  ---
6
6
 
7
- <p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React course</a>.</p>
7
+ <p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.<br>Also check out his <a href="https://LearnNode.com/friend/AWESOME">Node.js</a>, <a href="https://ReactForBeginners.com/friend/AWESOME">React</a>, <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> courses.</p>
8
8
 
9
9
  ---
10
10
 
@@ -12,9 +12,13 @@
12
12
  ## Install
13
13
 
14
14
  ```
15
- $ npm install --save query-string
15
+ $ npm install query-string
16
16
  ```
17
17
 
18
+ <a href="https://www.patreon.com/sindresorhus">
19
+ <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
20
+ </a>
21
+
18
22
 
19
23
  ## Usage
20
24
 
@@ -56,6 +60,8 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
56
60
 
57
61
  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`.
58
62
 
63
+ URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
64
+
59
65
  #### arrayFormat
60
66
 
61
67
  Type: `string`<br>
@@ -131,10 +137,44 @@ queryString.stringify({foo: [1,2,3]});
131
137
  // => foo=1&foo=2&foo=3
132
138
  ```
133
139
 
140
+ #### sort
141
+
142
+ Type: `Function` `boolean`
143
+
144
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
145
+
146
+ ```js
147
+ const order = ['c', 'a', 'b'];
148
+ queryString.stringify({ a: 1, b: 2, c: 3}, {
149
+ sort: (m, n) => order.indexOf(m) >= order.indexOf(n)
150
+ });
151
+ // => 'c=3&a=1&b=2'
152
+ ```
153
+
154
+ ```js
155
+ queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false});
156
+ // => 'c=3&a=1&b=2'
157
+ ```
158
+
159
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
160
+
134
161
  ### .extract(*string*)
135
162
 
136
163
  Extract a query string from a URL that can be passed into `.parse()`.
137
164
 
165
+ ### .parseUrl(*string*, *[options]*)
166
+
167
+ Extract the URL and the query string as an object.
168
+
169
+ The `options` are the same as for `.parse()`.
170
+
171
+ Returns an object with a `url` and `query` property.
172
+
173
+ ```js
174
+ queryString.parseUrl('https://foo.bar?foo=bar');
175
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
176
+ ```
177
+
138
178
 
139
179
  ## Nesting
140
180