Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

WordWrap //JavaScript Function (See related posts)



[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");
};

Comments on this post

nkallen posts on Jul 15, 2006 at 21:08
This is very clever, if unreadable. But I think it has a bug in it, since the
j.input.length + (j = s.substr(m).match(/^\S*/)).input.length + j[0].length

clause is not functionally different than
j.input.length + (j = s.substr(m).match(/^\S*/)).input.length

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
m + (j = s.substr(m).match(/^\S*/)).input.length
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:

String.prototype.wordWrap = function(m, b, c){
    var i, j, s, r = this.split("\n");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
    return r.join("\n");
};


I.e., just replace your mysterious clause with m.
susan12345 posts on Jan 09, 2008 at 04:48
hi! everybody your "clever but unreadable code" does'nt seem to work for the strings like qqqqqqqqqqqqqqqq...and longer ones....it wraps this string like this qqqqqq qqqqqqq qqqqqqqqq ...i.e with spaces...please find a better solution but quickly....
jonasraoni posts on Apr 12, 2008 at 11:14
Hi, I didn't checked comments on my codes for a long time, so I'll answer :)

@susan12345
Here it's working fine my code version...

document.write("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "
"));

And

document.write("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "
", true));



@nkallen
If I did all those things, there must be a reason, normally I don't make duplicated code, so that was the reason, test the above 2 lines with your version.
jonasraoni posts on Apr 12, 2008 at 11:26
It replaced my br tag aaaaaaa, I won't try to post "& lt ;" (maybe it work: &lt;) xD

alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true));

alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true));
jonasraoni posts on Apr 12, 2008 at 11:26
AAAAAAAAAAAAAAAAAAAAAAAAAA

alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n"));

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts