Skip to content

Instantly share code, notes, and snippets.

@piscis
Created February 22, 2012 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piscis/1883583 to your computer and use it in GitHub Desktop.
Save piscis/1883583 to your computer and use it in GitHub Desktop.
Iphone like shortening function to short Text
var longText = "Super Duper Long Description from the manual for the dashboard";
var shortText = "Main Screen";
var shortTxt = function(txt,max,fill) {
var sTxt = txt,
max = max || 12,
fill = fill || '...';
if(txt.length>=max){
var offset = Math.floor(((max-fill.length) / 2));
var offsetLeft = (offset*2)<(max+fill.length) ? (offset+1) : offset;
sTxt = txt.substr(0,offsetLeft) + fill + txt.substr((0-offset));
}
return sTxt;
}
console.log(shortTxt(longText)); // Super...oard
console.log(shortTxt(shortText)); // Main Screen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment