-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
row.ts
90 lines (78 loc) 路 2.11 KB
/
row.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
import { Cell, Direction, ICell } from "./cell.ts";
/** Row type */
export type IRow<T extends ICell = ICell> = T[] | Row<T>;
/** Json row. */
export type IDataRow = Record<string, string | number>;
/** Row options. */
export interface IRowOptions {
indent?: number;
border?: boolean;
align?: Direction;
}
/**
* Row representation.
*/
export class Row<T extends ICell = ICell> extends Array<T> {
protected options: IRowOptions = {};
/**
* Create a new row. If cells is a row, all cells and options of the row will
* be copied to the new row.
* @param cells Cells or row.
*/
public static from<T extends ICell = ICell>(cells: IRow<T>): Row<T> {
const row = new this(...cells);
if (cells instanceof Row) {
row.options = { ...cells.options };
}
return row;
}
/** Clone row recursively with all options. */
public clone(): Row {
const row = new Row(
...this.map((cell: T) => cell instanceof Cell ? cell.clone() : cell),
);
row.options = { ...this.options };
return row;
}
/**
* 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;
}
/**
* Align row 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 row has border. */
public getBorder(): boolean {
return this.options.border === true;
}
/** Check if row or any child cell has border. */
public hasBorder(): boolean {
return this.getBorder() ||
this.some((cell) => cell instanceof Cell && cell.getBorder());
}
/** Get row alignment. */
public getAlign(): Direction {
return this.options.align ?? "left";
}
}