Skip to content

Commit 7e0c356

Browse files
Satya Rohithsindresorhus
Satya Rohith
authored andcommittedApr 22, 2019
Add space option (#10)
Fixes #7
1 parent 21df450 commit 7e0c356

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed
 

‎index.d.ts

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ declare namespace cliTruncate {
66
@default 'end'
77
*/
88
readonly position?: 'start' | 'middle' | 'end';
9+
10+
/**
11+
Add a space between the text and the ellipsis.
12+
13+
@default false
14+
15+
@example
16+
```
17+
cliTruncate('unicorns', 5, {position: 'end', space: true});
18+
//=> 'uni …'
19+
20+
cliTruncate('unicorns', 5, {position: 'end', space: false});
21+
//=> 'unic…'
22+
23+
cliTruncate('unicorns', 6, {position: 'start', space: true});
24+
//=> '… orns'
25+
26+
cliTruncate('unicorns', 7, {position: 'middle', space: true});
27+
//=> 'uni … s'
28+
```
29+
*/
30+
readonly space?: boolean;
931
}
1032
}
1133

‎index.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ module.exports = (text, columns, options) => {
88
...options
99
};
1010

11-
const {position} = options;
12-
const ellipsis = '…';
11+
const {position, space} = options;
12+
let ellipsis = '…';
13+
let ellipsisWidth = 1;
1314

1415
if (typeof text !== 'string') {
1516
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
@@ -34,16 +35,35 @@ module.exports = (text, columns, options) => {
3435
}
3536

3637
if (position === 'start') {
37-
return ellipsis + sliceAnsi(text, length - columns + 1, length);
38+
if (space === true) {
39+
ellipsis += ' ';
40+
ellipsisWidth = 2;
41+
}
42+
43+
return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length);
3844
}
3945

4046
if (position === 'middle') {
47+
if (space === true) {
48+
ellipsis = ' ' + ellipsis + ' ';
49+
ellipsisWidth = 3;
50+
}
51+
4152
const half = Math.floor(columns / 2);
42-
return sliceAnsi(text, 0, half) + ellipsis + sliceAnsi(text, length - (columns - half) + 1, length);
53+
return (
54+
sliceAnsi(text, 0, half) +
55+
ellipsis +
56+
sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
57+
);
4358
}
4459

4560
if (position === 'end') {
46-
return sliceAnsi(text, 0, columns - 1) + ellipsis;
61+
if (space === true) {
62+
ellipsis = ' ' + ellipsis;
63+
ellipsisWidth = 2;
64+
}
65+
66+
return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
4767
}
4868

4969
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);

‎readme.md

+21
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ Values: `start` `middle` `end`
7373

7474
Position to truncate the string.
7575

76+
##### space
77+
78+
Type: `boolean`<br>
79+
Default: `false`
80+
81+
Add a space between the text and the ellipsis.
82+
83+
```js
84+
cliTruncate('unicorns', 5, {space: false});
85+
//=> 'unic…'
86+
87+
cliTruncate('unicorns', 5, {space: true});
88+
//=> 'uni …'
89+
90+
cliTruncate('unicorns', 6, {position: 'start', space: true});
91+
//=> '… orns'
92+
93+
cliTruncate('unicorns', 7, {position: 'middle', space: true});
94+
//=> 'uni … s'
95+
```
96+
7697

7798
## Related
7899

‎test.js

+14
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ test('main', t => {
2020
t.is(cliTruncate('unicorn', 5, {position: 'middle'}), 'un…rn');
2121
t.is(cliTruncate('unicorns', 6, {position: 'middle'}), 'uni…ns');
2222
});
23+
24+
test('space option', t => {
25+
t.is(cliTruncate('unicorns', 5, {position: 'end', space: true}), 'uni …');
26+
t.is(cliTruncate('unicorns', 6, {position: 'start', space: true}), '… orns');
27+
t.is(cliTruncate('unicorns', 7, {position: 'middle', space: true}), 'uni … s');
28+
t.is(cliTruncate('unicorns', 5, {position: 'end', space: false}), 'unic…');
29+
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {space: true}), '\u001B[31munic\u001B[39m …');
30+
t.is(cliTruncate('Plant a tree every day.', 14, {space: true}), 'Plant a tree …');
31+
t.is(cliTruncate('안녕하세요', 4, {space: true}), '안 …', 'wide char');
32+
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {position: 'start', space: true}), '… \u001B[31mcorn\u001B[39m');
33+
t.is(cliTruncate('\u001B[31municornsareawesome\u001B[39m', 10, {position: 'middle', space: true}), '\u001B[31munico\u001B[39m … \u001B[31mme\u001B[39m');
34+
t.is(cliTruncate('Plant a tree every day.', 14, {position: 'middle', space: true}), 'Plant a … day.');
35+
t.is(cliTruncate('안녕하세요', 4, {position: 'start', space: true}), '… 요', 'wide char');
36+
});

0 commit comments

Comments
 (0)
Please sign in to comment.