Skip to content

Commit

Permalink
Add space option (#10)
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
Satya Rohith authored and sindresorhus committed Apr 22, 2019
1 parent 21df450 commit 7e0c356
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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}`);
Expand Down
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ Values: `start` `middle` `end`

Position to truncate the string.

##### space

Type: `boolean`<br>
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

Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 7e0c356

Please sign in to comment.