qs.js 18 KB

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