Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
lib: refactor cli table
The cli table used multi line template strings which are normally
not used in our code base and it also upper cased a regular function
name. This is changed by this patch.
  • Loading branch information
BridgeAR committed May 25, 2018
commit bc0aab2ecab8565cd50b8a815974ff00f46d1f1d
23 changes: 10 additions & 13 deletions lib/internal/cli_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Buffer } = require('buffer');
const { removeColors } = require('internal/util');
const HasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
const hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know @bmeck has also been doing it this way, (at least guessing from the reasons that I use it) that it matches the name the spec uses?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR are you attached to this particular change? It's the only thing stopping me from approving & landing to be honest and I've looked at this PR several times. I believe we should have a convention and follow it — at the moment uppercase is the way we do this in most files. I'm not opposed to switching but I don't think it should be done incrementally.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched it back even though this is really weird for me as it is not a class. IMO we should only have upper case characters for classes.


const tableChars = {
/* eslint-disable node-core/non-ascii-character */
Expand Down Expand Up @@ -51,31 +51,28 @@ const table = (head, columns) => {
for (var i = 0; i < head.length; i++) {
const column = columns[i];
for (var j = 0; j < longestColumn; j++) {
if (!rows[j])
if (rows[j] === undefined)
rows[j] = [];
const v = rows[j][i] = HasOwnProperty(column, j) ? column[j] : '';
const value = rows[j][i] = hasOwnProperty(column, j) ? column[j] : '';
const width = columnWidths[i] || 0;
const counted = countSymbols(v);
const counted = countSymbols(value);
columnWidths[i] = Math.max(width, counted);
}
}

const divider = columnWidths.map((i) =>
tableChars.middleMiddle.repeat(i + 2));

const tl = tableChars.topLeft;
const tr = tableChars.topRight;
const lm = tableChars.leftMiddle;
let result = `${tl}${divider.join(tableChars.topMiddle)}${tr}
${renderRow(head, columnWidths)}
${lm}${divider.join(tableChars.rowMiddle)}${tableChars.rightMiddle}
`;
let result = `${tableChars.topLeft}${divider.join(tableChars.topMiddle)}` +
`${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` +
`${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}` +
`${tableChars.rightMiddle}\n`;

for (const row of rows)
result += `${renderRow(row, columnWidths)}\n`;

result += `${tableChars.bottomLeft}${
divider.join(tableChars.bottomMiddle)}${tableChars.bottomRight}`;
result += `${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}` +
tableChars.bottomRight;

return result;
};
Expand Down