query-string 4.3.0 → 4.3.4

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 +15 -11
  2. package/package.json +1 -1
  3. package/readme.md +16 -0
package/index.js CHANGED
@@ -46,9 +46,9 @@ function parserForArrayFormat(opts) {
46
46
  switch (opts.arrayFormat) {
47
47
  case 'index':
48
48
  return function (key, value, accumulator) {
49
- result = /\[(\d*)]$/.exec(key);
49
+ result = /\[(\d*)\]$/.exec(key);
50
50
 
51
- key = key.replace(/\[\d*]$/, '');
51
+ key = key.replace(/\[\d*\]$/, '');
52
52
 
53
53
  if (!result) {
54
54
  accumulator[key] = value;
@@ -64,13 +64,15 @@ function parserForArrayFormat(opts) {
64
64
 
65
65
  case 'bracket':
66
66
  return function (key, value, accumulator) {
67
- result = /(\[])$/.exec(key);
67
+ result = /(\[\])$/.exec(key);
68
+ key = key.replace(/\[\]$/, '');
68
69
 
69
- key = key.replace(/\[]$/, '');
70
-
71
- if (!result || accumulator[key] === undefined) {
70
+ if (!result) {
72
71
  accumulator[key] = value;
73
72
  return;
73
+ } else if (accumulator[key] === undefined) {
74
+ accumulator[key] = [value];
75
+ return;
74
76
  }
75
77
 
76
78
  accumulator[key] = [].concat(accumulator[key], value);
@@ -96,11 +98,11 @@ function encode(value, opts) {
96
98
  return value;
97
99
  }
98
100
 
99
- function sorter(input) {
101
+ function keysSorter(input) {
100
102
  if (Array.isArray(input)) {
101
103
  return input.sort();
102
104
  } else if (typeof input === 'object') {
103
- return sorter(Object.keys(input)).sort(function (a, b) {
105
+ return keysSorter(Object.keys(input)).sort(function (a, b) {
104
106
  return Number(a) - Number(b);
105
107
  }).map(function (key) {
106
108
  return input[key];
@@ -148,10 +150,12 @@ exports.parse = function (str, opts) {
148
150
  });
149
151
 
150
152
  return Object.keys(ret).sort().reduce(function (result, key) {
151
- if (Boolean(ret[key]) && typeof ret[key] === 'object') {
152
- result[key] = sorter(ret[key]);
153
+ var val = ret[key];
154
+ if (Boolean(val) && typeof val === 'object' && !Array.isArray(val)) {
155
+ // Sort object keys, not values
156
+ result[key] = keysSorter(val);
153
157
  } else {
154
- result[key] = ret[key];
158
+ result[key] = val;
155
159
  }
156
160
 
157
161
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "4.3.0",
3
+ "version": "4.3.4",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
package/readme.md CHANGED
@@ -163,6 +163,22 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
163
163
  ```
164
164
 
165
165
 
166
+ ## Falsy values
167
+
168
+ Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
169
+
170
+ ```js
171
+ queryString.stringify({foo: false});
172
+ //=> 'foo=false'
173
+
174
+ queryString.stringify({foo: null});
175
+ //=> 'foo'
176
+
177
+ queryString.stringify({foo: undefined});
178
+ //=> ''
179
+ ```
180
+
181
+
166
182
  ## License
167
183
 
168
184
  MIT © [Sindre Sorhus](https://sindresorhus.com)