You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qs.js 25KB

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