From 2c57172751bdcfcf6e63bc600fa0c507d002c1cb Mon Sep 17 00:00:00 2001 From: AllMightySauron Date: Fri, 5 Aug 2022 10:33:06 +0100 Subject: [PATCH] sanity check for unknown styles on setStyle() --- ascii-table3.js | 6 ++++-- package.json | 2 +- test/ascii-table3.test.js | 11 +++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ascii-table3.js b/ascii-table3.js index b1739d0..49f0e2f 100644 --- a/ascii-table3.js +++ b/ascii-table3.js @@ -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; } diff --git a/package.json b/package.json index 723cd0e..3df1ed9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ascii-table3", - "version": "0.7.6", + "version": "0.7.7", "author": "João Simões (https://github.com/AllMightySauron)", "description": "Javascript ASCII renderer for beautiful console-based tables", "repository": { diff --git a/test/ascii-table3.test.js b/test/ascii-table3.test.js index 16516a1..7d3834f 100644 --- a/test/ascii-table3.test.js +++ b/test/ascii-table3.test.js @@ -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', () => {