-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
_utils.ts
41 lines (36 loc) 路 1.04 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
/**
* Get next words from the beginning of [content] until all words have a length lower or equal then [length].
*
* @param length Max length of all words.
* @param content The text content.
*/
import { Cell, CellType } from "./cell.ts";
import { consumeWords } from "./consume_words.ts";
import { stripColor, unicodeWidth } from "./deps.ts";
/**
* Get longest cell from given row index.
*/
export function longest(
index: number,
rows: Array<Array<CellType>>,
maxWidth?: number,
): number {
const cellLengths = rows.map((row) => {
const cell = row[index];
const cellValue = cell instanceof Cell && cell.getColSpan() > 1
? ""
: cell?.toString() || "";
return cellValue
.split("\n")
.map((line: string) => {
const str = typeof maxWidth === "undefined"
? line
: consumeWords(maxWidth, line);
return strLength(str) || 0;
});
}).flat();
return Math.max(...cellLengths);
}
export const strLength = (str: string): number => {
return unicodeWidth(stripColor(str));
};