Last active
August 23, 2019 23:03
-
-
Save leodutra/5358601 to your computer and use it in GitHub Desktop.
Limit String Size - JavaScript
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
function limitStringBy (num, str, complement) { | |
if (str.length > num) { | |
const k = str.substr(0, num).lastIndexOf('\u0020') | |
return str.substr(0, k !== -1 ? k : num) + (complement || '...') | |
} | |
return str | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment