-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
cell.ts
146 lines (127 loc) 路 3.23 KB
/
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
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
/** Cell type */
// deno-lint-ignore ban-types
export type ICell = number | string | String | Cell;
export type Direction = "left" | "right" | "center";
/** Cell options. */
export interface ICellOptions {
border?: boolean;
colSpan?: number;
rowSpan?: number;
align?: Direction;
}
/** Cell representation. */
export class Cell {
protected options: ICellOptions = {};
/** Get cell length. */
public get length(): number {
return this.toString().length;
}
/**
* Create a new cell. If value is a cell, the value and all options of the cell
* will be copied to the new cell.
* @param value Cell or cell value.
*/
public static from(value: ICell): Cell {
const cell = new this(value);
if (value instanceof Cell) {
cell.options = { ...value.options };
}
return cell;
}
/**
* Cell constructor.
* @param value Cell value.
*/
public constructor(private value: ICell) {}
/** Get cell value. */
public toString(): string {
return this.value.toString();
}
/**
* Set cell value.
* @param value Cell or cell value.
*/
public setValue(value: ICell): this {
this.value = value;
return this;
}
/**
* Clone cell with all options.
* @param value Cell or cell value.
*/
public clone(value?: ICell): Cell {
const cell = new Cell(value ?? this);
cell.options = { ...this.options };
return cell;
}
/**
* Setter:
*/
/**
* Enable/disable cell border.
* @param enable Enable/disable cell border.
* @param override Override existing value.
*/
public border(enable: boolean, override = true): this {
if (override || typeof this.options.border === "undefined") {
this.options.border = enable;
}
return this;
}
/**
* Set col span.
* @param span Number of cols to span.
* @param override Override existing value.
*/
public colSpan(span: number, override = true): this {
if (override || typeof this.options.colSpan === "undefined") {
this.options.colSpan = span;
}
return this;
}
/**
* Set row span.
* @param span Number of rows to span.
* @param override Override existing value.
*/
public rowSpan(span: number, override = true): this {
if (override || typeof this.options.rowSpan === "undefined") {
this.options.rowSpan = span;
}
return this;
}
/**
* Align cell content.
* @param direction Align direction.
* @param override Override existing value.
*/
public align(direction: Direction, override = true): this {
if (override || typeof this.options.align === "undefined") {
this.options.align = direction;
}
return this;
}
/**
* Getter:
*/
/** Check if cell has border. */
public getBorder(): boolean {
return this.options.border === true;
}
/** Get col span. */
public getColSpan(): number {
return typeof this.options.colSpan === "number" && this.options.colSpan > 0
? this.options.colSpan
: 1;
}
/** Get row span. */
public getRowSpan(): number {
return typeof this.options.rowSpan === "number" && this.options.rowSpan > 0
? this.options.rowSpan
: 1;
}
/** Get row span. */
public getAlign(): Direction {
return this.options.align ?? "left";
}
}