wrap-ansi 5.0.0 → 5.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.
Files changed (3) hide show
  1. package/index.js +32 -6
  2. package/package.json +1 -1
  3. package/readme.md +19 -0
package/index.js CHANGED
@@ -60,6 +60,26 @@ const wrapWord = (rows, word, columns) => {
60
60
  }
61
61
  };
62
62
 
63
+ // Trims spaces from a string ignoring invisible sequences
64
+ const stringVisibleTrimSpacesRight = str => {
65
+ const words = str.split(' ');
66
+ let last = words.length;
67
+
68
+ while (last > 0) {
69
+ if (stringWidth(words[last - 1]) > 0) {
70
+ break;
71
+ }
72
+
73
+ last--;
74
+ }
75
+
76
+ if (last === words.length) {
77
+ return str;
78
+ }
79
+
80
+ return words.slice(0, last).join(' ') + words.slice(last).join('');
81
+ };
82
+
63
83
  // The wrap-ansi module can be invoked
64
84
  // in either 'hard' or 'soft' wrap mode
65
85
  //
@@ -77,7 +97,7 @@ const exec = (string, columns, options = {}) => {
77
97
  let escapeCode;
78
98
 
79
99
  const lengths = wordLengths(string);
80
- const rows = [''];
100
+ let rows = [''];
81
101
 
82
102
  for (const [index, word] of string.split(' ').entries()) {
83
103
  if (options.trim !== false) {
@@ -87,14 +107,16 @@ const exec = (string, columns, options = {}) => {
87
107
  let rowLength = stringWidth(rows[rows.length - 1]);
88
108
 
89
109
  if (index !== 0) {
90
- if (rowLength === columns && (options.wordWrap === false || options.trim === false)) {
110
+ if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
91
111
  // If we start with a new word but the current row length equals the length of the columns, add a new row
92
112
  rows.push('');
93
113
  rowLength = 0;
94
114
  }
95
115
 
96
- rows[rows.length - 1] += ' ';
97
- rowLength++;
116
+ if (rowLength > 0 || options.trim === false) {
117
+ rows[rows.length - 1] += ' ';
118
+ rowLength++;
119
+ }
98
120
  }
99
121
 
100
122
  // In 'hard' wrap mode, the length of a line is
@@ -111,7 +133,7 @@ const exec = (string, columns, options = {}) => {
111
133
  continue;
112
134
  }
113
135
 
114
- if (rowLength + lengths[index] > columns && rowLength > 0) {
136
+ if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
115
137
  if (options.wordWrap === false && rowLength < columns) {
116
138
  wrapWord(rows, word, columns);
117
139
  continue;
@@ -128,7 +150,11 @@ const exec = (string, columns, options = {}) => {
128
150
  rows[rows.length - 1] += word;
129
151
  }
130
152
 
131
- pre = rows.map(row => options.trim === false ? row : row.trim()).join('\n');
153
+ if (options.trim !== false) {
154
+ rows = rows.map(stringVisibleTrimSpacesRight);
155
+ }
156
+
157
+ pre = rows.join('\n');
132
158
 
133
159
  for (const [index, character] of [...pre].entries()) {
134
160
  ret += character;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrap-ansi",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Wordwrap a string with ANSI escape codes",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/wrap-ansi",
package/readme.md CHANGED
@@ -24,6 +24,20 @@ console.log(wrapAnsi(input, 20));
24
24
 
25
25
  <img width="331" src="screenshot.png">
26
26
 
27
+ ---
28
+
29
+ <div align="center">
30
+ <b>
31
+ <a href="https://tidelift.com/subscription/pkg/npm-wrap_ansi?utm_source=npm-wrap-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
32
+ </b>
33
+ <br>
34
+ <sub>
35
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
36
+ </sub>
37
+ </div>
38
+
39
+ ---
40
+
27
41
 
28
42
  ## API
29
43
 
@@ -84,6 +98,11 @@ Whitespace on all lines is removed by default. Set this option to `false` if you
84
98
  - [Benjamin Coe](https://github.com/bcoe)
85
99
 
86
100
 
101
+ ## Security
102
+
103
+ To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
104
+
105
+
87
106
  ## License
88
107
 
89
108
  MIT