Skip to content

Commit 607e755

Browse files
Merge pull request #2 from Kreozot/noSort_option
Added 'noSort' option to disable sorting.
2 parents c0180db + e25ac56 commit 607e755

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ console.log(columns(values));
3838
- `newline` `{String}` (default: `'\n'`) Newline character.
3939
- `padding` `{Number}` (default: `2`) Space between columns.
4040
- `width` `{Number}` (default: `process.stdout.columns`) Max width of list.
41+
- `noSort` `{Boolean}` (default: `false`) No use sorting.
4142

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

src/cli-columns.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var defaults = {
77
character: ' ',
88
newline: '\n',
99
padding: 2,
10-
width: 0
10+
width: 0,
11+
noSort: false
1112
};
1213

1314
function byPlainText(a, b) {
@@ -45,8 +46,10 @@ function columns(values, options) {
4546

4647
var cells = values
4748
.filter(Boolean)
48-
.map(String)
49-
.sort(byPlainText);
49+
.map(String);
50+
if (!options.noSort) {
51+
cells = cells.sort(byPlainText);
52+
}
5053

5154
var termWidth = options.width || process.stdout.columns;
5255
var cellWidth = Math.max.apply(null, cells.map(stringWidth)) + options.padding;

test/cli-columns.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@ test('should print complex list', async assert => {
5555

5656
assert.is(stripAnsi(cols), expected);
5757
});
58+
59+
test('should no sort with noSort option', async assert => {
60+
var cols = columns(
61+
[
62+
'foo', 'bar', 'baz',
63+
chalk.cyan('嶜憃撊') + ' 噾噿嚁',
64+
'blue' + chalk.bgBlue('berry'),
65+
chalk.red('apple'), 'pomegranate',
66+
'durian', chalk.green('star fruit'),
67+
'apricot', 'banana pineapple'
68+
],
69+
{
70+
width: 80,
71+
noSort: true
72+
}
73+
);
74+
75+
// Visual test
76+
console.log(chalk.yellow(cols) + '\n');
77+
78+
var expected =
79+
'foo 嶜憃撊 噾噿嚁 pomegranate apricot \n' +
80+
'bar blueberry durian banana pineapple \n' +
81+
'baz apple star fruit ';
82+
83+
assert.is(stripAnsi(cols), expected);
84+
});

0 commit comments

Comments
 (0)