Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/cli-truncate
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b76c964800573b5131e4965b3c0f2eaff22a3650
Choose a base ref
...
head repository: sindresorhus/cli-truncate
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4ded8c0075e0bedd4dba69db9017d0ec7c6798a3
Choose a head ref
  • 14 commits
  • 13 files changed
  • 6 contributors

Commits on Jan 13, 2017

  1. Verified

    This commit was signed with the committer’s verified signature.
    hyoban Stephen Zhou
    Copy the full SHA
    bd8025b View commit details
  2. 1.0.0

    sindresorhus committed Jan 13, 2017
    Copy the full SHA
    4da60e7 View commit details

Commits on Jul 23, 2017

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    f379a0e View commit details
  2. Meta tweaks

    sindresorhus committed Jul 23, 2017
    Copy the full SHA
    a437535 View commit details
  3. 1.1.0

    sindresorhus committed Jul 23, 2017
    Copy the full SHA
    94f2697 View commit details

Commits on Apr 2, 2019

  1. Require Node.js 8

    Closes #12
    sindresorhus committed Apr 2, 2019
    Copy the full SHA
    27ed208 View commit details

Commits on Apr 11, 2019

  1. Copy the full SHA
    1c65dfa View commit details
  2. Meta tweaks

    sindresorhus committed Apr 11, 2019
    Copy the full SHA
    21df450 View commit details

Commits on Apr 22, 2019

  1. Add space option (#10)

    Fixes #7
    Satya Rohith authored and sindresorhus committed Apr 22, 2019
    Copy the full SHA
    7e0c356 View commit details

Commits on Apr 27, 2019

  1. Add Node.js 12 to testing (#14)

    coreyfarrell authored and sindresorhus committed Apr 27, 2019
    Copy the full SHA
    5744234 View commit details

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019
    Copy the full SHA
    21f9d9b View commit details

Commits on Jun 13, 2019

  1. Add preferTruncationOnSpace option (#11)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    ammarbinfaisal and sindresorhus committed Jun 13, 2019
    Copy the full SHA
    baf4461 View commit details
  2. Meta tweaks

    sindresorhus committed Jun 13, 2019
    Copy the full SHA
    d474918 View commit details
  3. 2.0.0

    sindresorhus committed Jun 13, 2019
    Copy the full SHA
    4ded8c0 View commit details
Showing with 352 additions and 116 deletions.
  1. +1 −1 .editorconfig
  2. +1 −1 .gitattributes
  3. +3 −0 .github/funding.yml
  4. +1 −0 .gitignore
  5. +1 −0 .npmrc
  6. +3 −4 .travis.yml
  7. +96 −0 index.d.ts
  8. +81 −18 index.js
  9. +8 −0 index.test-d.ts
  10. +4 −16 license
  11. +43 −40 package.json
  12. +69 −14 readme.md
  13. +41 −22 test.js
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
3 changes: 3 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
- '12'
- '10'
- '8'
96 changes: 96 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
declare namespace cliTruncate {
interface Options {
/**
Position to truncate the string.
@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;

/**
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
@default false
@example
```
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
//=> '…rainbow dragons'
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
//=> 'unicorns…dragons'
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
//=> 'unico…'
````
*/
readonly preferTruncationOnSpace?: boolean;
}
}

/**
Truncate a string to a specific width in the terminal.
@param text - Text to truncate.
@param columns - Columns to occupy in the terminal.
@example
```
import cliTruncate = require('cli-truncate');
cliTruncate('unicorn', 4);
//=> 'uni…'
// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> '…orn'
cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'
cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni\u001B[39m…'
// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00…'
// Truncate fullwidth characters
cliTruncate('안녕하세요', 3);
//=> '안…'
// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns));
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
```
*/
declare function cliTruncate(
text: string,
columns: number,
options?: cliTruncate.Options
): string;

export = cliTruncate;
99 changes: 81 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
'use strict';
var sliceAnsi = require('slice-ansi');
var stringWidth = require('string-width');
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');

module.exports = function (input, columns, options) {
options = options || {};
function getIndexOfNearestSpace(string, index, shouldSearchRight) {
if (string.charAt(index) === ' ') {
return index;
}

for (let i = 1; i <= 3; i++) {
if (shouldSearchRight) {
if (string.charAt(index + i) === ' ') {
return index + i;
}
} else if (string.charAt(index - i) === ' ') {
return index - i;
}
}

return index;
}

var position = options.position || 'end';
var ellipsis = '…';
module.exports = (text, columns, options) => {
options = {
position: 'end',
preferTruncationOnSpace: false,
...options
};

if (typeof input !== 'string') {
throw new TypeError('Expected `input` to be a string, got ' + typeof input);
const {position, space, preferTruncationOnSpace} = options;
let ellipsis = '…';
let ellipsisWidth = 1;

if (typeof text !== 'string') {
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
}

if (typeof columns !== 'number') {
throw new TypeError('Expected `columns` to be a number, got ' + typeof columns);
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}

if (columns < 1) {
@@ -24,20 +47,60 @@ module.exports = function (input, columns, options) {
return ellipsis;
}

var length = stringWidth(input);
const length = stringWidth(text);

if (length <= columns) {
return input;
return text;
}

if (position === 'start') {
return ellipsis + sliceAnsi(input, length - columns + 1, length);
} else if (position === 'middle') {
var half = Math.floor(columns / 2);
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
} else if (position === 'end') {
return sliceAnsi(input, 0, columns - 1) + ellipsis;
if (preferTruncationOnSpace) {
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
return ellipsis + sliceAnsi(text, nearestSpace, length).trim();
}

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);

if (preferTruncationOnSpace) {
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + ellipsis + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
}

return (
sliceAnsi(text, 0, half) +
ellipsis +
sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
);
}

if (position === 'end') {
if (preferTruncationOnSpace) {
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
return sliceAnsi(text, 0, nearestSpace) + 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);
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
};
8 changes: 8 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expectType} from 'tsd';
import cliTruncate = require('.');

expectType<string>(cliTruncate('unicorn', 4));
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'}));
expectType<string>(cliTruncate('unicorn', 4, {position: 'middle'}));
expectType<string>(cliTruncate('unicorn', 4, {position: 'end'}));
expectType<string>(cliTruncate('unicorn', 4, {position: 'end', preferTruncationOnSpace: true}));
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83 changes: 43 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
{
"name": "cli-truncate",
"version": "0.2.1",
"description": "Truncate a string to a specific width in the terminal",
"license": "MIT",
"repository": "sindresorhus/cli-truncate",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"truncate",
"ellipsis",
"text",
"limit",
"slice",
"cli",
"terminal",
"term",
"shell",
"width",
"ansi"
],
"dependencies": {
"slice-ansi": "0.0.4",
"string-width": "^1.0.1"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "cli-truncate",
"version": "2.0.0",
"description": "Truncate a string to a specific width in the terminal",
"license": "MIT",
"repository": "sindresorhus/cli-truncate",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"truncate",
"ellipsis",
"text",
"limit",
"slice",
"cli",
"terminal",
"term",
"shell",
"width",
"ansi",
"string"
],
"dependencies": {
"slice-ansi": "^2.1.0",
"string-width": "^4.1.0"
},
"devDependencies": {
"ava": "^2.1.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
83 changes: 69 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@

> Truncate a string to a specific width in the terminal
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk).
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters.


## Install

```
$ npm install --save cli-truncate
$ npm install cli-truncate
```


@@ -20,17 +20,28 @@ const cliTruncate = require('cli-truncate');
cliTruncate('unicorn', 4);
//=> 'uni…'

// truncate at different positions
// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> '…orn'

cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'

cliTruncate('\u001b[31municorn\u001b[39m', 4);
//=> '\u001b[31muni\u001b[39m…'
cliTruncate('unicorns rainbow dragons', 6, {position: 'end'})
//=> 'unico…'

// truncate the paragraph to the terminal width
cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni\u001B[39m…'

// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00…'

// Truncate fullwidth characters
cliTruncate('안녕하세요', 3);
//=> '안…'

// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns));
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
@@ -39,9 +50,9 @@ cliTruncate(paragraph, process.stdout.columns));

## API

### cliTruncate(input, columns, [options])
### cliTruncate(text, columns, options?)

#### input
#### text

Type: `string`

@@ -55,21 +66,65 @@ Columns to occupy in the terminal.

#### options

Type: `object`

##### position

Type: `string`<br>
Default: `'end'`<br>
Values: `'start'`, `'middle'`, `'end'`
Values: `'start'` `'middle'` `'end'`

Position to truncate the string.

##### space

## Related
Type: `boolean`<br>
Default: `false`

- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
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'
```

##### preferTruncationOnSpace

## License
Type: `boolean`<br>
Default: `false`

MIT © [Sindre Sorhus](https://sindresorhus.com)
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.

```js
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
//=> '…rainbow dragons'

// without preferTruncationOnSpace
cliTruncate('unicorns rainbow dragons', 20, {position: 'start'})
//=> '…rns rainbow dragons'

cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true})
//=> 'unicorns…dragons'

cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
//=> 'unico…'

// preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
//=> 'uni…ns'
```


## Related

- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
63 changes: 41 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import test from 'ava';
import m from './';
import cliTruncate from '.';

// because of https://github.com/chalk/slice-ansi/issues/6
const mootEscapes = '\u001b[31m\u001b[31m\u001b[31m\u001b[31m';
test('main', t => {
t.is(cliTruncate('unicorn', 4), 'uni…');
t.is(cliTruncate('unicorn', 4, {position: 'end'}), 'uni…');
t.is(cliTruncate('unicorn', 1), '…');
t.is(cliTruncate('unicorn', 0), '');
t.is(cliTruncate('unicorn', -4), '');
t.is(cliTruncate('unicorn', 20), 'unicorn');
t.is(cliTruncate('unicorn', 7), 'unicorn');
t.is(cliTruncate('unicorn', 6), 'unico…');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 1), '…');
t.is(cliTruncate('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…');
t.is(cliTruncate('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs');
t.is(cliTruncate('안녕하세요', 3), '안…', 'wide char');
t.is(cliTruncate('unicorn', 5, {position: 'start'}), '…corn');
t.is(cliTruncate('unicorn', 6, {position: 'start'}), '…icorn');
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');
});

test(t => {
t.is(m('unicorn', 4), 'uni…');
t.is(m('unicorn', 4, {position: 'end'}), 'uni…');
t.is(m('unicorn', 1), '…');
t.is(m('unicorn', 0), '');
t.is(m('unicorn', -4), '');
t.is(m('unicorn', 20), 'unicorn');
t.is(m('unicorn', 7), 'unicorn');
t.is(m('unicorn', 6), 'unico…');
t.is(m('\u001b[31municorn\u001b[39m', 7), '\u001b[31municorn\u001b[39m');
t.is(m('\u001b[31municorn\u001b[39m', 1), '…');
t.is(m('\u001b[31municorn\u001b[39m', 4), mootEscapes + '\u001b[31muni\u001b[39m…');
// TODO
t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
t.is(m('unicorn', 5, {position: 'start'}), '…corn');
t.is(m('unicorn', 6, {position: 'start'}), '…icorn');
t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn');
t.is(m('unicorns', 6, {position: 'middle'}), 'uni…ns');
test('preferTruncationOnSpace option', t => {
t.is(cliTruncate('unicorns are awesome', 15, {position: 'start', preferTruncationOnSpace: true}), '…are awesome');
t.is(cliTruncate('dragons are awesome', 15, {position: 'end', preferTruncationOnSpace: true}), 'dragons are…');
t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'start', preferTruncationOnSpace: true}), '…agons');
t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true}), 'unico…');
t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true}), 'uni…ns');
t.is(cliTruncate('unicorns partying with dragons', 20, {position: 'middle', preferTruncationOnSpace: true}), 'unicorns…dragons');
});