parse.js 5.3 KB

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