Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export default function cliTruncate(text, columns, options = {}) {
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}

// A `NaN` budget (e.g. `process.stdout.columns - n` when stdout is not a TTY)
// means the target width is unknown — return the text untruncated rather than
// appending a truncation character to the full string.
if (Number.isNaN(columns)) {
return text;
}

if (columns < 1) {
return '';
}
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ test('main', t => {
t.is(cliTruncate('u', 1), 'u');
});

test('NaN columns', t => {
t.is(cliTruncate('unicorns', Number.NaN), 'unicorns');
t.is(cliTruncate('\u001B[31municorns\u001B[39m', Number.NaN), '\u001B[31municorns\u001B[39m');
});

test('space option', t => {
t.is(cliTruncate('unicorns', 5, {position: 'end', space: true}), 'uni …');
t.is(cliTruncate('unicorns', 6, {position: 'start', space: true}), '… orns');
Expand Down