Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Created October 15, 2014 17:22
Show Gist options
  • Save tcelestino/b5d224fa081265a66787 to your computer and use it in GitHub Desktop.
Save tcelestino/b5d224fa081265a66787 to your computer and use it in GitHub Desktop.
truncate string
/**
* truncate title
* @param {String} text suffix
* @param {Number} size text
* @return {String} text truncate
*/
var truncateText = function(str, size, suffix) {
var newTitle;
if (str.length > size) {
newTitle = str.substr(0, size - suffix.length) + suffix;
} else {
if (str.length + suffix.length > size) {
newTitle = str.substr(0, (str.length - suffix.length)) + suffix;
} else {
newTitle = str;
}
}
return newTitle;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment