diff options
author | Liam <towerofnix@gmail.com> | 2017-01-07 18:26:02 -0400 |
---|---|---|
committer | Liam <towerofnix@gmail.com> | 2017-01-07 18:26:02 -0400 |
commit | 16da7fb310198851c2e4b02abedfb24979287242 (patch) | |
tree | d7546f7c1a3c3833e6450ea1e10af388f8848bb5 /util/wrap.js |
Initial commit
Diffstat (limited to 'util/wrap.js')
-rw-r--r-- | util/wrap.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/util/wrap.js b/util/wrap.js new file mode 100644 index 0000000..78e5233 --- /dev/null +++ b/util/wrap.js @@ -0,0 +1,22 @@ +module.exports = function wrap(str, width) { + // Wraps a string into separate lines. Returns an array of strings, for + // each line of the text. + + const lines = [] + const words = str.split(' ') + + let curLine = words[0] + + for (let word of words.slice(1)) { + if (curLine.length + word.length > width) { + lines.push(curLine) + curLine = word + } else { + curLine += ' ' + word + } + } + + lines.push(curLine) + + return lines +} |