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