source: imaps-frontend/node_modules/word-wrap/index.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[d565449]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
8function 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
17function trimTabAndSpaces(str) {
18 const lines = str.split('\n');
19 const trimmedLines = lines.map((line) => trimEnd(line));
20 return trimmedLines.join('\n');
21}
22
23module.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
59function identity(str) {
60 return str;
61}
Note: See TracBrowser for help on using the repository browser.