1 (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Qs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
4 var Stringify = require('./stringify');
5 var Parse = require('./parse');
12 },{"./parse":2,"./stringify":3}],2:[function(require,module,exports){
15 var Utils = require('./utils');
17 var has = Object.prototype.hasOwnProperty;
24 strictNullHandling: false,
26 allowPrototypes: false,
31 var parseValues = function parseValues(str, options) {
33 var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
35 for (var i = 0; i < parts.length; ++i) {
37 var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
41 key = options.decoder(part);
42 val = options.strictNullHandling ? null : '';
44 key = options.decoder(part.slice(0, pos));
45 val = options.decoder(part.slice(pos + 1));
47 if (has.call(obj, key)) {
48 obj[key] = [].concat(obj[key]).concat(val);
57 var parseObject = function parseObject(chain, val, options) {
62 var root = chain.shift();
67 obj = obj.concat(parseObject(chain, val, options));
69 obj = options.plainObjects ? Object.create(null) : {};
70 var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
71 var index = parseInt(cleanRoot, 10);
75 String(index) === cleanRoot &&
77 (options.parseArrays && index <= options.arrayLimit)
80 obj[index] = parseObject(chain, val, options);
82 obj[cleanRoot] = parseObject(chain, val, options);
89 var parseKeys = function parseKeys(givenKey, val, options) {
94 // Transform dot notation to bracket notation
95 var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
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 && has.call(Object.prototype, segment[1])) {
113 if (!options.allowPrototypes) {
118 keys.push(segment[1]);
121 // Loop through children appending to the array until we hit depth
124 while ((segment = child.exec(key)) !== null && i < options.depth) {
126 if (!options.plainObjects && has.call(Object.prototype, segment[1].replace(/\[|\]/g, ''))) {
127 if (!options.allowPrototypes) {
131 keys.push(segment[1]);
134 // If there's a remainder, just add whatever is left
137 keys.push('[' + key.slice(segment.index) + ']');
140 return parseObject(keys, val, options);
143 module.exports = function (str, opts) {
144 var options = opts || {};
146 if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
147 throw new TypeError('Decoder has to be a function.');
150 options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
151 options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
152 options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
153 options.parseArrays = options.parseArrays !== false;
154 options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
155 options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
156 options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
157 options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
158 options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
159 options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
161 if (str === '' || str === null || typeof str === 'undefined') {
162 return options.plainObjects ? Object.create(null) : {};
165 var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
166 var obj = options.plainObjects ? Object.create(null) : {};
168 // Iterate over the keys and setup the new object
170 var keys = Object.keys(tempObj);
171 for (var i = 0; i < keys.length; ++i) {
173 var newObj = parseKeys(key, tempObj[key], options);
174 obj = Utils.merge(obj, newObj, options);
177 return Utils.compact(obj);
180 },{"./utils":4}],3:[function(require,module,exports){
183 var Utils = require('./utils');
185 var arrayPrefixGenerators = {
186 brackets: function brackets(prefix) {
187 return prefix + '[]';
189 indices: function indices(prefix, key) {
190 return prefix + '[' + key + ']';
192 repeat: function repeat(prefix) {
199 strictNullHandling: false,
202 encoder: Utils.encode
205 var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) {
207 if (typeof filter === 'function') {
208 obj = filter(prefix, obj);
209 } else if (obj instanceof Date) {
210 obj = obj.toISOString();
211 } else if (obj === null) {
212 if (strictNullHandling) {
213 return encoder ? encoder(prefix) : prefix;
219 if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
221 return [encoder(prefix) + '=' + encoder(obj)];
223 return [prefix + '=' + String(obj)];
228 if (typeof obj === 'undefined') {
233 if (Array.isArray(filter)) {
236 var keys = Object.keys(obj);
237 objKeys = sort ? keys.sort(sort) : keys;
240 for (var i = 0; i < objKeys.length; ++i) {
241 var key = objKeys[i];
243 if (skipNulls && obj[key] === null) {
247 if (Array.isArray(obj)) {
248 values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
250 values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
257 module.exports = function (object, opts) {
259 var options = opts || {};
260 var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
261 var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
262 var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
263 var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
264 var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
265 var sort = typeof options.sort === 'function' ? options.sort : null;
266 var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
270 if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
271 throw new TypeError('Encoder has to be a function.');
274 if (typeof options.filter === 'function') {
275 filter = options.filter;
276 obj = filter('', obj);
277 } else if (Array.isArray(options.filter)) {
278 objKeys = filter = options.filter;
283 if (typeof obj !== 'object' || obj === null) {
288 if (options.arrayFormat in arrayPrefixGenerators) {
289 arrayFormat = options.arrayFormat;
290 } else if ('indices' in options) {
291 arrayFormat = options.indices ? 'indices' : 'repeat';
293 arrayFormat = 'indices';
296 var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
299 objKeys = Object.keys(obj);
306 for (var i = 0; i < objKeys.length; ++i) {
307 var key = objKeys[i];
309 if (skipNulls && obj[key] === null) {
313 keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
316 return keys.join(delimiter);
319 },{"./utils":4}],4:[function(require,module,exports){
322 var hexTable = (function () {
323 var array = new Array(256);
324 for (var i = 0; i < 256; ++i) {
325 array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
331 exports.arrayToObject = function (source, options) {
332 var obj = options.plainObjects ? Object.create(null) : {};
333 for (var i = 0; i < source.length; ++i) {
334 if (typeof source[i] !== 'undefined') {
342 exports.merge = function (target, source, options) {
347 if (typeof source !== 'object') {
348 if (Array.isArray(target)) {
350 } else if (typeof target === 'object') {
351 target[source] = true;
353 return [target, source];
359 if (typeof target !== 'object') {
360 return [target].concat(source);
363 var mergeTarget = target;
364 if (Array.isArray(target) && !Array.isArray(source)) {
365 mergeTarget = exports.arrayToObject(target, options);
368 return Object.keys(source).reduce(function (acc, key) {
369 var value = source[key];
371 if (Object.prototype.hasOwnProperty.call(acc, key)) {
372 acc[key] = exports.merge(acc[key], value, options);
380 exports.decode = function (str) {
382 return decodeURIComponent(str.replace(/\+/g, ' '));
388 exports.encode = function (str) {
389 // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
390 // It has been adapted here for stricter adherence to RFC 3986
391 if (str.length === 0) {
395 var string = typeof str === 'string' ? str : String(str);
398 for (var i = 0; i < string.length; ++i) {
399 var c = string.charCodeAt(i);
406 (c >= 0x30 && c <= 0x39) || // 0-9
407 (c >= 0x41 && c <= 0x5A) || // a-z
408 (c >= 0x61 && c <= 0x7A) // A-Z
410 out += string.charAt(i);
415 out = out + hexTable[c];
420 out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
424 if (c < 0xD800 || c >= 0xE000) {
425 out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
430 c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
431 out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)];
437 exports.compact = function (obj, references) {
438 if (typeof obj !== 'object' || obj === null) {
442 var refs = references || [];
443 var lookup = refs.indexOf(obj);
450 if (Array.isArray(obj)) {
453 for (var i = 0; i < obj.length; ++i) {
454 if (obj[i] && typeof obj[i] === 'object') {
455 compacted.push(exports.compact(obj[i], refs));
456 } else if (typeof obj[i] !== 'undefined') {
457 compacted.push(obj[i]);
464 var keys = Object.keys(obj);
465 for (var j = 0; j < keys.length; ++j) {
467 obj[key] = exports.compact(obj[key], refs);
473 exports.isRegExp = function (obj) {
474 return Object.prototype.toString.call(obj) === '[object RegExp]';
477 exports.isBuffer = function (obj) {
478 if (obj === null || typeof obj === 'undefined') {
482 return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));