Skip to content

Commit 76d163e

Browse files
committedAug 31, 2018
Require Node.js 6
1 parent 14f3af7 commit 76d163e

File tree

4 files changed

+46
-58
lines changed

4 files changed

+46
-58
lines changed
 

‎.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

‎.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: node_js
22
node_js:
3+
- '10'
34
- '8'
45
- '6'
5-
- '4'
6-
after_success: npm run coveralls
6+
after_success:
7+
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

‎index.js

+37-48
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,29 @@ const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
1414

1515
// Calculate the length of words split on ' ', ignoring
1616
// 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));
1818

1919
// Wrap a long word across multiple rows
2020
// 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];
2323

2424
let insideEscape = false;
2525
let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
2626

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);
3129

32-
if (visible + charLength <= cols) {
33-
rows[rows.length - 1] += char;
30+
if (visible + characterLength <= columns) {
31+
rows[rows.length - 1] += character;
3432
} else {
35-
rows.push(char);
33+
rows.push(character);
3634
visible = 0;
3735
}
3836

39-
if (ESCAPES.has(char)) {
37+
if (ESCAPES.has(character)) {
4038
insideEscape = true;
41-
} else if (insideEscape && char === 'm') {
39+
} else if (insideEscape && character === 'm') {
4240
insideEscape = false;
4341
continue;
4442
}
@@ -47,9 +45,9 @@ const wrapWord = (rows, word, cols) => {
4745
continue;
4846
}
4947

50-
visible += charLength;
48+
visible += characterLength;
5149

52-
if (visible === cols && i < arr.length - 1) {
50+
if (visible === columns && index < characters.length - 1) {
5351
rows.push('');
5452
visible = 0;
5553
}
@@ -66,33 +64,27 @@ const wrapWord = (rows, word, cols) => {
6664
// in either 'hard' or 'soft' wrap mode
6765
//
6866
// 'hard' will never allow a string to take up more
69-
// than cols characters
67+
// than columns characters
7068
//
7169
// '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();
7773
}
7874

7975
let pre = '';
8076
let ret = '';
8177
let escapeCode;
8278

83-
const lengths = wordLengths(str);
84-
const words = str.split(' ');
79+
const lengths = wordLengths(string);
8580
const rows = [''];
8681

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()) {
9183
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
9284
let rowLength = stringWidth(rows[rows.length - 1]);
9385

9486
if (rowLength || word === '') {
95-
if (rowLength === cols && options.wordWrap === false) {
87+
if (rowLength === columns && options.wordWrap === false) {
9688
// If we start with a new word but the current row length equals the length of the columns, add a new row
9789
rows.push('');
9890
rowLength = 0;
@@ -103,51 +95,48 @@ const exec = (str, cols, opts) => {
10395
}
10496

10597
// 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) {
108100
if (rowLength) {
109101
rows.push('');
110102
}
111-
wrapWord(rows, word, cols);
103+
wrapWord(rows, word, columns);
112104
continue;
113105
}
114106

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);
118110
continue;
119111
}
120112

121113
rows.push('');
122114
}
123115

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);
126118
continue;
127119
}
128120

129121
rows[rows.length - 1] += word;
130122
}
131123

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');
137125

138-
ret += char;
126+
for (const [index, character] of [...pre].entries()) {
127+
ret += character;
139128

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)));
142131
escapeCode = code === END_CODE ? null : code;
143132
}
144133

145134
const code = ansiStyles.codes.get(Number(escapeCode));
146135

147136
if (escapeCode && code) {
148-
if (pre[i + 1] === '\n') {
137+
if (pre[index + 1] === '\n') {
149138
ret += wrapAnsi(code);
150-
} else if (char === '\n') {
139+
} else if (character === '\n') {
151140
ret += wrapAnsi(escapeCode);
152141
}
153142
}
@@ -157,10 +146,10 @@ const exec = (str, cols, opts) => {
157146
};
158147

159148
// 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)
162151
.normalize()
163152
.split('\n')
164-
.map(line => exec(line, cols, opts))
153+
.map(line => exec(line, columns, options))
165154
.join('\n');
166155
};

‎package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=4"
13+
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && nyc ava",
17-
"coveralls": "nyc report --reporter=text-lcov | coveralls"
16+
"test": "xo && nyc ava"
1817
},
1918
"files": [
2019
"index.js"
@@ -52,12 +51,12 @@
5251
"strip-ansi": "^4.0.0"
5352
},
5453
"devDependencies": {
55-
"ava": "^0.23.0",
54+
"ava": "^0.25.0",
5655
"chalk": "^2.0.1",
5756
"coveralls": "^3.0.0",
5857
"has-ansi": "^3.0.0",
59-
"nyc": "^11.0.3",
58+
"nyc": "^13.0.1",
6059
"strip-ansi": "^4.0.0",
61-
"xo": "^0.18.2"
60+
"xo": "^0.22.0"
6261
}
6362
}

0 commit comments

Comments
 (0)
Please sign in to comment.