-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils.js
193 lines (164 loc) · 4.75 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict"
var wcwidth = require('./width')
/**
* repeat string `str` up to total length of `len`
*
* @param String str string to repeat
* @param Number len total length of output string
*/
function repeatString(str, len) {
return Array.apply(null, {length: len + 1}).join(str).slice(0, len)
}
/**
* Pad `str` up to total length `max` with `chr`.
* If `str` is longer than `max`, padRight will return `str` unaltered.
*
* @param String str string to pad
* @param Number max total length of output string
* @param String chr optional. Character to pad with. default: ' '
* @return String padded str
*/
function padRight(str, max, chr) {
str = str != null ? str : ''
str = String(str)
var length = max - wcwidth(str)
if (length <= 0) return str
return str + repeatString(chr || ' ', length)
}
/**
* Pad `str` up to total length `max` with `chr`.
* If `str` is longer than `max`, padCenter will return `str` unaltered.
*
* @param String str string to pad
* @param Number max total length of output string
* @param String chr optional. Character to pad with. default: ' '
* @return String padded str
*/
function padCenter(str, max, chr) {
str = str != null ? str : ''
str = String(str)
var length = max - wcwidth(str)
if (length <= 0) return str
var lengthLeft = Math.floor(length/2)
var lengthRight = length - lengthLeft
return repeatString(chr || ' ', lengthLeft) + str + repeatString(chr || ' ', lengthRight)
}
/**
* Pad `str` up to total length `max` with `chr`, on the left.
* If `str` is longer than `max`, padRight will return `str` unaltered.
*
* @param String str string to pad
* @param Number max total length of output string
* @param String chr optional. Character to pad with. default: ' '
* @return String padded str
*/
function padLeft(str, max, chr) {
str = str != null ? str : ''
str = String(str)
var length = max - wcwidth(str)
if (length <= 0) return str
return repeatString(chr || ' ', length) + str
}
/**
* Split a String `str` into lines of maxiumum length `max`.
* Splits on word boundaries. Preserves existing new lines.
*
* @param String str string to split
* @param Number max length of each line
* @return Array Array containing lines.
*/
function splitIntoLines(str, max) {
function _splitIntoLines(str, max) {
return str.trim().split(' ').reduce(function(lines, word) {
var line = lines[lines.length - 1]
if (line && wcwidth(line.join(' ')) + wcwidth(word) < max) {
lines[lines.length - 1].push(word) // add to line
}
else lines.push([word]) // new line
return lines
}, []).map(function(l) {
return l.join(' ')
})
}
return str.split('\n').map(function(str) {
return _splitIntoLines(str, max)
}).reduce(function(lines, line) {
return lines.concat(line)
}, [])
}
/**
* Add spaces and `truncationChar` between words of
* `str` which are longer than `max`.
*
* @param String str string to split
* @param Number max length of each line
* @param Number truncationChar character to append to split words
* @return String
*/
function splitLongWords(str, max, truncationChar) {
str = str.trim()
var result = []
var words = str.split(' ')
var remainder = ''
var truncationWidth = wcwidth(truncationChar)
while (remainder || words.length) {
if (remainder) {
var word = remainder
remainder = ''
} else {
var word = words.shift()
}
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
}
result.push(word)
}
return result.join(' ')
}
/**
* Truncate `str` into total width `max`
* If `str` is shorter than `max`, will return `str` unaltered.
*
* @param String str string to truncated
* @param Number max total wcwidth of output string
* @return String truncated str
*/
function truncateString(str, max) {
str = str != null ? str : ''
str = String(str)
if(max == Infinity) return str
var i = 0
var wwidth = 0
while (i < str.length) {
var w = wcwidth(str.charAt(i))
if(w + wwidth > max)
break
wwidth += w
++i
}
return str.slice(0, i)
}
/**
* Exports
*/
module.exports.padRight = padRight
module.exports.padCenter = padCenter
module.exports.padLeft = padLeft
module.exports.splitIntoLines = splitIntoLines
module.exports.splitLongWords = splitLongWords
module.exports.truncateString = truncateString