drawBorder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.drawBorderTop = exports.drawBorderJoin = exports.drawBorderBottom = exports.drawBorder = exports.createTableBorderGetter = void 0;
  4. const drawContent_1 = require("./drawContent");
  5. const drawBorder = (columnWidths, config) => {
  6. const { separator, drawVerticalLine } = config;
  7. const columns = columnWidths.map((size) => {
  8. return config.separator.body.repeat(size);
  9. });
  10. return drawContent_1.drawContent(columns, {
  11. drawSeparator: drawVerticalLine,
  12. separatorGetter: (index, columnCount) => {
  13. if (index === 0) {
  14. return separator.left;
  15. }
  16. if (index === columnCount) {
  17. return separator.right;
  18. }
  19. return separator.join;
  20. },
  21. }) + '\n';
  22. };
  23. exports.drawBorder = drawBorder;
  24. const drawBorderTop = (columnWidths, config) => {
  25. const result = drawBorder(columnWidths, {
  26. ...config,
  27. separator: {
  28. body: config.border.topBody,
  29. join: config.border.topJoin,
  30. left: config.border.topLeft,
  31. right: config.border.topRight,
  32. },
  33. });
  34. if (result === '\n') {
  35. return '';
  36. }
  37. return result;
  38. };
  39. exports.drawBorderTop = drawBorderTop;
  40. const drawBorderJoin = (columnWidths, config) => {
  41. return drawBorder(columnWidths, {
  42. ...config,
  43. separator: {
  44. body: config.border.joinBody,
  45. join: config.border.joinJoin,
  46. left: config.border.joinLeft,
  47. right: config.border.joinRight,
  48. },
  49. });
  50. };
  51. exports.drawBorderJoin = drawBorderJoin;
  52. const drawBorderBottom = (columnWidths, config) => {
  53. return drawBorder(columnWidths, {
  54. ...config,
  55. separator: {
  56. body: config.border.bottomBody,
  57. join: config.border.bottomJoin,
  58. left: config.border.bottomLeft,
  59. right: config.border.bottomRight,
  60. },
  61. });
  62. };
  63. exports.drawBorderBottom = drawBorderBottom;
  64. const createTableBorderGetter = (columnWidths, config) => {
  65. return (index, size) => {
  66. if (!config.header) {
  67. if (index === 0) {
  68. return drawBorderTop(columnWidths, config);
  69. }
  70. if (index === size) {
  71. return drawBorderBottom(columnWidths, config);
  72. }
  73. return drawBorderJoin(columnWidths, config);
  74. }
  75. // Deal with the header
  76. if (index === 0) {
  77. return drawBorderTop(columnWidths, {
  78. ...config,
  79. border: {
  80. ...config.border,
  81. topJoin: config.border.topBody,
  82. },
  83. });
  84. }
  85. if (index === 1) {
  86. return drawBorderJoin(columnWidths, {
  87. ...config,
  88. border: {
  89. ...config.border,
  90. joinJoin: config.border.headerJoin,
  91. },
  92. });
  93. }
  94. if (index === size) {
  95. return drawBorderBottom(columnWidths, config);
  96. }
  97. return drawBorderJoin(columnWidths, config);
  98. };
  99. };
  100. exports.createTableBorderGetter = createTableBorderGetter;