3 var wcwidth = require('./width')
6 * repeat string `str` up to total length of `len`
8 * @param String str string to repeat
9 * @param Number len total length of output string
12 function repeatString(str, len) {
13 return Array.apply(null, {length: len + 1}).join(str).slice(0, len)
17 * Pad `str` up to total length `max` with `chr`.
18 * If `str` is longer than `max`, padRight will return `str` unaltered.
20 * @param String str string to pad
21 * @param Number max total length of output string
22 * @param String chr optional. Character to pad with. default: ' '
23 * @return String padded str
26 function padRight(str, max, chr) {
27 str = str != null ? str : ''
29 var length = max - wcwidth(str)
30 if (length <= 0) return str
31 return str + repeatString(chr || ' ', length)
35 * Pad `str` up to total length `max` with `chr`.
36 * If `str` is longer than `max`, padCenter will return `str` unaltered.
38 * @param String str string to pad
39 * @param Number max total length of output string
40 * @param String chr optional. Character to pad with. default: ' '
41 * @return String padded str
44 function padCenter(str, max, chr) {
45 str = str != null ? str : ''
47 var length = max - wcwidth(str)
48 if (length <= 0) return str
49 var lengthLeft = Math.floor(length/2)
50 var lengthRight = length - lengthLeft
51 return repeatString(chr || ' ', lengthLeft) + str + repeatString(chr || ' ', lengthRight)
55 * Pad `str` up to total length `max` with `chr`, on the left.
56 * If `str` is longer than `max`, padRight will return `str` unaltered.
58 * @param String str string to pad
59 * @param Number max total length of output string
60 * @param String chr optional. Character to pad with. default: ' '
61 * @return String padded str
64 function padLeft(str, max, chr) {
65 str = str != null ? str : ''
67 var length = max - wcwidth(str)
68 if (length <= 0) return str
69 return repeatString(chr || ' ', length) + str
73 * Split a String `str` into lines of maxiumum length `max`.
74 * Splits on word boundaries. Preserves existing new lines.
76 * @param String str string to split
77 * @param Number max length of each line
78 * @return Array Array containing lines.
81 function splitIntoLines(str, max) {
82 function _splitIntoLines(str, max) {
83 return str.trim().split(' ').reduce(function(lines, word) {
84 var line = lines[lines.length - 1]
85 if (line && wcwidth(line.join(' ')) + wcwidth(word) < max) {
86 lines[lines.length - 1].push(word) // add to line
88 else lines.push([word]) // new line
90 }, []).map(function(l) {
94 return str.split('\n').map(function(str) {
95 return _splitIntoLines(str, max)
96 }).reduce(function(lines, line) {
97 return lines.concat(line)
102 * Add spaces and `truncationChar` between words of
103 * `str` which are longer than `max`.
105 * @param String str string to split
106 * @param Number max length of each line
107 * @param Number truncationChar character to append to split words
111 function splitLongWords(str, max, truncationChar) {
114 var words = str.split(' ')
117 var truncationWidth = wcwidth(truncationChar)
119 while (remainder || words.length) {
124 var word = words.shift()
127 if (wcwidth(word) > max) {
128 // slice is based on length no wcwidth
131 var limit = max - truncationWidth
132 while (i < word.length) {
133 var w = wcwidth(word.charAt(i))
134 if (w + wwidth > limit) {
141 remainder = word.slice(i) // get remainder
142 // save remainder for next loop
144 word = word.slice(0, i) // grab truncated word
145 word += truncationChar // add trailing … or whatever
150 return result.join(' ')
155 * Truncate `str` into total width `max`
156 * If `str` is shorter than `max`, will return `str` unaltered.
158 * @param String str string to truncated
159 * @param Number max total wcwidth of output string
160 * @return String truncated str
163 function truncateString(str, max) {
165 str = str != null ? str : ''
168 if(max == Infinity) return str
172 while (i < str.length) {
173 var w = wcwidth(str.charAt(i))
179 return str.slice(0, i)
188 module.exports.padRight = padRight
189 module.exports.padCenter = padCenter
190 module.exports.padLeft = padLeft
191 module.exports.splitIntoLines = splitIntoLines
192 module.exports.splitLongWords = splitLongWords
193 module.exports.truncateString = truncateString