diff --git a/index.js b/index.js index 8fb2728..b136421 100644 --- a/index.js +++ b/index.js @@ -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 ''; } diff --git a/test.js b/test.js index 90bf745..1ec0ede 100644 --- a/test.js +++ b/test.js @@ -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');