3 var utils = require('./utils');
5 var has = Object.prototype.hasOwnProperty;
9 allowPrototypes: false,
11 decoder: utils.decode,
16 strictNullHandling: false
19 var parseValues = function parseQueryStringValues(str, options) {
21 var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
23 for (var i = 0; i < parts.length; ++i) {
25 var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
29 key = options.decoder(part);
30 val = options.strictNullHandling ? null : '';
32 key = options.decoder(part.slice(0, pos));
33 val = options.decoder(part.slice(pos + 1));
35 if (has.call(obj, key)) {
36 obj[key] = [].concat(obj[key]).concat(val);
45 var parseObject = function parseObjectRecursive(chain, val, options) {
50 var root = chain.shift();
55 obj = obj.concat(parseObject(chain, val, options));
57 obj = options.plainObjects ? Object.create(null) : {};
58 var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
59 var index = parseInt(cleanRoot, 10);
63 String(index) === cleanRoot &&
65 (options.parseArrays && index <= options.arrayLimit)
68 obj[index] = parseObject(chain, val, options);
70 obj[cleanRoot] = parseObject(chain, val, options);
77 var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
82 // Transform dot notation to bracket notation
83 var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
87 var brackets = /(\[[^[\]]*])/;
88 var child = /(\[[^[\]]*])/g;
92 var segment = brackets.exec(key);
93 var parent = segment ? key.slice(0, segment.index) : key;
95 // Stash the parent if it exists
99 // If we aren't using plain objects, optionally prefix keys
100 // that would overwrite object prototype properties
101 if (!options.plainObjects && has.call(Object.prototype, parent)) {
102 if (!options.allowPrototypes) {
110 // Loop through children appending to the array until we hit depth
113 while ((segment = child.exec(key)) !== null && i < options.depth) {
115 if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
116 if (!options.allowPrototypes) {
120 keys.push(segment[1]);
123 // If there's a remainder, just add whatever is left
126 keys.push('[' + key.slice(segment.index) + ']');
129 return parseObject(keys, val, options);
132 module.exports = function (str, opts) {
133 var options = opts || {};
135 if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
136 throw new TypeError('Decoder has to be a function.');
139 options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
140 options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
141 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
142 options.parseArrays = options.parseArrays !== false;
143 options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
144 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
145 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
146 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
147 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
148 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
150 if (str === '' || str === null || typeof str === 'undefined') {
151 return options.plainObjects ? Object.create(null) : {};
154 var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
155 var obj = options.plainObjects ? Object.create(null) : {};
157 // Iterate over the keys and setup the new object
159 var keys = Object.keys(tempObj);
160 for (var i = 0; i < keys.length; ++i) {
162 var newObj = parseKeys(key, tempObj[key], options);
163 obj = utils.merge(obj, newObj, options);
166 return utils.compact(obj);