less than 1 minute read

Sample function to cut string into desired limit value.

const limitTitle = (title, limit = 17) => {
  const newTitle = [];
  if(title.length > limit) {
    title.split(' ').reduce((acc, cur) => {
      if (acc + cur.length <= limit) {
        newTitle.push(cur);
      }
      return acc + cur.length
    }, 0);
    return `${newTitle.join(' ')} ...`;
  }
  return title;
}

console.log(limitTitle("This is an example title"));

Comments