X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=node_modules%2Funderscore.string%2Fwrap.js;fp=node_modules%2Funderscore.string%2Fwrap.js;h=dcdc9a1eed7e5c2102f89d043ea0f3984dc52aa6;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/node_modules/underscore.string/wrap.js b/node_modules/underscore.string/wrap.js new file mode 100644 index 000000000..dcdc9a1ee --- /dev/null +++ b/node_modules/underscore.string/wrap.js @@ -0,0 +1,102 @@ +// Wrap +// wraps a string by a certain width + +var makeString = require('./helper/makeString'); + +module.exports = function wrap(str, options){ + str = makeString(str); + + options = options || {}; + + var width = options.width || 75; + var seperator = options.seperator || '\n'; + var cut = options.cut || false; + var preserveSpaces = options.preserveSpaces || false; + var trailingSpaces = options.trailingSpaces || false; + + var result; + + if(width <= 0){ + return str; + } + + else if(!cut){ + + var words = str.split(" "); + var current_column = 0; + result = ""; + + while(words.length > 0){ + + // if adding a space and the next word would cause this line to be longer than width... + if(1 + words[0].length + current_column > width){ + //start a new line if this line is not already empty + if(current_column > 0){ + // add a space at the end of the line is preserveSpaces is true + if (preserveSpaces){ + result += ' '; + current_column++; + } + // fill the rest of the line with spaces if trailingSpaces option is true + else if(trailingSpaces){ + while(current_column < width){ + result += ' '; + current_column++; + } + } + //start new line + result += seperator; + current_column = 0; + } + } + + // if not at the begining of the line, add a space in front of the word + if(current_column > 0){ + result += " "; + current_column++; + } + + // tack on the next word, update current column, a pop words array + result += words[0]; + current_column += words[0].length; + words.shift(); + + } + + // fill the rest of the line with spaces if trailingSpaces option is true + if(trailingSpaces){ + while(current_column < width){ + result += ' '; + current_column++; + } + } + + return result; + + } + + else { + + var index = 0; + result = ""; + + // walk through each character and add seperators where appropriate + while(index < str.length){ + if(index % width == 0 && index > 0){ + result += seperator; + } + result += str.charAt(index); + index++; + } + + // fill the rest of the line with spaces if trailingSpaces option is true + if(trailingSpaces){ + while(index % width > 0){ + result += ' '; + index++; + } + } + + return result; + } +};