parse.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. var Utils = require('./utils');
  3. var has = Object.prototype.hasOwnProperty;
  4. var defaults = {
  5. delimiter: '&',
  6. depth: 5,
  7. arrayLimit: 20,
  8. parameterLimit: 1000,
  9. strictNullHandling: false,
  10. plainObjects: false,
  11. allowPrototypes: false,
  12. allowDots: false,
  13. decoder: Utils.decode
  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.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -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 brackets = /(\[[^[\]]*])/;
  73. var child = /(\[[^[\]]*])/g;
  74. // Get the parent
  75. var segment = brackets.exec(key);
  76. var parent = segment ? key.slice(0, segment.index) : key;
  77. // Stash the parent if it exists
  78. var keys = [];
  79. if (parent) {
  80. // If we aren't using plain objects, optionally prefix keys
  81. // that would overwrite object prototype properties
  82. if (!options.plainObjects && has.call(Object.prototype, parent)) {
  83. if (!options.allowPrototypes) {
  84. return;
  85. }
  86. }
  87. keys.push(parent);
  88. }
  89. // Loop through children appending to the array until we hit depth
  90. var i = 0;
  91. while ((segment = child.exec(key)) !== null && i < options.depth) {
  92. i += 1;
  93. if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
  94. if (!options.allowPrototypes) {
  95. return;
  96. }
  97. }
  98. keys.push(segment[1]);
  99. }
  100. // If there's a remainder, just add whatever is left
  101. if (segment) {
  102. keys.push('[' + key.slice(segment.index) + ']');
  103. }
  104. return parseObject(keys, val, options);
  105. };
  106. module.exports = function (str, opts) {
  107. var options = opts || {};
  108. if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
  109. throw new TypeError('Decoder has to be a function.');
  110. }
  111. options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
  112. options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
  113. options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
  114. options.parseArrays = options.parseArrays !== false;
  115. options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
  116. options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
  117. options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
  118. options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
  119. options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
  120. options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  121. if (str === '' || str === null || typeof str === 'undefined') {
  122. return options.plainObjects ? Object.create(null) : {};
  123. }
  124. var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  125. var obj = options.plainObjects ? Object.create(null) : {};
  126. // Iterate over the keys and setup the new object
  127. var keys = Object.keys(tempObj);
  128. for (var i = 0; i < keys.length; ++i) {
  129. var key = keys[i];
  130. var newObj = parseKeys(key, tempObj[key], options);
  131. obj = Utils.merge(obj, newObj, options);
  132. }
  133. return Utils.compact(obj);
  134. };