qs.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. serializeDate: function serializeDate(date) { // eslint-disable-line func-name-matching
  184. return toISO.call(date);
  185. },
  186. skipNulls: false,
  187. strictNullHandling: false
  188. };
  189. var stringify = function stringify( // eslint-disable-line func-name-matching
  190. object,
  191. prefix,
  192. generateArrayPrefix,
  193. strictNullHandling,
  194. skipNulls,
  195. encoder,
  196. filter,
  197. sort,
  198. allowDots,
  199. serializeDate,
  200. formatter
  201. ) {
  202. var obj = object;
  203. if (typeof filter === 'function') {
  204. obj = filter(prefix, obj);
  205. } else if (obj instanceof Date) {
  206. obj = serializeDate(obj);
  207. } else if (obj === null) {
  208. if (strictNullHandling) {
  209. return encoder ? encoder(prefix) : prefix;
  210. }
  211. obj = '';
  212. }
  213. if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) {
  214. if (encoder) {
  215. return [formatter(encoder(prefix)) + '=' + formatter(encoder(obj))];
  216. }
  217. return [formatter(prefix) + '=' + formatter(String(obj))];
  218. }
  219. var values = [];
  220. if (typeof obj === 'undefined') {
  221. return values;
  222. }
  223. var objKeys;
  224. if (Array.isArray(filter)) {
  225. objKeys = filter;
  226. } else {
  227. var keys = Object.keys(obj);
  228. objKeys = sort ? keys.sort(sort) : keys;
  229. }
  230. for (var i = 0; i < objKeys.length; ++i) {
  231. var key = objKeys[i];
  232. if (skipNulls && obj[key] === null) {
  233. continue;
  234. }
  235. if (Array.isArray(obj)) {
  236. values = values.concat(stringify(
  237. obj[key],
  238. generateArrayPrefix(prefix, key),
  239. generateArrayPrefix,
  240. strictNullHandling,
  241. skipNulls,
  242. encoder,
  243. filter,
  244. sort,
  245. allowDots,
  246. serializeDate,
  247. formatter
  248. ));
  249. } else {
  250. values = values.concat(stringify(
  251. obj[key],
  252. prefix + (allowDots ? '.' + key : '[' + key + ']'),
  253. generateArrayPrefix,
  254. strictNullHandling,
  255. skipNulls,
  256. encoder,
  257. filter,
  258. sort,
  259. allowDots,
  260. serializeDate,
  261. formatter
  262. ));
  263. }
  264. }
  265. return values;
  266. };
  267. module.exports = function (object, opts) {
  268. var obj = object;
  269. var options = opts || {};
  270. if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
  271. throw new TypeError('Encoder has to be a function.');
  272. }
  273. var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
  274. var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  275. var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
  276. var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
  277. var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
  278. var sort = typeof options.sort === 'function' ? options.sort : null;
  279. var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
  280. var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate;
  281. if (typeof options.format === 'undefined') {
  282. options.format = formats.default;
  283. } else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) {
  284. throw new TypeError('Unknown format option provided.');
  285. }
  286. var formatter = formats.formatters[options.format];
  287. var objKeys;
  288. var filter;
  289. if (typeof options.filter === 'function') {
  290. filter = options.filter;
  291. obj = filter('', obj);
  292. } else if (Array.isArray(options.filter)) {
  293. filter = options.filter;
  294. objKeys = filter;
  295. }
  296. var keys = [];
  297. if (typeof obj !== 'object' || obj === null) {
  298. return '';
  299. }
  300. var arrayFormat;
  301. if (options.arrayFormat in arrayPrefixGenerators) {
  302. arrayFormat = options.arrayFormat;
  303. } else if ('indices' in options) {
  304. arrayFormat = options.indices ? 'indices' : 'repeat';
  305. } else {
  306. arrayFormat = 'indices';
  307. }
  308. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  309. if (!objKeys) {
  310. objKeys = Object.keys(obj);
  311. }
  312. if (sort) {
  313. objKeys.sort(sort);
  314. }
  315. for (var i = 0; i < objKeys.length; ++i) {
  316. var key = objKeys[i];
  317. if (skipNulls && obj[key] === null) {
  318. continue;
  319. }
  320. keys = keys.concat(stringify(
  321. obj[key],
  322. key,
  323. generateArrayPrefix,
  324. strictNullHandling,
  325. skipNulls,
  326. encoder,
  327. filter,
  328. sort,
  329. allowDots,
  330. serializeDate,
  331. formatter
  332. ));
  333. }
  334. return keys.join(delimiter);
  335. };
  336. },{"./formats":1,"./utils":5}],5:[function(require,module,exports){
  337. 'use strict';
  338. var has = Object.prototype.hasOwnProperty;
  339. var hexTable = (function () {
  340. var array = [];
  341. for (var i = 0; i < 256; ++i) {
  342. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  343. }
  344. return array;
  345. }());
  346. exports.arrayToObject = function (source, options) {
  347. var obj = options && options.plainObjects ? Object.create(null) : {};
  348. for (var i = 0; i < source.length; ++i) {
  349. if (typeof source[i] !== 'undefined') {
  350. obj[i] = source[i];
  351. }
  352. }
  353. return obj;
  354. };
  355. exports.merge = function (target, source, options) {
  356. if (!source) {
  357. return target;
  358. }
  359. if (typeof source !== 'object') {
  360. if (Array.isArray(target)) {
  361. target.push(source);
  362. } else if (typeof target === 'object') {
  363. if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
  364. target[source] = true;
  365. }
  366. } else {
  367. return [target, source];
  368. }
  369. return target;
  370. }
  371. if (typeof target !== 'object') {
  372. return [target].concat(source);
  373. }
  374. var mergeTarget = target;
  375. if (Array.isArray(target) && !Array.isArray(source)) {
  376. mergeTarget = exports.arrayToObject(target, options);
  377. }
  378. if (Array.isArray(target) && Array.isArray(source)) {
  379. source.forEach(function (item, i) {
  380. if (has.call(target, i)) {
  381. if (target[i] && typeof target[i] === 'object') {
  382. target[i] = exports.merge(target[i], item, options);
  383. } else {
  384. target.push(item);
  385. }
  386. } else {
  387. target[i] = item;
  388. }
  389. });
  390. return target;
  391. }
  392. return Object.keys(source).reduce(function (acc, key) {
  393. var value = source[key];
  394. if (Object.prototype.hasOwnProperty.call(acc, key)) {
  395. acc[key] = exports.merge(acc[key], value, options);
  396. } else {
  397. acc[key] = value;
  398. }
  399. return acc;
  400. }, mergeTarget);
  401. };
  402. exports.decode = function (str) {
  403. try {
  404. return decodeURIComponent(str.replace(/\+/g, ' '));
  405. } catch (e) {
  406. return str;
  407. }
  408. };
  409. exports.encode = function (str) {
  410. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  411. // It has been adapted here for stricter adherence to RFC 3986
  412. if (str.length === 0) {
  413. return str;
  414. }
  415. var string = typeof str === 'string' ? str : String(str);
  416. var out = '';
  417. for (var i = 0; i < string.length; ++i) {
  418. var c = string.charCodeAt(i);
  419. if (
  420. c === 0x2D || // -
  421. c === 0x2E || // .
  422. c === 0x5F || // _
  423. c === 0x7E || // ~
  424. (c >= 0x30 && c <= 0x39) || // 0-9
  425. (c >= 0x41 && c <= 0x5A) || // a-z
  426. (c >= 0x61 && c <= 0x7A) // A-Z
  427. ) {
  428. out += string.charAt(i);
  429. continue;
  430. }
  431. if (c < 0x80) {
  432. out = out + hexTable[c];
  433. continue;
  434. }
  435. if (c < 0x800) {
  436. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  437. continue;
  438. }
  439. if (c < 0xD800 || c >= 0xE000) {
  440. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  441. continue;
  442. }
  443. i += 1;
  444. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  445. out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; // eslint-disable-line max-len
  446. }
  447. return out;
  448. };
  449. exports.compact = function (obj, references) {
  450. if (typeof obj !== 'object' || obj === null) {
  451. return obj;
  452. }
  453. var refs = references || [];
  454. var lookup = refs.indexOf(obj);
  455. if (lookup !== -1) {
  456. return refs[lookup];
  457. }
  458. refs.push(obj);
  459. if (Array.isArray(obj)) {
  460. var compacted = [];
  461. for (var i = 0; i < obj.length; ++i) {
  462. if (obj[i] && typeof obj[i] === 'object') {
  463. compacted.push(exports.compact(obj[i], refs));
  464. } else if (typeof obj[i] !== 'undefined') {
  465. compacted.push(obj[i]);
  466. }
  467. }
  468. return compacted;
  469. }
  470. var keys = Object.keys(obj);
  471. keys.forEach(function (key) {
  472. obj[key] = exports.compact(obj[key], refs);
  473. });
  474. return obj;
  475. };
  476. exports.isRegExp = function (obj) {
  477. return Object.prototype.toString.call(obj) === '[object RegExp]';
  478. };
  479. exports.isBuffer = function (obj) {
  480. if (obj === null || typeof obj === 'undefined') {
  481. return false;
  482. }
  483. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  484. };
  485. },{}]},{},[2])(2)
  486. });