3 var utils = require('./utils');
5 var has = Object.prototype.hasOwnProperty;
9 allowPrototypes: false,
12 charsetSentinel: false,
14 decoder: utils.decode,
17 ignoreQueryPrefix: false,
18 interpretNumericEntities: false,
22 strictNullHandling: false
25 var interpretNumericEntities = function (str) {
26 return str.replace(/&#(\d+);/g, function ($0, numberStr) {
27 return String.fromCharCode(parseInt(numberStr, 10));
31 // This is what browsers will submit when the ✓ character occurs in an
32 // application/x-www-form-urlencoded body and the encoding of the page containing
33 // the form is iso-8859-1, or when the submitted form has an accept-charset
34 // attribute of iso-8859-1. Presumably also with other charsets that do not contain
35 // the ✓ character, such as us-ascii.
36 var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('✓')
38 // These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded.
39 var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
41 var parseValues = function parseQueryStringValues(str, options) {
43 var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
44 var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
45 var parts = cleanStr.split(options.delimiter, limit);
46 var skipIndex = -1; // Keep track of where the utf8 sentinel was found
49 var charset = options.charset;
50 if (options.charsetSentinel) {
51 for (i = 0; i < parts.length; ++i) {
52 if (parts[i].indexOf('utf8=') === 0) {
53 if (parts[i] === charsetSentinel) {
55 } else if (parts[i] === isoSentinel) {
56 charset = 'iso-8859-1';
59 i = parts.length; // The eslint settings do not allow break;
64 for (i = 0; i < parts.length; ++i) {
65 if (i === skipIndex) {
70 var bracketEqualsPos = part.indexOf(']=');
71 var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1;
75 key = options.decoder(part, defaults.decoder, charset);
76 val = options.strictNullHandling ? null : '';
78 key = options.decoder(part.slice(0, pos), defaults.decoder, charset);
79 val = options.decoder(part.slice(pos + 1), defaults.decoder, charset);
82 if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
83 val = interpretNumericEntities(val);
86 if (val && options.comma && val.indexOf(',') > -1) {
90 if (has.call(obj, key)) {
91 obj[key] = utils.combine(obj[key], val);
100 var parseObject = function (chain, val, options) {
103 for (var i = chain.length - 1; i >= 0; --i) {
107 if (root === '[]' && options.parseArrays) {
108 obj = [].concat(leaf);
110 obj = options.plainObjects ? Object.create(null) : {};
111 var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
112 var index = parseInt(cleanRoot, 10);
113 if (!options.parseArrays && cleanRoot === '') {
117 && root !== cleanRoot
118 && String(index) === cleanRoot
120 && (options.parseArrays && index <= options.arrayLimit)
125 obj[cleanRoot] = leaf;
135 var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
140 // Transform dot notation to bracket notation
141 var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
145 var brackets = /(\[[^[\]]*])/;
146 var child = /(\[[^[\]]*])/g;
150 var segment = brackets.exec(key);
151 var parent = segment ? key.slice(0, segment.index) : key;
153 // Stash the parent if it exists
157 // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
158 if (!options.plainObjects && has.call(Object.prototype, parent)) {
159 if (!options.allowPrototypes) {
167 // Loop through children appending to the array until we hit depth
170 while ((segment = child.exec(key)) !== null && i < options.depth) {
172 if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
173 if (!options.allowPrototypes) {
177 keys.push(segment[1]);
180 // If there's a remainder, just add whatever is left
183 keys.push('[' + key.slice(segment.index) + ']');
186 return parseObject(keys, val, options);
189 var normalizeParseOptions = function normalizeParseOptions(opts) {
194 if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') {
195 throw new TypeError('Decoder has to be a function.');
198 if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
199 throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined');
201 var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
204 allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
205 allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
206 arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit,
208 charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
209 comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
210 decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
211 delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
212 depth: typeof opts.depth === 'number' ? opts.depth : defaults.depth,
213 ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
214 interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
215 parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
216 parseArrays: opts.parseArrays !== false,
217 plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
218 strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
222 module.exports = function (str, opts) {
223 var options = normalizeParseOptions(opts);
225 if (str === '' || str === null || typeof str === 'undefined') {
226 return options.plainObjects ? Object.create(null) : {};
229 var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
230 var obj = options.plainObjects ? Object.create(null) : {};
232 // Iterate over the keys and setup the new object
234 var keys = Object.keys(tempObj);
235 for (var i = 0; i < keys.length; ++i) {
237 var newObj = parseKeys(key, tempObj[key], options);
238 obj = utils.merge(obj, newObj, options);
241 return utils.compact(obj);