Created
December 15, 2016 16:08
-
-
Save williamho/c477033b47e52ec5ca9bae2f6895e949 to your computer and use it in GitHub Desktop.
hard wrap selected textarea to 72 columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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