qs.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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){
  2. 'use strict';
  3. var Stringify = require('./stringify');
  4. var Parse = require('./parse');
  5. module.exports = {
  6. stringify: Stringify,
  7. parse: Parse
  8. };
  9. },{"./parse":2,"./stringify":3}],2:[function(require,module,exports){
  10. 'use strict';
  11. var Utils = require('./utils');
  12. var has = Object.prototype.hasOwnProperty;
  13. var defaults = {
  14. delimiter: '&',
  15. depth: 5,
  16. arrayLimit: 20,
  17. parameterLimit: 1000,
  18. strictNullHandling: false,
  19. plainObjects: false,
  20. allowPrototypes: false,
  21. allowDots: false,
  22. decoder: Utils.decode
  23. };
  24. var parseValues = function parseValues(str, options) {
  25. var obj = {};
  26. var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
  27. for (var i = 0; i < parts.length; ++i) {
  28. var part = parts[i];
  29. var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
  30. var key, val;
  31. if (pos === -1) {
  32. key = options.decoder(part);
  33. val = options.strictNullHandling ? null : '';
  34. } else {
  35. key = options.decoder(part.slice(0, pos));
  36. val = options.decoder(part.slice(pos + 1));
  37. }
  38. if (has.call(obj, key)) {
  39. obj[key] = [].concat(obj[key]).concat(val);
  40. } else {
  41. obj[key] = val;
  42. }
  43. }
  44. return obj;
  45. };
  46. var parseObject = function parseObject(chain, val, options) {
  47. if (!chain.length) {
  48. return val;
  49. }
  50. var root = chain.shift();
  51. var obj;
  52. if (root === '[]') {
  53. obj = [];
  54. obj = obj.concat(parseObject(chain, val, options));
  55. } else {
  56. obj = options.plainObjects ? Object.create(null) : {};
  57. var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
  58. var index = parseInt(cleanRoot, 10);
  59. if (
  60. !isNaN(index) &&
  61. root !== cleanRoot &&
  62. String(index) === cleanRoot &&
  63. index >= 0 &&
  64. (options.parseArrays && index <= options.arrayLimit)
  65. ) {
  66. obj = [];
  67. obj[index] = parseObject(chain, val, options);
  68. } else {
  69. obj[cleanRoot] = parseObject(chain, val, options);
  70. }
  71. }
  72. return obj;
  73. };
  74. var parseKeys = function parseKeys(givenKey, val, options) {
  75. if (!givenKey) {
  76. return;
  77. }
  78. // Transform dot notation to bracket notation
  79. var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
  80. // The regex chunks
  81. var brackets = /(\[[^[\]]*])/;
  82. var child = /(\[[^[\]]*])/g;
  83. // Get the parent
  84. var segment = brackets.exec(key);
  85. var parent = segment ? key.slice(0, segment.index) : key;
  86. // Stash the parent if it exists
  87. var keys = [];
  88. if (parent) {
  89. // If we aren't using plain objects, optionally prefix keys
  90. // that would overwrite object prototype properties
  91. if (!options.plainObjects && has.call(Object.prototype, parent)) {
  92. if (!options.allowPrototypes) {
  93. return;
  94. }
  95. }
  96. keys.push(parent);
  97. }
  98. // Loop through children appending to the array until we hit depth
  99. var i = 0;
  100. while ((segment = child.exec(key)) !== null && i < options.depth) {
  101. i += 1;
  102. if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
  103. if (!options.allowPrototypes) {
  104. return;
  105. }
  106. }
  107. keys.push(segment[1]);
  108. }
  109. // If there's a remainder, just add whatever is left
  110. if (segment) {
  111. keys.push('[' + key.slice(segment.index) + ']');
  112. }
  113. return parseObject(keys, val, options);
  114. };
  115. module.exports = function (str, opts) {
  116. var options = opts || {};
  117. if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
  118. throw new TypeError('Decoder has to be a function.');
  119. }
  120. options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
  121. options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
  122. options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
  123. options.parseArrays = options.parseArrays !== false;
  124. options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
  125. options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
  126. options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
  127. options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
  128. options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
  129. options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  130. if (str === '' || str === null || typeof str === 'undefined') {
  131. return options.plainObjects ? Object.create(null) : {};
  132. }
  133. var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  134. var obj = options.plainObjects ? Object.create(null) : {};
  135. // Iterate over the keys and setup the new object
  136. var keys = Object.keys(tempObj);
  137. for (var i = 0; i < keys.length; ++i) {
  138. var key = keys[i];
  139. var newObj = parseKeys(key, tempObj[key], options);
  140. obj = Utils.merge(obj, newObj, options);
  141. }
  142. return Utils.compact(obj);
  143. };
  144. },{"./utils":4}],3:[function(require,module,exports){
  145. 'use strict';
  146. var Utils = require('./utils');
  147. var arrayPrefixGenerators = {
  148. brackets: function brackets(prefix) {
  149. return prefix + '[]';
  150. },
  151. indices: function indices(prefix, key) {
  152. return prefix + '[' + key + ']';
  153. },
  154. repeat: function repeat(prefix) {
  155. return prefix;
  156. }
  157. };
  158. var defaults = {
  159. delimiter: '&',
  160. strictNullHandling: false,
  161. skipNulls: false,
  162. encode: true,
  163. encoder: Utils.encode
  164. };
  165. var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) {
  166. var obj = object;
  167. if (typeof filter === 'function') {
  168. obj = filter(prefix, obj);
  169. } else if (obj instanceof Date) {
  170. obj = obj.toISOString();
  171. } else if (obj === null) {
  172. if (strictNullHandling) {
  173. return encoder ? encoder(prefix) : prefix;
  174. }
  175. obj = '';
  176. }
  177. if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
  178. if (encoder) {
  179. return [encoder(prefix) + '=' + encoder(obj)];
  180. }
  181. return [prefix + '=' + String(obj)];
  182. }
  183. var values = [];
  184. if (typeof obj === 'undefined') {
  185. return values;
  186. }
  187. var objKeys;
  188. if (Array.isArray(filter)) {
  189. objKeys = filter;
  190. } else {
  191. var keys = Object.keys(obj);
  192. objKeys = sort ? keys.sort(sort) : keys;
  193. }
  194. for (var i = 0; i < objKeys.length; ++i) {
  195. var key = objKeys[i];
  196. if (skipNulls && obj[key] === null) {
  197. continue;
  198. }
  199. if (Array.isArray(obj)) {
  200. values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  201. } else {
  202. values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  203. }
  204. }
  205. return values;
  206. };
  207. module.exports = function (object, opts) {
  208. var obj = object;
  209. var options = opts || {};
  210. var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
  211. var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  212. var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
  213. var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
  214. var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
  215. var sort = typeof options.sort === 'function' ? options.sort : null;
  216. var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
  217. var objKeys;
  218. var filter;
  219. if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
  220. throw new TypeError('Encoder has to be a function.');
  221. }
  222. if (typeof options.filter === 'function') {
  223. filter = options.filter;
  224. obj = filter('', obj);
  225. } else if (Array.isArray(options.filter)) {
  226. objKeys = filter = options.filter;
  227. }
  228. var keys = [];
  229. if (typeof obj !== 'object' || obj === null) {
  230. return '';
  231. }
  232. var arrayFormat;
  233. if (options.arrayFormat in arrayPrefixGenerators) {
  234. arrayFormat = options.arrayFormat;
  235. } else if ('indices' in options) {
  236. arrayFormat = options.indices ? 'indices' : 'repeat';
  237. } else {
  238. arrayFormat = 'indices';
  239. }
  240. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  241. if (!objKeys) {
  242. objKeys = Object.keys(obj);
  243. }
  244. if (sort) {
  245. objKeys.sort(sort);
  246. }
  247. for (var i = 0; i < objKeys.length; ++i) {
  248. var key = objKeys[i];
  249. if (skipNulls && obj[key] === null) {
  250. continue;
  251. }
  252. keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
  253. }
  254. return keys.join(delimiter);
  255. };
  256. },{"./utils":4}],4:[function(require,module,exports){
  257. 'use strict';
  258. var hexTable = (function () {
  259. var array = new Array(256);
  260. for (var i = 0; i < 256; ++i) {
  261. array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
  262. }
  263. return array;
  264. }());
  265. var has = Object.prototype.hasOwnProperty;
  266. exports.arrayToObject = function (source, options) {
  267. var obj = options.plainObjects ? Object.create(null) : {};
  268. for (var i = 0; i < source.length; ++i) {
  269. if (typeof source[i] !== 'undefined') {
  270. obj[i] = source[i];
  271. }
  272. }
  273. return obj;
  274. };
  275. exports.merge = function (target, source, options) {
  276. if (!source) {
  277. return target;
  278. }
  279. if (typeof source !== 'object') {
  280. if (Array.isArray(target)) {
  281. target.push(source);
  282. } else if (typeof target === 'object') {
  283. if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
  284. target[source] = true;
  285. }
  286. } else {
  287. return [target, source];
  288. }
  289. return target;
  290. }
  291. if (typeof target !== 'object') {
  292. return [target].concat(source);
  293. }
  294. var mergeTarget = target;
  295. if (Array.isArray(target) && !Array.isArray(source)) {
  296. mergeTarget = exports.arrayToObject(target, options);
  297. }
  298. return Object.keys(source).reduce(function (acc, key) {
  299. var value = source[key];
  300. if (has.call(acc, key)) {
  301. acc[key] = exports.merge(acc[key], value, options);
  302. } else {
  303. acc[key] = value;
  304. }
  305. return acc;
  306. }, mergeTarget);
  307. };
  308. exports.decode = function (str) {
  309. try {
  310. return decodeURIComponent(str.replace(/\+/g, ' '));
  311. } catch (e) {
  312. return str;
  313. }
  314. };
  315. exports.encode = function (str) {
  316. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  317. // It has been adapted here for stricter adherence to RFC 3986
  318. if (str.length === 0) {
  319. return str;
  320. }
  321. var string = typeof str === 'string' ? str : String(str);
  322. var out = '';
  323. for (var i = 0; i < string.length; ++i) {
  324. var c = string.charCodeAt(i);
  325. if (
  326. c === 0x2D || // -
  327. c === 0x2E || // .
  328. c === 0x5F || // _
  329. c === 0x7E || // ~
  330. (c >= 0x30 && c <= 0x39) || // 0-9
  331. (c >= 0x41 && c <= 0x5A) || // a-z
  332. (c >= 0x61 && c <= 0x7A) // A-Z
  333. ) {
  334. out += string.charAt(i);
  335. continue;
  336. }
  337. if (c < 0x80) {
  338. out = out + hexTable[c];
  339. continue;
  340. }
  341. if (c < 0x800) {
  342. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  343. continue;
  344. }
  345. if (c < 0xD800 || c >= 0xE000) {
  346. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  347. continue;
  348. }
  349. i += 1;
  350. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  351. out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)];
  352. }
  353. return out;
  354. };
  355. exports.compact = function (obj, references) {
  356. if (typeof obj !== 'object' || obj === null) {
  357. return obj;
  358. }
  359. var refs = references || [];
  360. var lookup = refs.indexOf(obj);
  361. if (lookup !== -1) {
  362. return refs[lookup];
  363. }
  364. refs.push(obj);
  365. if (Array.isArray(obj)) {
  366. var compacted = [];
  367. for (var i = 0; i < obj.length; ++i) {
  368. if (obj[i] && typeof obj[i] === 'object') {
  369. compacted.push(exports.compact(obj[i], refs));
  370. } else if (typeof obj[i] !== 'undefined') {
  371. compacted.push(obj[i]);
  372. }
  373. }
  374. return compacted;
  375. }
  376. var keys = Object.keys(obj);
  377. for (var j = 0; j < keys.length; ++j) {
  378. var key = keys[j];
  379. obj[key] = exports.compact(obj[key], refs);
  380. }
  381. return obj;
  382. };
  383. exports.isRegExp = function (obj) {
  384. return Object.prototype.toString.call(obj) === '[object RegExp]';
  385. };
  386. exports.isBuffer = function (obj) {
  387. if (obj === null || typeof obj === 'undefined') {
  388. return false;
  389. }
  390. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  391. };
  392. },{}]},{},[1])(1)
  393. });