qs.js 19 KB

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