3 var Utils = require('./utils');
10 strictNullHandling: false,
12 allowPrototypes: false,
16 var has = Object.prototype.hasOwnProperty;
18 internals.parseValues = function (str, options) {
20 var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
22 for (var i = 0; i < parts.length; ++i) {
24 var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
27 obj[Utils.decode(part)] = '';
29 if (options.strictNullHandling) {
30 obj[Utils.decode(part)] = null;
33 var key = Utils.decode(part.slice(0, pos));
34 var val = Utils.decode(part.slice(pos + 1));
36 if (has.call(obj, key)) {
37 obj[key] = [].concat(obj[key]).concat(val);
47 internals.parseObject = function (chain, val, options) {
52 var root = chain.shift();
57 obj = obj.concat(internals.parseObject(chain, val, options));
59 obj = options.plainObjects ? Object.create(null) : {};
60 var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
61 var index = parseInt(cleanRoot, 10);
65 String(index) === cleanRoot &&
67 (options.parseArrays && index <= options.arrayLimit)
70 obj[index] = internals.parseObject(chain, val, options);
72 obj[cleanRoot] = internals.parseObject(chain, val, options);
79 internals.parseKeys = function (givenKey, val, options) {
84 // Transform dot notation to bracket notation
85 var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
89 var brackets = /(\[[^[\]]*])/;
90 var child = /(\[[^[\]]*])/g;
94 var segment = brackets.exec(key);
95 var parent = segment ? key.slice(0, segment.index) : key;
97 // Stash the parent if it exists
101 // If we aren't using plain objects, optionally prefix keys
102 // that would overwrite object prototype properties
103 if (!options.plainObjects && has.call(Object.prototype, parent)) {
104 if (!options.allowPrototypes) {
112 // Loop through children appending to the array until we hit depth
115 while ((segment = child.exec(key)) !== null && i < options.depth) {
117 if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
118 if (!options.allowPrototypes) {
122 keys.push(segment[1]);
125 // If there's a remainder, just add whatever is left
128 keys.push('[' + key.slice(segment.index) + ']');
131 return internals.parseObject(keys, val, options);
134 module.exports = function (str, opts) {
135 var options = opts || {};
136 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
137 options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
138 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
139 options.parseArrays = options.parseArrays !== false;
140 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots;
141 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects;
142 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes;
143 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
144 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
149 typeof str === 'undefined'
151 return options.plainObjects ? Object.create(null) : {};
154 var tempObj = typeof str === 'string' ? internals.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 = internals.parseKeys(key, tempObj[key], options);
163 obj = Utils.merge(obj, newObj, options);
166 return Utils.compact(obj);