UNPKG

4.9 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.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;
7const slice_ansi_1 = __importDefault(require("slice-ansi"));
8const string_width_1 = __importDefault(require("string-width"));
9const strip_ansi_1 = __importDefault(require("strip-ansi"));
10const getBorderCharacters_1 = require("./getBorderCharacters");
11/**
12 * Converts Windows-style newline to Unix-style
13 *
14 * @internal
15 */
16const normalizeString = (input) => {
17 return input.replace(/\r\n/g, '\n');
18};
19exports.normalizeString = normalizeString;
20/**
21 * Splits ansi string by newlines
22 *
23 * @internal
24 */
25const 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};
36exports.splitAnsi = splitAnsi;
37/**
38 * Merges user provided border characters with the default border ("honeywell") characters.
39 *
40 * @internal
41 */
42const makeBorderConfig = (border) => {
43 return {
44 ...(0, getBorderCharacters_1.getBorderCharacters)('honeywell'),
45 ...border,
46 };
47};
48exports.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 */
56const 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};
64exports.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 */
72const 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};
76exports.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 */
86const 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};
92exports.distributeUnevenly = distributeUnevenly;
93const sequence = (start, end) => {
94 return Array.from({ length: end - start + 1 }, (_, index) => {
95 return index + start;
96 });
97};
98exports.sequence = sequence;
99const sumArray = (array) => {
100 return array.reduce((accumulator, element) => {
101 return accumulator + element;
102 }, 0);
103};
104exports.sumArray = sumArray;
105const extractTruncates = (config) => {
106 return config.columns.map(({ truncate }) => {
107 return truncate;
108 });
109};
110exports.extractTruncates = extractTruncates;
111const flatten = (array) => {
112 return [].concat(...array);
113};
114exports.flatten = flatten;
115const 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};
123exports.findOriginalRowIndex = findOriginalRowIndex;
124const 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};
131exports.calculateRangeCoordinate = calculateRangeCoordinate;
132const areCellEqual = (cell1, cell2) => {
133 return cell1.row === cell2.row && cell1.col === cell2.col;
134};
135exports.areCellEqual = areCellEqual;
136const 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};
142exports.isCellInRange = isCellInRange;
143//# sourceMappingURL=utils.js.map
\No newline at end of file