1+ import { RawColumn } from "./rawColumn" ;
2+ import { ColumnPositioning } from "./columnPositioning" ;
3+
14export class Column {
25 private _normalizedColumnValues : string [ ] = [ ] ;
36
@@ -74,67 +77,3 @@ export class Column {
7477 return this . _normalizedColumnValues . length == 1 ;
7578 }
7679}
77-
78- export enum ColumnPositioning {
79- Unkown ,
80- First ,
81- Middle ,
82- Last
83- }
84-
85- export class ColumnFactory {
86- public static * generateColumns ( rawRows : string [ ] [ ] ) : Iterable < Column > {
87- const rowCount = rawRows . length ;
88- let colCount = rawRows [ 0 ] . length ;
89-
90- let rawColumns : RawColumn [ ] = [ ] ;
91-
92- // Create "raw" columns first to be able to specify the position (first/middle/last) of all columns later (based
93- // on maxLength) taking into account that columns can still be added or removed from the begginning/end.
94- for ( let col = 0 ; col < colCount ; col ++ ) {
95- rawColumns . push ( new RawColumn ( rawRows . map ( r => r [ col ] . trim ( ) ) ) ) ;
96- }
97-
98- if ( colCount > 1 && ! rawColumns [ 0 ] . isEmpty ( ) && rawColumns [ colCount - 1 ] . isEmpty ( ) ) {
99- // if the first column is not empty, but the last one is, then remove the last one
100- rawColumns . splice ( colCount - 1 , 1 ) ;
101- colCount -- ;
102- } else if ( colCount > 1 && rawColumns [ 0 ] . isEmpty ( ) && ! rawColumns [ colCount - 1 ] . isEmpty ( ) ) {
103- // add an empty column at the end if the first one is an empty column but the last one isn't
104- const emptyRawRows = new Array ( rowCount ) . fill ( 0 ) ;
105- rawColumns . push ( new RawColumn ( emptyRawRows ) ) ;
106- colCount ++ ;
107- }
108-
109- // now that the types can be determined create the real columns
110- for ( let col = 0 ; col < colCount ; col ++ ) {
111- const columnPositioning = col == 0
112- ? ColumnPositioning . First
113- : col == colCount - 1
114- ? ColumnPositioning . Last
115- : ColumnPositioning . Middle ;
116- yield new Column ( rawColumns [ col ] , columnPositioning ) ;
117- }
118- }
119- }
120-
121- export class RawColumn {
122- public cellLength : number = 0 ;
123- constructor ( public columnValues : string [ ] ) {
124- this . _setCellLength ( ) ;
125- }
126-
127- isEmpty ( ) : boolean {
128- return this . cellLength == 0 ;
129- }
130-
131- private _setCellLength ( ) : void {
132- // determine the expected maximum length of this column
133- const rowCount = this . columnValues . length ;
134- for ( let row = 0 ; row < rowCount ; row ++ ) {
135- const currentLength = this . columnValues [ row ] . length ;
136- if ( currentLength > this . cellLength )
137- this . cellLength = currentLength ;
138- }
139- }
140- }
0 commit comments