Skip to content

Commit

Permalink
feat(table): add static Table.chars() method to set global default …
Browse files Browse the repository at this point in the history
…table chars (#107)
  • Loading branch information
c4spar committed Nov 3, 2020
1 parent 41b2038 commit fec09df
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
3 changes: 2 additions & 1 deletion table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ Enable table border. Doesn't override row and cell settings.

#### .chars(chars)

Override default border characters. Doesn't ovrride child settings.
Override default border characters. Doesn't override row and cell settings.
To change the default border characters globally you can use the static `Table.chars(chars)` method.

| Argument | Type | Required | Description |
| ----- | :--: | :--: | ----------- |
Expand Down
9 changes: 8 additions & 1 deletion table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ export interface ITableSettings extends Required<ITableOptions> {
export type ITable<T extends IRow = IRow> = T[] | Table<T>;

export class Table<T extends IRow = IRow> extends Array<T> {
protected static _chars: IBorder = Object.assign({}, border);
protected options: ITableSettings = {
indent: 0,
border: false,
maxColWidth: Infinity,
minColWidth: 0,
padding: 1,
chars: border,
chars: Object.assign({}, Table._chars),
};
private headerRow?: Row;

Expand All @@ -44,6 +45,12 @@ export class Table<T extends IRow = IRow> extends Array<T> {
return new this().fromJson(rows);
}

/** Set global default border characters. */
public static chars(chars: IBorderOptions): typeof Table {
Object.assign(this._chars, chars);
return this;
}

public static render<T extends IRow>(rows: ITable<T>): void {
Table.from(rows).render();
}
Expand Down
104 changes: 104 additions & 0 deletions table/test/border_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { border } from "../border.ts";
import { Table } from "../table.ts";
import { assertEquals } from "../../dev_deps.ts";

Deno.test("default table chars", () => {
assertEquals(
Table
.from([
["Row 1 Column 1", "Row 1 Column 2", "Row 1 Column 3"],
["Row 2 Column 1", "Row 2 Column 2", "Row 2 Column 3"],
["Row 3 Column 1", "Row 3 Column 2", "Row 3 Column 3"],
])
.border(true)
.toString(),
`
┌────────────────┬────────────────┬────────────────┐
│ Row 1 Column 1 │ Row 1 Column 2 │ Row 1 Column 3 │
├────────────────┼────────────────┼────────────────┤
│ Row 2 Column 1 │ Row 2 Column 2 │ Row 2 Column 3 │
├────────────────┼────────────────┼────────────────┤
│ Row 3 Column 1 │ Row 3 Column 2 │ Row 3 Column 3 │
└────────────────┴────────────────┴────────────────┘`.slice(1),
);
});

Deno.test("custom global table chars", () => {
assertEquals(
Table
.chars({
top: "a",
topMid: "b",
topLeft: "c",
topRight: "d",
bottom: "e",
bottomMid: "f",
bottomLeft: "g",
bottomRight: "h",
left: "i",
right: "j",
middle: "k",
mid: "l",
leftMid: "m",
rightMid: "n",
midMid: "o",
})
.from([
["+++ ++", "+++", "++++++++ +++ ++++"],
["++", "++++ ++++++ ++", "+++ +"],
["++++ +++++", "++ ++++", "+++ +++++++"],
])
.border(true)
.toString(),
`
caaaaaaaaaaaabaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaad
i +++ ++ k +++ k ++++++++ +++ ++++ j
mllllllllllllollllllllllllllllollllllllllllllllllln
i ++ k ++++ ++++++ ++ k +++ + j
mllllllllllllollllllllllllllllollllllllllllllllllln
i ++++ +++++ k ++ ++++ k +++ +++++++ j
geeeeeeeeeeeefeeeeeeeeeeeeeeeefeeeeeeeeeeeeeeeeeeeh`.slice(1),
);
// reset default border chars
Table.chars(border);
});

Deno.test("custom table chars", () => {
assertEquals(
Table
.from([
["+++ ++", "+++", "++++++++ +++ ++++"],
["++", "++++ ++++++ ++", "+++ +"],
["++++ +++++", "++ ++++", "+++ +++++++"],
])
.chars({
top: "a",
topMid: "b",
topLeft: "c",
topRight: "d",
bottom: "e",
bottomMid: "f",
bottomLeft: "g",
bottomRight: "h",
left: "i",
right: "j",
middle: "k",
mid: "l",
leftMid: "m",
rightMid: "n",
midMid: "o",
})
.border(true)
.toString(),
`
caaaaaaaaaaaabaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaad
i +++ ++ k +++ k ++++++++ +++ ++++ j
mllllllllllllollllllllllllllllollllllllllllllllllln
i ++ k ++++ ++++++ ++ k +++ + j
mllllllllllllollllllllllllllllollllllllllllllllllln
i ++++ +++++ k ++ ++++ k +++ +++++++ j
geeeeeeeeeeeefeeeeeeeeeeeeeeeefeeeeeeeeeeeeeeeeeeeh`.slice(1),
);
// reset default border chars
Table.chars(border);
});

0 comments on commit fec09df

Please sign in to comment.