From fbe2f9b9ab3f688820103b8a65fbf78fbe670306 Mon Sep 17 00:00:00 2001 From: yujay Date: Wed, 17 Nov 2021 11:47:20 -0800 Subject: [PATCH] handle undefined options --- lib/index.js | 2 +- test/index.test.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index b44ae8a..c8d1790 100644 --- a/lib/index.js +++ b/lib/index.js @@ -48,7 +48,7 @@ function Table (options){ , head: [] }, options); - if (options.rows) { + if (options && options.rows) { for (var i = 0; i < options.rows.length; i++) { this.push(options.rows[i]); } diff --git a/test/index.test.js b/test/index.test.js index 11af839..19068ba 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -309,4 +309,25 @@ module.exports = { table.toString().should.eql(expected.join("\n")); }, + 'test undefined options in constructor': () => { + const table = new Table(); + table.push( + ['foo', 'bar'] + , ['frobnicate', 'bar', ] + ); + + const off = '\u001b[39m'; + const grey = '\u001b[90m'; + + const expected = [ + grey + '┌────────────┬─────┐' + off + , grey + '│' + off +' foo ' + grey + '│' + off + ' bar ' + grey + '│' + off + , grey + '├────────────┼─────┤' + off + , grey + '│' + off + ' frobnicate ' + grey + '│' + off + ' bar ' + grey + '│' + off + , grey + '└────────────┴─────┘' + off + ]; + + table.toString().should.eql(expected.join('\n')); + }, + };