3 var Utils = require('./utils');
5 var has = Object.prototype.hasOwnProperty;
12 strictNullHandling: false,
14 allowPrototypes: false,
19 var parseValues = function parseValues(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 parseObject(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[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 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 parseKeys(givenKey, val, options) {
82 // Transform dot notation to bracket notation
83 var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
87 var parent = /^([^\[\]]*)/;
88 var child = /(\[[^\[\]]*\])/g;
92 var segment = parent.exec(key);
94 // Stash the parent if it exists
98 // If we aren't using plain objects, optionally prefix keys
99 // that would overwrite object prototype properties
100 if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
101 if (!options.allowPrototypes) {
106 keys.push(segment[1]);
109 // Loop through children appending to the array until we hit depth
112 while ((segment = child.exec(key)) !== null && i < options.depth) {
114 if (!options.plainObjects && has.call(Object.prototype, segment[1].replace(/\[|\]/g, ''))) {
115 if (!options.allowPrototypes) {
119 keys.push(segment[1]);
122 // If there's a remainder, just add whatever is left
125 keys.push('[' + key.slice(segment.index) + ']');
128 return parseObject(keys, val, options);
131 module.exports = function (str, opts) {
132 var options = opts || {};
134 if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
135 throw new TypeError('Decoder has to be a function.');
138 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
139 options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
140 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
141 options.parseArrays = options.parseArrays !== false;
142 options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
143 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
144 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
145 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
146 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
147 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
149 if (str === '' || str === null || typeof str === 'undefined') {
150 return options.plainObjects ? Object.create(null) : {};
153 var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
154 var obj = options.plainObjects ? Object.create(null) : {};
156 // Iterate over the keys and setup the new object
158 var keys = Object.keys(tempObj);
159 for (var i = 0; i < keys.length; ++i) {
161 var newObj = parseKeys(key, tempObj[key], options);
162 obj = Utils.merge(obj, newObj, options);
165 return Utils.compact(obj);