From 4323d60549b20822129b6f20577c8678a9877335 Mon Sep 17 00:00:00 2001 From: Nahiyan Kamal Date: Sun, 2 Jul 2023 11:49:03 +0200 Subject: [PATCH] fix: Allow empty Column Title (#451) --- src/internalTable/input-converter.ts | 2 +- src/utils/table-helpers.ts | 2 +- .../__snapshots__/headerTitle.test.ts.snap | 9 ++++ test/internalTable/headerTitle.test.ts | 46 +++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/internalTable/input-converter.ts b/src/internalTable/input-converter.ts index c3800e6f..ed752012 100644 --- a/src/internalTable/input-converter.ts +++ b/src/internalTable/input-converter.ts @@ -17,7 +17,7 @@ export const rawColumnToInternalColumn = ( column: ColumnOptionsRaw ): Column => ({ name: column.name, - title: column.title || column.name, + title: column.title ?? column.name, ...objIfExists('color', column.color as COLOR), ...objIfExists('maxLen', column.maxLen), ...objIfExists('minLen', column.minLen), diff --git a/src/utils/table-helpers.ts b/src/utils/table-helpers.ts index 113560ac..c3961fe8 100644 --- a/src/utils/table-helpers.ts +++ b/src/utils/table-helpers.ts @@ -73,7 +73,7 @@ export const createColumFromComputedColumn = ( column: ComputedColumn ): Column => ({ name: column.name, - title: column.title || column.name, + title: column.title ?? column.name, ...objIfExists('color', column.color as COLOR), ...objIfExists('maxLen', column.maxLen), ...objIfExists('minLen', column.minLen), diff --git a/test/internalTable/__snapshots__/headerTitle.test.ts.snap b/test/internalTable/__snapshots__/headerTitle.test.ts.snap index b6b825be..e3b0a416 100644 --- a/test/internalTable/__snapshots__/headerTitle.test.ts.snap +++ b/test/internalTable/__snapshots__/headerTitle.test.ts.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Testing Title Of Column Empty string title means blank title 1`] = ` +"┌───┬───────────────────────────────────┬───────────────────────────┐ +│   │   │ Other two should be blank │ +├───┼───────────────────────────────────┼───────────────────────────┤ +│ 2 │  This row is blue │  10.212  │ +│ 3 │ I would like some red wine please │  10.212  │ +└───┴───────────────────────────────────┴───────────────────────────┘" +`; + exports[`Testing Title Of Column title is used in table printing 1`] = ` "┌──────────────────────┬───────────────────────────────────┬────────────────────────┐ │ Red Left Align Index │  Right Align Text │ Big Green Value Center │ diff --git a/test/internalTable/headerTitle.test.ts b/test/internalTable/headerTitle.test.ts index d842a9c2..983c9ab1 100644 --- a/test/internalTable/headerTitle.test.ts +++ b/test/internalTable/headerTitle.test.ts @@ -47,6 +47,52 @@ describe('Testing Title Of Column', () => { console.log(returned); expect(returned).toMatchSnapshot(); + }); + + it('Empty string title means blank title', () => { + // Create a table + const p = new Table({ + columns: [ + { + name: 'red_left_align_index', + alignment: 'left', + title: '', + }, + { + name: 'right_align_text', + alignment: 'right', + title: '', + }, + { + name: 'green_value_center', + alignment: 'center', + title: 'Other two should be blank', + }, + ], + }); + + // add rows with color + p.addRow( + { + red_left_align_index: 2, + right_align_text: 'This row is blue', + green_value_center: 10.212, + }, + { color: 'blue' } + ); + p.addRow( + { + red_left_align_index: 3, + right_align_text: 'I would like some red wine please', + green_value_center: 10.212, + }, + { color: 'red' } + ); + + // print + const returned = renderTable(p.table); + console.log(returned); + expect(returned).toMatchSnapshot(); }); });