Skip to content

Instantly share code, notes, and snippets.

@ugurozpinar
Created April 9, 2014 12:25
Show Gist options
  • Save ugurozpinar/10263513 to your computer and use it in GitHub Desktop.
Save ugurozpinar/10263513 to your computer and use it in GitHub Desktop.
Javascript truncate text (middle)
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