From 3f5cfe87f93b6f1e7a5b3b4233b70880c5ea9e2a Mon Sep 17 00:00:00 2001 From: NB10328 Date: Thu, 12 Nov 2020 14:42:49 +0000 Subject: [PATCH] added support for cell margin --- README.md | 285 ++++++++++++++++++++------------------ ascii-table3.js | 49 +++++-- package.json | 2 +- samples.js | 36 +++-- test/ascii-table3.test.js | 216 +++++++++++++++++++++-------- 5 files changed, 370 insertions(+), 218 deletions(-) diff --git a/README.md b/README.md index 767b03f..9bfd4df 100644 --- a/README.md +++ b/README.md @@ -37,36 +37,39 @@ var table = console.log(table.toString()); ``` ```asciidoc -+-------------------+ -| Sample table | -+-----+---+---------+ -|Name |Age|Eye color| -+-----+---+---------+ -|John | 23| green | -|Mary | 16| brown | -|Rita | 47| blue | -|Peter| 8| brown | -+-----+---+---------+ ++-------------------------+ +| Sample table | ++-------+-----+-----------+ +| Name | Age | Eye color | ++-------+-----+-----------+ +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | ++-------+-----+-----------+ ``` We can make simpler tables without a title or headings as well. -```javascript -var table = new AsciiTable3.AsciiTable3(); -table - .addRow('a', 'apple', 'Some longer string') - .addRow('b', 'banana', 'hi') - .addRow('c', 'carrot', 'meow') - .addRow('e', 'elephants'); +```javascript +var table = + new AsciiTable3.AsciiTable3() + .setAlign(3, AsciiTable3.CENTER) + .addRowMatrix([ + ['John', 23, 'green'], + ['Mary', 16, 'brown'], + ['Rita', 47, 'blue'], + ['Peter', 8, 'brown'] + ]); console.log(table.toString()); ``` ```asciidoc -+-+---------+------------------+ -|a|apple |Some longer string| -|b|banana |hi | -|c|carrot |meow | -|e|elephants| | -+-+---------+------------------+ ++-------+----+-------+ +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | ++-------+----+-------+ ``` ## Using styles @@ -91,15 +94,13 @@ console.log(table.toString()); ``` ```asciidoc -------------------- - Sample table -------------------- -Name Age Eye color ------ --- --------- -John 23 green -Mary 16 brown -Rita 47 blue -Peter 8 brown +------------------------- + Name Age Eye color +------- ----- ----------- + John 23 green + Mary 16 brown + Rita 47 blue + Peter 8 brown ``` These styles range from simple to more elaborate. @@ -109,16 +110,16 @@ table.setStyle('unicode-single'); console.log(table.toString()); ``` ```asciidoc -┌───────────────────┐ -│ Sample table │ -├─────┬───┬─────────┤ -│Name │Age│Eye color│ -├─────┼───┼─────────┤ -│John │ 23│ green │ -│Mary │ 16│ brown │ -│Rita │ 47│ blue │ -│Peter│ 8│ brown │ -└─────┴───┴─────────┘ +┌─────────────────────────┐ +│ Sample table │ +├───────┬─────┬───────────┤ +│ Name │ Age │ Eye color │ +├───────┼─────┼───────────┤ +│ John │ 23 │ green │ +│ Mary │ 16 │ brown │ +│ Rita │ 47 │ blue │ +│ Peter │ 8 │ brown │ +└───────┴─────┴───────────┘ ``` # API @@ -134,7 +135,7 @@ Creates new table object. ```javascript var AsciiTable3 = require('ascii-table3'); -var table = AsciiTable3.AsciiTable3('Class data'); +var table = AsciiTable3.AsciiTable3('Data'); ``` ## Static Methods @@ -425,7 +426,16 @@ table.addRowMatrix([ [2, 'John', 34] , [3, 'Jim', 83] ]); + +console.log(table.toString()); +``` +```asciidoc ++---+------+----+ +| 2 | John | 34 | +| 3 | Jim | 83 | ++---+------+----+ ``` + #### instance.getRows() Get the multidimension array of rows from the table. @@ -447,7 +457,7 @@ console.log(table.getRows()); ] ``` -### Style +### Styles #### instance.setStyle(name) @@ -459,76 +469,76 @@ Currently available styles are: * **none** - Borderless ```asciidoc - Sample table -Name Age Eye color -John 23 green -Mary 16 brown -Rita 47 blue -Peter 8 brown + Sample table + Name Age Eye color + John 23 green + Mary 16 brown + Rita 47 blue + Peter 8 brown ``` * **compact** - Compact ```asciidoc -------------------- - Sample table -------------------- -Name Age Eye color ------ --- --------- -John 23 green -Mary 16 brown -Rita 47 blue -Peter 8 brown -``` -* **ascii-table** - mimics the original "ascii-table" package table style +------------------------- + Sample table +------------------------- + Name Age Eye color +------- ----- ----------- + John 23 green + Mary 16 brown + Rita 47 blue + Peter 8 brown +``` +* **ascii-table** - mimics the original `ascii-table` npm package table style ```asciidoc -.-------------------. -| Sample table | -|-------------------| -|Name |Age|Eye color| -|-------------------| -|John | 23| green | -|Mary | 16| brown | -|Rita | 47| blue | -|Peter| 8| brown | -.-------------------. +.-------------------------. +| Sample table | +|-------------------------| +| Name | Age | Eye color | +|-------------------------| +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | +.-------------------------. ``` * `**ramac** - beautified 7-bit ASCII output (default) ```asciidoc -+-------------------+ -| Sample table | -+-----+---+---------+ -|Name |Age|Eye color| -+-----+---+---------+ -|John | 23| green | -|Mary | 16| brown | -|Rita | 47| blue | -|Peter| 8| brown | -+-----+---+---------+ ++-------------------------+ +| Sample table | ++-------+-----+-----------+ +| Name | Age | Eye color | ++-------+-----+-----------+ +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | ++-------+-----+-----------+ ``` * **unicode-single** - Single line unicode chars border ```asciidoc -┌───────────────────┐ -│ Sample table │ -├─────┬───┬─────────┤ -│Name │Age│Eye color│ -├─────┼───┼─────────┤ -│John │ 23│ green │ -│Mary │ 16│ brown │ -│Rita │ 47│ blue │ -│Peter│ 8│ brown │ -└─────┴───┴─────────┘ +┌─────────────────────────┐ +│ Sample table │ +├───────┬─────┬───────────┤ +│ Name │ Age │ Eye color │ +├───────┼─────┼───────────┤ +│ John │ 23 │ green │ +│ Mary │ 16 │ brown │ +│ Rita │ 47 │ blue │ +│ Peter │ 8 │ brown │ +└───────┴─────┴───────────┘ ``` * **unicode-double**- Double line unicode chars border ```asciidoc -╔═══════════════════╗ -║ Sample table ║ -╠═════╦═══╦═════════╣ -║Name ║Age║Eye color║ -╠═════╬═══╬═════════╣ -║John ║ 23║ green ║ -║Mary ║ 16║ brown ║ -║Rita ║ 47║ blue ║ -║Peter║ 8║ brown ║ -╚═════╩═══╩═════════╝ +╔═════════════════════════╗ +║ Sample table ║ +╠═══════╦═════╦═══════════╣ +║ Name ║ Age ║ Eye color ║ +╠═══════╬═════╬═══════════╣ +║ John ║ 23 ║ green ║ +║ Mary ║ 16 ║ brown ║ +║ Rita ║ 47 ║ blue ║ +║ Peter ║ 8 ║ brown ║ +╚═══════╩═════╩═══════════╝ ``` #### instance.getStyle() @@ -597,12 +607,12 @@ var table = console.log(table); ``` ```asciidoc - Sample table -Name Age Eye color -John 23 green -Mary 16 brown -Rita 47 blue -Peter 8 brown + Sample table + Name Age Eye color + John 23 green + Mary 16 brown + Rita 47 blue + Peter 8 brown ``` #### instance.setWidth(idx, width) @@ -626,21 +636,19 @@ var table = ]); // set the age column width to 5 characters -table.setWidth(2, 5); +table.setWidth(1, 10); console.log(table.toString()); ``` ```asciidoc -+---------------------+ -| Sample table | -+-----+-----+---------+ -|Name |Age |Eye color| -+-----+-----+---------+ -|John | 23|green | -|Mary | 16|brown | -|Rita | 47|blue | -|Peter| 8|brown | -+-----+-----+---------+ ++----------+-----+-----------+ +| Name | Age | Eye color | ++----------+-----+-----------+ +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | ++----------+-----+-----------+ ``` #### instance.getWidth(idx) @@ -670,6 +678,7 @@ Example: var table = new AsciiTable3.AsciiTable3('Sample table') .setHeading('Name', 'Age', 'Eye color') + .setCellMargin(0) .addRowMatrix([ ['John', 23, 'green'], ['Mary', 16, 'brown'], @@ -722,12 +731,12 @@ table console.log(table.toString()); ``` ``` -+-+---------+------------------+ -|a| apple |Some longer string| -|b| banana | hi| -|c| carrot | meow| -|e|elephants| | -+-+---------+------------------+ ++---+-----------+--------------------+ +| a | apple | Some longer string | +| b | banana | hi | +| c | carrot | meow | +| e | elephants | | ++---+-----------+--------------------+ ``` #### instance.setAlignLeft(idx) @@ -793,11 +802,11 @@ table.sort(function(a, b) { console.log(table.toString()) ``` ```asciidoc -+-+----+--+ -|2|John|34| -|1|Bob |52| -|3|Jim |83| -+-+----+--+ ++---+------+----+ +| 2 | John | 34 | +| 1 | Bob | 52 | +| 3 | Jim | 83 | ++---+------+----+ ``` #### instance.sortColumn(index, comparefunc) @@ -898,14 +907,16 @@ var table = console.log(table.toString()); ``` ```asciidoc -+-----+---+---------+ -|Name |Age|Eye color| -+-----+---+---------+ -|John | 23| green | -|Mary | 16| brown | -|Rita | 47| blue | -|Peter| 8| brown | -+-----+---+---------+ ++-------------------------+ +| Sample table | ++-------+-----+-----------+ +| Name | Age | Eye color | ++-------+-----+-----------+ +| John | 23 | green | +| Mary | 16 | brown | +| Rita | 47 | blue | +| Peter | 8 | brown | ++-------+-----+-----------+ ``` # Install diff --git a/ascii-table3.js b/ascii-table3.js index 1ecc7f7..ed08aba 100644 --- a/ascii-table3.js +++ b/ascii-table3.js @@ -200,14 +200,20 @@ class AsciiTable3 { * @returns {AsciiTable3} The AsciiTable3 object instance. */ clear() { + // clear title and set defualt alignment this.setTitle(); this.setTitleAlignCenter(); + // clear heading and set defualt alignment this.setHeading(); this.setHeadingAlignCenter(); + // clear data rows this.clearRows(); + // set default cell margin + this.setCellMargin(1); + return this; } @@ -237,7 +243,7 @@ class AsciiTable3 { * @returns {string} The table title. */ getTitle() { - return this.title; + return this.title ? this.title : ''; } /** @@ -305,7 +311,7 @@ class AsciiTable3 { * @returns {*[]} Array with heading values. */ getHeading() { - return Array.from(this.heading); + return this.heading ? Array.from(this.heading): []; } /** @@ -388,7 +394,7 @@ class AsciiTable3 { * @returns {*[]} Array with row cell values (column array). */ getRows() { - return Array.from(this.rows); + return this.rows ? Array.from(this.rows): []; } /** @@ -451,6 +457,25 @@ class AsciiTable3 { } } + /** + * Sets the internal cell margin (in characters) + * @param {number} margin + * @returns {AsciiTable3} The AsciiTable3 object instance. + */ + setCellMargin(margin) { + this.cellMargin = margin; + + return this; + } + + /** + * Gets the internal cell margin (in characters) + * @returns {number} The cell margin in characters. + */ + getCellMargin() { + return this.cellMargin; + } + /** * Sets the alignment direction for a given column. * @param {number} idx Column index to align (starts at 1). @@ -579,8 +604,8 @@ class AsciiTable3 { const headings = this.getHeading(); - // init col sizes - if (this.headings && this.headings.length > 0) { + // init col sizes (heading) + if (headings.length > 0) { // use heading colSizes = AsciiTable3.arrayFill(headings.length, 0); } else { @@ -591,7 +616,7 @@ class AsciiTable3 { // loop over headings for(var col = 0; col < headings.length; col++) { // get current cell value string - const cell = '' + headings[col]; + const cell = ''.padStart(this.getCellMargin()) + '' + headings[col]+ ''.padStart(this.getCellMargin()); if (cell.length > colSizes[col]) colSizes[col] = cell.length; } @@ -601,7 +626,7 @@ class AsciiTable3 { // loop over columns for(var col = 0; col < row.length; col++) { // get current cell value string - const cell = '' + row[col]; + const cell = ''.padStart(this.getCellMargin()) + '' + row[col]+ ''.padStart(this.getCellMargin()); if (cell.length > colSizes[col]) colSizes[col] = cell.length; } @@ -686,7 +711,10 @@ class AsciiTable3 { var cellAligned; if (cell) { - cellAligned = AsciiTable3.align(this.getAlign(col + 1), cell, colsWidth[col]); + cellAligned = + AsciiTable3.align(this.getAlign(col + 1), + ''.padStart(this.getCellMargin()) + cell + ''.padStart(this.getCellMargin()) , + colsWidth[col]); } else { cellAligned = ''.padStart(colsWidth[col]); } @@ -705,8 +733,8 @@ class AsciiTable3 { * @returns {string} String rendiring of this instance table. */ toString() { + // determine table columns max width const colsWidth = this.getColumnsWidth(); - //console.log(colsWidth); // get style const style = this.getStyle(); @@ -715,12 +743,11 @@ class AsciiTable3 { const maxWidth = colsWidth.reduce(function(a, b) { return a + b; }, 0) + // data column sizes (colsWidth.length - 1) * style.borders.data.colSeparator.length; // mid column separators - //console.log(maxWidth); var result = ''; // title - if (this.getTitle()) { + if (this.getTitle() && this.getTitle().length > 0) { // top line (above title) result += style.borders.top.left + ''.padStart(maxWidth, style.borders.top.center) + style.borders.top.right + '\n'; diff --git a/package.json b/package.json index 2648d9c..6053686 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ascii-table3", - "version": "0.0.5", + "version": "0.0.6", "author": "João Simões (https://github.com/AllMightySauron)", "description": "Javascript ASCII renderer for beautiful console-based tables", "repository": { diff --git a/samples.js b/samples.js index 2ab2232..8c78f3b 100644 --- a/samples.js +++ b/samples.js @@ -16,8 +16,23 @@ var table = ['Rita', 47, 'blue'], ['Peter', 8, 'brown'] ]); + +console.log(`initial table:\n${table.toString()}`); + +/* ----------------------------------- + -- Example 2. cell margins + ----------------------------------- */ +table.setCellMargin(0); +console.log(`cell margin = 0:\n${table.toString()}`); + +table.setCellMargin(2); +console.log(`cell margin = 2:\n${table.toString()}`); -// test out styles +/* ----------------------------------- + -- Example 3. Test out styles + ----------------------------------- */ +table.setCellMargin(1); + table.setStyle('none'); console.log(`none:\n${table.toString()}`); @@ -37,14 +52,17 @@ table.setStyle('unicode-double'); console.log(`unicode-double:\n${table.toString()}`); /* ----------------------------------- - -- Example 2. no title or heading + -- Example 4. setting column width ----------------------------------- */ -var table2 = new AsciiTable3.AsciiTable3(); + table.setStyle("ramac"); + table.setWidth(1, 10); + console.log(`name width = 10:\n${table.toString()}`); -table2 - .addRow('a', 'apple', 'Some longer string') - .addRow('b', 'banana', 'hi') - .addRow('c', 'carrot', 'meow') - .addRow('e', 'elephants'); +/* ----------------------------------- + -- Example 5. no title or heading + ----------------------------------- */ +table.setTitle(); +table.setHeading(); -console.log(table2.toString()); +table.setStyle('ramac'); +console.log(`no title/heading:\n${table.toString()}`); diff --git a/test/ascii-table3.test.js b/test/ascii-table3.test.js index e2e68a4..99fe74a 100644 --- a/test/ascii-table3.test.js +++ b/test/ascii-table3.test.js @@ -74,24 +74,17 @@ describe('Array methods', () => { }); }); +const asciiTable = new AsciiTable3.AsciiTable3(); + // instance methods -describe('Table methods (normal)', () => { - const asciiTable = - new AsciiTable3.AsciiTable3('Dummy title') - .setHeading('Title', 'Count', 'Rate (%)') - .setAlign(1, AsciiTable3.CENTER) - .setAlign(2, AsciiTable3.RIGHT) - .setWidth(1, 5) - .addRow('Dummy 1', 10, 2.3) - .addRowMatrix([ ['Dummy 2', 5, 3.1], ['Dummy 3', 100, 3.14] ]); - - const tableJSON = JSON.parse(asciiTable.toJSON()); - - it('Title', () => { +describe('Title', () => { + + it('setTitle/getTitle', () => { + asciiTable.setTitle('Dummy title'); assert.strictEqual(asciiTable.getTitle(), 'Dummy title'); }); - it('Title align', () => { + it('setTitleAlign/getTitleAlign', () => { // default is center assert.strictEqual(asciiTable.getTitleAlign(), AsciiTable3.CENTER); @@ -104,12 +97,15 @@ describe('Table methods (normal)', () => { asciiTable.setTitleAlignCenter(); assert.strictEqual(asciiTable.getTitleAlign(), AsciiTable3.CENTER); }); +}); - it ('Heading', () => { +describe('Heading', () => { + it ('setHeading/getHeading', () => { + asciiTable.setHeading('Title', 'Count', 'Rate (%)'); assert.notStrictEqual(asciiTable.getHeading(), [ 'Title', 'Count', 'Rate (%)' ]); }); - it('Heading align', () => { + it('setHeadingAlign/getHeadingAlign', () => { // default is center assert.strictEqual(asciiTable.getHeadingAlign(), AsciiTable3.CENTER); @@ -122,14 +118,20 @@ describe('Table methods (normal)', () => { asciiTable.setHeadingAlignCenter(); assert.strictEqual(asciiTable.getHeadingAlign(), AsciiTable3.CENTER); }); - - it ('Col width', () => { - assert.strictEqual(asciiTable.getWidth(1), 5); - assert.strictEqual(asciiTable.getWidth(2), undefined); - assert.strictEqual(asciiTable.getWidth(3), undefined); +}); + +describe('DataRows', () => { + it ('addRow/getRows', () => { + asciiTable.addRow('Dummy 1', 10, 2.3); + + const rows = asciiTable.getRows(); + assert.strict(rows.length, 1); + assert.notStrictEqual(rows[0], [ 'Dummy 1', 10, 2.3 ]); }); - it ('addRow/addRowMatrix', () => { + it ('addRowMatrix/getRows', () => { + asciiTable.addRowMatrix([ ['Dummy 2', 5, 3.1], ['Dummy 3', 100, 3.14] ]); + const rows = asciiTable.getRows(); assert.strictEqual(rows.length, 3); @@ -137,27 +139,10 @@ describe('Table methods (normal)', () => { assert.notStrictEqual(rows[1], [ 'Dummy 2', 5, 3.1 ]); assert.notStrictEqual(rows[2], [ 'Dummy 3', 100, 3.14 ]); }); +}); - it('Data align', () => { - // default alignment should be auto - assert.strictEqual(asciiTable.getAlign(1), AsciiTable3.CENTER); - assert.strictEqual(asciiTable.getAlign(2), AsciiTable3.RIGHT); - assert.strictEqual(asciiTable.getAlign(3), AsciiTable3.AUTO); - }); - - it ('toJSON', () => { - assert.strictEqual(tableJSON.title, asciiTable.getTitle()); - assert.notStrictEqual(tableJSON.heading, asciiTable.getHeading()); - assert.notStrictEqual(tableJSON.rows, asciiTable.getRows()); - }); - - it ('fromJSON', () => { - const newTable = new AsciiTable3.AsciiTable3().fromJSON(tableJSON); - - assert.strictEqual(newTable.toJSON(), asciiTable.toJSON()); - }); - - it ('sort', () => { +describe('Sorting', () => { + it ('sort', () => { const aTable = new AsciiTable3.AsciiTable3('one column data table') .addRowMatrix([ [1], [9], [5] ]); @@ -177,9 +162,54 @@ describe('Table methods (normal)', () => { assert.notStrictEqual(aTable, [ ['a', 1], ['c', 5], ['b', 9] ]); }); +}); - it ('toString', () => { - // ramac (default) +describe('Styling', () => { + it ('setStyle/getStyle', () => { + const style = asciiTable.getStyle(); + + // ramac should be the default + assert.strictEqual(style.name, "ramac"); + + asciiTable.setStyle("none"); + assert.strictEqual(asciiTable.getStyle().name, "none"); + }); + + it ('removeBorder', () => { + asciiTable.removeBorder(); + assert.strictEqual(asciiTable.getStyle().name, "none"); + }); + + it ('setWidth/getWidth', () => { + asciiTable.setWidth(1, 5); + + assert.strictEqual(asciiTable.getWidth(1), 5); + assert.strictEqual(asciiTable.getWidth(2), undefined); + assert.strictEqual(asciiTable.getWidth(3), undefined); + }); + + it ('setWidths/getWidths', () => { + asciiTable.setWidths([5, undefined, undefined]); + assert.notStrictEqual(asciiTable.getWidths(), [5, undefined, undefined]); + }); + + it('setAlign/getAlign', () => { + asciiTable + .setAlign(1, AsciiTable3.CENTER) + .setAlign(2, AsciiTable3.RIGHT); + + // default alignment should be auto + assert.strictEqual(asciiTable.getAlign(1), AsciiTable3.CENTER); + assert.strictEqual(asciiTable.getAlign(2), AsciiTable3.RIGHT); + assert.strictEqual(asciiTable.getAlign(3), AsciiTable3.AUTO); + }); +}); + +describe('Rendering', () => { + it ('toString (full)', () => { + asciiTable.setCellMargin(0); + + asciiTable.setStyle("ramac"); assert.strictEqual( asciiTable.toString(), '+--------------------+\n' + @@ -244,26 +274,50 @@ describe('Table methods (normal)', () => { '╚═════╩═════╩════════╝\n' ); }); -}); -describe('Table methods (no heading)', () => { - const asciiTable = - new AsciiTable3.AsciiTable3('Dummy title') - .setAlign(1, AsciiTable3.LEFT) - .setAlign(2, AsciiTable3.RIGHT) - .setWidths([10, 4, 6]) - .addRowMatrix([ ['Dummy 1', 10, 2.3], ['Dummy 2', 5, 3.1], ['Dummy 3', 100, 3.14] ]); - - const tableJSON = JSON.parse(asciiTable.toJSON()); + it ('toString (cell margins)', () => { + asciiTable.setWidths(); + asciiTable.setStyle('ramac'); + + asciiTable.setCellMargin(1); + assert.strictEqual(asciiTable.toString(), + '+----------------------------+\n' + + '| Dummy title |\n' + + '+---------+-------+----------+\n' + + '| Title | Count | Rate (%) |\n' + + '+---------+-------+----------+\n' + + '| Dummy 1 | 10 | 2.3 |\n' + + '| Dummy 2 | 5 | 3.1 |\n' + + '| Dummy 3 | 100 | 3.14 |\n' + + '+---------+-------+----------+\n' + ); - it ('Col width', () => { - assert.notStrictEqual(asciiTable.getWidths(), [5, 4, 6]); + asciiTable.setCellMargin(2); + assert.strictEqual(asciiTable.toString(), + '+----------------------------------+\n' + + '| Dummy title |\n' + + '+-----------+---------+------------+\n' + + '| Title | Count | Rate (%) |\n' + + '+-----------+---------+------------+\n' + + '| Dummy 1 | 10 | 2.3 |\n' + + '| Dummy 2 | 5 | 3.1 |\n' + + '| Dummy 3 | 100 | 3.14 |\n' + + '+-----------+---------+------------+\n' + ); }); - it ('toString', () => { + it ('toString (no heading)', () => { + const aTable = new AsciiTable3.AsciiTable3('Dummy title') + .setAlign(1, AsciiTable3.LEFT) + .setAlign(2, AsciiTable3.RIGHT) + .setWidths([10, 4, 6]) + .setCellMargin(0) + .addRowMatrix([ ['Dummy 1', 10, 2.3], ['Dummy 2', 5, 3.1], ['Dummy 3', 100, 3.14] ]); + // ramac + aTable.setStyle("ramac"); assert.strictEqual( - asciiTable.toString(), + aTable.toString(), '+----------------------+\n' + '| Dummy title |\n' + '+----------+----+------+\n' + @@ -273,14 +327,56 @@ describe('Table methods (no heading)', () => { '+----------+----+------+\n' ); - asciiTable.setStyle("none"); + aTable.setStyle("none"); assert.strictEqual( - asciiTable.toString(), + aTable.toString(), ' Dummy title \n' + 'Dummy 1 10 2.3\n' + 'Dummy 2 5 3.1\n' + 'Dummy 3 100 3.14\n' ); }); +}); + +describe('Serialization', () => { + const aTable = new AsciiTable3.AsciiTable3('Title') + .setHeading('Name', 'Age', 'Gender') + .setWidths([10, 3, 6]) + .addRowMatrix([ + ['Mary', 20, 'F'], + ['John', 23, 'M'], + ['Susan', 32, 'F'] + ]); + + const tableJSON = JSON.parse(aTable.toJSON()); + + it ('toJSON', () => { + assert.strictEqual(tableJSON.title, aTable.getTitle()); + assert.notStrictEqual(tableJSON.widths, aTable.getWidths()); + assert.notStrictEqual(tableJSON.heading, aTable.getHeading()); + assert.notStrictEqual(tableJSON.rows, aTable.getRows()); + }); + + it ('fromJSON', () => { + const newTable = new AsciiTable3.AsciiTable3().fromJSON(tableJSON); + + assert.strictEqual(newTable.toJSON(), aTable.toJSON()); + }); +}); + +describe('Clearing data', () => { + it ('clearRows', () => { + assert.strictEqual(asciiTable.getRows().length, 3); + asciiTable.clearRows(); + assert.strictEqual(asciiTable.getRows().length, 0); + }); + + it ('clear', () => { + asciiTable.clear(); + + assert.strictEqual(asciiTable.getTitle(), ''); + assert.notStrictEqual(asciiTable.getHeading(), []); + assert.strictEqual(asciiTable.getRows().length, 0); + }); });