Created
August 30, 2018 19:02
-
-
Save juanbrusco/d2779bc62c49dd67f1f249e21f93d3ad to your computer and use it in GitHub Desktop.
Truncate a string - Javascript
This file contains hidden or 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 (str, length, ending) { | |
if (length == null) { | |
length = 100; | |
} | |
if (ending == null) { | |
ending = '...'; | |
} | |
if (str.length > length) { | |
return str.substring(0, length - ending.length) + ending; | |
} else { | |
return str; | |
} | |
}; | |
console.log(truncate('JS string exercises.')) | |
console.log(truncate('JS string exercises.',19)) | |
console.log(truncate('S string exercises.',15,'...')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment