@cjser/cli-truncate 6.0.1-cjser.2 → 6.1.1-cjser.2

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.
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  ));
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
 
29
- // packages/@cjser/cli-truncate.tmp-26-1782231691521/index.js
29
+ // packages/@cjser/cli-truncate.tmp-26-1783614088739/index.js
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
32
  default: () => cliTruncate
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  module.exports = __toCommonJS(index_exports);
35
35
  var import_slice_ansi = __toESM(require("@cjser/slice-ansi"), 1);
36
36
  var import_string_width = __toESM(require("@cjser/string-width"), 1);
37
+ var validPositions = /* @__PURE__ */ new Set(["start", "middle", "end"]);
37
38
  function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
38
39
  if (string.charAt(wantedIndex) === " ") {
39
40
  return wantedIndex;
@@ -47,6 +48,23 @@ function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
47
48
  }
48
49
  return wantedIndex;
49
50
  }
51
+ function validateInput(text, columns, position, truncationCharacter) {
52
+ if (typeof text !== "string") {
53
+ throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
54
+ }
55
+ if (typeof columns !== "number") {
56
+ throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
57
+ }
58
+ if (!Number.isFinite(columns)) {
59
+ throw new TypeError(`Expected \`columns\` to be a finite number, got ${columns}`);
60
+ }
61
+ if (!validPositions.has(position)) {
62
+ throw new TypeError(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
63
+ }
64
+ if (typeof truncationCharacter !== "string") {
65
+ throw new TypeError(`Expected \`options.truncationCharacter\` to be a string, got ${typeof truncationCharacter}`);
66
+ }
67
+ }
50
68
  function cliTruncate(text, columns, options = {}) {
51
69
  const {
52
70
  position = "end",
@@ -54,12 +72,7 @@ function cliTruncate(text, columns, options = {}) {
54
72
  preferTruncationOnSpace = false
55
73
  } = options;
56
74
  let { truncationCharacter = "\u2026" } = options;
57
- if (typeof text !== "string") {
58
- throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
59
- }
60
- if (typeof columns !== "number") {
61
- throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
62
- }
75
+ validateInput(text, columns, position, truncationCharacter);
63
76
  if (columns < 1) {
64
77
  return "";
65
78
  }
@@ -122,7 +135,7 @@ function cliTruncate(text, columns, options = {}) {
122
135
  }
123
136
  if (position === "start") {
124
137
  if (preferTruncationOnSpace) {
125
- const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
138
+ const nearestSpace = getIndexOfNearestSpace(text, length - columns + (0, import_string_width.default)(truncationCharacter), true);
126
139
  const right2 = (0, import_slice_ansi.default)(text, nearestSpace, length).trim();
127
140
  return prependWithInheritedStyleFromStart(truncationCharacter, right2);
128
141
  }
@@ -150,7 +163,7 @@ function cliTruncate(text, columns, options = {}) {
150
163
  }
151
164
  if (position === "end") {
152
165
  if (preferTruncationOnSpace) {
153
- const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
166
+ const nearestSpace = getIndexOfNearestSpace(text, columns - (0, import_string_width.default)(truncationCharacter));
154
167
  const left2 = (0, import_slice_ansi.default)(text, 0, nearestSpace);
155
168
  return appendWithInheritedStyleFromEnd(left2, truncationCharacter);
156
169
  }
@@ -160,5 +173,4 @@ function cliTruncate(text, columns, options = {}) {
160
173
  const left = (0, import_slice_ansi.default)(text, 0, columns - (0, import_string_width.default)(truncationCharacter));
161
174
  return appendWithInheritedStyleFromEnd(left, truncationCharacter);
162
175
  }
163
- throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
164
176
  }
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 '@cjser/slice-ansi';
2
2
  import stringWidth from '@cjser/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
- if (typeof text !== 'string') {
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 '';
@@ -116,7 +134,7 @@ export default function cliTruncate(text, columns, options = {}) {
116
134
 
117
135
  if (position === 'start') {
118
136
  if (preferTruncationOnSpace) {
119
- const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
137
+ const nearestSpace = getIndexOfNearestSpace(text, length - columns + stringWidth(truncationCharacter), true);
120
138
  const right = sliceAnsi(text, nearestSpace, length).trim();
121
139
  return prependWithInheritedStyleFromStart(truncationCharacter, right);
122
140
  }
@@ -161,7 +179,7 @@ export default function cliTruncate(text, columns, options = {}) {
161
179
 
162
180
  if (position === 'end') {
163
181
  if (preferTruncationOnSpace) {
164
- const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
182
+ const nearestSpace = getIndexOfNearestSpace(text, columns - stringWidth(truncationCharacter));
165
183
  const left = sliceAnsi(text, 0, nearestSpace);
166
184
  return appendWithInheritedStyleFromEnd(left, truncationCharacter);
167
185
  }
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@cjser/cli-truncate",
3
- "version": "6.0.1-cjser.2",
3
+ "version": "6.1.1-cjser.2",
4
4
  "description": "Truncate a string to a specific width in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -47,7 +47,7 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "@cjser/slice-ansi": "9.0.0-cjser.2",
50
- "@cjser/string-width": "8.2.1-cjser.2"
50
+ "@cjser/string-width": "8.2.2-cjser.2"
51
51
  },
52
52
  "devDependencies": {
53
53
  "ava": "^7.0.0",
@@ -57,16 +57,16 @@
57
57
  "types": "./index.d.ts",
58
58
  "main": "./dist-cjser/index.cjs",
59
59
  "cjser": {
60
- "sourceVersion": "6.0.1",
60
+ "sourceVersion": "6.1.1",
61
61
  "cjserVersion": 2,
62
62
  "original": {
63
63
  "name": "cli-truncate",
64
- "version": "6.0.1",
64
+ "version": "6.1.1",
65
65
  "exports": {
66
66
  "types": "./index.d.ts",
67
67
  "default": "./index.js"
68
68
  },
69
- "repository": "sindresorhus/cli-truncate",
69
+ "repository": "github:sindresorhus/cli-truncate",
70
70
  "dependencies": {
71
71
  "slice-ansi": "^9.0.0",
72
72
  "string-width": "^8.2.0"
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