Skip to content

Instantly share code, notes, and snippets.

@williamho
Created December 15, 2016 16:08
Show Gist options
  • Save williamho/c477033b47e52ec5ca9bae2f6895e949 to your computer and use it in GitHub Desktop.
Save williamho/c477033b47e52ec5ca9bae2f6895e949 to your computer and use it in GitHub Desktop.
hard wrap selected textarea to 72 columns
(function() {
function wrap(s, maxLength) {
return s.split(' ').reduce(function(acc, x) {
var i = acc.length - 1;
var last = acc[i] == '' ? x : (acc[i] + ' ' + x);
if (last.length > maxLength) {
acc.push(x);
} else {
acc[i] = last;
}
return acc;
}, ['']).join('\n')
};
var maxLength = 72;
document.activeElement.value = wrap(document.activeElement.value, maxLength);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment