From 7e0c356a0eaf1793d284d24ff9905f0c72879d16 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Mon, 22 Apr 2019 09:14:10 +0530 Subject: [PATCH] Add `space` option (#10) Fixes #7 --- index.d.ts | 22 ++++++++++++++++++++++ index.js | 30 +++++++++++++++++++++++++----- readme.md | 21 +++++++++++++++++++++ test.js | 14 ++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index edef609..44dcb5b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,28 @@ declare namespace cliTruncate { @default 'end' */ readonly position?: 'start' | 'middle' | 'end'; + + /** + Add a space between the text and the ellipsis. + + @default false + + @example + ``` + cliTruncate('unicorns', 5, {position: 'end', space: true}); + //=> 'uni …' + + cliTruncate('unicorns', 5, {position: 'end', space: false}); + //=> 'unic…' + + cliTruncate('unicorns', 6, {position: 'start', space: true}); + //=> '… orns' + + cliTruncate('unicorns', 7, {position: 'middle', space: true}); + //=> 'uni … s' + ``` + */ + readonly space?: boolean; } } diff --git a/index.js b/index.js index 0caa947..3d9348e 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,9 @@ module.exports = (text, columns, options) => { ...options }; - const {position} = options; - const ellipsis = '…'; + const {position, space} = options; + let ellipsis = '…'; + let ellipsisWidth = 1; if (typeof text !== 'string') { throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`); @@ -34,16 +35,35 @@ module.exports = (text, columns, options) => { } if (position === 'start') { - return ellipsis + sliceAnsi(text, length - columns + 1, length); + if (space === true) { + ellipsis += ' '; + ellipsisWidth = 2; + } + + return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length); } if (position === 'middle') { + if (space === true) { + ellipsis = ' ' + ellipsis + ' '; + ellipsisWidth = 3; + } + const half = Math.floor(columns / 2); - return sliceAnsi(text, 0, half) + ellipsis + sliceAnsi(text, length - (columns - half) + 1, length); + return ( + sliceAnsi(text, 0, half) + + ellipsis + + sliceAnsi(text, length - (columns - half) + ellipsisWidth, length) + ); } if (position === 'end') { - return sliceAnsi(text, 0, columns - 1) + ellipsis; + if (space === true) { + ellipsis = ' ' + ellipsis; + ellipsisWidth = 2; + } + + return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis; } throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`); diff --git a/readme.md b/readme.md index 67cda28..ee7c049 100644 --- a/readme.md +++ b/readme.md @@ -73,6 +73,27 @@ Values: `start` `middle` `end` Position to truncate the string. +##### space + +Type: `boolean`
+Default: `false` + +Add a space between the text and the ellipsis. + +```js +cliTruncate('unicorns', 5, {space: false}); +//=> 'unic…' + +cliTruncate('unicorns', 5, {space: true}); +//=> 'uni …' + +cliTruncate('unicorns', 6, {position: 'start', space: true}); +//=> '… orns' + +cliTruncate('unicorns', 7, {position: 'middle', space: true}); +//=> 'uni … s' +``` + ## Related diff --git a/test.js b/test.js index 84d8622..249fac7 100644 --- a/test.js +++ b/test.js @@ -20,3 +20,17 @@ test('main', t => { t.is(cliTruncate('unicorn', 5, {position: 'middle'}), 'un…rn'); t.is(cliTruncate('unicorns', 6, {position: 'middle'}), 'uni…ns'); }); + +test('space option', t => { + t.is(cliTruncate('unicorns', 5, {position: 'end', space: true}), 'uni …'); + t.is(cliTruncate('unicorns', 6, {position: 'start', space: true}), '… orns'); + t.is(cliTruncate('unicorns', 7, {position: 'middle', space: true}), 'uni … s'); + t.is(cliTruncate('unicorns', 5, {position: 'end', space: false}), 'unic…'); + t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {space: true}), '\u001B[31munic\u001B[39m …'); + t.is(cliTruncate('Plant a tree every day.', 14, {space: true}), 'Plant a tree …'); + t.is(cliTruncate('안녕하세요', 4, {space: true}), '안 …', 'wide char'); + t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {position: 'start', space: true}), '… \u001B[31mcorn\u001B[39m'); + t.is(cliTruncate('\u001B[31municornsareawesome\u001B[39m', 10, {position: 'middle', space: true}), '\u001B[31munico\u001B[39m … \u001B[31mme\u001B[39m'); + t.is(cliTruncate('Plant a tree every day.', 14, {position: 'middle', space: true}), 'Plant a … day.'); + t.is(cliTruncate('안녕하세요', 4, {position: 'start', space: true}), '… 요', 'wide char'); +});