1 | /*!
|
---|
2 | * word-wrap <https://github.com/jonschlinkert/word-wrap>
|
---|
3 | *
|
---|
4 | * Copyright (c) 2014-2023, Jon Schlinkert.
|
---|
5 | * Released under the MIT License.
|
---|
6 | */
|
---|
7 |
|
---|
8 | function trimEnd(str) {
|
---|
9 | let lastCharPos = str.length - 1;
|
---|
10 | let lastChar = str[lastCharPos];
|
---|
11 | while(lastChar === ' ' || lastChar === '\t') {
|
---|
12 | lastChar = str[--lastCharPos];
|
---|
13 | }
|
---|
14 | return str.substring(0, lastCharPos + 1);
|
---|
15 | }
|
---|
16 |
|
---|
17 | function trimTabAndSpaces(str) {
|
---|
18 | const lines = str.split('\n');
|
---|
19 | const trimmedLines = lines.map((line) => trimEnd(line));
|
---|
20 | return trimmedLines.join('\n');
|
---|
21 | }
|
---|
22 |
|
---|
23 | module.exports = function(str, options) {
|
---|
24 | options = options || {};
|
---|
25 | if (str == null) {
|
---|
26 | return str;
|
---|
27 | }
|
---|
28 |
|
---|
29 | var width = options.width || 50;
|
---|
30 | var indent = (typeof options.indent === 'string')
|
---|
31 | ? options.indent
|
---|
32 | : ' ';
|
---|
33 |
|
---|
34 | var newline = options.newline || '\n' + indent;
|
---|
35 | var escape = typeof options.escape === 'function'
|
---|
36 | ? options.escape
|
---|
37 | : identity;
|
---|
38 |
|
---|
39 | var regexString = '.{1,' + width + '}';
|
---|
40 | if (options.cut !== true) {
|
---|
41 | regexString += '([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)';
|
---|
42 | }
|
---|
43 |
|
---|
44 | var re = new RegExp(regexString, 'g');
|
---|
45 | var lines = str.match(re) || [];
|
---|
46 | var result = indent + lines.map(function(line) {
|
---|
47 | if (line.slice(-1) === '\n') {
|
---|
48 | line = line.slice(0, line.length - 1);
|
---|
49 | }
|
---|
50 | return escape(line);
|
---|
51 | }).join(newline);
|
---|
52 |
|
---|
53 | if (options.trim === true) {
|
---|
54 | result = trimTabAndSpaces(result);
|
---|
55 | }
|
---|
56 | return result;
|
---|
57 | };
|
---|
58 |
|
---|
59 | function identity(str) {
|
---|
60 | return str;
|
---|
61 | }
|
---|