diff options
author | Timothy J Fontaine <tjfontaine@gmail.com> | 2014-01-22 08:42:05 -0800 |
---|---|---|
committer | Timothy J Fontaine <tjfontaine@gmail.com> | 2014-01-22 08:42:05 -0800 |
commit | fdc3cc9d7a2d1e12c74fc0c0939d22f40e58b52b (patch) | |
tree | 0e73ef7de9054f7d9b2fe90a5aec9ad072716865 /deps/npm/node_modules/columnify/utils.js | |
parent | 6514a4128c3e7aa6c1bb1c6f3f018412fa76e5d9 (diff) | |
parent | 25f9e92813eda927c3d3eaa56dab3a397afadebe (diff) | |
download | node-merge-test.tar.gz |
Merge remote-tracking branch 'upstream/v0.10'merge-test
Conflicts:
node.gyp
Diffstat (limited to 'deps/npm/node_modules/columnify/utils.js')
-rw-r--r-- | deps/npm/node_modules/columnify/utils.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/deps/npm/node_modules/columnify/utils.js b/deps/npm/node_modules/columnify/utils.js new file mode 100644 index 000000000..bd7641da4 --- /dev/null +++ b/deps/npm/node_modules/columnify/utils.js @@ -0,0 +1,76 @@ +/** + * 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 = 1 + max - str.length + if (length <= 0) return str + return str + Array.apply(null, {length: length}) + .join(chr || ' ') +} + +/** + * Split a String `str` into lines of maxiumum length `max`. + * Splits on word boundaries. + * + * @param String str string to split + * @param Number max length of each line + * @return Array Array containing lines. + */ + +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(' ') + }) +} + +/** + * 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, result) { + str = str.trim() + result = result || [] + if (!str) return result.join(' ') || '' + var words = str.split(' ') + var word = words.shift() || str + + if (word.length > max) { + var remainder = word.slice(max - truncationChar.length) // get remainder + words.unshift(remainder) // save remainder for next loop + + word = word.slice(0, max - truncationChar.length) // grab truncated word + word += truncationChar // add trailing … or whatever + } + result.push(word) + return splitLongWords(words.join(' '), max, truncationChar, result) +} + +/** + * Exports + */ + +module.exports.padRight = padRight +module.exports.splitIntoLines = splitIntoLines +module.exports.splitLongWords = splitLongWords |