utils.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.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 = 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 ? '' : 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. ...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;