diff options
author | Rebecca Turner <me@re-becca.org> | 2016-01-07 02:48:15 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2016-01-07 04:21:07 +0300 |
commit | 74d92a08d72ce3603244de4bb3e3706d2b928cef (patch) | |
tree | c3593048c4fa4ee27ef6afc5632464558bc5cbb4 /node_modules/columnify/utils.js | |
parent | 6b0031e28c0b10fb2622fdadde41f5cd294348e8 (diff) |
columnify@1.5.4
Some bug fixes around large inputs
Credit: @timoxley
Diffstat (limited to 'node_modules/columnify/utils.js')
-rw-r--r-- | node_modules/columnify/utils.js | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/node_modules/columnify/utils.js b/node_modules/columnify/utils.js index 30682af3b..df3e6cc44 100644 --- a/node_modules/columnify/utils.js +++ b/node_modules/columnify/utils.js @@ -108,33 +108,46 @@ function splitIntoLines(str, max) { * @return String */ -function splitLongWords(str, max, truncationChar, result) { +function splitLongWords(str, max, truncationChar) { str = str.trim() - result = result || [] - if (!str) return result.join(' ') || '' + var result = [] var words = str.split(' ') - var word = words.shift() || str - if (wcwidth(word) > max) { - // slice is based on length no wcwidth - var i = 0 - var wwidth = 0 - var limit = max - wcwidth(truncationChar) - while (i < word.length) { - var w = wcwidth(word.charAt(i)) - if(w + wwidth > limit) - break - wwidth += w - ++i + var remainder = '' + + var truncationWidth = wcwidth(truncationChar) + + while (remainder || words.length) { + if (remainder) { + var word = remainder + remainder = '' + } else { + var word = words.shift() } - var remainder = word.slice(i) // get remainder - words.unshift(remainder) // save remainder for next loop + if (wcwidth(word) > max) { + // slice is based on length no wcwidth + var i = 0 + var wwidth = 0 + var limit = max - truncationWidth + while (i < word.length) { + var w = wcwidth(word.charAt(i)) + if (w + wwidth > limit) { + break + } + wwidth += w + ++i + } + + remainder = word.slice(i) // get remainder + // save remainder for next loop - word = word.slice(0, i) // grab truncated word - word += truncationChar // add trailing … or whatever + word = word.slice(0, i) // grab truncated word + word += truncationChar // add trailing … or whatever + } + result.push(word) } - result.push(word) - return splitLongWords(words.join(' '), max, truncationChar, result) + + return result.join(' ') } |