Socket
Socket
Sign inDemoInstall

cli-truncate

Package Overview
Dependencies
8
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.0 to 3.1.0

20

index.d.ts

@@ -53,2 +53,22 @@ export interface Options {

readonly preferTruncationOnSpace?: boolean;
/**
The character to use at the breaking point.
@default '…'
@example
```
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns', 5, {position: 'end'});
//=> 'unic…'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
//=> 'unic.'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
//=> 'unico'
*/
readonly truncationCharacter?: string;
}

@@ -55,0 +75,0 @@

29

index.js

@@ -26,2 +26,3 @@ import sliceAnsi from 'slice-ansi';

preferTruncationOnSpace: false,
truncationCharacter: '…',
...options,

@@ -31,4 +32,3 @@ };

const {position, space, preferTruncationOnSpace} = options;
let ellipsis = '…';
let ellipsisWidth = 1;
let {truncationCharacter} = options;

@@ -48,3 +48,3 @@ if (typeof text !== 'string') {

if (columns === 1) {
return ellipsis;
return truncationCharacter;
}

@@ -61,11 +61,10 @@

const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
return ellipsis + sliceAnsi(text, nearestSpace, length).trim();
return truncationCharacter + sliceAnsi(text, nearestSpace, length).trim();
}
if (space === true) {
ellipsis += ' ';
ellipsisWidth = 2;
truncationCharacter += ' ';
}
return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length);
return truncationCharacter + sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
}

@@ -75,4 +74,3 @@

if (space === true) {
ellipsis = ` ${ellipsis} `;
ellipsisWidth = 3;
truncationCharacter = ` ${truncationCharacter} `;
}

@@ -85,3 +83,3 @@

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

@@ -91,4 +89,4 @@

sliceAnsi(text, 0, half)
+ ellipsis
+ sliceAnsi(text, length - (columns - half) + ellipsisWidth, length)
+ truncationCharacter
+ sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length)
);

@@ -100,11 +98,10 @@ }

const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
return sliceAnsi(text, 0, nearestSpace) + ellipsis;
return sliceAnsi(text, 0, nearestSpace) + truncationCharacter;
}
if (space === true) {
ellipsis = ` ${ellipsis}`;
ellipsisWidth = 2;
truncationCharacter = ` ${truncationCharacter}`;
}
return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis;
return sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)) + truncationCharacter;
}

@@ -111,0 +108,0 @@

{
"name": "cli-truncate",
"version": "3.0.0",
"version": "3.1.0",
"description": "Truncate a string to a specific width in the terminal",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -127,2 +127,22 @@ # cli-truncate

##### truncationCharacter
Type: `string`\
Default: `…`
The character to use at the breaking point.
```js
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns', 5, {position: 'end'});
//=> 'unic…'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
//=> 'unic.'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
//=> 'unico'
```
## Related

@@ -129,0 +149,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc