qs.js 18 KB

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