1 | "use strict";
|
2 |
|
3 | function wordWrap(str, options = {}) {
|
4 | if (typeof str !== 'string')
|
5 | throw new TypeError('str must be a string!');
|
6 | const width = options.width || 80;
|
7 | const regex = new RegExp(`(.{1,${width}})(\\s+|$)`, 'g');
|
8 | const lines = str.split('\n');
|
9 | for (let i = 0, len = lines.length; i < len; i++) {
|
10 | const line = lines[i];
|
11 | if (line.length > width) {
|
12 | lines[i] = line.replace(regex, '$1\n').trim();
|
13 | }
|
14 | }
|
15 | return lines.join('\n');
|
16 | }
|
17 | module.exports = wordWrap;
|
18 |
|
\ | No newline at end of file |