A Pen by Saman

Thumbnail
This awesome code was written by xengil, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright xengil ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  Saman</title>
  
  
  
  
  
</head>

<body>

  <div class="expression nf-truncate nf-truncate-last">
A subsequent change by British engineers altered the wheels to make pedaling more efficient. They saw that when a rider turned the pedals once, the [[=front=]] wheel turned once. If the front wheel was small, the bicycle traveled just a small distance with each turn. They reasoned that if the front wheel were larger, the bicycle would travel a greater distance with each turn of the pedals. So they designed a bicycle with a giant front wheel.
</div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/xengil/a-pen-by-saman-PQQrNe */
var el = document.querySelector('.nf-truncate'), text = el.innerText;
var regex = /\[\[=(.*?)=\]\]/g;
var word = text.indexOf('[[=subsequent=]]'), l = text.length;

function strtochunks(str, size) {
  var start = 0, end = size, chunks = [], l = str.length;

  do {
    end = str.indexOf(' ', start + size);
    chunks.push(str.slice(start, (end > 0) ? end : text.length));
    start = end;
  } while (end <= l && end !== -1);
  
  return chunks;
}

var chunks = strtochunks(text, 90);

chunks.forEach(t => console.log(t.length, t));

function truncate(str, at, limit, before) {
  var from = (typeof before !== 'undefined') ? str.lastIndexOf(' ', at - limit) : str.indexOf(' ', at + limit);
  if(from === -1) return at;
  return (typeof before !== 'undefined') ? from + 1 : from;
}

/**
console.log(
  text.slice(0, truncate(text, text.length, 150, true)).length,
  text.slice(truncate(text, text.length, 150, true)).length
);
console.log(
  text.slice(0, truncate(text, 0, 150)).length,
  text.slice(truncate(text, 0, 150)).length
)
*/

Comments