1- import { RawColumn } from "./rawColumn" ;
21import { ColumnPositioning } from "./columnPositioning" ;
2+ import { Cell } from "./cell" ;
33
44export class Column {
55 private _normalizedColumnValues : string [ ] = [ ] ;
6+ private _columnType : ColumnPositioning ;
7+ private _columnLength : number = 0 ;
68
7- constructor (
8- private _rawColumn : RawColumn ,
9- private _columnType : ColumnPositioning ) {
10- this . _normalizeColumns ( ) ;
9+ constructor ( private _cells : Cell [ ] ) {
10+ this . _setColumnLength ( ) ;
1111 }
1212
13- public getSize ( ) : number {
14- return this . _normalizedColumnValues . length ;
13+ public getNumberOfRows ( ) : number {
14+ return this . _cells . length + 1 ;
1515 }
1616
1717 public isEmpty ( ) : boolean {
18- return this . _rawColumn . isEmpty ( ) ;
18+ return this . _columnLength == 0 ;
1919 }
2020
2121 public getValue ( row : number ) : string {
@@ -26,24 +26,30 @@ export class Column {
2626 return this . _columnType ;
2727 }
2828
29+ public setPositioning ( position : ColumnPositioning ) : void {
30+ this . _columnType = position ;
31+ this . _normalizeColumns ( ) ;
32+ }
33+
2934 private _normalizeColumns ( ) : void {
30- for ( let row = 0 , rowCount = this . _rawColumn . columnValues . length ; row < rowCount ; row ++ ) {
35+ for ( let row = 0 , rowCount = this . _cells . length ; row < rowCount ; row ++ ) {
3136 const currentRowValue = this . isEmpty ( ) && this . _columnType == ColumnPositioning . Middle
32- ? " "
33- : this . _rawColumn . columnValues [ row ] ;
37+ ? Cell . Empty
38+ : this . _cells [ row ] ;
3439 this . _addValueWithPadding ( currentRowValue , " " ) ;
3540 if ( row == 0 )
36- this . _addValueWithPadding ( "-" , "-" ) ;
41+ this . _addValueWithPadding ( new Cell ( "-" ) , "-" ) ;
3742 }
3843 }
3944
40- private _addValueWithPadding ( value : string , padChar : string ) : void {
45+ private _addValueWithPadding ( cell : Cell , padChar : string ) : void {
4146 let newValue = "" ;
47+ const cellValue = cell . getValue ( ) ;
4248 if ( ! this . isEmpty ( ) || this . _columnType == ColumnPositioning . Middle ) {
43- const left = this . _getLeftPad ( value , padChar ) ;
44- const right = this . _getRightPad ( value , padChar ) ;
49+ const left = this . _getLeftPad ( cellValue , padChar ) ;
50+ const right = this . _getRightPad ( cell , padChar ) ;
4551
46- newValue = left + value + right ;
52+ newValue = left + cellValue + right ;
4753 }
4854 this . _normalizedColumnValues . push ( newValue ) ;
4955 }
@@ -63,16 +69,25 @@ export class Column {
6369 }
6470 }
6571
66- private _getRightPad ( value : string , rightPad : string ) : string {
72+ private _getRightPad ( cell : Cell , rightPad : string ) : string {
6773 const seperatorBeingAdded = this . _oneRowExists ( ) ;
6874 // only the separator has padding in the last column
6975 if ( this . _columnType == ColumnPositioning . Last && ! seperatorBeingAdded )
7076 return "" ;
7177
72- const extraPaddingCount = Math . max ( this . _rawColumn . cellLength - value . length + 2 , 2 ) ;
78+ const extraPaddingCount = Math . max ( this . _columnLength - cell . getLength ( ) + 2 , 2 ) ;
7379 return new Array ( extraPaddingCount ) . join ( rightPad ) ;
7480 }
7581
82+ private _setColumnLength ( ) : void {
83+ const rowCount = this . _cells . length ;
84+ for ( let row = 0 ; row < rowCount ; row ++ ) {
85+ const currentLength = this . _cells [ row ] . getLength ( ) ;
86+ if ( currentLength > this . _columnLength )
87+ this . _columnLength = currentLength ;
88+ }
89+ }
90+
7691 private _oneRowExists ( ) : boolean {
7792 return this . _normalizedColumnValues . length == 1 ;
7893 }
0 commit comments