2 // wraps a string by a certain width
4 var makeString = require('./helper/makeString');
6 module.exports = function wrap(str, options){
9 options = options || {};
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;
25 var words = str.split(" ");
26 var current_column = 0;
29 while(words.length > 0){
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
40 // fill the rest of the line with spaces if trailingSpaces option is true
41 else if(trailingSpaces){
42 while(current_column < width){
53 // if not at the begining of the line, add a space in front of the word
54 if(current_column > 0){
59 // tack on the next word, update current column, a pop words array
61 current_column += words[0].length;
66 // fill the rest of the line with spaces if trailingSpaces option is true
68 while(current_column < width){
83 // walk through each character and add seperators where appropriate
84 while(index < str.length){
85 if(index % width == 0 && index > 0){
88 result += str.charAt(index);
92 // fill the rest of the line with spaces if trailingSpaces option is true
94 while(index % width > 0){