🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

cli-truncate

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-truncate - npm Package Compare versions

Comparing version
6.0.1
to
6.1.0
+2
-2
index.d.ts

@@ -79,3 +79,3 @@ export type Options = {

@param text - The text to truncate.
@param columns - The number of columns to occupy in the terminal.
@param columns - The finite number of columns to occupy in the terminal.

@@ -111,3 +111,3 @@ @example

const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns);
cliTruncate(paragraph, process.stdout.columns ?? 80);
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'

@@ -114,0 +114,0 @@ ```

import sliceAnsi from 'slice-ansi';
import stringWidth from 'string-width';
const validPositions = new Set(['start', 'middle', 'end']);
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {

@@ -21,2 +23,24 @@ if (string.charAt(wantedIndex) === ' ') {

function validateInput(text, columns, position, truncationCharacter) {
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}`);
}
if (!Number.isFinite(columns)) {
throw new TypeError(`Expected \`columns\` to be a finite number, got ${columns}`);
}
if (!validPositions.has(position)) {
throw new TypeError(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
}
if (typeof truncationCharacter !== 'string') {
throw new TypeError(`Expected \`options.truncationCharacter\` to be a string, got ${typeof truncationCharacter}`);
}
}
export default function cliTruncate(text, columns, options = {}) {

@@ -31,10 +55,4 @@ const {

if (typeof text !== 'string') {
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
}
validateInput(text, columns, position, truncationCharacter);
if (typeof columns !== 'number') {
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}
if (columns < 1) {

@@ -178,4 +196,2 @@ return '';

}
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
}
{
"name": "cli-truncate",
"version": "6.0.1",
"version": "6.1.0",
"description": "Truncate a string to a specific width in the terminal",
"license": "MIT",
"repository": "sindresorhus/cli-truncate",
"repository": "github:sindresorhus/cli-truncate",
"funding": "https://github.com/sponsors/sindresorhus",

@@ -8,0 +8,0 @@ "author": {

@@ -47,3 +47,3 @@ # cli-truncate

const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns);
cliTruncate(paragraph, process.stdout.columns ?? 80);
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'

@@ -66,3 +66,3 @@ ```

The number of columns to occupy in the terminal.
The finite number of columns to occupy in the terminal.

@@ -69,0 +69,0 @@ #### options