DEV Community

Discussion on: 5 JavaScript functions to add to your utilities library

Collapse
 
slowmove profile image
Erik Hoffman

Even better, it should split at the end of a word and not in the middle of one

function summarize(str, max) {
  if (str.length <= max) return str;
  const subString = str.substring(0, max - 1);
  return subString.substring(0, subString.lastIndexOf(" ")) + "...";
}
Collapse
 
mottie profile image
Rob Garrison

Even better, use the unicode ellipsis (; \u2026).