Security update to Drupal 8.4.6
[yaffs-website] / node_modules / underscore.string / wrap.js
1 // Wrap
2 // wraps a string by a certain width
3
4 var makeString = require('./helper/makeString');
5
6 module.exports = function wrap(str, options){
7         str = makeString(str);
8
9         options = options || {};
10
11         var width = options.width || 75;
12         var seperator = options.seperator || '\n';
13         var cut = options.cut || false;
14         var preserveSpaces = options.preserveSpaces || false;
15         var trailingSpaces = options.trailingSpaces || false;
16
17   var result;
18
19         if(width <= 0){
20                 return str;
21         }
22
23         else if(!cut){
24
25                 var words = str.split(" ");
26                 var current_column = 0;
27                 result = "";
28
29                 while(words.length > 0){
30                         
31                         // if adding a space and the next word would cause this line to be longer than width...
32                         if(1 + words[0].length + current_column > width){
33                                 //start a new line if this line is not already empty
34                                 if(current_column > 0){
35                                         // add a space at the end of the line is preserveSpaces is true
36                                         if (preserveSpaces){
37                                                 result += ' ';
38                                                 current_column++;
39                                         }
40                                         // fill the rest of the line with spaces if trailingSpaces option is true
41                                         else if(trailingSpaces){
42                                                 while(current_column < width){
43                                                         result += ' ';
44                                                         current_column++;
45                                                 }                                               
46                                         }
47                                         //start new line
48                                         result += seperator;
49                                         current_column = 0;
50                                 }
51                         }
52
53                         // if not at the begining of the line, add a space in front of the word
54                         if(current_column > 0){
55                                 result += " ";
56                                 current_column++;
57                         }
58
59                         // tack on the next word, update current column, a pop words array
60                         result += words[0];
61                         current_column += words[0].length;
62                         words.shift();
63
64                 }
65
66                 // fill the rest of the line with spaces if trailingSpaces option is true
67                 if(trailingSpaces){
68                         while(current_column < width){
69                                 result += ' ';
70                                 current_column++;
71                         }                                               
72                 }
73
74                 return result;
75
76         }
77
78         else {
79
80                 var index = 0;
81                 result = "";
82
83                 // walk through each character and add seperators where appropriate
84                 while(index < str.length){
85                         if(index % width == 0 && index > 0){
86                                 result += seperator;
87                         }
88                         result += str.charAt(index);
89                         index++;
90                 }
91
92                 // fill the rest of the line with spaces if trailingSpaces option is true
93                 if(trailingSpaces){
94                         while(index % width > 0){
95                                 result += ' ';
96                                 index++;
97                         }                                               
98                 }
99                 
100                 return result;
101         }
102 };