Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. // Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment
  3. const INDENT_REGEX = /^(?:( )+|\t+)/;
  4. function getMostUsed(indents) {
  5. let result = 0;
  6. let maxUsed = 0;
  7. let maxWeight = 0;
  8. for (const [key, [usedCount, weight]] of indents) {
  9. if (usedCount > maxUsed || (usedCount === maxUsed && weight > maxWeight)) {
  10. maxUsed = usedCount;
  11. maxWeight = weight;
  12. result = key;
  13. }
  14. }
  15. return result;
  16. }
  17. module.exports = string => {
  18. if (typeof string !== 'string') {
  19. throw new TypeError('Expected a string');
  20. }
  21. // Remember the size of previous line's indentation
  22. let previousSize = 0;
  23. let previousIndentType;
  24. // Indents key (ident type + size of the indents/unindents)
  25. let key;
  26. // Remember how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
  27. // The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
  28. //
  29. // indents = {
  30. // t3: [1, 0],
  31. // t4: [1, 5],
  32. // s5: [1, 0],
  33. // s12: [1, 0],
  34. // }
  35. const indents = new Map();
  36. for (const line of string.split(/\n/g)) {
  37. if (!line) {
  38. // Ignore empty lines
  39. continue;
  40. }
  41. let indent;
  42. let indentType;
  43. let weight;
  44. let entry;
  45. const matches = line.match(INDENT_REGEX);
  46. if (matches === null) {
  47. previousSize = 0;
  48. previousIndentType = '';
  49. } else {
  50. indent = matches[0].length;
  51. if (matches[1]) {
  52. indentType = 's';
  53. } else {
  54. indentType = 't';
  55. }
  56. if (indentType !== previousIndentType) {
  57. previousSize = 0;
  58. }
  59. previousIndentType = indentType;
  60. weight = 0;
  61. const indentDifference = indent - previousSize;
  62. previousSize = indent;
  63. // Previous line have same indent?
  64. if (indentDifference === 0) {
  65. weight++;
  66. // We use the key from previous loop
  67. } else {
  68. key = indentType + String(indentDifference > 0 ? indentDifference : -indentDifference);
  69. }
  70. // Update the stats
  71. entry = indents.get(key);
  72. if (entry === undefined) {
  73. entry = [1, 0]; // Init
  74. } else {
  75. entry = [++entry[0], entry[1] + weight];
  76. }
  77. indents.set(key, entry);
  78. }
  79. }
  80. const result = getMostUsed(indents);
  81. let amount = 0;
  82. let type;
  83. let indent = '';
  84. if (result !== 0) {
  85. amount = Number(result.slice(1));
  86. if (result[0] === 's') {
  87. type = 'space';
  88. indent = ' '.repeat(amount);
  89. } else {
  90. type = 'tab';
  91. indent = '\t'.repeat(amount);
  92. }
  93. }
  94. return {
  95. amount,
  96. type,
  97. indent
  98. };
  99. };