summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/columnify/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/columnify/utils.js')
-rw-r--r--deps/npm/node_modules/columnify/utils.js104
1 files changed, 89 insertions, 15 deletions
diff --git a/deps/npm/node_modules/columnify/utils.js b/deps/npm/node_modules/columnify/utils.js
index bd7641da42..12c4f003c5 100644
--- a/deps/npm/node_modules/columnify/utils.js
+++ b/deps/npm/node_modules/columnify/utils.js
@@ -1,3 +1,7 @@
+"use strict"
+
+var wcwidth = require('./width')
+
/**
* Pad `str` up to total length `max` with `chr`.
* If `str` is longer than `max`, padRight will return `str` unaltered.
@@ -11,15 +15,33 @@
function padRight(str, max, chr) {
str = str != null ? str : ''
str = String(str)
- var length = 1 + max - str.length
+ var length = 1 + max - wcwidth(str)
if (length <= 0) return str
return str + Array.apply(null, {length: length})
.join(chr || ' ')
}
/**
+ * 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 = 1 + max - wcwidth(str)
+ if (length <= 0) return str
+ return Array.apply(null, {length: length}).join(chr || ' ') + str
+}
+
+/**
* Split a String `str` into lines of maxiumum length `max`.
- * Splits on word boundaries.
+ * Splits on word boundaries. Preserves existing new lines.
*
* @param String str string to split
* @param Number max length of each line
@@ -27,16 +49,23 @@ function padRight(str, max, chr) {
*/
function splitIntoLines(str, max) {
- return str.trim().split(' ').reduce(function(lines, word) {
- var line = lines[lines.length - 1]
- if (line && line.join(' ').length + word.length < max) {
- lines[lines.length - 1].push(word) // add to line
- }
- else lines.push([word]) // new line
- return lines
- }, []).map(function(l) {
- return l.join(' ')
- })
+ 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)
+ }, [])
}
/**
@@ -55,22 +84,67 @@ function splitLongWords(str, max, truncationChar, result) {
if (!str) return result.join(' ') || ''
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
+ }
- if (word.length > max) {
- var remainder = word.slice(max - truncationChar.length) // get remainder
+ var remainder = word.slice(i) // get remainder
words.unshift(remainder) // save remainder for next loop
- word = word.slice(0, max - truncationChar.length) // grab truncated word
+ word = word.slice(0, i) // grab truncated word
word += truncationChar // add trailing … or whatever
}
result.push(word)
return splitLongWords(words.join(' '), max, truncationChar, result)
}
+
+/**
+ * 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.padLeft = padLeft
module.exports.splitIntoLines = splitIntoLines
module.exports.splitLongWords = splitLongWords
+module.exports.truncateString = truncateString
+