cli-truncate 3.1.0 → 4.0.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.
Files changed (4) hide show
  1. package/index.d.ts +4 -4
  2. package/index.js +13 -16
  3. package/package.json +10 -7
  4. package/readme.md +5 -17
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export interface Options {
1
+ export type Options = {
2
2
  /**
3
3
  The position to truncate the string.
4
4
 
@@ -70,12 +70,12 @@ export interface Options {
70
70
  //=> 'unico'
71
71
  */
72
72
  readonly truncationCharacter?: string;
73
- }
73
+ };
74
74
 
75
75
  /**
76
76
  Truncate a string to a specific width in the terminal.
77
77
 
78
- @param text - Text to truncate.
78
+ @param text - The text to truncate.
79
79
  @param columns - The number of columns to occupy in the terminal.
80
80
 
81
81
  @example
@@ -105,7 +105,7 @@ cliTruncate('안녕하세요', 3);
105
105
 
106
106
  // Truncate the paragraph to the terminal width
107
107
  const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
108
- cliTruncate(paragraph, process.stdout.columns));
108
+ cliTruncate(paragraph, process.stdout.columns);
109
109
  //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
110
110
  ```
111
111
  */
package/index.js CHANGED
@@ -6,29 +6,26 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
6
6
  return wantedIndex;
7
7
  }
8
8
 
9
- for (let index = 1; index <= 3; index++) {
10
- if (shouldSearchRight) {
11
- if (string.charAt(wantedIndex + index) === ' ') {
12
- return wantedIndex + index;
13
- }
14
- } else if (string.charAt(wantedIndex - index) === ' ') {
15
- return wantedIndex - index;
9
+ const direction = shouldSearchRight ? 1 : -1;
10
+
11
+ for (let index = 0; index <= 3; index++) {
12
+ const finalIndex = wantedIndex + (index * direction);
13
+ if (string.charAt(finalIndex) === ' ') {
14
+ return finalIndex;
16
15
  }
17
16
  }
18
17
 
19
18
  return wantedIndex;
20
19
  }
21
20
 
22
- export default function cliTruncate(text, columns, options) {
23
- options = {
24
- position: 'end',
25
- preferTruncationOnSpace: false,
26
- truncationCharacter: '…',
27
- ...options,
28
- };
21
+ export default function cliTruncate(text, columns, options = {}) {
22
+ const {
23
+ position = 'end',
24
+ space = false,
25
+ preferTruncationOnSpace = false,
26
+ } = options;
29
27
 
30
- const {position, space, preferTruncationOnSpace} = options;
31
- let {truncationCharacter} = options;
28
+ let {truncationCharacter = '…'} = options;
32
29
 
33
30
  if (typeof text !== 'string') {
34
31
  throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-truncate",
3
- "version": "3.1.0",
3
+ "version": "4.0.0",
4
4
  "description": "Truncate a string to a specific width in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/cli-truncate",
@@ -11,9 +11,12 @@
11
11
  "url": "https://sindresorhus.com"
12
12
  },
13
13
  "type": "module",
14
- "exports": "./index.js",
14
+ "exports": {
15
+ "types": "./index.d.ts",
16
+ "default": "./index.js"
17
+ },
15
18
  "engines": {
16
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
19
+ "node": ">=18"
17
20
  },
18
21
  "scripts": {
19
22
  "test": "xo && ava && tsd"
@@ -38,11 +41,11 @@
38
41
  ],
39
42
  "dependencies": {
40
43
  "slice-ansi": "^5.0.0",
41
- "string-width": "^5.0.0"
44
+ "string-width": "^7.0.0"
42
45
  },
43
46
  "devDependencies": {
44
- "ava": "^3.15.0",
45
- "tsd": "^0.17.0",
46
- "xo": "^0.44.0"
47
+ "ava": "^5.3.1",
48
+ "tsd": "^0.29.0",
49
+ "xo": "^0.56.0"
47
50
  }
48
51
  }
package/readme.md CHANGED
@@ -6,8 +6,8 @@ Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code
6
6
 
7
7
  ## Install
8
8
 
9
- ```
10
- $ npm install cli-truncate
9
+ ```sh
10
+ npm install cli-truncate
11
11
  ```
12
12
 
13
13
  ## Usage
@@ -41,7 +41,7 @@ cliTruncate('안녕하세요', 3);
41
41
 
42
42
  // Truncate the paragraph to the terminal width
43
43
  const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
44
- cliTruncate(paragraph, process.stdout.columns));
44
+ cliTruncate(paragraph, process.stdout.columns);
45
45
  //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
46
46
  ```
47
47
 
@@ -53,7 +53,7 @@ cliTruncate(paragraph, process.stdout.columns));
53
53
 
54
54
  Type: `string`
55
55
 
56
- Text to truncate.
56
+ The text to truncate.
57
57
 
58
58
  #### columns
59
59
 
@@ -69,7 +69,7 @@ Type: `object`
69
69
 
70
70
  Type: `string`\
71
71
  Default: `'end'`\
72
- Values: `'start'` `'middle'` `'end'`
72
+ Values: `'start' | 'middle' | 'end'`
73
73
 
74
74
  The position to truncate the string.
75
75
 
@@ -148,15 +148,3 @@ cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
148
148
 
149
149
  - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
150
150
  - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
151
-
152
- ---
153
-
154
- <div align="center">
155
- <b>
156
- <a href="https://tidelift.com/subscription/pkg/npm-cli-truncate?utm_source=npm-cli-truncate&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
157
- </b>
158
- <br>
159
- <sub>
160
- Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
161
- </sub>
162
- </div>