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;
31 key = Utils.decode(part);
32 val = options.strictNullHandling ? null : '';
34 key = Utils.decode(part.slice(0, pos));
35 val = Utils.decode(part.slice(pos + 1));
37 if (Object.prototype.hasOwnProperty.call(obj, key)) {
38 obj[key] = [].concat(obj[key]).concat(val);
48 internals.parseObject = function (chain, val, options) {
54 var root = chain.shift();
59 obj = obj.concat(internals.parseObject(chain, val, options));
62 obj = options.plainObjects ? Object.create(null) : {};
63 var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
64 var index = parseInt(cleanRoot, 10);
65 var indexString = '' + index;
68 indexString === cleanRoot &&
70 (options.parseArrays &&
71 index <= options.arrayLimit)) {
74 obj[index] = internals.parseObject(chain, val, options);
77 obj[cleanRoot] = internals.parseObject(chain, val, options);
85 internals.parseKeys = function (key, val, options) {
91 // Transform dot notation to bracket notation
93 if (options.allowDots) {
94 key = key.replace(/\.([^\.\[]+)/g, '[$1]');
99 var parent = /^([^\[\]]*)/;
100 var child = /(\[[^\[\]]*\])/g;
104 var segment = parent.exec(key);
106 // Stash the parent if it exists
110 // If we aren't using plain objects, optionally prefix keys
111 // that would overwrite object prototype properties
112 if (!options.plainObjects &&
113 Object.prototype.hasOwnProperty(segment[1])) {
115 if (!options.allowPrototypes) {
120 keys.push(segment[1]);
123 // Loop through children appending to the array until we hit depth
126 while ((segment = child.exec(key)) !== null && i < options.depth) {
129 if (!options.plainObjects &&
130 Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
132 if (!options.allowPrototypes) {
136 keys.push(segment[1]);
139 // If there's a remainder, just add whatever is left
142 keys.push('[' + key.slice(segment.index) + ']');
145 return internals.parseObject(keys, val, options);
149 module.exports = function (str, options) {
151 options = options || {};
152 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
153 options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
154 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
155 options.parseArrays = options.parseArrays !== false;
156 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots;
157 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects;
158 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes;
159 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
160 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
164 typeof str === 'undefined') {
166 return options.plainObjects ? Object.create(null) : {};
169 var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
170 var obj = options.plainObjects ? Object.create(null) : {};
172 // Iterate over the keys and setup the new object
174 var keys = Object.keys(tempObj);
175 for (var i = 0, il = keys.length; i < il; ++i) {
177 var newObj = internals.parseKeys(key, tempObj[key], options);
178 obj = Utils.merge(obj, newObj, options);
181 return Utils.compact(obj);