qs.js 17 KB

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