Skip to content

Instantly share code, notes, and snippets.

@s-lyn
Last active June 9, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-lyn/adf217455d39650a6afbcf21fd946f4e to your computer and use it in GitHub Desktop.
Save s-lyn/adf217455d39650a6afbcf21fd946f4e to your computer and use it in GitHub Desktop.
Function to shorten the string to the specified length
/**
* 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