parse.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. var utils = require('./utils');
  3. var has = Object.prototype.hasOwnProperty;
  4. var defaults = {
  5. allowDots: false,
  6. allowPrototypes: false,
  7. arrayLimit: 20,
  8. decoder: utils.decode,
  9. delimiter: '&',
  10. depth: 5,
  11. parameterLimit: 1000,
  12. plainObjects: false,
  13. strictNullHandling: false
  14. };
  15. var parseValues = function parseQueryStringValues(str, options) {
  16. var obj = {};
  17. var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
  18. for (var i = 0; i < parts.length; ++i) {
  19. var part = parts[i];
  20. var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
  21. var key, val;
  22. if (pos === -1) {
  23. key = options.decoder(part);
  24. val = options.strictNullHandling ? null : '';
  25. } else {
  26. key = options.decoder(part.slice(0, pos));
  27. val = options.decoder(part.slice(pos + 1));
  28. }
  29. if (has.call(obj, key)) {
  30. obj[key] = [].concat(obj[key]).concat(val);
  31. } else {
  32. obj[key] = val;
  33. }
  34. }
  35. return obj;
  36. };
  37. var parseObject = function parseObjectRecursive(chain, val, options) {
  38. if (!chain.length) {
  39. return val;
  40. }
  41. var root = chain.shift();
  42. var obj;
  43. if (root === '[]' && options.parseArrays) {
  44. obj = [];
  45. obj = obj.concat(parseObject(chain, val, options));
  46. } else {
  47. obj = options.plainObjects ? Object.create(null) : {};
  48. var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
  49. var index = parseInt(cleanRoot, 10);
  50. if (!options.parseArrays && cleanRoot === '') {
  51. obj = { 0: val };
  52. } else if (
  53. !isNaN(index)
  54. && root !== cleanRoot
  55. && String(index) === cleanRoot
  56. && index >= 0
  57. && (options.parseArrays && index <= options.arrayLimit)
  58. ) {
  59. obj = [];
  60. obj[index] = parseObject(chain, val, options);
  61. } else if (cleanRoot !== '__proto__') {
  62. obj[cleanRoot] = parseObject(chain, val, options);
  63. }
  64. }
  65. return obj;
  66. };
  67. var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
  68. if (!givenKey) {
  69. return;
  70. }
  71. // Transform dot notation to bracket notation
  72. var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
  73. // The regex chunks
  74. var brackets = /(\[[^[\]]*])/;
  75. var child = /(\[[^[\]]*])/g;
  76. // Get the parent
  77. var segment = brackets.exec(key);
  78. var parent = segment ? key.slice(0, segment.index) : key;
  79. // Stash the parent if it exists
  80. var keys = [];
  81. if (parent) {
  82. // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
  83. if (!options.plainObjects && has.call(Object.prototype, parent)) {
  84. if (!options.allowPrototypes) {
  85. return;
  86. }
  87. }
  88. keys.push(parent);
  89. }
  90. // Loop through children appending to the array until we hit depth
  91. var i = 0;
  92. while ((segment = child.exec(key)) !== null && i < options.depth) {
  93. i += 1;
  94. if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
  95. if (!options.allowPrototypes) {
  96. return;
  97. }
  98. }
  99. keys.push(segment[1]);
  100. }
  101. // If there's a remainder, just add whatever is left
  102. if (segment) {
  103. keys.push('[' + key.slice(segment.index) + ']');
  104. }
  105. return parseObject(keys, val, options);
  106. };
  107. module.exports = function (str, opts) {
  108. var options = opts || {};
  109. if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
  110. throw new TypeError('Decoder has to be a function.');
  111. }
  112. options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
  113. options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
  114. options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
  115. options.parseArrays = options.parseArrays !== false;
  116. options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
  117. options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
  118. options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
  119. options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
  120. options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
  121. options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  122. if (str === '' || str === null || typeof str === 'undefined') {
  123. return options.plainObjects ? Object.create(null) : {};
  124. }
  125. var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  126. var obj = options.plainObjects ? Object.create(null) : {};
  127. // Iterate over the keys and setup the new object
  128. var keys = Object.keys(tempObj);
  129. for (var i = 0; i < keys.length; ++i) {
  130. var key = keys[i];
  131. var newObj = parseKeys(key, tempObj[key], options);
  132. obj = utils.merge(obj, newObj, options);
  133. }
  134. return utils.compact(obj);
  135. };