2424 */
2525function test ( ) {
2626 console . log ( "projectcsv.test()" ) ;
27- }
27+ }
28+ 29+ /**
30+ * Creates an unstyled, bare-bones html table
31+ * from the provided 2D(Multidimentional Array).
32+ *
33+ * @param {type } tableArray the 2D array.
34+ * @param {type } useHeaders will make the first row
35+ * in the table bold if true.
36+ * @param {type } dupeHeaders will duplicate the first row
37+ * in the table if true and useHeaders is true.
38+ * @returns {String } the constructed html table as text.
39+ */
40+ function getTable ( tableArray , useHeaders , dupeHeaders ) {
41+ var tableOpen = "<table>" ;
42+ var tableClose = "</table>" ;
43+ 44+ var headerCell = "<th>{@val}</th>" ;
45+ var cell = "<td>{@val}</td>" ;
46+ 47+ var rowOpen = "<tr>" ;
48+ var rowClose = "</tr>" ;
49+ 50+ var table = tableOpen ;
51+ 52+ for ( i = 0 ; i < tableArray . length ; i ++ ) {
53+ //Row
54+ if ( i === 1 && useHeaders && dupeHeaders ) {
55+ i = 0 ;
56+ useHeaders = false ;
57+ dupeHeaders = false ;
58+ }
59+ 60+ table += rowOpen ;
61+ for ( j = 0 ; j < tableArray [ i ] . length ; j ++ ) {
62+ //Cell
63+ if ( i === 0 && useHeaders ) {
64+ table += headerCell . replace ( "{@val}" , tableArray [ i ] [ j ] ) ;
65+ } else {
66+ table += cell . replace ( "{@val}" , tableArray [ i ] [ j ] ) ;
67+ }
68+ }
69+ 70+ table += rowClose ;
71+ }
72+ 73+ return table + tableClose ;
74+ }
75+ 76+ /**
77+ * Creates a 2D (Multidimentional) array from
78+ * CSV data in string form.
79+ *
80+ * @param {type } csv the CSV data.
81+ * @param {type } separator the character used
82+ * to separate the columns/cells.
83+ * @param {type } quotes ignores the separator
84+ * in quoted text.
85+ * @param {type } maxRows the maximum rows
86+ * to scan.
87+ * @returns {Array|csvTo2DArray.table } the CSV data
88+ * as a 2D (Multidimentional) array.
89+ */
90+ function csvTo2DArray ( csv , separator , quotes , maxRows ) {
91+ var table = [ ] ;
92+ var rows = 0 ;
93+ 94+ csv . split ( "\n" ) . map ( function ( row ) {
95+ if ( maxRows !== "0" )
96+ if ( rows >= maxRows )
97+ return ;
98+ 99+ var tableRow = getRow ( row , separator , quotes ) ;
100+ 101+ if ( tableRow === null )
102+ return table ;
103+ 104+ table . push ( tableRow ) ;
105+ rows ++ ;
106+ } ) ;
107+ 108+ return table ;
109+ }
110+ 111+ /**
112+ * Creates an array from a CSV row (line)
113+ *
114+ * @param {type } row the CSV row.
115+ * @param {type } separator character used to separate
116+ * cells/columns
117+ * @param {type } quotes ignores the separator
118+ * in quoted text.
119+ * @returns {Array|getRow.trow } the CSV row as an array.
120+ */
121+ function getRow ( row , separator , quotes ) {
122+ if ( row . length === 0 )
123+ return null ;
124+ 125+ isQuoted = false ;
126+ var trow = [ ] ;
127+ var cell = "" ;
128+ 129+ for ( var i = 0 ; i < row . length ; i ++ ) {
130+ var char = row . charAt ( i ) ;
131+ 132+ if ( quotes ) {
133+ if ( char === '\"' || char === '\'' ) {
134+ isQuoted = ! isQuoted ;
135+ continue ;
136+ }
137+ }
138+ 139+ if ( char === separator && ! isQuoted ) {
140+ trow . push ( cell ) ;
141+ cell = "" ;
142+ continue ;
143+ }
144+ 145+ cell += char ;
146+ }
147+ 148+ trow . push ( cell ) ;
149+ return trow ;
150+ }
0 commit comments