callLog.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var callLog_exports = {};
  20. __export(callLog_exports, {
  21. compressCallLog: () => compressCallLog,
  22. findRepeatedSubsequencesForTest: () => findRepeatedSubsequencesForTest
  23. });
  24. module.exports = __toCommonJS(callLog_exports);
  25. function compressCallLog(log) {
  26. const lines = [];
  27. for (const block of findRepeatedSubsequences(log)) {
  28. for (let i = 0; i < block.sequence.length; i++) {
  29. const line = block.sequence[i];
  30. const leadingWhitespace = line.match(/^\s*/);
  31. const whitespacePrefix = " " + leadingWhitespace?.[0] || "";
  32. const countPrefix = `${block.count} \xD7 `;
  33. if (block.count > 1 && i === 0)
  34. lines.push(whitespacePrefix + countPrefix + line.trim());
  35. else if (block.count > 1)
  36. lines.push(whitespacePrefix + " ".repeat(countPrefix.length - 2) + "- " + line.trim());
  37. else
  38. lines.push(whitespacePrefix + "- " + line.trim());
  39. }
  40. }
  41. return lines;
  42. }
  43. function findRepeatedSubsequences(s) {
  44. const n = s.length;
  45. const result = [];
  46. let i = 0;
  47. const arraysEqual = (a1, a2) => {
  48. if (a1.length !== a2.length)
  49. return false;
  50. for (let j = 0; j < a1.length; j++) {
  51. if (a1[j] !== a2[j])
  52. return false;
  53. }
  54. return true;
  55. };
  56. while (i < n) {
  57. let maxRepeatCount = 1;
  58. let maxRepeatSubstr = [s[i]];
  59. let maxRepeatLength = 1;
  60. for (let p = 1; p <= n - i; p++) {
  61. const substr = s.slice(i, i + p);
  62. let k = 1;
  63. while (i + p * k <= n && arraysEqual(s.slice(i + p * (k - 1), i + p * k), substr))
  64. k += 1;
  65. k -= 1;
  66. if (k > 1 && k * p > maxRepeatCount * maxRepeatLength) {
  67. maxRepeatCount = k;
  68. maxRepeatSubstr = substr;
  69. maxRepeatLength = p;
  70. }
  71. }
  72. result.push({ sequence: maxRepeatSubstr, count: maxRepeatCount });
  73. i += maxRepeatLength * maxRepeatCount;
  74. }
  75. return result;
  76. }
  77. const findRepeatedSubsequencesForTest = findRepeatedSubsequences;
  78. // Annotate the CommonJS export names for ESM import in node:
  79. 0 && (module.exports = {
  80. compressCallLog,
  81. findRepeatedSubsequencesForTest
  82. });