Created
February 22, 2012 09:27
-
-
Save piscis/1883583 to your computer and use it in GitHub Desktop.
Iphone like shortening function to short Text
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 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