From 65f9cfd0cbbfbc9469f54716f02de7f884db7d4f Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Fri, 27 Aug 2021 22:06:05 +0200 Subject: [PATCH 1/9] Refactored the code into sub-functions --- index.js | 148 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 98 insertions(+), 50 deletions(-) diff --git a/index.js b/index.js index f4737f6..87c4466 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,10 @@ const camelCase = require('camelcase'); const ansiAlign = require('ansi-align'); const wrapAnsi = require('wrap-ansi'); +const NL = '\n'; +const PAD = ' '; +const BORDERS_WIDTH = 2; + const terminalColumns = () => { const {env, stdout, stderr} = process; @@ -71,6 +75,79 @@ const getBorderChars = borderStyle => { return chararacters; }; +const makeContentText = (text, padding, columns, align) => { + text = ansiAlign(text, {align}); + let lines = text.split(NL); + const textWidth = widestLine(text); + + const max = columns - padding.left - padding.right; + + if (textWidth > max) { + const newLines = []; + for (const line of lines) { + const createdLines = wrapAnsi(line, max, {hard: true}); + const alignedLines = ansiAlign(createdLines, {align}); + const alignedLinesArray = alignedLines.split('\n'); + const longestLength = Math.max(...alignedLinesArray.map(s => stringWidth(s))); + + for (const alignedLine of alignedLinesArray) { + let paddedLine; + switch (align) { + case 'center': + paddedLine = PAD.repeat((max - longestLength) / 2) + alignedLine; + break; + case 'right': + paddedLine = PAD.repeat(max - longestLength) + alignedLine; + break; + default: + paddedLine = alignedLine; + break; + } + + newLines.push(paddedLine); + } + } + + lines = newLines; + } + + if (align === 'center' && textWidth < max) { + lines = lines.map(line => PAD.repeat((max - textWidth) / 2) + line); + } else if (align === 'right' && textWidth < max) { + lines = lines.map(line => PAD.repeat(max - textWidth) + line); + } + + const paddingLeft = PAD.repeat(padding.left); + const paddingRight = PAD.repeat(padding.right); + + lines = lines.map(line => paddingLeft + line + paddingRight); + + lines = lines.map(line => { + if (columns - stringWidth(line) > 0) { + switch (align) { + case 'center': + return line + PAD.repeat(columns - stringWidth(line)); + case 'right': + return line + PAD.repeat(columns - stringWidth(line)); + default: + return line + PAD.repeat(columns - stringWidth(line)); + } + } + + return line; + }); + + if (padding.top > 0) { + lines = new Array(padding.top).fill(PAD.repeat(columns)).concat(lines); + } + + if (padding.bottom > 0) { + lines = lines.concat(new Array(padding.bottom).fill(PAD.repeat(columns))); + } + + return lines.join(NL); +}; + const isHex = color => color.match(/^#(?:[0-f]{3}){1,2}$/i); const isColorValid = color => typeof color === 'string' && ((chalk[color]) || isHex(color)); const getColorFn = color => isHex(color) ? chalk.hex(color) : chalk[color]; @@ -105,47 +182,26 @@ module.exports = (text, options) => { const colorizeContent = content => options.backgroundColor ? getBGColorFn(options.backgroundColor)(content) : content; - const NL = '\n'; - const PAD = ' '; const columns = terminalColumns(); - text = ansiAlign(text, {align: options.align}); - - let lines = text.split(NL); - let contentWidth = widestLine(text) + padding.left + padding.right; - const BORDERS_WIDTH = 2; - if (contentWidth + BORDERS_WIDTH > columns) { - contentWidth = columns - BORDERS_WIDTH; - const max = contentWidth - padding.left - padding.right; - const newLines = []; - for (const line of lines) { - const createdLines = wrapAnsi(line, max, {hard: true}); - const alignedLines = ansiAlign(createdLines, {align: options.align}); - const alignedLinesArray = alignedLines.split('\n'); - const longestLength = Math.max(...alignedLinesArray.map(s => stringWidth(s))); - - for (const alignedLine of alignedLinesArray) { - let paddedLine; - switch (options.align) { - case 'center': - paddedLine = PAD.repeat((max - longestLength) / 2) + alignedLine; - break; - case 'right': - paddedLine = PAD.repeat(max - longestLength) + alignedLine; - break; - default: - paddedLine = alignedLine; - break; - } + if ((margin.left && margin.right) && contentWidth + BORDERS_WIDTH + margin.left + margin.right > columns) { + // Let's assume we have margins: left = 3, right = 5, in total = 8 + const spaceForMargins = columns - contentWidth - BORDERS_WIDTH; + // Let's assume we have space = 4 + const multiplier = spaceForMargins / (margin.left + margin.right); + // Here: multiplier = 4/8 = 0.5 + margin.left = Math.floor(margin.left * multiplier); + margin.right = Math.floor(margin.right * multiplier); + // Left: 3 * 0.5 = 1.5 -> 1 + // Right: 6 * 0.5 = 3 + } - newLines.push(paddedLine); - } - } + // Prevent content from exceeding the console's width + contentWidth = Math.min(contentWidth, columns - BORDERS_WIDTH - margin.left - margin.right); - lines = newLines; - } + text = makeContentText(text, padding, contentWidth, options.align); if (contentWidth + BORDERS_WIDTH + margin.left + margin.right > columns) { // Let's assume we have margins: left = 3, right = 5, in total = 8 @@ -159,23 +215,14 @@ module.exports = (text, options) => { // Right: 6 * 0.5 = 3 } - if (padding.top > 0) { - lines = new Array(padding.top).fill('').concat(lines); - } - - if (padding.bottom > 0) { - lines = lines.concat(new Array(padding.bottom).fill('')); - } - - const paddingLeft = PAD.repeat(padding.left); let marginLeft = PAD.repeat(margin.left); if (options.float === 'center') { - const padWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0); - marginLeft = PAD.repeat(padWidth); + const marWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0); + marginLeft = PAD.repeat(marWidth); } else if (options.float === 'right') { - const padWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0); - marginLeft = PAD.repeat(padWidth); + const marWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0); + marginLeft = PAD.repeat(marWidth); } const horizontal = chars.horizontal.repeat(contentWidth); @@ -185,9 +232,10 @@ module.exports = (text, options) => { const LINE_SEPARATOR = (contentWidth + BORDERS_WIDTH + margin.left >= columns) ? '' : NL; + const lines = text.split(NL); + const middle = lines.map(line => { - const paddingRight = PAD.repeat(contentWidth - stringWidth(line) - padding.left); - return marginLeft + side + colorizeContent(paddingLeft + line + paddingRight) + side; + return marginLeft + side + colorizeContent(line) + side; }).join(LINE_SEPARATOR); return top + LINE_SEPARATOR + middle + LINE_SEPARATOR + bottom; From a19c2bdc514818935c6ed051bfc2309fb2a55806 Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 30 Aug 2021 02:19:02 +0200 Subject: [PATCH 2/9] Fix margin error shrink on specific cases --- index.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/index.js b/index.js index 87c4466..d0ae0f3 100644 --- a/index.js +++ b/index.js @@ -184,7 +184,7 @@ module.exports = (text, options) => { const columns = terminalColumns(); - let contentWidth = widestLine(text) + padding.left + padding.right; + let contentWidth = widestLine(wrapAnsi(text, columns - BORDERS_WIDTH, {hard: true})) + padding.left + padding.right; if ((margin.left && margin.right) && contentWidth + BORDERS_WIDTH + margin.left + margin.right > columns) { // Let's assume we have margins: left = 3, right = 5, in total = 8 @@ -203,18 +203,6 @@ module.exports = (text, options) => { text = makeContentText(text, padding, contentWidth, options.align); - if (contentWidth + BORDERS_WIDTH + margin.left + margin.right > columns) { - // Let's assume we have margins: left = 3, right = 5, in total = 8 - const spaceForMargins = columns - contentWidth - BORDERS_WIDTH; - // Let's assume we have space = 4 - const multiplier = spaceForMargins / (margin.left + margin.right); - // Here: multiplier = 4/8 = 0.5 - margin.left = Math.floor(margin.left * multiplier); - margin.right = Math.floor(margin.right * multiplier); - // Left: 3 * 0.5 = 1.5 -> 1 - // Right: 6 * 0.5 = 3 - } - let marginLeft = PAD.repeat(margin.left); if (options.float === 'center') { From 6da1b52f8d40c1d8608dca02eb72c260f2d68ee3 Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 30 Aug 2021 02:20:38 +0200 Subject: [PATCH 3/9] Remove outdated tests that aren't relevant anymore --- test.js | 131 -------------------------------------------------------- 1 file changed, 131 deletions(-) diff --git a/test.js b/test.js index 1967983..9d74be9 100644 --- a/test.js +++ b/test.js @@ -524,134 +524,3 @@ test('text is right-aligned after wrapping', t => { const expected = '│' + ' '.repeat(padding) + 'x│'; t.is(lines[2], expected); }); - -// TODO: Find out why it fails on GitHub Actions. -if (!process.env.CI) { - test('text is centered after wrapping when using words', t => { - const width = process.stdout.columns || 120; - const sentence = 'x'.repeat(width / 3) + ' '; - const longContent = sentence.repeat(3).trim(); - const box = boxen(longContent, {align: 'center'}); - - t.is(box.length, width * 4); - - const lines = []; - for (let index = 0; index < 4; ++index) { - const line = box.slice(index * width, (index + 1) * width); - t.is(line.length, width, 'Length of line #' + index); - t.is(line, line.trim(), 'No margin of line #' + index); - - lines.push(line); - } - - const checkAlign = index => { - const line = lines[index]; - const lineWithoutBorders = line.slice(1, -1); - const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; - const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; - - t.true(paddingLeft > 0, 'Padding left in line #' + index); - t.true(paddingRight > 0, 'Padding right in line #' + index); - t.true(Math.abs(paddingLeft - paddingRight) <= 1, 'Left and right padding are not (almost) equal in line #' + index); - }; - - checkAlign(1); - checkAlign(2); - }); -} - -test('text is left-aligned after wrapping when using words', t => { - const width = process.stdout.columns || 120; - const sentence = 'x'.repeat(width / 3) + ' '; - const longContent = sentence.repeat(3).trim(); - const box = boxen(longContent, {align: 'left'}); - - t.is(box.length, width * 4); - - const lines = []; - for (let index = 0; index < 4; ++index) { - const line = box.slice(index * width, (index + 1) * width); - t.is(line.length, width, 'Length of line #' + index); - t.is(line, line.trim(), 'No margin of line #' + index); - - lines.push(line); - } - - const checkAlign = index => { - const line = lines[index]; - const lineWithoutBorders = line.slice(1, -1); - const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; - const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; - - t.is(paddingLeft, 0, 'Padding left in line #' + index); - t.true(paddingRight > 0, 'Padding right in line #' + index); - }; - - checkAlign(1); - checkAlign(2); -}); - -test('text is right-aligned after wrapping when using words', t => { - const width = process.stdout.columns || 120; - const sentence = 'x'.repeat(width / 3) + ' '; - const longContent = sentence.repeat(3).trim(); - const box = boxen(longContent, {align: 'right'}); - - t.is(box.length, width * 4); - - const lines = []; - for (let index = 0; index < 4; ++index) { - const line = box.slice(index * width, (index + 1) * width); - t.is(line.length, width, 'Length of line #' + index); - t.is(line, line.trim(), 'No margin of line #' + index); - - lines.push(line); - } - - const checkAlign = index => { - const line = lines[index]; - const lineWithoutBorders = line.slice(1, -1); - const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; - const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; - - t.true(paddingLeft > 0, 'Padding left in line #' + index); - t.is(paddingRight, 0, 'Padding right in line #' + index); - }; - - checkAlign(1); - checkAlign(2); -}); - -test('text is right-aligned after wrapping when using words, with padding', t => { - const width = process.stdout.columns || 120; - const sentence = 'x'.repeat(width / 3) + ' '; - const longContent = sentence.repeat(3).trim(); - const box = boxen(longContent, { - align: 'right', - padding: {left: 1, right: 1, top: 0, bottom: 0} - }); - - t.is(box.length, width * 4); - - const lines = []; - for (let index = 0; index < 4; ++index) { - const line = box.slice(index * width, (index + 1) * width); - t.is(line.length, width, 'Length of line #' + index); - t.is(line, line.trim(), 'No margin of line #' + index); - - lines.push(line); - } - - const checkAlign = index => { - const line = lines[index]; - const lineWithoutBorders = line.slice(1, -1); - const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; - const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; - - t.true(paddingLeft > 0, 'Padding left in line #' + index); - t.is(paddingRight, 1, 'Padding right in line #' + index); - }; - - checkAlign(1); - checkAlign(2); -}); From 39693c06f97821001dcf47ffc6c6c6bda4e0e2af Mon Sep 17 00:00:00 2001 From: Caesarovich <38408878+Caesarovich@users.noreply.github.com> Date: Mon, 6 Sep 2021 02:10:20 +0200 Subject: [PATCH 4/9] Code reduction Reduce two lines into one Co-authored-by: Sindre Sorhus --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d0ae0f3..7aa93e4 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,7 @@ module.exports = (text, options) => { let marginLeft = PAD.repeat(margin.left); if (options.float === 'center') { - const marWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0); + const marginWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0); marginLeft = PAD.repeat(marWidth); } else if (options.float === 'right') { const marWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0); From cf55cc30776ba2a29a6455536574c1f130a9104d Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 6 Sep 2021 02:22:02 +0200 Subject: [PATCH 5/9] Reforming corretly last commit --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7aa93e4..a7d7c0d 100644 --- a/index.js +++ b/index.js @@ -207,10 +207,10 @@ module.exports = (text, options) => { if (options.float === 'center') { const marginWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0); - marginLeft = PAD.repeat(marWidth); + marginLeft = PAD.repeat(marginWidth); } else if (options.float === 'right') { - const marWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0); - marginLeft = PAD.repeat(marWidth); + const marginWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0); + marginLeft = PAD.repeat(marginWidth); } const horizontal = chars.horizontal.repeat(contentWidth); From b93b3cc3aca435d28287341fe721fa52404c1a7a Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 6 Sep 2021 03:09:31 +0200 Subject: [PATCH 6/9] Fix margin shrink error on negative values --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a7d7c0d..f62f871 100644 --- a/index.js +++ b/index.js @@ -192,8 +192,8 @@ module.exports = (text, options) => { // Let's assume we have space = 4 const multiplier = spaceForMargins / (margin.left + margin.right); // Here: multiplier = 4/8 = 0.5 - margin.left = Math.floor(margin.left * multiplier); - margin.right = Math.floor(margin.right * multiplier); + margin.left = Math.max(0, Math.floor(margin.left * multiplier)); + margin.right = Math.max(0, Math.floor(margin.right * multiplier)); // Left: 3 * 0.5 = 1.5 -> 1 // Right: 6 * 0.5 = 3 } From e750e40c1d2d7656aeac78ee9467ea18bffd34d5 Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 6 Sep 2021 03:19:55 +0200 Subject: [PATCH 7/9] Added tests for box overflows --- test.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test.js b/test.js index 9d74be9..1e8c2a3 100644 --- a/test.js +++ b/test.js @@ -524,3 +524,72 @@ test('text is right-aligned after wrapping', t => { const expected = '│' + ' '.repeat(padding) + 'x│'; t.is(lines[2], expected); }); + +test('box not overflowing terminal', t => { + const width = process.stdout.columns || 120; + const longContent = 'x'.repeat(width * 4); + const box = boxen(longContent); + + const lines = []; + for (let index = 1; index < 6; ++index) { + const line = box.slice(index * width, (index + 1) * width); + lines.push(line); + } + + lines.forEach(line => { + t.is(line[0], '│', 'First character of line isn\'t box border'); + t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); + }); +}); + +test('box not overflowing terminal with padding', t => { + const width = process.stdout.columns || 120; + const longContent = 'x'.repeat(width * 4); + const box = boxen(longContent, {padding: 2}); + + const lines = []; + for (let index = 1; index < 10; ++index) { + const line = box.slice(index * width, (index + 1) * width); + lines.push(line); + } + + lines.forEach(line => { + t.is(line[0], '│', 'First character of line isn\'t box border'); + t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); + }); +}); + +test('box not overflowing terminal with padding and margin', t => { + const width = process.stdout.columns || 120; + const longContent = 'x'.repeat(width * 4); + const box = boxen(longContent, {padding: 2, margin: {left: 2, right: 2}}); + + const lines = []; + for (let index = 1; index < 10; ++index) { + const line = box.slice(index * width, (index + 1) * width); + lines.push(line); + } + + lines.forEach(line => { + t.is(line[0], '│', 'First character of line isn\'t box border'); + t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); + }); +}); + +test('box not overflowing terminal with words and margin', t => { + const width = process.stdout.columns || 120; + const word = 'x'.repeat(width / 2) + ' '; + const longContent = word.repeat(5); + const box = boxen(longContent, {margin: {left: 2, right: 2}}); + + const lines = []; + for (let index = 1; index < 6; ++index) { + const line = box.slice(index * (word.length + 4), (index + 1) * (word.length + 4)); + lines.push(line); + } + + lines.forEach(line => { + t.is(line.trim()[0], '│', 'First character of line isn\'t box border'); + t.is(line.trim()[word.length], '│', 'Last character of line isn\'t box border'); + }); +}); From c3c1335513ffc01102bfef4a0347e8d90e181ea8 Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 6 Sep 2021 14:51:46 +0200 Subject: [PATCH 8/9] Re-adapted unit tests --- test.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/test.js b/test.js index 1e8c2a3..c826851 100644 --- a/test.js +++ b/test.js @@ -593,3 +593,94 @@ test('box not overflowing terminal with words and margin', t => { t.is(line.trim()[word.length], '│', 'Last character of line isn\'t box border'); }); }); + +test('text is centered after wrapping when using words', t => { + const width = process.stdout.columns || 120; + const sentence = 'x'.repeat(width / 4) + ' '; + const longContent = sentence.repeat(4).trim(); + const box = boxen(longContent, {align: 'center'}); + + const lines = box.split('\n'); + + const checkAlign = (index, leftPad, rightPad) => { + const line = lines[index]; + const lineWithoutBorders = line.slice(1, -1); + const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; + const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; + + t.is(paddingLeft, leftPad, 'Padding left in line #' + index); + t.is(paddingRight, rightPad, 'Padding right in line #' + index); + }; + + checkAlign(1, 0, 0); + checkAlign(2, sentence.length, sentence.length); +}); + +test('text is left-aligned after wrapping when using words', t => { + const width = process.stdout.columns || 120; + const sentence = 'x'.repeat(width / 4) + ' '; + const longContent = sentence.repeat(4).trim(); + const box = boxen(longContent); + + const lines = box.split('\n'); + + const checkAlign = (index, leftPad, rightPad) => { + const line = lines[index]; + const lineWithoutBorders = line.slice(1, -1); + const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; + const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; + + t.is(paddingLeft, leftPad, 'Padding left in line #' + index); + t.is(paddingRight, rightPad, 'Padding right in line #' + index); + }; + + checkAlign(1, 0, 0); + checkAlign(2, 0, sentence.length * 2); +}); + +test('text is right-aligned after wrapping when using words', t => { + const width = process.stdout.columns || 120; + const sentence = 'x'.repeat(width / 4) + ' '; + const longContent = sentence.repeat(4).trim(); + const box = boxen(longContent, {align: 'right'}); + + const lines = box.split('\n'); + + const checkAlign = (index, leftPad, rightPad) => { + const line = lines[index]; + const lineWithoutBorders = line.slice(1, -1); + const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; + const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; + + t.is(paddingLeft, leftPad, 'Padding left in line #' + index); + t.is(paddingRight, rightPad, 'Padding right in line #' + index); + }; + + checkAlign(1, 0, 0); + checkAlign(2, sentence.length * 2, 0); +}); + +test('text is right-aligned after wrapping when using words, with padding', t => { + const width = process.stdout.columns || 120; + const sentence = 'x'.repeat(width / 4) + ' '; + const longContent = sentence.repeat(4).trim(); + const box = boxen(longContent, { + align: 'right', + padding: {left: 1, right: 1, top: 0, bottom: 0} + }); + + const lines = box.split('\n'); + + const checkAlign = (index, leftPad, rightPad) => { + const line = lines[index]; + const lineWithoutBorders = line.slice(1, -1); + const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; + const paddingRight = lineWithoutBorders.length - lineWithoutBorders.trimEnd().length; + + t.is(paddingLeft, leftPad, 'Padding left in line #' + index); + t.is(paddingRight, rightPad, 'Padding right in line #' + index); + }; + + checkAlign(1, 1, 1); + checkAlign(2, (sentence.length * 2) + 1, 1); +}); From 51ef182421f3aea1486aa88ee7bbcaaee5285677 Mon Sep 17 00:00:00 2001 From: Caesarovich Date: Mon, 6 Sep 2021 16:13:22 +0200 Subject: [PATCH 9/9] Improved code readability --- test.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test.js b/test.js index c826851..d786357 100644 --- a/test.js +++ b/test.js @@ -536,10 +536,10 @@ test('box not overflowing terminal', t => { lines.push(line); } - lines.forEach(line => { + for (const line of lines) { t.is(line[0], '│', 'First character of line isn\'t box border'); t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); - }); + } }); test('box not overflowing terminal with padding', t => { @@ -553,10 +553,10 @@ test('box not overflowing terminal with padding', t => { lines.push(line); } - lines.forEach(line => { + for (const line of lines) { t.is(line[0], '│', 'First character of line isn\'t box border'); t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); - }); + } }); test('box not overflowing terminal with padding and margin', t => { @@ -570,10 +570,10 @@ test('box not overflowing terminal with padding and margin', t => { lines.push(line); } - lines.forEach(line => { + for (const line of lines) { t.is(line[0], '│', 'First character of line isn\'t box border'); t.is(line[width - 1], '│', 'Last character of line isn\'t box border'); - }); + } }); test('box not overflowing terminal with words and margin', t => { @@ -588,10 +588,10 @@ test('box not overflowing terminal with words and margin', t => { lines.push(line); } - lines.forEach(line => { + for (const line of lines) { t.is(line.trim()[0], '│', 'First character of line isn\'t box border'); t.is(line.trim()[word.length], '│', 'Last character of line isn\'t box border'); - }); + } }); test('text is centered after wrapping when using words', t => { @@ -602,7 +602,7 @@ test('text is centered after wrapping when using words', t => { const lines = box.split('\n'); - const checkAlign = (index, leftPad, rightPad) => { + const checkAlign = ({index, leftPad, rightPad}) => { const line = lines[index]; const lineWithoutBorders = line.slice(1, -1); const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; @@ -612,8 +612,8 @@ test('text is centered after wrapping when using words', t => { t.is(paddingRight, rightPad, 'Padding right in line #' + index); }; - checkAlign(1, 0, 0); - checkAlign(2, sentence.length, sentence.length); + checkAlign({index: 1, leftPad: 0, rightPad: 0}); + checkAlign({index: 2, leftPad: sentence.length, rightPad: sentence.length}); }); test('text is left-aligned after wrapping when using words', t => { @@ -624,7 +624,7 @@ test('text is left-aligned after wrapping when using words', t => { const lines = box.split('\n'); - const checkAlign = (index, leftPad, rightPad) => { + const checkAlign = ({index, leftPad, rightPad}) => { const line = lines[index]; const lineWithoutBorders = line.slice(1, -1); const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; @@ -634,8 +634,8 @@ test('text is left-aligned after wrapping when using words', t => { t.is(paddingRight, rightPad, 'Padding right in line #' + index); }; - checkAlign(1, 0, 0); - checkAlign(2, 0, sentence.length * 2); + checkAlign({index: 1, leftPad: 0, rightPad: 0}); + checkAlign({index: 2, leftPad: 0, rightPad: sentence.length * 2}); }); test('text is right-aligned after wrapping when using words', t => { @@ -646,7 +646,7 @@ test('text is right-aligned after wrapping when using words', t => { const lines = box.split('\n'); - const checkAlign = (index, leftPad, rightPad) => { + const checkAlign = ({index, leftPad, rightPad}) => { const line = lines[index]; const lineWithoutBorders = line.slice(1, -1); const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; @@ -656,8 +656,8 @@ test('text is right-aligned after wrapping when using words', t => { t.is(paddingRight, rightPad, 'Padding right in line #' + index); }; - checkAlign(1, 0, 0); - checkAlign(2, sentence.length * 2, 0); + checkAlign({index: 1, leftPad: 0, rightPad: 0}); + checkAlign({index: 2, leftPad: sentence.length * 2, rightPad: 0}); }); test('text is right-aligned after wrapping when using words, with padding', t => { @@ -671,7 +671,7 @@ test('text is right-aligned after wrapping when using words, with padding', t => const lines = box.split('\n'); - const checkAlign = (index, leftPad, rightPad) => { + const checkAlign = ({index, leftPad, rightPad}) => { const line = lines[index]; const lineWithoutBorders = line.slice(1, -1); const paddingLeft = lineWithoutBorders.length - lineWithoutBorders.trimStart().length; @@ -681,6 +681,6 @@ test('text is right-aligned after wrapping when using words, with padding', t => t.is(paddingRight, rightPad, 'Padding right in line #' + index); }; - checkAlign(1, 1, 1); - checkAlign(2, (sentence.length * 2) + 1, 1); + checkAlign({index: 1, leftPad: 1, rightPad: 1}); + checkAlign({index: 2, leftPad: (sentence.length * 2) + 1, rightPad: 1}); });