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
Prev Previous commit
Next Next commit
dynamically calculate amount of spaces based on command names
  • Loading branch information
silverwind committed Sep 13, 2016
commit db73c0dcf1709fec543a75203ea210840a7c1357
8 changes: 6 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,9 +1259,13 @@ function defineDefaultCommands(repl) {
repl.defineCommand('help', {
help: 'Print this help message',
action: function() {
Object.keys(this.commands).sort().forEach((name) => {
const names = Object.keys(this.commands).sort();
const longestNameLength = names.reduce((max, name) => {
return Math.max(max, name.length);

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.

@silverwind (max, name) => Math.max(max, name.length) ? is it against style guide?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not, but it won't fit on 80 chars so I went with the longer form.

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.

Why not named function? Feel free to ignore it

const maxFun = (max, name) => Math.max(max, name.length);
const longestNameLength = names.reduce(maxFun, 0);

or

const nameLengthList = names.map((n) => n.length);
const longestNameLength = nameLengthList.reduce(Math.max, 0);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TBH, I find that these reduce readability as compared to what I have now.

}, 1);
names.forEach((name) => {
const cmd = this.commands[name];
const spaces = ' '.repeat(Math.max(9 - name.length, 1));
const spaces = ' '.repeat(longestNameLength - name.length + 3);
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
this.outputStream.write(line);
});
Expand Down