qs.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. var Format = {
  6. RFC1738: 'RFC1738',
  7. RFC3986: 'RFC3986'
  8. };
  9. module.exports = {
  10. 'default': Format.RFC3986,
  11. formatters: {
  12. RFC1738: function (value) {
  13. return replace.call(value, percentTwenties, '+');
  14. },
  15. RFC3986: function (value) {
  16. return String(value);
  17. }
  18. },
  19. RFC1738: Format.RFC1738,
  20. RFC3986: Format.RFC3986
  21. };
  22. },{}],2:[function(require,module,exports){
  23. 'use strict';
  24. var stringify = require('./stringify');
  25. var parse = require('./parse');
  26. var formats = require('./formats');
  27. module.exports = {
  28. formats: formats,
  29. parse: parse,
  30. stringify: stringify
  31. };
  32. },{"./formats":1,"./parse":3,"./stringify":4}],3:[function(require,module,exports){
  33. 'use strict';
  34. var utils = require('./utils');
  35. var has = Object.prototype.hasOwnProperty;
  36. var isArray = Array.isArray;
  37. var defaults = {
  38. allowDots: false,
  39. allowPrototypes: false,
  40. arrayLimit: 20,
  41. charset: 'utf-8',
  42. charsetSentinel: false,
  43. comma: false,
  44. decoder: utils.decode,
  45. delimiter: '&',
  46. depth: 5,
  47. ignoreQueryPrefix: false,
  48. interpretNumericEntities: false,
  49. parameterLimit: 1000,
  50. parseArrays: true,
  51. plainObjects: false,
  52. strictNullHandling: false
  53. };
  54. var interpretNumericEntities = function (str) {
  55. return str.replace(/&#(\d+);/g, function ($0, numberStr) {
  56. return String.fromCharCode(parseInt(numberStr, 10));
  57. });
  58. };
  59. var parseArrayValue = function (val, options) {
  60. if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) {
  61. return val.split(',');
  62. }
  63. return val;
  64. };
  65. // This is what browsers will submit when the ✓ character occurs in an
  66. // application/x-www-form-urlencoded body and the encoding of the page containing
  67. // the form is iso-8859-1, or when the submitted form has an accept-charset
  68. // attribute of iso-8859-1. Presumably also with other charsets that do not contain
  69. // the ✓ character, such as us-ascii.
  70. var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('&#10003;')
  71. // These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded.
  72. var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
  73. var parseValues = function parseQueryStringValues(str, options) {
  74. var obj = {};
  75. var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
  76. var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
  77. var parts = cleanStr.split(options.delimiter, limit);
  78. var skipIndex = -1; // Keep track of where the utf8 sentinel was found
  79. var i;
  80. var charset = options.charset;
  81. if (options.charsetSentinel) {
  82. for (i = 0; i < parts.length; ++i) {
  83. if (parts[i].indexOf('utf8=') === 0) {
  84. if (parts[i] === charsetSentinel) {
  85. charset = 'utf-8';
  86. } else if (parts[i] === isoSentinel) {
  87. charset = 'iso-8859-1';
  88. }
  89. skipIndex = i;
  90. i = parts.length; // The eslint settings do not allow break;
  91. }
  92. }
  93. }
  94. for (i = 0; i < parts.length; ++i) {
  95. if (i === skipIndex) {
  96. continue;
  97. }
  98. var part = parts[i];
  99. var bracketEqualsPos = part.indexOf(']=');
  100. var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1;
  101. var key, val;
  102. if (pos === -1) {
  103. key = options.decoder(part, defaults.decoder, charset, 'key');
  104. val = options.strictNullHandling ? null : '';
  105. } else {
  106. key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
  107. val = utils.maybeMap(
  108. parseArrayValue(part.slice(pos + 1), options),
  109. function (encodedVal) {
  110. return options.decoder(encodedVal, defaults.decoder, charset, 'value');
  111. }
  112. );
  113. }
  114. if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
  115. val = interpretNumericEntities(val);
  116. }
  117. if (part.indexOf('[]=') > -1) {
  118. val = isArray(val) ? [val] : val;
  119. }
  120. if (has.call(obj, key)) {
  121. obj[key] = utils.combine(obj[key], val);
  122. } else {
  123. obj[key] = val;
  124. }
  125. }
  126. return obj;
  127. };
  128. var parseObject = function (chain, val, options, valuesParsed) {
  129. var leaf = valuesParsed ? val : parseArrayValue(val, options);
  130. for (var i = chain.length - 1; i >= 0; --i) {
  131. var obj;
  132. var root = chain[i];
  133. if (root === '[]' && options.parseArrays) {
  134. obj = [].concat(leaf);
  135. } else {
  136. obj = options.plainObjects ? Object.create(null) : {};
  137. var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
  138. var index = parseInt(cleanRoot, 10);
  139. if (!options.parseArrays && cleanRoot === '') {
  140. obj = { 0: leaf };
  141. } else if (
  142. !isNaN(index)
  143. && root !== cleanRoot
  144. && String(index) === cleanRoot
  145. && index >= 0
  146. && (options.parseArrays && index <= options.arrayLimit)
  147. ) {
  148. obj = [];
  149. obj[index] = leaf;
  150. } else {
  151. obj[cleanRoot] = leaf;
  152. }
  153. }
  154. leaf = obj;
  155. }
  156. return leaf;
  157. };
  158. var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
  159. if (!givenKey) {
  160. return;
  161. }
  162. // Transform dot notation to bracket notation
  163. var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
  164. // The regex chunks
  165. var brackets = /(\[[^[\]]*])/;
  166. var child = /(\[[^[\]]*])/g;
  167. // Get the parent
  168. var segment = options.depth > 0 && brackets.exec(key);
  169. var parent = segment ? key.slice(0, segment.index) : key;
  170. // Stash the parent if it exists
  171. var keys = [];
  172. if (parent) {
  173. // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
  174. if (!options.plainObjects && has.call(Object.prototype, parent)) {
  175. if (!options.allowPrototypes) {
  176. return;
  177. }
  178. }
  179. keys.push(parent);
  180. }
  181. // Loop through children appending to the array until we hit depth
  182. var i = 0;
  183. while (options.depth > 0 && (segment = child.exec(key)) !== null && i < options.depth) {
  184. i += 1;
  185. if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
  186. if (!options.allowPrototypes) {
  187. return;
  188. }
  189. }
  190. keys.push(segment[1]);
  191. }
  192. // If there's a remainder, just add whatever is left
  193. if (segment) {
  194. keys.push('[' + key.slice(segment.index) + ']');
  195. }
  196. return parseObject(keys, val, options, valuesParsed);
  197. };
  198. var normalizeParseOptions = function normalizeParseOptions(opts) {
  199. if (!opts) {
  200. return defaults;
  201. }
  202. if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') {
  203. throw new TypeError('Decoder has to be a function.');
  204. }
  205. if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
  206. throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
  207. }
  208. var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
  209. return {
  210. allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
  211. allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
  212. arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit,
  213. charset: charset,
  214. charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
  215. comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
  216. decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
  217. delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
  218. // eslint-disable-next-line no-implicit-coercion, no-extra-parens
  219. depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth,
  220. ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
  221. interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
  222. parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
  223. parseArrays: opts.parseArrays !== false,
  224. plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
  225. strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
  226. };
  227. };
  228. module.exports = function (str, opts) {
  229. var options = normalizeParseOptions(opts);
  230. if (str === '' || str === null || typeof str === 'undefined') {
  231. return options.plainObjects ? Object.create(null) : {};
  232. }
  233. var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  234. var obj = options.plainObjects ? Object.create(null) : {};
  235. // Iterate over the keys and setup the new object
  236. var keys = Object.keys(tempObj);
  237. for (var i = 0; i < keys.length; ++i) {
  238. var key = keys[i];
  239. var newObj = parseKeys(key, tempObj[key], options, typeof str === 'string');
  240. obj = utils.merge(obj, newObj, options);
  241. }
  242. return utils.compact(obj);
  243. };
  244. },{"./utils":5}],4:[function(require,module,exports){
  245. 'use strict';
  246. var utils = require('./utils');
  247. var formats = require('./formats');
  248. var has = Object.prototype.hasOwnProperty;
  249. var arrayPrefixGenerators = {
  250. brackets: function brackets(prefix) {
  251. return prefix + '[]';
  252. },
  253. comma: 'comma',
  254. indices: function indices(prefix, key) {
  255. return prefix + '[' + key + ']';
  256. },
  257. repeat: function repeat(prefix) {
  258. return prefix;
  259. }
  260. };
  261. var isArray = Array.isArray;
  262. var push = Array.prototype.push;
  263. var pushToArray = function (arr, valueOrArray) {
  264. push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
  265. };
  266. var toISO = Date.prototype.toISOString;
  267. var defaultFormat = formats['default'];
  268. var defaults = {
  269. addQueryPrefix: false,
  270. allowDots: false,
  271. charset: 'utf-8',
  272. charsetSentinel: false,
  273. delimiter: '&',
  274. encode: true,
  275. encoder: utils.encode,
  276. encodeValuesOnly: false,
  277. format: defaultFormat,
  278. formatter: formats.formatters[defaultFormat],
  279. // deprecated
  280. indices: false,
  281. serializeDate: function serializeDate(date) {
  282. return toISO.call(date);
  283. },
  284. skipNulls: false,
  285. strictNullHandling: false
  286. };
  287. var isNonNullishPrimitive = function isNonNullishPrimitive(v) {
  288. return typeof v === 'string'
  289. || typeof v === 'number'
  290. || typeof v === 'boolean'
  291. || typeof v === 'symbol'
  292. || typeof v === 'bigint';
  293. };
  294. var stringify = function stringify(
  295. object,
  296. prefix,
  297. generateArrayPrefix,
  298. strictNullHandling,
  299. skipNulls,
  300. encoder,
  301. filter,
  302. sort,
  303. allowDots,
  304. serializeDate,
  305. format,
  306. formatter,
  307. encodeValuesOnly,
  308. charset
  309. ) {
  310. var obj = object;
  311. if (typeof filter === 'function') {
  312. obj = filter(prefix, obj);
  313. } else if (obj instanceof Date) {
  314. obj = serializeDate(obj);
  315. } else if (generateArrayPrefix === 'comma' && isArray(obj)) {
  316. obj = utils.maybeMap(obj, function (value) {
  317. if (value instanceof Date) {
  318. return serializeDate(value);
  319. }
  320. return value;
  321. });
  322. }
  323. if (obj === null) {
  324. if (strictNullHandling) {
  325. return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix;
  326. }
  327. obj = '';
  328. }
  329. if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
  330. if (encoder) {
  331. var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key', format);
  332. return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
  333. }
  334. return [formatter(prefix) + '=' + formatter(String(obj))];
  335. }
  336. var values = [];
  337. if (typeof obj === 'undefined') {
  338. return values;
  339. }
  340. var objKeys;
  341. if (generateArrayPrefix === 'comma' && isArray(obj)) {
  342. // we need to join elements in
  343. objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }];
  344. } else if (isArray(filter)) {
  345. objKeys = filter;
  346. } else {
  347. var keys = Object.keys(obj);
  348. objKeys = sort ? keys.sort(sort) : keys;
  349. }
  350. for (var i = 0; i < objKeys.length; ++i) {
  351. var key = objKeys[i];
  352. var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];
  353. if (skipNulls && value === null) {
  354. continue;
  355. }
  356. var keyPrefix = isArray(obj)
  357. ? typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix
  358. : prefix + (allowDots ? '.' + key : '[' + key + ']');
  359. pushToArray(values, stringify(
  360. value,
  361. keyPrefix,
  362. generateArrayPrefix,
  363. strictNullHandling,
  364. skipNulls,
  365. encoder,
  366. filter,
  367. sort,
  368. allowDots,
  369. serializeDate,
  370. format,
  371. formatter,
  372. encodeValuesOnly,
  373. charset
  374. ));
  375. }
  376. return values;
  377. };
  378. var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
  379. if (!opts) {
  380. return defaults;
  381. }
  382. if (opts.encoder !== null && opts.encoder !== undefined && typeof opts.encoder !== 'function') {
  383. throw new TypeError('Encoder has to be a function.');
  384. }
  385. var charset = opts.charset || defaults.charset;
  386. if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
  387. throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
  388. }
  389. var format = formats['default'];
  390. if (typeof opts.format !== 'undefined') {
  391. if (!has.call(formats.formatters, opts.format)) {
  392. throw new TypeError('Unknown format option provided.');
  393. }
  394. format = opts.format;
  395. }
  396. var formatter = formats.formatters[format];
  397. var filter = defaults.filter;
  398. if (typeof opts.filter === 'function' || isArray(opts.filter)) {
  399. filter = opts.filter;
  400. }
  401. return {
  402. addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
  403. allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
  404. charset: charset,
  405. charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
  406. delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
  407. encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
  408. encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
  409. encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
  410. filter: filter,
  411. format: format,
  412. formatter: formatter,
  413. serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
  414. skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
  415. sort: typeof opts.sort === 'function' ? opts.sort : null,
  416. strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
  417. };
  418. };
  419. module.exports = function (object, opts) {
  420. var obj = object;
  421. var options = normalizeStringifyOptions(opts);
  422. var objKeys;
  423. var filter;
  424. if (typeof options.filter === 'function') {
  425. filter = options.filter;
  426. obj = filter('', obj);
  427. } else if (isArray(options.filter)) {
  428. filter = options.filter;
  429. objKeys = filter;
  430. }
  431. var keys = [];
  432. if (typeof obj !== 'object' || obj === null) {
  433. return '';
  434. }
  435. var arrayFormat;
  436. if (opts && opts.arrayFormat in arrayPrefixGenerators) {
  437. arrayFormat = opts.arrayFormat;
  438. } else if (opts && 'indices' in opts) {
  439. arrayFormat = opts.indices ? 'indices' : 'repeat';
  440. } else {
  441. arrayFormat = 'indices';
  442. }
  443. var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  444. if (!objKeys) {
  445. objKeys = Object.keys(obj);
  446. }
  447. if (options.sort) {
  448. objKeys.sort(options.sort);
  449. }
  450. for (var i = 0; i < objKeys.length; ++i) {
  451. var key = objKeys[i];
  452. if (options.skipNulls && obj[key] === null) {
  453. continue;
  454. }
  455. pushToArray(keys, stringify(
  456. obj[key],
  457. key,
  458. generateArrayPrefix,
  459. options.strictNullHandling,
  460. options.skipNulls,
  461. options.encode ? options.encoder : null,
  462. options.filter,
  463. options.sort,
  464. options.allowDots,
  465. options.serializeDate,
  466. options.format,
  467. options.formatter,
  468. options.encodeValuesOnly,
  469. options.charset
  470. ));
  471. }
  472. var joined = keys.join(options.delimiter);
  473. var prefix = options.addQueryPrefix === true ? '?' : '';
  474. if (options.charsetSentinel) {
  475. if (options.charset === 'iso-8859-1') {
  476. // encodeURIComponent('&#10003;'), the "numeric entity" representation of a checkmark
  477. prefix += 'utf8=%26%2310003%3B&';
  478. } else {
  479. // encodeURIComponent('✓')
  480. prefix += 'utf8=%E2%9C%93&';
  481. }
  482. }
  483. return joined.length > 0 ? prefix + joined : '';
  484. };
  485. },{"./formats":1,"./utils":5}],5:[function(require,module,exports){
  486. 'use strict';
  487. var formats = require('./formats');
  488. var has = Object.prototype.hasOwnProperty;
  489. var isArray = Array.isArray;
  490. var hexTable = (function () {
  491. var array = [];
  492. for (var i = 0; i < 256; ++i) {
  493. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  494. }
  495. return array;
  496. }());
  497. var compactQueue = function compactQueue(queue) {
  498. while (queue.length > 1) {
  499. var item = queue.pop();
  500. var obj = item.obj[item.prop];
  501. if (isArray(obj)) {
  502. var compacted = [];
  503. for (var j = 0; j < obj.length; ++j) {
  504. if (typeof obj[j] !== 'undefined') {
  505. compacted.push(obj[j]);
  506. }
  507. }
  508. item.obj[item.prop] = compacted;
  509. }
  510. }
  511. };
  512. var arrayToObject = function arrayToObject(source, options) {
  513. var obj = options && options.plainObjects ? Object.create(null) : {};
  514. for (var i = 0; i < source.length; ++i) {
  515. if (typeof source[i] !== 'undefined') {
  516. obj[i] = source[i];
  517. }
  518. }
  519. return obj;
  520. };
  521. var merge = function merge(target, source, options) {
  522. /* eslint no-param-reassign: 0 */
  523. if (!source) {
  524. return target;
  525. }
  526. if (typeof source !== 'object') {
  527. if (isArray(target)) {
  528. target.push(source);
  529. } else if (target && typeof target === 'object') {
  530. if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
  531. target[source] = true;
  532. }
  533. } else {
  534. return [target, source];
  535. }
  536. return target;
  537. }
  538. if (!target || typeof target !== 'object') {
  539. return [target].concat(source);
  540. }
  541. var mergeTarget = target;
  542. if (isArray(target) && !isArray(source)) {
  543. mergeTarget = arrayToObject(target, options);
  544. }
  545. if (isArray(target) && isArray(source)) {
  546. source.forEach(function (item, i) {
  547. if (has.call(target, i)) {
  548. var targetItem = target[i];
  549. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  550. target[i] = merge(targetItem, item, options);
  551. } else {
  552. target.push(item);
  553. }
  554. } else {
  555. target[i] = item;
  556. }
  557. });
  558. return target;
  559. }
  560. return Object.keys(source).reduce(function (acc, key) {
  561. var value = source[key];
  562. if (has.call(acc, key)) {
  563. acc[key] = merge(acc[key], value, options);
  564. } else {
  565. acc[key] = value;
  566. }
  567. return acc;
  568. }, mergeTarget);
  569. };
  570. var assign = function assignSingleSource(target, source) {
  571. return Object.keys(source).reduce(function (acc, key) {
  572. acc[key] = source[key];
  573. return acc;
  574. }, target);
  575. };
  576. var decode = function (str, decoder, charset) {
  577. var strWithoutPlus = str.replace(/\+/g, ' ');
  578. if (charset === 'iso-8859-1') {
  579. // unescape never throws, no try...catch needed:
  580. return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
  581. }
  582. // utf-8
  583. try {
  584. return decodeURIComponent(strWithoutPlus);
  585. } catch (e) {
  586. return strWithoutPlus;
  587. }
  588. };
  589. var encode = function encode(str, defaultEncoder, charset, kind, format) {
  590. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  591. // It has been adapted here for stricter adherence to RFC 3986
  592. if (str.length === 0) {
  593. return str;
  594. }
  595. var string = str;
  596. if (typeof str === 'symbol') {
  597. string = Symbol.prototype.toString.call(str);
  598. } else if (typeof str !== 'string') {
  599. string = String(str);
  600. }
  601. if (charset === 'iso-8859-1') {
  602. return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
  603. return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
  604. });
  605. }
  606. var out = '';
  607. for (var i = 0; i < string.length; ++i) {
  608. var c = string.charCodeAt(i);
  609. if (
  610. c === 0x2D // -
  611. || c === 0x2E // .
  612. || c === 0x5F // _
  613. || c === 0x7E // ~
  614. || (c >= 0x30 && c <= 0x39) // 0-9
  615. || (c >= 0x41 && c <= 0x5A) // a-z
  616. || (c >= 0x61 && c <= 0x7A) // A-Z
  617. || (format === formats.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
  618. ) {
  619. out += string.charAt(i);
  620. continue;
  621. }
  622. if (c < 0x80) {
  623. out = out + hexTable[c];
  624. continue;
  625. }
  626. if (c < 0x800) {
  627. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  628. continue;
  629. }
  630. if (c < 0xD800 || c >= 0xE000) {
  631. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  632. continue;
  633. }
  634. i += 1;
  635. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  636. out += hexTable[0xF0 | (c >> 18)]
  637. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  638. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  639. + hexTable[0x80 | (c & 0x3F)];
  640. }
  641. return out;
  642. };
  643. var compact = function compact(value) {
  644. var queue = [{ obj: { o: value }, prop: 'o' }];
  645. var refs = [];
  646. for (var i = 0; i < queue.length; ++i) {
  647. var item = queue[i];
  648. var obj = item.obj[item.prop];
  649. var keys = Object.keys(obj);
  650. for (var j = 0; j < keys.length; ++j) {
  651. var key = keys[j];
  652. var val = obj[key];
  653. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  654. queue.push({ obj: obj, prop: key });
  655. refs.push(val);
  656. }
  657. }
  658. }
  659. compactQueue(queue);
  660. return value;
  661. };
  662. var isRegExp = function isRegExp(obj) {
  663. return Object.prototype.toString.call(obj) === '[object RegExp]';
  664. };
  665. var isBuffer = function isBuffer(obj) {
  666. if (!obj || typeof obj !== 'object') {
  667. return false;
  668. }
  669. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  670. };
  671. var combine = function combine(a, b) {
  672. return [].concat(a, b);
  673. };
  674. var maybeMap = function maybeMap(val, fn) {
  675. if (isArray(val)) {
  676. var mapped = [];
  677. for (var i = 0; i < val.length; i += 1) {
  678. mapped.push(fn(val[i]));
  679. }
  680. return mapped;
  681. }
  682. return fn(val);
  683. };
  684. module.exports = {
  685. arrayToObject: arrayToObject,
  686. assign: assign,
  687. combine: combine,
  688. compact: compact,
  689. decode: decode,
  690. encode: encode,
  691. isBuffer: isBuffer,
  692. isRegExp: isRegExp,
  693. maybeMap: maybeMap,
  694. merge: merge
  695. };
  696. },{"./formats":1}]},{},[2])(2)
  697. });