[UPDATED CODE AND HELP CAN BE FOUND HERE]
usage
alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n+", true)); alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n>", false));
code
//+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/string/wordwrap [v1.0] String.prototype.wordWrap = function(m, b, c){ var i, j, l, s, r; if(m < 1) return this; for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s) for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")) j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length; return r.join("\n"); };
clause is not functionally different than
as you're substr-ing a string length greater than the string itself. Javascript doesn't complain of course, but this is wrong.
Furthermore, this very complicated clause is written more comprehensibly as
sincec j.input.length is always just m.
This, in turn, is equivalent to
s.length
In other words, I think this whole clause is a waste of code.
On another note, I needed a word wrap function that will wrap at spaces if possible AND break-up any words longer than m. Neither values of c (true or false) as paramters to your function will result in this since although c=true will breakup single words longer than m, it doesn't wrap at word boundaries where this is possible, and will break up words shorter than m. I can't imagine anyone wanting word-wrap functionality along these lines! I rewrote your function to generate the output I want:
"My word is biiiiiiiiiiiiiiig" => "My word is\nbiiiiiiiii\niiig"
Here is the function:
I.e., just replace your mysterious clause with m.