Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active August 23, 2019 23:03
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 leodutra/5358601 to your computer and use it in GitHub Desktop.
Save leodutra/5358601 to your computer and use it in GitHub Desktop.
Limit String Size - JavaScript
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