Skip to content

Commit

Permalink
sanity check for unknown styles on setStyle()
Browse files Browse the repository at this point in the history
  • Loading branch information
AllMightySauron authored and AllMightySauron committed Aug 5, 2022
1 parent dd9a742 commit 2c57172
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions ascii-table3.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,14 @@ class AsciiTable3 {

/**
* Sets the output style for this table instance.
* @param {string} name The desired style name.
* @param {string} name The desired style name (defaults to "ramac" if not found).
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setStyle(name) {
/** @type {Style} */
this.style = this.styles.find(style => style.name == name);
const foundStyle = this.styles.find(style => style.name == name);

this.style = foundStyle ? foundStyle : this.styles.find(style => style.name == "ramac");

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ascii-table3",
"version": "0.7.6",
"version": "0.7.7",
"author": "João Simões <[email protected]> (https://github.com/AllMightySauron)",
"description": "Javascript ASCII renderer for beautiful console-based tables",
"repository": {
Expand Down
11 changes: 7 additions & 4 deletions test/ascii-table3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,17 @@ describe('Sorting', () => {
describe('Styling', () => {
it ('setStyle/getStyle', () => {
const aTable = new AsciiTable3();

const style = aTable.getStyle();

// ramac should be the default
assert.strictEqual(style.name, "ramac");
// ramac should be the default style
assert.strictEqual(aTable.getStyle().name, "ramac");

// set a new style
aTable.setStyle("none");
assert.strictEqual(aTable.getStyle().name, "none");

// set an unknown style (defaults to ramac)
aTable.setStyle("unknown");
assert.strictEqual(aTable.getStyle().name, "ramac");
});

it('getStyles', () => {
Expand Down

0 comments on commit 2c57172

Please sign in to comment.