parse.js 5.8 KB

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