cli-truncate
Advanced tools
+2
-2
@@ -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 @@ ``` |
+25
-9
| 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}`); | ||
| } |
+2
-2
| { | ||
| "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": { |
+2
-2
@@ -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 |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
14199
4.17%239
5.29%