query-string 4.3.3 → 5.1.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.
Files changed (4) hide show
  1. package/index.js +25 -7
  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) {
@@ -65,10 +66,12 @@ function parserForArrayFormat(opts) {
65
66
  case 'bracket':
66
67
  return function (key, value, accumulator) {
67
68
  result = /(\[\])$/.exec(key);
68
-
69
69
  key = key.replace(/\[\]$/, '');
70
70
 
71
- if (!result || accumulator[key] === undefined) {
71
+ if (!result) {
72
+ accumulator[key] = value;
73
+ return;
74
+ } else if (accumulator[key] === undefined) {
72
75
  accumulator[key] = [value];
73
76
  return;
74
77
  }
@@ -111,7 +114,11 @@ function keysSorter(input) {
111
114
  }
112
115
 
113
116
  exports.extract = function (str) {
114
- return str.split('?')[1] || '';
117
+ var queryStart = str.indexOf('?');
118
+ if (queryStart === -1) {
119
+ return '';
120
+ }
121
+ return str.slice(queryStart + 1);
115
122
  };
116
123
 
117
124
  exports.parse = function (str, opts) {
@@ -127,7 +134,7 @@ exports.parse = function (str, opts) {
127
134
  return ret;
128
135
  }
129
136
 
130
- str = str.trim().replace(/^(\?|#|&)/, '');
137
+ str = str.trim().replace(/^[?#&]/, '');
131
138
 
132
139
  if (!str) {
133
140
  return ret;
@@ -142,9 +149,9 @@ exports.parse = function (str, opts) {
142
149
 
143
150
  // missing `=` should be `null`:
144
151
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
145
- val = val === undefined ? null : decodeURIComponent(val);
152
+ val = val === undefined ? null : decodeComponent(val);
146
153
 
147
- formatter(decodeURIComponent(key), val, ret);
154
+ formatter(decodeComponent(key), val, ret);
148
155
  });
149
156
 
150
157
  return Object.keys(ret).sort().reduce(function (result, key) {
@@ -169,9 +176,13 @@ exports.stringify = function (obj, opts) {
169
176
 
170
177
  opts = objectAssign(defaults, opts);
171
178
 
179
+ if (opts.sort === false) {
180
+ opts.sort = function () {};
181
+ }
182
+
172
183
  var formatter = encoderForArrayFormat(opts);
173
184
 
174
- return obj ? Object.keys(obj).sort().map(function (key) {
185
+ return obj ? Object.keys(obj).sort(opts.sort).map(function (key) {
175
186
  var val = obj[key];
176
187
 
177
188
  if (val === undefined) {
@@ -201,3 +212,10 @@ exports.stringify = function (obj, opts) {
201
212
  return x.length > 0;
202
213
  }).join('&') : '';
203
214
  };
215
+
216
+ exports.parseUrl = function (str, opts) {
217
+ return {
218
+ url: str.split('?')[0] || '',
219
+ query: this.parse(this.extract(str), opts)
220
+ };
221
+ };
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.3",
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.0",
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