Created
October 15, 2014 17:22
-
-
Save tcelestino/b5d224fa081265a66787 to your computer and use it in GitHub Desktop.
truncate string
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 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