Skip to content

Instantly share code, notes, and snippets.

@galvez
Created July 6, 2012 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvez/3059901 to your computer and use it in GitHub Desktop.
Save galvez/3059901 to your computer and use it in GitHub Desktop.
deorphanize.js
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