Skip to content

Instantly share code, notes, and snippets.

@ebragaparah
Last active April 4, 2016 03:13
Show Gist options
  • Save ebragaparah/64e6bbbdaf46a42341921a6b40caa7ee to your computer and use it in GitHub Desktop.
Save ebragaparah/64e6bbbdaf46a42341921a6b40caa7ee to your computer and use it in GitHub Desktop.
It's an approach that truncate a string given the maximum size, but following some conditions
function truncateString(str, num) {
var points = '...';
return num < str.length ?
((num < points.length) ?
str.substr(0, num).concat(points) :
str.substr(0, num - points.length).concat(points)) :
str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment