cli-truncate 6.0.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -2
- package/index.js +25 -9
- package/package.json +2 -2
- package/readme.md +2 -2
package/index.d.ts
CHANGED
|
@@ -76,7 +76,7 @@ export type Options = {
|
|
|
76
76
|
Truncate a string to a specific width in the terminal.
|
|
77
77
|
|
|
78
78
|
@param text - The text to truncate.
|
|
79
|
-
@param columns - The number of columns to occupy in the terminal.
|
|
79
|
+
@param columns - The finite number of columns to occupy in the terminal.
|
|
80
80
|
|
|
81
81
|
@example
|
|
82
82
|
```
|
|
@@ -107,7 +107,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
107
107
|
|
|
108
108
|
// Truncate the paragraph to the terminal width
|
|
109
109
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
110
|
-
cliTruncate(paragraph, process.stdout.columns);
|
|
110
|
+
cliTruncate(paragraph, process.stdout.columns ?? 80);
|
|
111
111
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
112
112
|
```
|
|
113
113
|
*/
|
package/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import sliceAnsi from 'slice-ansi';
|
|
2
2
|
import stringWidth from 'string-width';
|
|
3
3
|
|
|
4
|
+
const validPositions = new Set(['start', 'middle', 'end']);
|
|
5
|
+
|
|
4
6
|
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
5
7
|
if (string.charAt(wantedIndex) === ' ') {
|
|
6
8
|
return wantedIndex;
|
|
@@ -18,6 +20,28 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
|
|
|
18
20
|
return wantedIndex;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
function validateInput(text, columns, position, truncationCharacter) {
|
|
24
|
+
if (typeof text !== 'string') {
|
|
25
|
+
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof columns !== 'number') {
|
|
29
|
+
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!Number.isFinite(columns)) {
|
|
33
|
+
throw new TypeError(`Expected \`columns\` to be a finite number, got ${columns}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!validPositions.has(position)) {
|
|
37
|
+
throw new TypeError(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (typeof truncationCharacter !== 'string') {
|
|
41
|
+
throw new TypeError(`Expected \`options.truncationCharacter\` to be a string, got ${typeof truncationCharacter}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
21
45
|
export default function cliTruncate(text, columns, options = {}) {
|
|
22
46
|
const {
|
|
23
47
|
position = 'end',
|
|
@@ -27,13 +51,7 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
27
51
|
|
|
28
52
|
let {truncationCharacter = '…'} = options;
|
|
29
53
|
|
|
30
|
-
|
|
31
|
-
throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (typeof columns !== 'number') {
|
|
35
|
-
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
|
|
36
|
-
}
|
|
54
|
+
validateInput(text, columns, position, truncationCharacter);
|
|
37
55
|
|
|
38
56
|
if (columns < 1) {
|
|
39
57
|
return '';
|
|
@@ -173,6 +191,4 @@ export default function cliTruncate(text, columns, options = {}) {
|
|
|
173
191
|
const left = sliceAnsi(text, 0, columns - stringWidth(truncationCharacter));
|
|
174
192
|
return appendWithInheritedStyleFromEnd(left, truncationCharacter);
|
|
175
193
|
}
|
|
176
|
-
|
|
177
|
-
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
|
|
178
194
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cli-truncate",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Truncate a string to a specific width in the terminal",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"repository": "sindresorhus/cli-truncate",
|
|
6
|
+
"repository": "github:sindresorhus/cli-truncate",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
8
|
"author": {
|
|
9
9
|
"name": "Sindre Sorhus",
|
package/readme.md
CHANGED
|
@@ -44,7 +44,7 @@ cliTruncate('안녕하세요', 3);
|
|
|
44
44
|
|
|
45
45
|
// Truncate the paragraph to the terminal width
|
|
46
46
|
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
|
|
47
|
-
cliTruncate(paragraph, process.stdout.columns);
|
|
47
|
+
cliTruncate(paragraph, process.stdout.columns ?? 80);
|
|
48
48
|
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
|
|
49
49
|
```
|
|
50
50
|
|
|
@@ -62,7 +62,7 @@ The text to truncate.
|
|
|
62
62
|
|
|
63
63
|
Type: `number`
|
|
64
64
|
|
|
65
|
-
The number of columns to occupy in the terminal.
|
|
65
|
+
The finite number of columns to occupy in the terminal.
|
|
66
66
|
|
|
67
67
|
#### options
|
|
68
68
|
|