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