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.
Files changed (3) hide show
  1. package/index.js +103 -93
  2. package/package.json +9 -8
  3. package/readme.md +8 -1
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,120 +107,125 @@ 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
- };
123
126
 
124
- exports.parse = function (str, opts) {
125
- opts = objectAssign({arrayFormat: 'none'}, opts);
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
- var formatter = parserForArrayFormat(opts);
133
+ const formatter = parserForArrayFormat(options);
128
134
 
129
135
  // Create an object with no prototype
130
- // https://github.com/sindresorhus/query-string/issues/47
131
- var ret = Object.create(null);
136
+ const ret = Object.create(null);
132
137
 
133
- if (typeof str !== 'string') {
138
+ if (typeof input !== 'string') {
134
139
  return ret;
135
140
  }
136
141
 
137
- str = str.trim().replace(/^[?#&]/, '');
142
+ input = input.trim().replace(/^[?#&]/, '');
138
143
 
139
- if (!str) {
144
+ if (!input) {
140
145
  return ret;
141
146
  }
142
147
 
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;
148
+ for (const param of input.split('&')) {
149
+ let [key, value] = param.replace(/\+/g, ' ').split('=');
149
150
 
150
- // missing `=` should be `null`:
151
+ // Missing `=` should be `null`:
151
152
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
152
- val = val === undefined ? null : decodeComponent(val);
153
+ value = value === undefined ? null : decode(value, options);
153
154
 
154
- formatter(decodeComponent(key), val, ret);
155
- });
155
+ formatter(decode(key, options), value, ret);
156
+ }
156
157
 
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)) {
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(val);
162
+ result[key] = keysSorter(value);
162
163
  } else {
163
- result[key] = val;
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
- exports.stringify = function (obj, opts) {
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
- opts = objectAssign(defaults, opts);
185
+ const formatter = encoderForArrayFormat(options);
186
+ const keys = Object.keys(obj);
178
187
 
179
- if (opts.sort === false) {
180
- opts.sort = function () {};
188
+ if (options.sort !== false) {
189
+ keys.sort(options.sort);
181
190
  }
182
191
 
183
- var formatter = encoderForArrayFormat(opts);
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 (val === undefined) {
195
+ if (value === undefined) {
189
196
  return '';
190
197
  }
191
198
 
192
- if (val === null) {
193
- return encode(key, opts);
199
+ if (value === null) {
200
+ return encode(key, options);
194
201
  }
195
202
 
196
- if (Array.isArray(val)) {
197
- var result = [];
203
+ if (Array.isArray(value)) {
204
+ const result = [];
198
205
 
199
- val.slice().forEach(function (val2) {
200
- if (val2 === undefined) {
201
- return;
206
+ for (const value2 of value.slice()) {
207
+ if (value2 === undefined) {
208
+ continue;
202
209
  }
203
210
 
204
- result.push(formatter(key, val2, result.length));
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, opts) + '=' + encode(val, opts);
211
- }).filter(function (x) {
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 = function (str, opts) {
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: str.split('?')[0] || '',
219
- query: this.parse(this.extract(str), opts)
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": "5.1.0",
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": ">=0.10.0"
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
- "object-assign": "^4.1.0",
39
- "strict-uri-encode": "^1.0.0"
38
+ "strict-uri-encode": "^2.0.0"
40
39
  },
41
40
  "devDependencies": {
42
- "ava": "^0.17.0",
43
- "xo": "^0.16.0"
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
- 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).
64
71
 
65
72
  #### arrayFormat
66
73