Last active
June 9, 2017 09:18
-
-
Save s-lyn/adf217455d39650a6afbcf21fd946f4e to your computer and use it in GitHub Desktop.
Function to shorten the string to the specified length
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
/** | |
* Shorten the string to the specified length. | |
* Example: shortenTo("Lorem ipsum dolor", 10); // "Lorem i..." | |
* @param {string} str | |
* @param {number} len | |
*/ | |
function shortenTo(str, len) { | |
const strLen = str.length; | |
return strLen < len ? str : str.substr(0, len - 3) + '...'; | |
} | |
module.exports = shortenTo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment