query-string 5.0.1 → 6.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 (3) hide show
  1. package/index.js +98 -86
  2. package/package.json +44 -43
  3. package/readme.md +48 -3
package/index.js CHANGED
@@ -1,52 +1,49 @@
1
1
  'use strict';
2
- var strictUriEncode = require('strict-uri-encode');
3
- var objectAssign = require('object-assign');
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(opts) {
7
- switch (opts.arrayFormat) {
5
+ function encoderForArrayFormat(options) {
6
+ switch (options.arrayFormat) {
8
7
  case 'index':
9
- return function (key, value, index) {
8
+ return (key, value, index) => {
10
9
  return value === null ? [
11
- encode(key, opts),
10
+ encode(key, options),
12
11
  '[',
13
12
  index,
14
13
  ']'
15
14
  ].join('') : [
16
- encode(key, opts),
15
+ encode(key, options),
17
16
  '[',
18
- encode(index, opts),
17
+ encode(index, options),
19
18
  ']=',
20
- encode(value, opts)
19
+ encode(value, options)
21
20
  ].join('');
22
21
  };
23
-
24
22
  case 'bracket':
25
- return function (key, value) {
26
- return value === null ? encode(key, opts) : [
27
- encode(key, opts),
23
+ return (key, value) => {
24
+ return value === null ? [encode(key, options), '[]'].join('') : [
25
+ encode(key, options),
28
26
  '[]=',
29
- encode(value, opts)
27
+ encode(value, options)
30
28
  ].join('');
31
29
  };
32
-
33
30
  default:
34
- return function (key, value) {
35
- return value === null ? encode(key, opts) : [
36
- encode(key, opts),
31
+ return (key, value) => {
32
+ return value === null ? encode(key, options) : [
33
+ encode(key, options),
37
34
  '=',
38
- encode(value, opts)
35
+ encode(value, options)
39
36
  ].join('');
40
37
  };
41
38
  }
42
39
  }
43
40
 
44
- function parserForArrayFormat(opts) {
45
- var result;
41
+ function parserForArrayFormat(options) {
42
+ let result;
46
43
 
47
- switch (opts.arrayFormat) {
44
+ switch (options.arrayFormat) {
48
45
  case 'index':
49
- return function (key, value, accumulator) {
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 function (key, value, accumulator) {
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
- } else if (accumulator[key] === undefined) {
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 function (key, value, accumulator) {
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, opts) {
95
- if (opts.encode) {
96
- return opts.strict ? strictUriEncode(value) : encodeURIComponent(value);
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,109 +107,116 @@ function encode(value, opts) {
102
107
  function keysSorter(input) {
103
108
  if (Array.isArray(input)) {
104
109
  return input.sort();
105
- } else if (typeof input === 'object') {
106
- return keysSorter(Object.keys(input)).sort(function (a, b) {
107
- return Number(a) - Number(b);
108
- }).map(function (key) {
109
- return input[key];
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
- exports.extract = function (str) {
117
- var queryStart = str.indexOf('?');
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
- };
126
+ return input.slice(queryStart + 1);
127
+ }
123
128
 
124
- exports.parse = function (str, opts) {
125
- opts = objectAssign({arrayFormat: 'none'}, opts);
129
+ function parse(input, options) {
130
+ options = Object.assign({decode: true, arrayFormat: 'none'}, options);
126
131
 
127
- var formatter = parserForArrayFormat(opts);
132
+ const formatter = parserForArrayFormat(options);
128
133
 
129
134
  // Create an object with no prototype
130
- // https://github.com/sindresorhus/query-string/issues/47
131
- var ret = Object.create(null);
135
+ const ret = Object.create(null);
132
136
 
133
- if (typeof str !== 'string') {
137
+ if (typeof input !== 'string') {
134
138
  return ret;
135
139
  }
136
140
 
137
- str = str.trim().replace(/^[?#&]/, '');
141
+ input = input.trim().replace(/^[?#&]/, '');
138
142
 
139
- if (!str) {
143
+ if (!input) {
140
144
  return ret;
141
145
  }
142
146
 
143
- str.split('&').forEach(function (param) {
144
- var parts = param.replace(/\+/g, ' ').split('=');
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;
147
+ for (const param of input.split('&')) {
148
+ let [key, value] = param.replace(/\+/g, ' ').split('=');
149
149
 
150
- // missing `=` should be `null`:
150
+ // Missing `=` should be `null`:
151
151
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
152
- val = val === undefined ? null : decodeComponent(val);
152
+ value = value === undefined ? null : decode(value, options);
153
153
 
154
- formatter(decodeComponent(key), val, ret);
155
- });
154
+ formatter(decode(key, options), value, ret);
155
+ }
156
156
 
157
- return Object.keys(ret).sort().reduce(function (result, key) {
158
- var val = ret[key];
159
- if (Boolean(val) && typeof val === 'object' && !Array.isArray(val)) {
157
+ return Object.keys(ret).sort().reduce((result, key) => {
158
+ const value = ret[key];
159
+ if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
160
160
  // Sort object keys, not values
161
- result[key] = keysSorter(val);
161
+ result[key] = keysSorter(value);
162
162
  } else {
163
- result[key] = val;
163
+ result[key] = value;
164
164
  }
165
165
 
166
166
  return result;
167
167
  }, Object.create(null));
168
- };
168
+ }
169
+
170
+ exports.extract = extract;
171
+ exports.parse = parse;
169
172
 
170
- exports.stringify = function (obj, opts) {
171
- var defaults = {
173
+ exports.stringify = (obj, options) => {
174
+ const defaults = {
172
175
  encode: true,
173
176
  strict: true,
174
177
  arrayFormat: 'none'
175
178
  };
176
179
 
177
- opts = objectAssign(defaults, opts);
180
+ options = Object.assign(defaults, options);
178
181
 
179
- var formatter = encoderForArrayFormat(opts);
182
+ if (options.sort === false) {
183
+ options.sort = () => {};
184
+ }
185
+
186
+ const formatter = encoderForArrayFormat(options);
180
187
 
181
- return obj ? Object.keys(obj).sort().map(function (key) {
182
- var val = obj[key];
188
+ return obj ? Object.keys(obj).sort(options.sort).map(key => {
189
+ const value = obj[key];
183
190
 
184
- if (val === undefined) {
191
+ if (value === undefined) {
185
192
  return '';
186
193
  }
187
194
 
188
- if (val === null) {
189
- return encode(key, opts);
195
+ if (value === null) {
196
+ return encode(key, options);
190
197
  }
191
198
 
192
- if (Array.isArray(val)) {
193
- var result = [];
199
+ if (Array.isArray(value)) {
200
+ const result = [];
194
201
 
195
- val.slice().forEach(function (val2) {
196
- if (val2 === undefined) {
197
- return;
202
+ for (const value2 of value.slice()) {
203
+ if (value2 === undefined) {
204
+ continue;
198
205
  }
199
206
 
200
- result.push(formatter(key, val2, result.length));
201
- });
207
+ result.push(formatter(key, value2, result.length));
208
+ }
202
209
 
203
210
  return result.join('&');
204
211
  }
205
212
 
206
- return encode(key, opts) + '=' + encode(val, opts);
207
- }).filter(function (x) {
208
- return x.length > 0;
209
- }).join('&') : '';
213
+ return encode(key, options) + '=' + encode(value, options);
214
+ }).filter(x => x.length > 0).join('&') : '';
215
+ };
216
+
217
+ exports.parseUrl = (input, options) => {
218
+ return {
219
+ url: input.split('?')[0] || '',
220
+ query: parse(extract(input), options)
221
+ };
210
222
  };
package/package.json CHANGED
@@ -1,45 +1,46 @@
1
1
  {
2
- "name": "query-string",
3
- "version": "5.0.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
- }
2
+ "name": "query-string",
3
+ "version": "6.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": ">=6"
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
+ "parse",
31
+ "stringify",
32
+ "encode",
33
+ "decode",
34
+ "searchparams"
35
+ ],
36
+ "dependencies": {
37
+ "decode-uri-component": "^0.2.0",
38
+ "strict-uri-encode": "^2.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "ava": "*",
42
+ "deep-equal": "^1.0.1",
43
+ "fast-check": "^1.0.1",
44
+ "xo": "*"
45
+ }
45
46
  }
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
 
@@ -15,6 +15,12 @@
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, use version 5: `npm install query-string@5`.
19
+
20
+ <a href="https://www.patreon.com/sindresorhus">
21
+ <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
22
+ </a>
23
+
18
24
 
19
25
  ## Usage
20
26
 
@@ -56,7 +62,12 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
56
62
 
57
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`.
58
64
 
59
- URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
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).
60
71
 
61
72
  #### arrayFormat
62
73
 
@@ -133,10 +144,44 @@ queryString.stringify({foo: [1,2,3]});
133
144
  // => foo=1&foo=2&foo=3
134
145
  ```
135
146
 
147
+ #### sort
148
+
149
+ Type: `Function` `boolean`
150
+
151
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
152
+
153
+ ```js
154
+ const order = ['c', 'a', 'b'];
155
+ queryString.stringify({ a: 1, b: 2, c: 3}, {
156
+ sort: (m, n) => order.indexOf(m) >= order.indexOf(n)
157
+ });
158
+ // => 'c=3&a=1&b=2'
159
+ ```
160
+
161
+ ```js
162
+ queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false});
163
+ // => 'c=3&a=1&b=2'
164
+ ```
165
+
166
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
167
+
136
168
  ### .extract(*string*)
137
169
 
138
170
  Extract a query string from a URL that can be passed into `.parse()`.
139
171
 
172
+ ### .parseUrl(*string*, *[options]*)
173
+
174
+ Extract the URL and the query string as an object.
175
+
176
+ The `options` are the same as for `.parse()`.
177
+
178
+ Returns an object with a `url` and `query` property.
179
+
180
+ ```js
181
+ queryString.parseUrl('https://foo.bar?foo=bar');
182
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
183
+ ```
184
+
140
185
 
141
186
  ## Nesting
142
187