From ce01874173f7affe067826843c21072851c80fac Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Sun, 10 Feb 2019 18:36:56 +0000 Subject: [PATCH] fix: correct cell width resolution logic (fixes #88, #9) --- src/calculateCellWidthIndex.js | 6 +++++- test/calculateCellWidthIndex.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/calculateCellWidthIndex.js b/src/calculateCellWidthIndex.js index f6b90dd..6e4f7a5 100644 --- a/src/calculateCellWidthIndex.js +++ b/src/calculateCellWidthIndex.js @@ -8,6 +8,10 @@ import stringWidth from 'string-width'; */ export default (cells) => { return cells.map((value) => { - return stringWidth(value); + return Math.max( + ...value.split('\n').map((line) => { + return stringWidth(line); + }) + ); }); }; diff --git a/test/calculateCellWidthIndex.js b/test/calculateCellWidthIndex.js index f8f9be7..b833498 100644 --- a/test/calculateCellWidthIndex.js +++ b/test/calculateCellWidthIndex.js @@ -17,4 +17,13 @@ describe('calculateCellWidthIndex', () => { expect(cellWidthIndex[2]).to.equal(6, 'third column'); }); }); + context('cell contains newline characters', () => { + it('picks the longest line length', () => { + const cellWidthIndex = calculateCellWidthIndex([ + 'aaaa\naa' + ]); + + expect(cellWidthIndex[0]).to.equal(4); + }); + }); });