parse.js 5.7 KB

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