Skip to content

Commit 6d3e34a

Browse files
committed
Introduce NotSet alignment
1 parent f9475c3 commit 6d3e34a

5 files changed

Lines changed: 24 additions & 21 deletions

File tree

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import { Alignment } from "../models/alignment";
22

33
export class AlignmentFactory {
4+
45
public createAlignments(cells: string[]): Alignment[] {
5-
let result: Alignment[] = [];
6+
return cells.map(this.alignmentOf);
7+
}
68

7-
const len: number = cells.length;
8-
for (let i = 0; i < len; i++) {
9-
const cell = cells[i];
10-
const left = cell[0] == ":";;
11-
const right = cell[cell.length - 1] == ":";
9+
private alignmentOf(cell: string): Alignment {
10+
const left = cell[0] == ":";
11+
const right = cell[cell.length - 1] == ":";
1212

13-
if (left && right)
14-
result.push(Alignment.Center);
15-
else if (right)
16-
result.push(Alignment.Right);
17-
else
18-
result.push(Alignment.Left);
19-
}
13+
if (left && right) return Alignment.Center;
14+
if (right) return Alignment.Right;
15+
if (left) return Alignment.Left;
2016

21-
return result;
17+
return Alignment.NotSet;
2218
}
2319
}

src/modelFactory/tableFactory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export class TableFactory {
1818
throw new Error("Can't create table model from null table text.");
1919

2020
const rowsWithoutSeparator = this._selectionInterpreter.allRows(text).filter((v, i) => i != 1);
21+
const separator = this._selectionInterpreter.separator(text);
2122

22-
const alignments: Alignment[] = rowsWithoutSeparator != null && rowsWithoutSeparator.length > 1
23-
? this._alignmentFactory.createAlignments(rowsWithoutSeparator[1])
23+
const alignments: Alignment[] = separator != null && separator.length > 0
24+
? this._alignmentFactory.createAlignments(separator)
2425
: [];
2526

2627
const cells = rowsWithoutSeparator.map(row => row.map(c => new Cell(c)));

src/models/alignment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum Alignment {
2+
NotSet,
23
Left,
34
Center,
45
Right

test/unitTests/modelFactory/alignmentFactory.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ suite("AlignmentFactory tests", () => {
1212
assert.throws(() => sut.createAlignments(separatorCells))
1313
});
1414

15-
test("createAlignments() with separators without alignment defaults to left alignment", () => {
15+
test("createAlignments() with separators without alignment defaults to NotSet alignment", () => {
1616
const separatorCells: string[] = ["---", "--", "-------"];
1717
const sut = createFactory();
1818

1919
const alignments = sut.createAlignments(separatorCells);
2020
assert.equal(alignments.length, 3);
21-
assert.equal(alignments[0], Alignment.Left);
22-
assert.equal(alignments[1], Alignment.Left);
23-
assert.equal(alignments[2], Alignment.Left);
21+
assert.equal(alignments[0], Alignment.NotSet);
22+
assert.equal(alignments[1], Alignment.NotSet);
23+
assert.equal(alignments[2], Alignment.NotSet);
2424
});
2525

2626
test("createAlignments() with separators having mixed alignments returns expected alignments", () => {
@@ -32,7 +32,7 @@ suite("AlignmentFactory tests", () => {
3232
assert.equal(alignments[0], Alignment.Center);
3333
assert.equal(alignments[1], Alignment.Left);
3434
assert.equal(alignments[2], Alignment.Left);
35-
assert.equal(alignments[3], Alignment.Left);
35+
assert.equal(alignments[3], Alignment.NotSet);
3636
assert.equal(alignments[4], Alignment.Right);
3737
assert.equal(alignments[5], Alignment.Center);
3838
});

test/unitTests/modelFactory/tableFactory.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,16 @@ suite("TableFactory tests", () => {
106106
const transformedTable = new Table([["c1", "c2", "", "c4"], ["a", "b", "", "d"]].map(row => row.map(c => new Cell(c))), expectedAlignmets);
107107
_alignmentFactoryMock.setup(m => m.createAlignments(It.isAny())).returns(() => expectedAlignmets);
108108
_transformer.setup(_ => _.process(It.isAny())).returns(() => transformedTable);
109+
109110
let selectionInterpreter: IMock<SelectionInterpreter> = Mock.ofType<SelectionInterpreter>();
110111
selectionInterpreter
111112
.setup(_ => _.allRows(It.isAny()))
112113
.returns(() => [["c1", "c2", "", "c4"], ["-","-","-","-"], ["a", "b", "", "d"]])
113114
.verifiable(Times.once());
115+
selectionInterpreter
116+
.setup(_ => _.separator(It.isAny()))
117+
.returns(() => ["-","-","-","-"])
118+
.verifiable(Times.once());
114119
const sut = createFactory(selectionInterpreter.object);
115120

116121
sut.getModel("test");

0 commit comments

Comments
 (0)