You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.9 KiB

7 years ago
  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. };
  10. internals.parseValues = function (str, options) {
  11. var obj = {};
  12. var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
  13. for (var i = 0, il = parts.length; i < il; ++i) {
  14. var part = parts[i];
  15. var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
  16. if (pos === -1) {
  17. obj[Utils.decode(part)] = '';
  18. }
  19. else {
  20. var key = Utils.decode(part.slice(0, pos));
  21. var val = Utils.decode(part.slice(pos + 1));
  22. if (!obj.hasOwnProperty(key)) {
  23. obj[key] = val;
  24. }
  25. else {
  26. obj[key] = [].concat(obj[key]).concat(val);
  27. }
  28. }
  29. }
  30. return obj;
  31. };
  32. internals.parseObject = function (chain, val, options) {
  33. if (!chain.length) {
  34. return val;
  35. }
  36. var root = chain.shift();
  37. var obj = {};
  38. if (root === '[]') {
  39. obj = [];
  40. obj = obj.concat(internals.parseObject(chain, val, options));
  41. }
  42. else {
  43. var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
  44. var index = parseInt(cleanRoot, 10);
  45. var indexString = '' + index;
  46. if (!isNaN(index) &&
  47. root !== cleanRoot &&
  48. indexString === cleanRoot &&
  49. index >= 0 &&
  50. index <= options.arrayLimit) {
  51. obj = [];
  52. obj[index] = internals.parseObject(chain, val, options);
  53. }
  54. else {
  55. obj[cleanRoot] = internals.parseObject(chain, val, options);
  56. }
  57. }
  58. return obj;
  59. };
  60. internals.parseKeys = function (key, val, options) {
  61. if (!key) {
  62. return;
  63. }
  64. // The regex chunks
  65. var parent = /^([^\[\]]*)/;
  66. var child = /(\[[^\[\]]*\])/g;
  67. // Get the parent
  68. var segment = parent.exec(key);
  69. // Don't allow them to overwrite object prototype properties
  70. if (Object.prototype.hasOwnProperty(segment[1])) {
  71. return;
  72. }
  73. // Stash the parent if it exists
  74. var keys = [];
  75. if (segment[1]) {
  76. keys.push(segment[1]);
  77. }
  78. // Loop through children appending to the array until we hit depth
  79. var i = 0;
  80. while ((segment = child.exec(key)) !== null && i < options.depth) {
  81. ++i;
  82. if (!Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
  83. keys.push(segment[1]);
  84. }
  85. }
  86. // If there's a remainder, just add whatever is left
  87. if (segment) {
  88. keys.push('[' + key.slice(segment.index) + ']');
  89. }
  90. return internals.parseObject(keys, val, options);
  91. };
  92. module.exports = function (str, options) {
  93. if (str === '' ||
  94. str === null ||
  95. typeof str === 'undefined') {
  96. return {};
  97. }
  98. options = options || {};
  99. options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
  100. options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
  101. options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
  102. options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
  103. var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
  104. var obj = {};
  105. // Iterate over the keys and setup the new object
  106. var keys = Object.keys(tempObj);
  107. for (var i = 0, il = keys.length; i < il; ++i) {
  108. var key = keys[i];
  109. var newObj = internals.parseKeys(key, tempObj[key], options);
  110. obj = Utils.merge(obj, newObj);
  111. }
  112. return Utils.compact(obj);
  113. };