qs 6.2.4 → 6.3.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.
package/dist/qs.js CHANGED
@@ -1,31 +1,53 @@
1
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Qs = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
1
+ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Qs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
2
  'use strict';
3
3
 
4
- var Stringify = require('./stringify');
5
- var Parse = require('./parse');
4
+ var replace = String.prototype.replace;
5
+ var percentTwenties = /%20/g;
6
6
 
7
7
  module.exports = {
8
- stringify: Stringify,
9
- parse: Parse
8
+ 'default': 'RFC3986',
9
+ formatters: {
10
+ RFC1738: function (value) {
11
+ return replace.call(value, percentTwenties, '+');
12
+ },
13
+ RFC3986: function (value) {
14
+ return value;
15
+ }
16
+ },
17
+ RFC1738: 'RFC1738',
18
+ RFC3986: 'RFC3986'
10
19
  };
11
20
 
12
- },{"./parse":2,"./stringify":3}],2:[function(require,module,exports){
21
+ },{}],2:[function(require,module,exports){
13
22
  'use strict';
14
23
 
15
- var Utils = require('./utils');
24
+ var stringify = require('./stringify');
25
+ var parse = require('./parse');
26
+ var formats = require('./formats');
27
+
28
+ module.exports = {
29
+ formats: formats,
30
+ parse: parse,
31
+ stringify: stringify
32
+ };
33
+
34
+ },{"./formats":1,"./parse":3,"./stringify":4}],3:[function(require,module,exports){
35
+ 'use strict';
36
+
37
+ var utils = require('./utils');
16
38
 
17
39
  var has = Object.prototype.hasOwnProperty;
18
40
 
19
41
  var defaults = {
42
+ allowDots: false,
43
+ allowPrototypes: false,
44
+ arrayLimit: 20,
45
+ decoder: utils.decode,
20
46
  delimiter: '&',
21
47
  depth: 5,
22
- arrayLimit: 20,
23
48
  parameterLimit: 1000,
24
- strictNullHandling: false,
25
49
  plainObjects: false,
26
- allowPrototypes: false,
27
- allowDots: false,
28
- decoder: Utils.decode
50
+ strictNullHandling: false
29
51
  };
30
52
 
31
53
  var parseValues = function parseValues(str, options) {
@@ -62,25 +84,23 @@ var parseObject = function parseObject(chain, val, options) {
62
84
  var root = chain.shift();
63
85
 
64
86
  var obj;
65
- if (root === '[]' && options.parseArrays) {
87
+ if (root === '[]') {
66
88
  obj = [];
67
89
  obj = obj.concat(parseObject(chain, val, options));
68
90
  } else {
69
91
  obj = options.plainObjects ? Object.create(null) : {};
70
- var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
92
+ var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
71
93
  var index = parseInt(cleanRoot, 10);
72
- if (!options.parseArrays && cleanRoot === '') {
73
- obj = { 0: val };
74
- } else if (
75
- !isNaN(index)
76
- && root !== cleanRoot
77
- && String(index) === cleanRoot
78
- && index >= 0
79
- && (options.parseArrays && index <= options.arrayLimit)
94
+ if (
95
+ !isNaN(index) &&
96
+ root !== cleanRoot &&
97
+ String(index) === cleanRoot &&
98
+ index >= 0 &&
99
+ (options.parseArrays && index <= options.arrayLimit)
80
100
  ) {
81
101
  obj = [];
82
102
  obj[index] = parseObject(chain, val, options);
83
- } else if (cleanRoot !== '__proto__') {
103
+ } else {
84
104
  obj[cleanRoot] = parseObject(chain, val, options);
85
105
  }
86
106
  }
@@ -94,30 +114,30 @@ var parseKeys = function parseKeys(givenKey, val, options) {
94
114
  }
95
115
 
96
116
  // Transform dot notation to bracket notation
97
- var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
117
+ var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
98
118
 
99
119
  // The regex chunks
100
120
 
101
- var brackets = /(\[[^[\]]*])/;
102
- var child = /(\[[^[\]]*])/g;
121
+ var parent = /^([^\[\]]*)/;
122
+ var child = /(\[[^\[\]]*\])/g;
103
123
 
104
124
  // Get the parent
105
125
 
106
- var segment = brackets.exec(key);
107
- var parent = segment ? key.slice(0, segment.index) : key;
126
+ var segment = parent.exec(key);
108
127
 
109
128
  // Stash the parent if it exists
110
129
 
111
130
  var keys = [];
112
- if (parent) {
113
- // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
114
- if (!options.plainObjects && has.call(Object.prototype, parent)) {
131
+ if (segment[1]) {
132
+ // If we aren't using plain objects, optionally prefix keys
133
+ // that would overwrite object prototype properties
134
+ if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
115
135
  if (!options.allowPrototypes) {
116
136
  return;
117
137
  }
118
138
  }
119
139
 
120
- keys.push(parent);
140
+ keys.push(segment[1]);
121
141
  }
122
142
 
123
143
  // Loop through children appending to the array until we hit depth
@@ -125,9 +145,9 @@ var parseKeys = function parseKeys(givenKey, val, options) {
125
145
  var i = 0;
126
146
  while ((segment = child.exec(key)) !== null && i < options.depth) {
127
147
  i += 1;
128
- if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
148
+ if (!options.plainObjects && has.call(Object.prototype, segment[1].replace(/\[|\]/g, ''))) {
129
149
  if (!options.allowPrototypes) {
130
- return;
150
+ continue;
131
151
  }
132
152
  }
133
153
  keys.push(segment[1]);
@@ -149,7 +169,7 @@ module.exports = function (str, opts) {
149
169
  throw new TypeError('Decoder has to be a function.');
150
170
  }
151
171
 
152
- options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
172
+ options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
153
173
  options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
154
174
  options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
155
175
  options.parseArrays = options.parseArrays !== false;
@@ -173,16 +193,17 @@ module.exports = function (str, opts) {
173
193
  for (var i = 0; i < keys.length; ++i) {
174
194
  var key = keys[i];
175
195
  var newObj = parseKeys(key, tempObj[key], options);
176
- obj = Utils.merge(obj, newObj, options);
196
+ obj = utils.merge(obj, newObj, options);
177
197
  }
178
198
 
179
- return Utils.compact(obj);
199
+ return utils.compact(obj);
180
200
  };
181
201
 
182
- },{"./utils":4}],3:[function(require,module,exports){
202
+ },{"./utils":5}],4:[function(require,module,exports){
183
203
  'use strict';
184
204
 
185
- var Utils = require('./utils');
205
+ var utils = require('./utils');
206
+ var formats = require('./formats');
186
207
 
187
208
  var arrayPrefixGenerators = {
188
209
  brackets: function brackets(prefix) {
@@ -196,31 +217,25 @@ var arrayPrefixGenerators = {
196
217
  }
197
218
  };
198
219
 
220
+ var toISO = Date.prototype.toISOString;
221
+
199
222
  var defaults = {
200
223
  delimiter: '&',
201
- strictNullHandling: false,
202
- skipNulls: false,
203
224
  encode: true,
204
- encoder: Utils.encode
225
+ encoder: utils.encode,
226
+ serializeDate: function serializeDate(date) {
227
+ return toISO.call(date);
228
+ },
229
+ skipNulls: false,
230
+ strictNullHandling: false
205
231
  };
206
232
 
207
- var isArray = Array.isArray;
208
- var stringify = function stringify(
209
- object,
210
- prefix,
211
- generateArrayPrefix,
212
- strictNullHandling,
213
- skipNulls,
214
- encoder,
215
- filter,
216
- sort,
217
- allowDots
218
- ) {
233
+ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots, serializeDate, formatter) {
219
234
  var obj = object;
220
235
  if (typeof filter === 'function') {
221
236
  obj = filter(prefix, obj);
222
237
  } else if (obj instanceof Date) {
223
- obj = obj.toISOString();
238
+ obj = serializeDate(obj);
224
239
  } else if (obj === null) {
225
240
  if (strictNullHandling) {
226
241
  return encoder ? encoder(prefix) : prefix;
@@ -229,11 +244,11 @@ var stringify = function stringify(
229
244
  obj = '';
230
245
  }
231
246
 
232
- if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
247
+ if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) {
233
248
  if (encoder) {
234
- return [encoder(prefix) + '=' + encoder(obj)];
249
+ return [formatter(encoder(prefix)) + '=' + formatter(encoder(obj))];
235
250
  }
236
- return [prefix + '=' + String(obj)];
251
+ return [formatter(prefix) + '=' + formatter(String(obj))];
237
252
  }
238
253
 
239
254
  var values = [];
@@ -243,7 +258,7 @@ var stringify = function stringify(
243
258
  }
244
259
 
245
260
  var objKeys;
246
- if (isArray(filter)) {
261
+ if (Array.isArray(filter)) {
247
262
  objKeys = filter;
248
263
  } else {
249
264
  var keys = Object.keys(obj);
@@ -257,7 +272,7 @@ var stringify = function stringify(
257
272
  continue;
258
273
  }
259
274
 
260
- if (isArray(obj)) {
275
+ if (Array.isArray(obj)) {
261
276
  values = values.concat(stringify(
262
277
  obj[key],
263
278
  generateArrayPrefix(prefix, key),
@@ -267,7 +282,9 @@ var stringify = function stringify(
267
282
  encoder,
268
283
  filter,
269
284
  sort,
270
- allowDots
285
+ allowDots,
286
+ serializeDate,
287
+ formatter
271
288
  ));
272
289
  } else {
273
290
  values = values.concat(stringify(
@@ -279,7 +296,9 @@ var stringify = function stringify(
279
296
  encoder,
280
297
  filter,
281
298
  sort,
282
- allowDots
299
+ allowDots,
300
+ serializeDate,
301
+ formatter
283
302
  ));
284
303
  }
285
304
  }
@@ -294,22 +313,29 @@ module.exports = function (object, opts) {
294
313
  var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
295
314
  var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
296
315
  var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
297
- var encoder = encode ? typeof options.encoder === 'function' ? options.encoder : defaults.encoder : null;
316
+ var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
298
317
  var sort = typeof options.sort === 'function' ? options.sort : null;
299
318
  var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
319
+ var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate;
320
+ if (typeof options.format === 'undefined') {
321
+ options.format = formats.default;
322
+ } else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) {
323
+ throw new TypeError('Unknown format option provided.');
324
+ }
325
+ var formatter = formats.formatters[options.format];
300
326
  var objKeys;
301
327
  var filter;
302
328
 
303
- if (options.encoder !== null && typeof options.encoder !== 'undefined' && typeof options.encoder !== 'function') {
329
+ if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
304
330
  throw new TypeError('Encoder has to be a function.');
305
331
  }
306
332
 
307
333
  if (typeof options.filter === 'function') {
308
334
  filter = options.filter;
309
335
  obj = filter('', obj);
310
- } else if (isArray(options.filter)) {
311
- objKeys = options.filter;
336
+ } else if (Array.isArray(options.filter)) {
312
337
  filter = options.filter;
338
+ objKeys = filter;
313
339
  }
314
340
 
315
341
  var keys = [];
@@ -353,27 +379,29 @@ module.exports = function (object, opts) {
353
379
  encoder,
354
380
  filter,
355
381
  sort,
356
- allowDots
382
+ allowDots,
383
+ serializeDate,
384
+ formatter
357
385
  ));
358
386
  }
359
387
 
360
388
  return keys.join(delimiter);
361
389
  };
362
390
 
363
- },{"./utils":4}],4:[function(require,module,exports){
391
+ },{"./formats":1,"./utils":5}],5:[function(require,module,exports){
364
392
  'use strict';
365
393
 
394
+ var has = Object.prototype.hasOwnProperty;
395
+
366
396
  var hexTable = (function () {
367
- var array = new Array(256);
397
+ var array = [];
368
398
  for (var i = 0; i < 256; ++i) {
369
- array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
399
+ array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
370
400
  }
371
401
 
372
402
  return array;
373
403
  }());
374
404
 
375
- var has = Object.prototype.hasOwnProperty;
376
-
377
405
  exports.arrayToObject = function (source, options) {
378
406
  var obj = options && options.plainObjects ? Object.create(null) : {};
379
407
  for (var i = 0; i < source.length; ++i) {
@@ -385,32 +413,16 @@ exports.arrayToObject = function (source, options) {
385
413
  return obj;
386
414
  };
387
415
 
388
- var isArray = Array.isArray;
389
-
390
- var arrayToObject = function arrayToObject(source, options) {
391
- var obj = options && options.plainObjects ? Object.create(null) : {};
392
- for (var i = 0; i < source.length; ++i) {
393
- if (typeof source[i] !== 'undefined') {
394
- obj[i] = source[i];
395
- }
396
- }
397
-
398
- return obj;
399
- };
400
-
401
- exports.merge = function merge(target, source, options) {
402
- /* eslint no-param-reassign: 0 */
416
+ exports.merge = function (target, source, options) {
403
417
  if (!source) {
404
418
  return target;
405
419
  }
406
420
 
407
421
  if (typeof source !== 'object') {
408
- if (isArray(target)) {
422
+ if (Array.isArray(target)) {
409
423
  target.push(source);
410
- } else if (target && typeof target === 'object') {
411
- if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
412
- target[source] = true;
413
- }
424
+ } else if (typeof target === 'object') {
425
+ target[source] = true;
414
426
  } else {
415
427
  return [target, source];
416
428
  }
@@ -418,21 +430,20 @@ exports.merge = function merge(target, source, options) {
418
430
  return target;
419
431
  }
420
432
 
421
- if (!target || typeof target !== 'object') {
433
+ if (typeof target !== 'object') {
422
434
  return [target].concat(source);
423
435
  }
424
436
 
425
437
  var mergeTarget = target;
426
- if (isArray(target) && !isArray(source)) {
427
- mergeTarget = arrayToObject(target, options);
438
+ if (Array.isArray(target) && !Array.isArray(source)) {
439
+ mergeTarget = exports.arrayToObject(target, options);
428
440
  }
429
441
 
430
- if (isArray(target) && isArray(source)) {
442
+ if (Array.isArray(target) && Array.isArray(source)) {
431
443
  source.forEach(function (item, i) {
432
444
  if (has.call(target, i)) {
433
- var targetItem = target[i];
434
- if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
435
- target[i] = merge(targetItem, item, options);
445
+ if (target[i] && typeof target[i] === 'object') {
446
+ target[i] = exports.merge(target[i], item, options);
436
447
  } else {
437
448
  target.push(item);
438
449
  }
@@ -446,8 +457,8 @@ exports.merge = function merge(target, source, options) {
446
457
  return Object.keys(source).reduce(function (acc, key) {
447
458
  var value = source[key];
448
459
 
449
- if (has.call(acc, key)) {
450
- acc[key] = merge(acc[key], value, options);
460
+ if (Object.prototype.hasOwnProperty.call(acc, key)) {
461
+ acc[key] = exports.merge(acc[key], value, options);
451
462
  } else {
452
463
  acc[key] = value;
453
464
  }
@@ -477,13 +488,13 @@ exports.encode = function (str) {
477
488
  var c = string.charCodeAt(i);
478
489
 
479
490
  if (
480
- c === 0x2D // -
481
- || c === 0x2E // .
482
- || c === 0x5F // _
483
- || c === 0x7E // ~
484
- || (c >= 0x30 && c <= 0x39) // 0-9
485
- || (c >= 0x41 && c <= 0x5A) // a-z
486
- || (c >= 0x61 && c <= 0x7A) // A-Z
491
+ c === 0x2D || // -
492
+ c === 0x2E || // .
493
+ c === 0x5F || // _
494
+ c === 0x7E || // ~
495
+ (c >= 0x30 && c <= 0x39) || // 0-9
496
+ (c >= 0x41 && c <= 0x5A) || // a-z
497
+ (c >= 0x61 && c <= 0x7A) // A-Z
487
498
  ) {
488
499
  out += string.charAt(i);
489
500
  continue;
@@ -506,11 +517,7 @@ exports.encode = function (str) {
506
517
 
507
518
  i += 1;
508
519
  c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
509
- /* eslint operator-linebreak: [2, "before"] */
510
- out += hexTable[0xF0 | (c >> 18)]
511
- + hexTable[0x80 | ((c >> 12) & 0x3F)]
512
- + hexTable[0x80 | ((c >> 6) & 0x3F)]
513
- + hexTable[0x80 | (c & 0x3F)];
520
+ out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)];
514
521
  }
515
522
 
516
523
  return out;
@@ -529,7 +536,7 @@ exports.compact = function (obj, references) {
529
536
 
530
537
  refs.push(obj);
531
538
 
532
- if (isArray(obj)) {
539
+ if (Array.isArray(obj)) {
533
540
  var compacted = [];
534
541
 
535
542
  for (var i = 0; i < obj.length; ++i) {
@@ -544,10 +551,9 @@ exports.compact = function (obj, references) {
544
551
  }
545
552
 
546
553
  var keys = Object.keys(obj);
547
- for (var j = 0; j < keys.length; ++j) {
548
- var key = keys[j];
554
+ keys.forEach(function (key) {
549
555
  obj[key] = exports.compact(obj[key], refs);
550
- }
556
+ });
551
557
 
552
558
  return obj;
553
559
  };
@@ -564,5 +570,5 @@ exports.isBuffer = function (obj) {
564
570
  return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
565
571
  };
566
572
 
567
- },{}]},{},[1])(1)
568
- });
573
+ },{}]},{},[2])(2)
574
+ });
package/lib/formats.js ADDED
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var replace = String.prototype.replace;
4
+ var percentTwenties = /%20/g;
5
+
6
+ module.exports = {
7
+ 'default': 'RFC3986',
8
+ formatters: {
9
+ RFC1738: function (value) {
10
+ return replace.call(value, percentTwenties, '+');
11
+ },
12
+ RFC3986: function (value) {
13
+ return value;
14
+ }
15
+ },
16
+ RFC1738: 'RFC1738',
17
+ RFC3986: 'RFC3986'
18
+ };
package/lib/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
2
 
3
- var Stringify = require('./stringify');
4
- var Parse = require('./parse');
3
+ var stringify = require('./stringify');
4
+ var parse = require('./parse');
5
+ var formats = require('./formats');
5
6
 
6
7
  module.exports = {
7
- stringify: Stringify,
8
- parse: Parse
8
+ formats: formats,
9
+ parse: parse,
10
+ stringify: stringify
9
11
  };
package/lib/parse.js CHANGED
@@ -1,19 +1,19 @@
1
1
  'use strict';
2
2
 
3
- var Utils = require('./utils');
3
+ var utils = require('./utils');
4
4
 
5
5
  var has = Object.prototype.hasOwnProperty;
6
6
 
7
7
  var defaults = {
8
+ allowDots: false,
9
+ allowPrototypes: false,
10
+ arrayLimit: 20,
11
+ decoder: utils.decode,
8
12
  delimiter: '&',
9
13
  depth: 5,
10
- arrayLimit: 20,
11
14
  parameterLimit: 1000,
12
- strictNullHandling: false,
13
15
  plainObjects: false,
14
- allowPrototypes: false,
15
- allowDots: false,
16
- decoder: Utils.decode
16
+ strictNullHandling: false
17
17
  };
18
18
 
19
19
  var parseValues = function parseValues(str, options) {
@@ -50,25 +50,23 @@ var parseObject = function parseObject(chain, val, options) {
50
50
  var root = chain.shift();
51
51
 
52
52
  var obj;
53
- if (root === '[]' && options.parseArrays) {
53
+ if (root === '[]') {
54
54
  obj = [];
55
55
  obj = obj.concat(parseObject(chain, val, options));
56
56
  } else {
57
57
  obj = options.plainObjects ? Object.create(null) : {};
58
- var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
58
+ var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
59
59
  var index = parseInt(cleanRoot, 10);
60
- if (!options.parseArrays && cleanRoot === '') {
61
- obj = { 0: val };
62
- } else if (
63
- !isNaN(index)
64
- && root !== cleanRoot
65
- && String(index) === cleanRoot
66
- && index >= 0
67
- && (options.parseArrays && index <= options.arrayLimit)
60
+ if (
61
+ !isNaN(index) &&
62
+ root !== cleanRoot &&
63
+ String(index) === cleanRoot &&
64
+ index >= 0 &&
65
+ (options.parseArrays && index <= options.arrayLimit)
68
66
  ) {
69
67
  obj = [];
70
68
  obj[index] = parseObject(chain, val, options);
71
- } else if (cleanRoot !== '__proto__') {
69
+ } else {
72
70
  obj[cleanRoot] = parseObject(chain, val, options);
73
71
  }
74
72
  }
@@ -82,30 +80,30 @@ var parseKeys = function parseKeys(givenKey, val, options) {
82
80
  }
83
81
 
84
82
  // Transform dot notation to bracket notation
85
- var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
83
+ var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
86
84
 
87
85
  // The regex chunks
88
86
 
89
- var brackets = /(\[[^[\]]*])/;
90
- var child = /(\[[^[\]]*])/g;
87
+ var parent = /^([^\[\]]*)/;
88
+ var child = /(\[[^\[\]]*\])/g;
91
89
 
92
90
  // Get the parent
93
91
 
94
- var segment = brackets.exec(key);
95
- var parent = segment ? key.slice(0, segment.index) : key;
92
+ var segment = parent.exec(key);
96
93
 
97
94
  // Stash the parent if it exists
98
95
 
99
96
  var keys = [];
100
- if (parent) {
101
- // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
102
- if (!options.plainObjects && has.call(Object.prototype, parent)) {
97
+ if (segment[1]) {
98
+ // If we aren't using plain objects, optionally prefix keys
99
+ // that would overwrite object prototype properties
100
+ if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
103
101
  if (!options.allowPrototypes) {
104
102
  return;
105
103
  }
106
104
  }
107
105
 
108
- keys.push(parent);
106
+ keys.push(segment[1]);
109
107
  }
110
108
 
111
109
  // Loop through children appending to the array until we hit depth
@@ -113,9 +111,9 @@ var parseKeys = function parseKeys(givenKey, val, options) {
113
111
  var i = 0;
114
112
  while ((segment = child.exec(key)) !== null && i < options.depth) {
115
113
  i += 1;
116
- if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
114
+ if (!options.plainObjects && has.call(Object.prototype, segment[1].replace(/\[|\]/g, ''))) {
117
115
  if (!options.allowPrototypes) {
118
- return;
116
+ continue;
119
117
  }
120
118
  }
121
119
  keys.push(segment[1]);
@@ -137,7 +135,7 @@ module.exports = function (str, opts) {
137
135
  throw new TypeError('Decoder has to be a function.');
138
136
  }
139
137
 
140
- options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
138
+ options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
141
139
  options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
142
140
  options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
143
141
  options.parseArrays = options.parseArrays !== false;
@@ -161,8 +159,8 @@ module.exports = function (str, opts) {
161
159
  for (var i = 0; i < keys.length; ++i) {
162
160
  var key = keys[i];
163
161
  var newObj = parseKeys(key, tempObj[key], options);
164
- obj = Utils.merge(obj, newObj, options);
162
+ obj = utils.merge(obj, newObj, options);
165
163
  }
166
164
 
167
- return Utils.compact(obj);
165
+ return utils.compact(obj);
168
166
  };