diff options
Diffstat (limited to 'deps/npm/node_modules/columnify/utils.js')
-rw-r--r-- | deps/npm/node_modules/columnify/utils.js | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/deps/npm/node_modules/columnify/utils.js b/deps/npm/node_modules/columnify/utils.js index 12c4f003c5..30682af3b1 100644 --- a/deps/npm/node_modules/columnify/utils.js +++ b/deps/npm/node_modules/columnify/utils.js @@ -3,6 +3,17 @@ 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. * @@ -15,10 +26,29 @@ var wcwidth = require('./width') function padRight(str, max, chr) { str = str != null ? str : '' str = String(str) - var length = 1 + max - wcwidth(str) + var length = max - wcwidth(str) if (length <= 0) return str - return str + Array.apply(null, {length: length}) - .join(chr || ' ') + 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) } /** @@ -34,9 +64,9 @@ function padRight(str, max, chr) { function padLeft(str, max, chr) { str = str != null ? str : '' str = String(str) - var length = 1 + max - wcwidth(str) + var length = max - wcwidth(str) if (length <= 0) return str - return Array.apply(null, {length: length}).join(chr || ' ') + str + return repeatString(chr || ' ', length) + str } /** @@ -143,8 +173,8 @@ function truncateString(str, max) { */ module.exports.padRight = padRight +module.exports.padCenter = padCenter module.exports.padLeft = padLeft module.exports.splitIntoLines = splitIntoLines module.exports.splitLongWords = splitLongWords module.exports.truncateString = truncateString - |