@@ -6,31 +6,43 @@ so it can be pasted into the medium of choice.
66The configuration is very limited by design, all that's configurable in the
77current version is the maximun width of the columns.
88
9- The API exposes only two methods: ` run (rows, [options ])` where ` rows ` is expected to be
9+ The API exposes only two methods to render a table : ` table (rows, [maxColWidth ])` where ` rows ` is expected to be
1010an array with an index for every row, and each row is also expected to be an array
11- with one index for every column, and ` getMaxColumnWidth(rows) ` to get the width of the
12- widest column.
11+ with one index for every column. The data in the columns can be of any Javascript type
12+ since it will be serialized before printed.
13+
14+ The second method to generate a table is: ` tableFromSerializedData(serializedRows, [maxColWidth]) `
15+ where ` serializedRows ` is expected to be in the same format as the previously described method,
16+ but all data must already be serialized. This method should be used when the data stays the same
17+ but are generated with multiple maxColWidths.
18+
19+ To serialize the data, the method ` serializeData(rows) ` is exposed. For the moment, all it does is to
20+ table ` JSON.stringify ` on the data.
21+
22+ To get the width of the widest column (can be used to set the max value on a slider), ` maxColumnWidth(rows) `
23+ is exposed. The rows should not already be serialized when calling this method.
24+
1325All rows should have the same number of columns, and the first row is expected to
1426be the header column with titles for each column.
1527
1628``` javascript
1729[
1830 [' first column' , ' second column' ], // title row
1931 [' my data row 1 col 1' , ' my data row 1 col 2' ], // first data row
20- [' my data row 2 col 1' , ' my data row 2 col 2' ], // second data row
32+ [' my data row 2 col 1' , ' my data row 2 col 2' ] // second data row
2133]
2234```
2335
2436With default max width, the above would produce:
2537
2638```
27- +===================+===================+
28- | first column | second column |
29- +===================+===================+
30- | my data row 1 col 1| my data row 1 col 2|
31- +-------------------+-------------------+
32- | my data row 2 col 1| my data row 2 col 2|
33- +-------------------+-------------------+
39+ ╒═════════════════════╤═════════════════════╕
40+ │" first column" │" second column" │
41+ ╞═════════════════════╪═════════════════════╡
42+ │" my data row 1 col 1"│" my data row 1 col 2"│
43+ ├─────────────────────┼─────────────────────┤
44+ │" my data row 2 col 1"│" my data row 2 col 2"│
45+ └─────────────────────┴─────────────────────┘
3446```
3547
3648## Installation
@@ -65,10 +77,10 @@ import AsciiTable from 'ascii-data-table'
6577const items = [[' x' , ' y' ], [' a' , ' b' ], [' c' , ' d' ]]
6678
6779// Not required, default is 30
68- const options = {maxColumnWidth : 15 }
80+ const maxColumnWidth = 15
6981
7082// Render and save in 'res'
71- const res = AsciiTable .run (items, options )
83+ const res = AsciiTable .table (items, maxColumnWidth )
7284```
7385
7486** In ES 5.5**
@@ -81,20 +93,29 @@ var AsciiTable = require('ascii-data-table').default
8193// var AsciiTable = require('lib/ascii-data-table').default
8294
8395var items = [[' x' , ' y' ], [' a' , ' b' ], [' c' , ' d' ]]
84- var res = AsciiTable .run (items)
96+ var res = AsciiTable .table (items)
8597```
8698
8799### In web browsers
88100A bundle for web browsers is created and can be found in ` lib ` .
89101
90102``` html
91- <script type =" text/javascript" src =" /components/lib/bundle.js" ></script >
92- <script type =" text/javascript" >
93- var items = [[' x' , ' y' ], [' a' , ' b' ], [' c' , ' d' ]]
94- var output = AsciiTable .run (items)
95- document .getElementById (' my-table' ).innerHTML = output
96- console .log (output)
97- </script >
103+ <html >
104+ <head >
105+ <script type =" text/javascript" src =" /components/lib/bundle.js" ></script >
106+ <script type =" text/javascript" >
107+ function load () {
108+ var items = [[' x' , ' y' ], [' a' , ' b' ], [' c' , ' d' ]]
109+ var output = AsciiTable .table (items)
110+ document .getElementById (' my-table' ).innerHTML = output
111+ console .log (output)
112+ }
113+ </script >
114+ </head >
115+ <body onload =" load()" >
116+ <pre id =" my-table" >loading...</pre >
117+ </body >
118+ </html >
98119```
99120
100121### For React >= 0.14
@@ -137,7 +158,7 @@ assumes there's a global variable named `angular` available.
137158 .module (' myApp' , [' AsciiTableModule' ])
138159 .controller (' TableController' , [' $scope' , ' AsciiTable' , function ($scope , AsciiTable ){
139160 var items = [[' x' , ' y' ], [' a' , ' b' ], [' c' , ' d' ]]
140- $scope .data = AsciiTable .run (items)
161+ $scope .data = AsciiTable .table (items)
141162 }])
142163 </script >
143164 </head >
@@ -152,11 +173,11 @@ You can try online here: [Online demo](https://oskarhane-dropshare-eu.s3-eu-cent
152173In the ` examples ` folder there are examples for node and web browser environments.
153174One cool thing in the browser demo is that you can hook up a range slider to the maximun
154175width of the columns, giving this effect:
155- ![ slider-gif-demo] ( https://oskarhane-dropshare-eu.s3-eu-central-1.amazonaws.com/ascii-data-table-slider-lfbBzm2sql/ascii-data-table-slider .gif )
176+ ![ slider-gif-demo] ( https://oskarhane-dropshare-eu.s3-eu-central-1.amazonaws.com/adt-2-Q2Qhvevx2E/adt-2 .gif )
156177
157178## Testing
158- Run ` npm test ` to execute test in both Node.js and browser environments.
159- Run ` npm run test:watch ` to have tests run on file changes.
179+ table ` npm test ` to execute test in both Node.js and browser environments.
180+ table ` npm table test:watch ` to have tests table on file changes.
160181
161182## Contributing
162- All bug reports, feature requests and pull requests are welcome. This project uses the [ Javascript Standard Style] ( http://standardjs.com ) and a lint check will run before all tests and builds.
183+ All bug reports, feature requests and pull requests are welcome. This project uses the [ Javascript Standard Style] ( http://standardjs.com ) and a lint check will table before all tests and builds.
0 commit comments