Skip to content

Commit

Permalink
Merge pull request #2 from Kreozot/noSort_option
Browse files Browse the repository at this point in the history
Added 'noSort' option to disable sorting.
  • Loading branch information
shannonmoeller committed Feb 7, 2017
2 parents c0180db + e25ac56 commit 607e755
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ console.log(columns(values));
- `newline` `{String}` (default: `'\n'`) Newline character.
- `padding` `{Number}` (default: `2`) Space between columns.
- `width` `{Number}` (default: `process.stdout.columns`) Max width of list.
- `noSort` `{Boolean}` (default: `false`) No use sorting.

Sorts and formats a list of values into columns suitable to display in a given width.

Expand Down
9 changes: 6 additions & 3 deletions src/cli-columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var defaults = {
character: ' ',
newline: '\n',
padding: 2,
width: 0
width: 0,
noSort: false
};

function byPlainText(a, b) {
Expand Down Expand Up @@ -45,8 +46,10 @@ function columns(values, options) {

var cells = values
.filter(Boolean)
.map(String)
.sort(byPlainText);
.map(String);
if (!options.noSort) {
cells = cells.sort(byPlainText);
}

var termWidth = options.width || process.stdout.columns;
var cellWidth = Math.max.apply(null, cells.map(stringWidth)) + options.padding;
Expand Down
27 changes: 27 additions & 0 deletions test/cli-columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@ test('should print complex list', async assert => {

assert.is(stripAnsi(cols), expected);
});

test('should no sort with noSort option', async assert => {
var cols = columns(
[
'foo', 'bar', 'baz',
chalk.cyan('嶜憃撊') + ' 噾噿嚁',
'blue' + chalk.bgBlue('berry'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'apricot', 'banana pineapple'
],
{
width: 80,
noSort: true
}
);

// Visual test
console.log(chalk.yellow(cols) + '\n');

var expected =
'foo 嶜憃撊 噾噿嚁 pomegranate apricot \n' +
'bar blueberry durian banana pineapple \n' +
'baz apple star fruit ';

assert.is(stripAnsi(cols), expected);
});

0 comments on commit 607e755

Please sign in to comment.