Created
July 6, 2012 12:30
-
-
Save galvez/3059901 to your computer and use it in GitHub Desktop.
deorphanize.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var deorphanize = function(text, line_length) { | |
// alert(typeof line_length) | |
if(typeof line_length == 'undefined') { | |
line_length = 40; | |
} | |
text = $.trim(text); | |
if(text.length > line_length) { | |
if(text.length < (line_length*2)) { | |
line_length = text.length/3; | |
} | |
} else { | |
return text; | |
} | |
var w = text.split(/\s+/); | |
var n = []; | |
var lc = line_length; | |
var i = w.length; | |
var t, lt; | |
while(i--) { | |
t = w[i]; | |
lt = t.length; | |
if((lc - lt) > 0) { | |
n.unshift(t); | |
lc -= lt; | |
} else { | |
n.unshift('<br>' + t); | |
lc = line_length - lc; | |
} | |
} | |
return n.join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment