forked from susisu/mte-kernel
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
table-cell.ts
115 lines (105 loc) · 2.82 KB
/
table-cell.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
import { Alignment } from './alignment';
/**
* A `TableCell` object represents a table cell.
*
* @private
*/
export class TableCell {
/**
* Raw content of the cell.
*/
public readonly rawContent: string;
/**
* Trimmed content of the cell.
*/
public readonly content: string;
/**
* Width of the left padding of the cell.
*/
public readonly paddingLeft: number;
/**
* Width of the right padding of the cell.
*/
public readonly paddingRight: number;
/**
* Creates a new `TableCell` object.
*
* @param rawContent - Raw content of the cell.
*/
constructor(rawContent: string) {
this.rawContent = rawContent;
this.content = rawContent.trim();
this.paddingLeft =
this.content === ''
? this.rawContent === ''
? 0
: 1
: this.rawContent.length - this.rawContent.trimLeft().length;
this.paddingRight =
this.rawContent.length - this.content.length - this.paddingLeft;
}
/**
* Convers the cell to a text representation.
*
* @returns The raw content of the cell.
*/
public toText(): string {
return this.rawContent;
}
/**
* Checks if the cell is a delimiter i.e. it only contains hyphens `-` with optional one
* leading and trailing colons `:`.
*
* @returns `true` if the cell is a delimiter.
*/
public isDelimiter(): boolean {
return /^\s*:?-+:?\s*$/.test(this.rawContent);
}
/**
* Returns the alignment the cell represents.
*
* @returns The alignment the cell represents; `undefined` if the cell is not a delimiter.
*/
public getAlignment(): Alignment | undefined {
if (!this.isDelimiter()) {
return undefined;
}
if (this.content[0] === ':') {
if (this.content[this.content.length - 1] === ':') {
return Alignment.CENTER;
}
return Alignment.LEFT;
}
if (this.content[this.content.length - 1] === ':') {
return Alignment.RIGHT;
}
return Alignment.NONE;
}
/**
* Computes a relative position in the trimmed content from that in the raw content.
*
* @param rawOffset - Relative position in the raw content.
* @returns - Relative position in the trimmed content.
*/
public computeContentOffset(rawOffset: number): number {
if (this.content === '') {
return 0;
}
if (rawOffset < this.paddingLeft) {
return 0;
}
if (rawOffset < this.paddingLeft + this.content.length) {
return rawOffset - this.paddingLeft;
}
return this.content.length;
}
/**
* Computes a relative position in the raw content from that in the trimmed content.
*
* @param contentOffset - Relative position in the trimmed content.
* @returns - Relative position in the raw content.
*/
public computeRawOffset(contentOffset: number): number {
return contentOffset + this.paddingLeft;
}
}