@@ -14,31 +14,29 @@ const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
14
14
15
15
// Calculate the length of words split on ' ', ignoring
16
16
// the extra characters added by ansi escape codes
17
- const wordLengths = str => str . split ( ' ' ) . map ( s => stringWidth ( s ) ) ;
17
+ const wordLengths = string => string . split ( ' ' ) . map ( character => stringWidth ( character ) ) ;
18
18
19
19
// Wrap a long word across multiple rows
20
20
// Ansi escape codes do not count towards length
21
- const wrapWord = ( rows , word , cols ) => {
22
- const arr = Array . from ( word ) ;
21
+ const wrapWord = ( rows , word , columns ) => {
22
+ const characters = [ ... word ] ;
23
23
24
24
let insideEscape = false ;
25
25
let visible = stringWidth ( stripAnsi ( rows [ rows . length - 1 ] ) ) ;
26
26
27
- for ( const item of arr . entries ( ) ) {
28
- const i = item [ 0 ] ;
29
- const char = item [ 1 ] ;
30
- const charLength = stringWidth ( char ) ;
27
+ for ( const [ index , character ] of characters . entries ( ) ) {
28
+ const characterLength = stringWidth ( character ) ;
31
29
32
- if ( visible + charLength <= cols ) {
33
- rows [ rows . length - 1 ] += char ;
30
+ if ( visible + characterLength <= columns ) {
31
+ rows [ rows . length - 1 ] += character ;
34
32
} else {
35
- rows . push ( char ) ;
33
+ rows . push ( character ) ;
36
34
visible = 0 ;
37
35
}
38
36
39
- if ( ESCAPES . has ( char ) ) {
37
+ if ( ESCAPES . has ( character ) ) {
40
38
insideEscape = true ;
41
- } else if ( insideEscape && char === 'm' ) {
39
+ } else if ( insideEscape && character === 'm' ) {
42
40
insideEscape = false ;
43
41
continue ;
44
42
}
@@ -47,9 +45,9 @@ const wrapWord = (rows, word, cols) => {
47
45
continue ;
48
46
}
49
47
50
- visible += charLength ;
48
+ visible += characterLength ;
51
49
52
- if ( visible === cols && i < arr . length - 1 ) {
50
+ if ( visible === columns && index < characters . length - 1 ) {
53
51
rows . push ( '' ) ;
54
52
visible = 0 ;
55
53
}
@@ -66,33 +64,27 @@ const wrapWord = (rows, word, cols) => {
66
64
// in either 'hard' or 'soft' wrap mode
67
65
//
68
66
// 'hard' will never allow a string to take up more
69
- // than cols characters
67
+ // than columns characters
70
68
//
71
69
// 'soft' allows long words to expand past the column length
72
- const exec = ( str , cols , opts ) => {
73
- const options = opts || { } ;
74
-
75
- if ( str . trim ( ) === '' ) {
76
- return options . trim === false ? str : str . trim ( ) ;
70
+ const exec = ( string , columns , options = { } ) => {
71
+ if ( string . trim ( ) === '' ) {
72
+ return options . trim === false ? string : string . trim ( ) ;
77
73
}
78
74
79
75
let pre = '' ;
80
76
let ret = '' ;
81
77
let escapeCode ;
82
78
83
- const lengths = wordLengths ( str ) ;
84
- const words = str . split ( ' ' ) ;
79
+ const lengths = wordLengths ( string ) ;
85
80
const rows = [ '' ] ;
86
81
87
- for ( const item of Array . from ( words ) . entries ( ) ) {
88
- const i = item [ 0 ] ;
89
- const word = item [ 1 ] ;
90
-
82
+ for ( const [ index , word ] of string . split ( ' ' ) . entries ( ) ) {
91
83
rows [ rows . length - 1 ] = options . trim === false ? rows [ rows . length - 1 ] : rows [ rows . length - 1 ] . trim ( ) ;
92
84
let rowLength = stringWidth ( rows [ rows . length - 1 ] ) ;
93
85
94
86
if ( rowLength || word === '' ) {
95
- if ( rowLength === cols && options . wordWrap === false ) {
87
+ if ( rowLength === columns && options . wordWrap === false ) {
96
88
// If we start with a new word but the current row length equals the length of the columns, add a new row
97
89
rows . push ( '' ) ;
98
90
rowLength = 0 ;
@@ -103,51 +95,48 @@ const exec = (str, cols, opts) => {
103
95
}
104
96
105
97
// In 'hard' wrap mode, the length of a line is
106
- // never allowed to extend past 'cols '
107
- if ( lengths [ i ] > cols && options . hard ) {
98
+ // never allowed to extend past 'columns '
99
+ if ( lengths [ index ] > columns && options . hard ) {
108
100
if ( rowLength ) {
109
101
rows . push ( '' ) ;
110
102
}
111
- wrapWord ( rows , word , cols ) ;
103
+ wrapWord ( rows , word , columns ) ;
112
104
continue ;
113
105
}
114
106
115
- if ( rowLength + lengths [ i ] > cols && rowLength > 0 ) {
116
- if ( options . wordWrap === false && rowLength < cols ) {
117
- wrapWord ( rows , word , cols ) ;
107
+ if ( rowLength + lengths [ index ] > columns && rowLength > 0 ) {
108
+ if ( options . wordWrap === false && rowLength < columns ) {
109
+ wrapWord ( rows , word , columns ) ;
118
110
continue ;
119
111
}
120
112
121
113
rows . push ( '' ) ;
122
114
}
123
115
124
- if ( rowLength + lengths [ i ] > cols && options . wordWrap === false ) {
125
- wrapWord ( rows , word , cols ) ;
116
+ if ( rowLength + lengths [ index ] > columns && options . wordWrap === false ) {
117
+ wrapWord ( rows , word , columns ) ;
126
118
continue ;
127
119
}
128
120
129
121
rows [ rows . length - 1 ] += word ;
130
122
}
131
123
132
- pre = rows . map ( r => options . trim === false ? r : r . trim ( ) ) . join ( '\n' ) ;
133
-
134
- for ( const item of Array . from ( pre ) . entries ( ) ) {
135
- const i = item [ 0 ] ;
136
- const char = item [ 1 ] ;
124
+ pre = rows . map ( row => options . trim === false ? row : row . trim ( ) ) . join ( '\n' ) ;
137
125
138
- ret += char ;
126
+ for ( const [ index , character ] of [ ...pre ] . entries ( ) ) {
127
+ ret += character ;
139
128
140
- if ( ESCAPES . has ( char ) ) {
141
- const code = parseFloat ( / \d [ ^ m ] * / . exec ( pre . slice ( i , i + 4 ) ) ) ;
129
+ if ( ESCAPES . has ( character ) ) {
130
+ const code = parseFloat ( / \d [ ^ m ] * / . exec ( pre . slice ( index , index + 4 ) ) ) ;
142
131
escapeCode = code === END_CODE ? null : code ;
143
132
}
144
133
145
134
const code = ansiStyles . codes . get ( Number ( escapeCode ) ) ;
146
135
147
136
if ( escapeCode && code ) {
148
- if ( pre [ i + 1 ] === '\n' ) {
137
+ if ( pre [ index + 1 ] === '\n' ) {
149
138
ret += wrapAnsi ( code ) ;
150
- } else if ( char === '\n' ) {
139
+ } else if ( character === '\n' ) {
151
140
ret += wrapAnsi ( escapeCode ) ;
152
141
}
153
142
}
@@ -157,10 +146,10 @@ const exec = (str, cols, opts) => {
157
146
} ;
158
147
159
148
// For each newline, invoke the method separately
160
- module . exports = ( str , cols , opts ) => {
161
- return String ( str )
149
+ module . exports = ( string , columns , options ) => {
150
+ return String ( string )
162
151
. normalize ( )
163
152
. split ( '\n' )
164
- . map ( line => exec ( line , cols , opts ) )
153
+ . map ( line => exec ( line , columns , options ) )
165
154
. join ( '\n' ) ;
166
155
} ;
0 commit comments