Created
April 9, 2014 12:25
-
-
Save ugurozpinar/10263513 to your computer and use it in GitHub Desktop.
Javascript truncate text (middle)
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
truncate('Javascript ile metni ortadan bölme',10,'...'); | |
// output : Java...lme | |
//unminified | |
var truncate = function (fullStr, strLen, separator) { | |
if (fullStr.length <= strLen) return fullStr; | |
separator = separator || '...'; | |
var sepLen = separator.length, | |
charsToShow = strLen - sepLen, | |
frontChars = Math.ceil(charsToShow/2), | |
backChars = Math.floor(charsToShow/2); | |
return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars); | |
}; | |
//minified | |
function truncate(a,b,c){if(a.length<=b)return a;c=c||"...";var d=b-c.length;b=Math.ceil(d/2);d=Math.floor(d/2);return a.substr(0,b)+c+a.substr(a.length-d)}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment