utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.isCellInRange = exports.areCellEqual = exports.calculateRangeCoordinate = exports.findOriginalRowIndex = exports.flatten = exports.extractTruncates = exports.sumArray = exports.sequence = exports.distributeUnevenly = exports.countSpaceSequence = exports.groupBySizes = exports.makeBorderConfig = exports.splitAnsi = exports.normalizeString = void 0;
  7. const slice_ansi_1 = __importDefault(require("slice-ansi"));
  8. const string_width_1 = __importDefault(require("string-width"));
  9. const strip_ansi_1 = __importDefault(require("strip-ansi"));
  10. const getBorderCharacters_1 = require("./getBorderCharacters");
  11. /**
  12. * Converts Windows-style newline to Unix-style
  13. *
  14. * @internal
  15. */
  16. const normalizeString = (input) => {
  17. return input.replace(/\r\n/g, '\n');
  18. };
  19. exports.normalizeString = normalizeString;
  20. /**
  21. * Splits ansi string by newlines
  22. *
  23. * @internal
  24. */
  25. const splitAnsi = (input) => {
  26. const lengths = (0, strip_ansi_1.default)(input).split('\n').map(string_width_1.default);
  27. const result = [];
  28. let startIndex = 0;
  29. lengths.forEach((length) => {
  30. result.push(length === 0 ? '' : (0, slice_ansi_1.default)(input, startIndex, startIndex + length));
  31. // Plus 1 for the newline character itself
  32. startIndex += length + 1;
  33. });
  34. return result;
  35. };
  36. exports.splitAnsi = splitAnsi;
  37. /**
  38. * Merges user provided border characters with the default border ("honeywell") characters.
  39. *
  40. * @internal
  41. */
  42. const makeBorderConfig = (border) => {
  43. return {
  44. ...(0, getBorderCharacters_1.getBorderCharacters)('honeywell'),
  45. ...border,
  46. };
  47. };
  48. exports.makeBorderConfig = makeBorderConfig;
  49. /**
  50. * Groups the array into sub-arrays by sizes.
  51. *
  52. * @internal
  53. * @example
  54. * groupBySizes(['a', 'b', 'c', 'd', 'e'], [2, 1, 2]) = [ ['a', 'b'], ['c'], ['d', 'e'] ]
  55. */
  56. const groupBySizes = (array, sizes) => {
  57. let startIndex = 0;
  58. return sizes.map((size) => {
  59. const group = array.slice(startIndex, startIndex + size);
  60. startIndex += size;
  61. return group;
  62. });
  63. };
  64. exports.groupBySizes = groupBySizes;
  65. /**
  66. * Counts the number of continuous spaces in a string
  67. *
  68. * @internal
  69. * @example
  70. * countGroupSpaces('a bc de f') = 3
  71. */
  72. const countSpaceSequence = (input) => {
  73. var _a, _b;
  74. return (_b = (_a = input.match(/\s+/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
  75. };
  76. exports.countSpaceSequence = countSpaceSequence;
  77. /**
  78. * Creates the non-increasing number array given sum and length
  79. * whose the difference between maximum and minimum is not greater than 1
  80. *
  81. * @internal
  82. * @example
  83. * distributeUnevenly(6, 3) = [2, 2, 2]
  84. * distributeUnevenly(8, 3) = [3, 3, 2]
  85. */
  86. const distributeUnevenly = (sum, length) => {
  87. const result = Array.from({ length }).fill(Math.floor(sum / length));
  88. return result.map((element, index) => {
  89. return element + (index < sum % length ? 1 : 0);
  90. });
  91. };
  92. exports.distributeUnevenly = distributeUnevenly;
  93. const sequence = (start, end) => {
  94. return Array.from({ length: end - start + 1 }, (_, index) => {
  95. return index + start;
  96. });
  97. };
  98. exports.sequence = sequence;
  99. const sumArray = (array) => {
  100. return array.reduce((accumulator, element) => {
  101. return accumulator + element;
  102. }, 0);
  103. };
  104. exports.sumArray = sumArray;
  105. const extractTruncates = (config) => {
  106. return config.columns.map(({ truncate }) => {
  107. return truncate;
  108. });
  109. };
  110. exports.extractTruncates = extractTruncates;
  111. const flatten = (array) => {
  112. return [].concat(...array);
  113. };
  114. exports.flatten = flatten;
  115. const findOriginalRowIndex = (mappedRowHeights, mappedRowIndex) => {
  116. const rowIndexMapping = (0, exports.flatten)(mappedRowHeights.map((height, index) => {
  117. return Array.from({ length: height }, () => {
  118. return index;
  119. });
  120. }));
  121. return rowIndexMapping[mappedRowIndex];
  122. };
  123. exports.findOriginalRowIndex = findOriginalRowIndex;
  124. const calculateRangeCoordinate = (spanningCellConfig) => {
  125. const { row, col, colSpan = 1, rowSpan = 1 } = spanningCellConfig;
  126. return { bottomRight: { col: col + colSpan - 1,
  127. row: row + rowSpan - 1 },
  128. topLeft: { col,
  129. row } };
  130. };
  131. exports.calculateRangeCoordinate = calculateRangeCoordinate;
  132. const areCellEqual = (cell1, cell2) => {
  133. return cell1.row === cell2.row && cell1.col === cell2.col;
  134. };
  135. exports.areCellEqual = areCellEqual;
  136. const isCellInRange = (cell, { topLeft, bottomRight }) => {
  137. return (topLeft.row <= cell.row &&
  138. cell.row <= bottomRight.row &&
  139. topLeft.col <= cell.col &&
  140. cell.col <= bottomRight.col);
  141. };
  142. exports.isCellInRange = isCellInRange;
  143. //# sourceMappingURL=utils.js.map