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