qs.js 14 KB

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