Skip to content

Commit f38ff08

Browse files
committed
Move extra pad calculation from RowViewModelFactory to BasePadCalculator
1 parent bd9c522 commit f38ff08

6 files changed

Lines changed: 34 additions & 49 deletions

File tree

src/padCalculation/basePadCalculator.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ export abstract class BasePadCalculator {
66
public abstract getRightPadding(paddingChar: string, table: Table, row: number, column: number): string;
77

88
protected baseGetRightPadding(paddingChar: string, table: Table, row: number, column: number): string {
9-
const cellTextLength = table.rows[row][column].getLength();
10-
let rightPadCount = table.getLongestColumnLengths()[column] > 0
11-
? table.getLongestColumnLengths()[column] - cellTextLength
9+
return paddingChar.repeat(this.getRightPadCount(table.getLongestColumnLengths()[column], table.rows[row][column].getLength()));
10+
}
11+
12+
private getRightPadCount(longestColumnLength: number, cellTextLength: number) {
13+
let rightPadCount = longestColumnLength > 0
14+
? longestColumnLength - cellTextLength
1215
: 1;
13-
if (table.getLongestColumnLengths()[column] > 0 && cellTextLength > 0)
16+
if ((cellTextLength == 0) || (longestColumnLength > 0 && cellTextLength > 0))
1417
rightPadCount++;
15-
return paddingChar.repeat(rightPadCount);
18+
return rightPadCount;
1619
}
1720
}

src/viewModelFactories/rowViewModelFactory.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,21 @@ export class RowViewModelFactory {
1313

1414
let resultRow = new Array(table.columnCount);
1515

16-
for(let col = 0; col < table.columnCount; col++) {
17-
let text = "";
18-
if (col == table.columnCount - 1) {
19-
if (table.rows[row][col].getValue() == "") {
20-
if (!table.hasRightBorder)
21-
text = "";
22-
else
23-
text = this._contentPadCalculator.getPadChar();
24-
}
25-
else {
26-
text = table.rows[row][col].getValue();
27-
}
28-
}
29-
else {
30-
text = table.rows[row][col].getValue() == ""
31-
? this._contentPadCalculator.getPadChar()
32-
: table.rows[row][col].getValue();
33-
}
34-
16+
for(let col = 0; col < table.columnCount; col++)
3517
resultRow[col] =
3618
this._contentPadCalculator.getLeftPadding(table, row, col) +
37-
text +
19+
table.rows[row][col].getValue() +
3820
this._contentPadCalculator.getRightPadding(table, row, col);
39-
}
21+
4022
return new RowViewModel(resultRow);
4123
}
4224

4325
public buildSeparator(table: Table): RowViewModel {
4426
let resultRow = new Array(table.columnCount);
4527
for(let col = 0; col < table.columnCount; col++) {
4628
resultRow[col] =
47-
this._separatorPadCalculator.getLeftPadding( table, 1, col) +
48-
this._separatorPadCalculator.getRightPadding( table, 0, col);
29+
this._separatorPadCalculator.getLeftPadding(table, 1, col) +
30+
this._separatorPadCalculator.getRightPadding(table, 0, col);
4931
}
5032
return new RowViewModel(resultRow);
5133
}

test/unitTests/padCalculation/firstColumnPadCalculator.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ suite("FirstColumnPadCalculator tests", () => {
9494
assert.equal(pad, " ");
9595
});
9696

97-
test("getRightPadding() First column is empty string gets right padded with 5 characters", () => {
97+
test("getRightPadding() First column is empty string gets right padded with 6 characters", () => {
9898
const sut = createCalculator();
9999
const table = tableFor([
100100
[ "aaaaa", "bbbbb", "ccccc" ],
@@ -103,7 +103,7 @@ suite("FirstColumnPadCalculator tests", () => {
103103

104104
const pad = getRightPad(sut, table);
105105

106-
assert.equal(pad, " ");
106+
assert.equal(pad, " ");
107107
});
108108

109109
function getLeftPad(sut: FirstColumnPadCalculator, table: Table): string {

test/unitTests/padCalculation/lastColumnPadCalculator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ suite("LastColumnPadCalculator tests", () => {
8686
assert.equal(pad, " ");
8787
});
8888

89-
test("getRightPadding() Last column is empty string gets right padded with maxColLength characters if there is right border", () => {
89+
test("getRightPadding() Last column is empty string gets right padded with maxColLength+1 characters if there is right border", () => {
9090
const sut = createCalculator();
9191
const table = tableFor([
9292
[ "aaaaa", "bbbbb", "ccccc" ],
@@ -96,10 +96,10 @@ suite("LastColumnPadCalculator tests", () => {
9696

9797
const pad = getRightPad(sut, table);
9898

99-
assert.equal(pad, " ");
99+
assert.equal(pad, " ");
100100
});
101101

102-
test("getRightPadding() Last column with 0 maxLength gets right padded with 1 characters if there is right border", () => {
102+
test("getRightPadding() Last column with 0 maxLength gets right padded with 2 characters if there is right border", () => {
103103
const sut = createCalculator();
104104
const table = tableFor([
105105
[ "aaaaa", "bbbbb", "" ],
@@ -109,7 +109,7 @@ suite("LastColumnPadCalculator tests", () => {
109109

110110
const pad = getRightPad(sut, table);
111111

112-
assert.equal(pad, " ");
112+
assert.equal(pad, " ");
113113
});
114114

115115
function getLeftPad(sut: LastColumnPadCalculator, table: Table): string {

test/unitTests/padCalculation/middleColumnPadCalculator.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ suite("MiddleColumnPadCalculator tests", () => {
3434
assert.equal(pad, " ");
3535
});
3636

37-
test("getRightPadding() Middle column is empty cell gets right padded with maxColLength-1 characters", () => {
37+
test("getRightPadding() Middle column is empty cell gets right padded with maxColLength characters", () => {
3838
const sut = createCalculator();
3939
const table = tableFor([
4040
[ "aaaaa", "bbbbb", "ccccc" ],
@@ -43,7 +43,7 @@ suite("MiddleColumnPadCalculator tests", () => {
4343

4444
const pad = getRightPad(sut, table);
4545

46-
assert.equal(pad, " ");
46+
assert.equal(pad, " ");
4747
});
4848

4949
test("getRightPadding() Middle column equal to maxColLength gets right padded with one character", () => {
@@ -94,7 +94,7 @@ suite("MiddleColumnPadCalculator tests", () => {
9494
assert.equal(pad, " ");
9595
});
9696

97-
test("getRightPadding() Middle column is empty string gets right padded with maxColLength characters", () => {
97+
test("getRightPadding() Middle column is empty string gets right padded with maxColLength+1 characters", () => {
9898
const sut = createCalculator();
9999
const table = tableFor([
100100
[ "aaaaa", "bbbbb", "ccccc" ],
@@ -103,10 +103,10 @@ suite("MiddleColumnPadCalculator tests", () => {
103103

104104
const pad = getRightPad(sut, table);
105105

106-
assert.equal(pad, " ");
106+
assert.equal(pad, " ");
107107
});
108108

109-
test("getRightPadding() Middle column with 0 maxLength gets right padded with 1 character", () => {
109+
test("getRightPadding() Middle column with 0 maxLength gets right padded with 2 character", () => {
110110
const sut = createCalculator();
111111
const table = tableFor([
112112
[ "aaaaa", "", "ccccc" ],
@@ -115,7 +115,7 @@ suite("MiddleColumnPadCalculator tests", () => {
115115

116116
const pad = getRightPad(sut, table);
117117

118-
assert.equal(pad, " ");
118+
assert.equal(pad, " ");
119119
});
120120

121121
test("Regular middle gets padded both left and right with expected amount", () => {

test/unitTests/viewModelFactories/rowViewModelFactory.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ suite("RowViewModelFactory.buildRow() tests", () => {
1616
_separatorPadCalculator = Mock.ofType<PadCalculator>();
1717
});
1818

19-
test("padCalculator called for all columns with expected values", () => {
19+
test("PadCalculator called for all columns with expected values", () => {
2020
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
2121
const row = 1;
2222
const table = threeColumnTable();
@@ -32,7 +32,7 @@ suite("RowViewModelFactory.buildRow() tests", () => {
3232
_contentPadCalculator.verify(_ => _.getRightPadding(table, row, 2), Times.once());
3333
});
3434

35-
test("value returned from padCalculator.getLeftPadding is used to start the row value", () => {
35+
test("Value returned from padCalculator.getLeftPadding is used to start the row value", () => {
3636
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
3737
const row = 1;
3838
const table = threeColumnTable();
@@ -43,7 +43,7 @@ suite("RowViewModelFactory.buildRow() tests", () => {
4343
assert.equal(rowViewModel.getValueAt(1).startsWith("test"), true);
4444
});
4545

46-
test("value returned from padCalculator.getRightPadding is used to end the row value", () => {
46+
test("Value returned from padCalculator.getRightPadding is used to end the row value", () => {
4747
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
4848
const row = 1;
4949
const table = threeColumnTable();
@@ -54,7 +54,7 @@ suite("RowViewModelFactory.buildRow() tests", () => {
5454
assert.equal(rowViewModel.getValueAt(1).endsWith("test"), true);
5555
});
5656

57-
test("Empty middle column uses left and right pad and also the pad char", () => {
57+
test("Empty middle column uses only left and right pad to create the value", () => {
5858
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
5959
const row = 1;
6060
const table = threeColumnTableWithEmptyMiddleColumn();
@@ -65,7 +65,7 @@ suite("RowViewModelFactory.buildRow() tests", () => {
6565
const rowViewModel = sut.buildRow(row, table);
6666

6767
assertExt.isNotNull(rowViewModel);
68-
assert.equal(rowViewModel.getValueAt(1), "LxR");
68+
assert.equal(rowViewModel.getValueAt(1), "LR");
6969
});
7070
});
7171

@@ -78,7 +78,7 @@ suite("RowViewModelFactory.buildSeparator() tests", () => {
7878
_separatorPadCalculator = Mock.ofType<PadCalculator>();
7979
});
8080

81-
test("padCalculator called for all columns with expected values", () => {
81+
test("PadCalculator called for all columns with expected values", () => {
8282
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
8383
const table = threeColumnTable();
8484

@@ -93,7 +93,7 @@ suite("RowViewModelFactory.buildSeparator() tests", () => {
9393
_separatorPadCalculator.verify(_ => _.getRightPadding(It.isAny(), It.isAny(), 2), Times.once());
9494
});
9595

96-
test("value returned from padCalculator.getLeftPadding is used to start the row value", () => {
96+
test("Value returned from padCalculator.getLeftPadding is used to start the row value", () => {
9797
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
9898
const table = threeColumnTable();
9999
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), 0)).returns(() => "test");
@@ -103,7 +103,7 @@ suite("RowViewModelFactory.buildSeparator() tests", () => {
103103
assert.equal(separatorRowViewModel.getValueAt(0).startsWith("test"), true);
104104
});
105105

106-
test("value returned from padCalculator.getRightPadding is used to end the row value", () => {
106+
test("Value returned from padCalculator.getRightPadding is used to end the row value", () => {
107107
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
108108
const table = threeColumnTable();
109109
_separatorPadCalculator.setup(_ => _.getRightPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "test");
@@ -113,7 +113,7 @@ suite("RowViewModelFactory.buildSeparator() tests", () => {
113113
assert.equal(separatorRowViewModel.getValueAt(1).endsWith("test"), true);
114114
});
115115

116-
test("Empty middle column does not use pad char", () => {
116+
test("Empty middle column uses only left and right pad to create the value", () => {
117117
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
118118
const table = threeColumnTableWithEmptyMiddleColumn();
119119
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "Left");

0 commit comments

Comments
 (0)