qs.js 19 KB

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