columnify
Advanced tools
Comparing version 1.1.0 to 1.2.0-alpha
11
index.js
@@ -6,2 +6,3 @@ "use strict" | ||
var padRight = utils.padRight | ||
var padCenter = utils.padCenter | ||
var padLeft = utils.padLeft | ||
@@ -19,2 +20,3 @@ var splitIntoLines = utils.splitIntoLines | ||
preserveNewLines: false, | ||
paddingChr: ' ', | ||
headingTransform: function(key) { | ||
@@ -161,3 +163,3 @@ return key.toUpperCase() | ||
var rows = createRows(items, columns, columnNames) // merge lines into rows | ||
var rows = createRows(items, columns, columnNames, options.paddingChr) // merge lines into rows | ||
// conceive output | ||
@@ -182,3 +184,3 @@ return rows.reduce(function(output, row) { | ||
function createRows(items, columns, columnNames) { | ||
function createRows(items, columns, columnNames, paddingChr) { | ||
return items.map(function(item) { | ||
@@ -196,4 +198,5 @@ var row = [] | ||
var val = item[columnName][i] || '' // || '' ensures empty columns get padded | ||
if (column.align == 'right') row[i].push(padLeft(val, column.width)) | ||
else row[i].push(padRight(val, column.width)) | ||
if (column.align == 'right') row[i].push(padLeft(val, column.width, paddingChr)) | ||
else if (column.align == 'center') row[i].push(padCenter(val, column.width, paddingChr)) | ||
else row[i].push(padRight(val, column.width, paddingChr)) | ||
}) | ||
@@ -200,0 +203,0 @@ } |
{ | ||
"name": "columnify", | ||
"version": "1.1.0", | ||
"version": "1.2.0-alpha", | ||
"description": "Render data in text columns, supports in-column text-wrap.", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"strip-ansi": "^0.2.1", | ||
"strip-ansi": "^1.0.0", | ||
"wcwidth.js": "~0.0.4" | ||
@@ -37,0 +37,0 @@ }, |
42
utils.js
@@ -6,2 +6,13 @@ "use strict" | ||
/** | ||
* 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`. | ||
@@ -19,9 +30,28 @@ * If `str` is longer than `max`, padRight will return `str` unaltered. | ||
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) | ||
} | ||
/** | ||
* Pad `str` up to total length `max` with `chr`, on the left. | ||
@@ -39,5 +69,5 @@ * If `str` is longer than `max`, padRight will return `str` unaltered. | ||
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 | ||
} | ||
@@ -149,2 +179,3 @@ | ||
module.exports.padRight = padRight | ||
module.exports.padCenter = padCenter | ||
module.exports.padLeft = padLeft | ||
@@ -154,2 +185,1 @@ module.exports.splitIntoLines = splitIntoLines | ||
module.exports.truncateString = truncateString | ||
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22300
374
1
+ Addedansi-regex@0.2.1(transitive)
+ Addedstrip-ansi@1.0.0(transitive)
- Removedansi-regex@0.1.0(transitive)
- Removedstrip-ansi@0.2.2(transitive)
Updatedstrip-ansi@^1.0.0