FlexTable (flextablejs) is a JavaScript library that provides a class with a simple API to work with tables (headers, rows, columns, sort, format) facilitating the analysis and data manipulation.
This library is commonly used with CSV Types to parse text files with fields delimited by a character (like CSV). FlexTable is compatible with the data structure produced by CSV Types. Every values-headers definition can be converted to a FlexTable. If the CSV file follow the types specs, multiple tables can be created from a single CSV file.
FlexTable (flextablejs) is a JavaScript library that provides a class with a simple API to work with tables (headers, rows, columns, sort, format) facilitating the analysis and data manipulation.
npm i flextablejs -S
Or from the repo:
npm i "http://github.com/Group4Layers/flextable.git"
It has been tested with node >= 6
, but it is widely used in Firefox and Chrome with building tools like webpack
.
This library is commonly used with CSV Types to parse text files with fields delimited by a character (like CSV). FlexTable is compatible with the data structure produced by CSV Types. Every values-headers definition can be converted to a FlexTable. If the CSV file follow the types specs, multiple tables can be created from a single CSV file.
The best way to learn something is to see how it behaves.
Starting from an empty table
const FlexTable = require('flextablejs').FlexTable;
let table = new FlexTable();
table.setColumn('device type', ['CPU', 'GPU', 'FPGA']);
table.setColumn('speedup', [1, 8.43, 2.3]);
table.appendRow(['Xeon Phi', 3.5]);
// get the indices and set values manually
let idx = table.idx;
// update the Xeon Phi
table.values[3][idx['speedup']] = 3.455;
// change the header name
table.headers[idx['speedup']] = 'S(t)';
delete table.idx; // remove the indices to force update
// convert a column (rounding)
table.setColumn('S(t)', value => Number(value.toFixed(2)))
let results = table.format('markdown');
console.log(results)
:
| device type | S(t) |
|-------------|------|
| CPU | 1 |
| GPU | 8.4 |
| FPGA | 2.3 |
| Xeon Phi | 3.5 |
Importing the data from a CSV with headers
#device type,S(t)
CPU,1
GPU,8.43
FPGA,2.3
Xeon Phi,3.455
const CSV = require('csv-types').CSV;
const FlexTable = require('flextablejs').FlexTable;
let parsed = new CSV().parse(`#device type,S(t)
CPU,1
GPU,8.43
FPGA,2.3
Xeon Phi,3.455
`);
let table = new FlexTable(parsed);
// convert a column (lower case, hyphen)
table.setColumn('device type', value => value.toLowerCase().replace(/ /g, '-'));
let results = table.format('md');
console.log(results)
:
| device type | S(t) |
|-------------|------|
| CPU | 1 |
| GPU | 8.4 |
| FPGA | 2.3 |
| Xeon Phi | 3.5 |
Until the README is completed and improved with real examples, we encourage you to read the source code documentation. It has examples of use: Docs.
- Write multiple examples showing the library.
- Perform more tests and increase the code coverage.
- Improve the documentation of the source code.
- Show real use cases.
You can request new features for this library by opening new issues. If we find it useful (or there are at least 2 users interested in a proposal) we can implement it. Also, we accept pull requests with bugfixes or new features.
npm test
npm run coverage
Test covered:
FlexTable
Rows
✓ append rows at the end
✓ insert rows at the middle
✓ insert rows at the beginning
✓ remove rows at the beginning
✓ remove rows at the middle
✓ remove rows at the end
✓ replace rows at the beginning
✓ replace rows at the middle
✓ replace rows at the end
✓ remove rows by a custom function (not true leaves unchanged)
✓ remove rows by a custom function (true removes)
✓ modify rows by a custom function (null leaves unchanged)
✓ modify rows by a custom function (not null modifies the row)
Columns
✓ setHeadersIndex
✓ setColumn triggers setHeadersIndex
✓ setColumn can remove a column that exists
✓ setColumn cannot remove a column that doesn't exist
✓ setColumns can remove columns
✓ setColumn can replace a column that exists
✓ setColumn can add a column that doesn't exist
✓ setColumns can replace and add columns
✓ setColumn can use a custom function to modify values (cast)
✓ setColumn can use a custom function to insert values (based on the row)
Create
✓ create an empty table, append a row and insert a row
✓ create an empty table and append rows
✓ create an empty table without headers and with rows throws
✓ create a table with rows
Sort
✓ can sort rows using mapchains and sorters (<num, skip)
✓ can sort rows using mapchains and sorters (>num, skip, <num)
✓ can sort rows using mapchains and sorters (custom funcs)
Format
✓ as markdown (default fmt)
✓ as markdown (custom fmt)
✓ as markdown (by column fmt)
✓ as markdown (escaping)
✓ as csv (default fmt)
✓ as csv (custom fmt)
✓ as csv (by column fmt)
✓ as csv (escaping)
clone
✓ supports cloning (not shared references)
39 passing (72ms)
nozalr [email protected] (Group4Layers®).
GitHub/Gitlab readers (repo, no docs): CHANGELOG.md.
ExImageInfo source code is released under the MIT License.
GitHub/Gitlab readers (repo, no docs): LICENSE.md.