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.

156 lines
3.8 KiB

8 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 <= options.arrayLimit) {
  50. obj = [];
  51. obj[index] = internals.parseObject(chain, val, options);
  52. }
  53. else {
  54. obj[cleanRoot] = internals.parseObject(chain, val, options);
  55. }
  56. }
  57. return obj;
  58. };
  59. internals.parseKeys = function (key, val, options) {
  60. if (!key) {
  61. return;
  62. }
  63. // The regex chunks
  64. var parent = /^([^\[\]]*)/;
  65. var child = /(\[[^\[\]]*\])/g;
  66. // Get the parent
  67. var segment = parent.exec(key);
  68. // Don't allow them to overwrite object prototype properties
  69. if (Object.prototype.hasOwnProperty(segment[1])) {
  70. return;
  71. }
  72. // Stash the parent if it exists
  73. var keys = [];
  74. if (segment[1]) {
  75. keys.push(segment[1]);
  76. }
  77. // Loop through children appending to the array until we hit depth
  78. var i = 0;
  79. while ((segment = child.exec(key)) !== null && i < options.depth) {
  80. ++i;
  81. if (!Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
  82. keys.push(segment[1]);
  83. }
  84. }
  85. // If there's a remainder, just add whatever is left
  86. if (segment) {
  87. keys.push('[' + key.slice(segment.index) + ']');
  88. }
  89. return internals.parseObject(keys, val, options);
  90. };
  91. module.exports = function (str, options) {
  92. if (str === '' ||
  93. str === null ||
  94. typeof str === 'undefined') {
  95. return {};
  96. }
  97. options = options || {};
  98. options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
  99. options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
  100. options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
  101. options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
  102. var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
  103. var obj = {};
  104. // Iterate over the keys and setup the new object
  105. var keys = Object.keys(tempObj);
  106. for (var i = 0, il = keys.length; i < il; ++i) {
  107. var key = keys[i];
  108. var newObj = internals.parseKeys(key, tempObj[key], options);
  109. obj = Utils.merge(obj, newObj);
  110. }
  111. return Utils.compact(obj);
  112. };