3 var Utils = require('./utils');
13 strictNullHandling: false,
15 allowPrototypes: false,
20 internals.parseValues = function (str, options) {
23 var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
25 for (var i = 0, il = parts.length; i < il; ++i) {
27 var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
30 obj[Utils.decode(part)] = '';
32 if (options.strictNullHandling) {
33 obj[Utils.decode(part)] = null;
37 var key = Utils.decode(part.slice(0, pos));
38 var val = Utils.decode(part.slice(pos + 1));
40 if (!Object.prototype.hasOwnProperty.call(obj, key)) {
44 obj[key] = [].concat(obj[key]).concat(val);
53 internals.parseObject = function (chain, val, options) {
59 var root = chain.shift();
64 obj = obj.concat(internals.parseObject(chain, val, options));
67 obj = options.plainObjects ? Object.create(null) : {};
68 var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
69 var index = parseInt(cleanRoot, 10);
70 var indexString = '' + index;
73 indexString === cleanRoot &&
75 (options.parseArrays &&
76 index <= options.arrayLimit)) {
79 obj[index] = internals.parseObject(chain, val, options);
82 obj[cleanRoot] = internals.parseObject(chain, val, options);
90 internals.parseKeys = function (key, val, options) {
96 // Transform dot notation to bracket notation
98 if (options.allowDots) {
99 key = key.replace(/\.([^\.\[]+)/g, '[$1]');
104 var parent = /^([^\[\]]*)/;
105 var child = /(\[[^\[\]]*\])/g;
109 var segment = parent.exec(key);
111 // Stash the parent if it exists
115 // If we aren't using plain objects, optionally prefix keys
116 // that would overwrite object prototype properties
117 if (!options.plainObjects &&
118 Object.prototype.hasOwnProperty(segment[1])) {
120 if (!options.allowPrototypes) {
125 keys.push(segment[1]);
128 // Loop through children appending to the array until we hit depth
131 while ((segment = child.exec(key)) !== null && i < options.depth) {
134 if (!options.plainObjects &&
135 Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
137 if (!options.allowPrototypes) {
141 keys.push(segment[1]);
144 // If there's a remainder, just add whatever is left
147 keys.push('[' + key.slice(segment.index) + ']');
150 return internals.parseObject(keys, val, options);
154 module.exports = function (str, options) {
156 options = options || {};
157 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
158 options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
159 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
160 options.parseArrays = options.parseArrays !== false;
161 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots;
162 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects;
163 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes;
164 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
165 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
169 typeof str === 'undefined') {
171 return options.plainObjects ? Object.create(null) : {};
174 var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
175 var obj = options.plainObjects ? Object.create(null) : {};
177 // Iterate over the keys and setup the new object
179 var keys = Object.keys(tempObj);
180 for (var i = 0, il = keys.length; i < il; ++i) {
182 var newObj = internals.parseKeys(key, tempObj[key], options);
183 obj = Utils.merge(obj, newObj, options);
186 return Utils.compact(obj);