parse.js 5.2 KB

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