3 var Utils = require('./utils');
13 strictNullHandling: false,
15 allowPrototypes: false
19 internals.parseValues = function (str, options) {
22 var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
24 for (var i = 0, il = parts.length; i < il; ++i) {
26 var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
29 obj[Utils.decode(part)] = '';
31 if (options.strictNullHandling) {
32 obj[Utils.decode(part)] = null;
36 var key = Utils.decode(part.slice(0, pos));
37 var val = Utils.decode(part.slice(pos + 1));
39 if (!Object.prototype.hasOwnProperty.call(obj, key)) {
43 obj[key] = [].concat(obj[key]).concat(val);
52 internals.parseObject = function (chain, val, options) {
58 var root = chain.shift();
63 obj = obj.concat(internals.parseObject(chain, val, options));
66 obj = options.plainObjects ? Object.create(null) : {};
67 var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
68 var index = parseInt(cleanRoot, 10);
69 var indexString = '' + index;
72 indexString === cleanRoot &&
74 (options.parseArrays &&
75 index <= options.arrayLimit)) {
78 obj[index] = internals.parseObject(chain, val, options);
81 obj[cleanRoot] = internals.parseObject(chain, val, options);
89 internals.parseKeys = function (key, val, options) {
95 // Transform dot notation to bracket notation
97 if (options.allowDots) {
98 key = key.replace(/\.([^\.\[]+)/g, '[$1]');
103 var parent = /^([^\[\]]*)/;
104 var child = /(\[[^\[\]]*\])/g;
108 var segment = parent.exec(key);
110 // Stash the parent if it exists
114 // If we aren't using plain objects, optionally prefix keys
115 // that would overwrite object prototype properties
116 if (!options.plainObjects &&
117 Object.prototype.hasOwnProperty(segment[1])) {
119 if (!options.allowPrototypes) {
124 keys.push(segment[1]);
127 // Loop through children appending to the array until we hit depth
130 while ((segment = child.exec(key)) !== null && i < options.depth) {
133 if (!options.plainObjects &&
134 Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
136 if (!options.allowPrototypes) {
140 keys.push(segment[1]);
143 // If there's a remainder, just add whatever is left
146 keys.push('[' + key.slice(segment.index) + ']');
149 return internals.parseObject(keys, val, options);
153 module.exports = function (str, options) {
155 options = options || {};
156 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
157 options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
158 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
159 options.parseArrays = options.parseArrays !== false;
160 options.allowDots = options.allowDots !== false;
161 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects;
162 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes;
163 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
164 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
168 typeof str === 'undefined') {
170 return options.plainObjects ? Object.create(null) : {};
173 var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
174 var obj = options.plainObjects ? Object.create(null) : {};
176 // Iterate over the keys and setup the new object
178 var keys = Object.keys(tempObj);
179 for (var i = 0, il = keys.length; i < il; ++i) {
181 var newObj = internals.parseKeys(key, tempObj[key], options);
182 obj = Utils.merge(obj, newObj, options);
185 return Utils.compact(obj);