Add ellipsis after given amount of characters

An example showing how to create function that adds ellipsis after given amount of characters using Tizen 2.3.
var ellipsis = function(txt, maxLen) {
  if (maxLen >= txt.length) {
    return txt;
  }
  var len = 0;
  var idx = 0;
  var words = txt.split(' ');
  words.every(function(word, i) {
    if (len + word.length + 3 > maxLen) {
      return false;
    }
    len += word.length + 1;
    idx = i;
    return true;
  });
  return words.slice(0, idx + 1).join(' ') + '...';
};

ellipsis('To jest bardzo dlugi tekst', 16); // "To jest..."
ellipsis('To jest bardzo dlugi tekst', 17); // "To jest bardzo..."
ellipsis('To jest bardzo dlugi tekst', 25); // "To jest bardzo dlugi..."
ellipsis('To jest bardzo dlugi tekst', 26); // "To jest bardzo dlugi tekst"
ellipsis('To jest bardzo dlugi tekst', 40); // "To jest bardzo dlugi tekst"

Responses

0 Replies