Skip to content

Instantly share code, notes, and snippets.

@veeracs
Last active September 28, 2016 22:31
Show Gist options
  • Save veeracs/4317f46fa14ef6ca5dd740e141841295 to your computer and use it in GitHub Desktop.
Save veeracs/4317f46fa14ef6ca5dd740e141841295 to your computer and use it in GitHub Desktop.
Ellipsis
(function ellipsis(str, charLimit) {
var result = [];
var letters = 0;
str.split(' ').forEach(function(word, index) {
if (letters < charLimit) {result.push(word)}
letters += word.length;
});
console.log(result.join(' ') + ' ...');
})("Hero congressman calls out conservatives on racist abortion restrictions", 40);
@endtwist
Copy link

endtwist commented Sep 28, 2016

((str, charLimit) =>
  str.length > charLimit
  ? str.split(' ').reduce((seq, word) =>
      seq + (seq.length < charLimit ? `${word} ` : ''), ''
    ) + '…'
  : str
)("Hero congressman calls out conservatives on racist abortion restrictions", 40);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment