columnify
Advanced tools
Comparing version 1.0.0 to 1.1.0
"use strict" | ||
var wcwidth = require('wcwidth.js')({ monkeypatch: false, control: 0 }) | ||
var wcwidth = require('./width') | ||
var utils = require('./utils') | ||
var padRight = utils.padRight | ||
var padLeft = utils.padLeft | ||
var splitIntoLines = utils.splitIntoLines | ||
@@ -191,3 +192,4 @@ var splitLongWords = utils.splitLongWords | ||
var val = item[columnName][i] || '' // || '' ensures empty columns get padded | ||
row[i].push(padRight(val, column.width)) | ||
if (column.align == 'right') row[i].push(padLeft(val, column.width)) | ||
else row[i].push(padRight(val, column.width)) | ||
}) | ||
@@ -194,0 +196,0 @@ } |
{ | ||
"name": "columnify", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Render data in text columns, supports in-column text-wrap.", | ||
@@ -12,2 +12,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chalk": "^0.4.0", | ||
"faucet": "0.0.1", | ||
@@ -34,2 +35,3 @@ "tape": "~2.12.3" | ||
"dependencies": { | ||
"strip-ansi": "^0.2.1", | ||
"wcwidth.js": "~0.0.4" | ||
@@ -36,0 +38,0 @@ }, |
@@ -194,2 +194,22 @@ # columnify | ||
### Align Right | ||
```js | ||
var data = { | ||
"mocha@1.18.2": 1, | ||
"commander@2.0.0": 1, | ||
"debug@0.8.1": 1 | ||
} | ||
columnify(data, {config: {value: {align: 'right'}}}) | ||
``` | ||
#### Output: | ||
``` | ||
KEY VALUE | ||
mocha@1.18.2 1 | ||
commander@2.0.0 1 | ||
debug@0.8.1 1 | ||
``` | ||
### Preserve existing newlines | ||
@@ -196,0 +216,0 @@ |
21
utils.js
"use strict" | ||
var wcwidth = require('wcwidth.js')({ monkeypatch: false, control: 0 }) | ||
var wcwidth = require('./width') | ||
@@ -25,2 +25,20 @@ /** | ||
/** | ||
* 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`. | ||
@@ -129,2 +147,3 @@ * Splits on word boundaries. Preserves existing new lines. | ||
module.exports.padRight = padRight | ||
module.exports.padLeft = padLeft | ||
module.exports.splitIntoLines = splitIntoLines | ||
@@ -131,0 +150,0 @@ module.exports.splitLongWords = splitLongWords |
21203
6
344
349
2
3
+ Addedstrip-ansi@^0.2.1
+ Addedansi-regex@0.1.0(transitive)
+ Addedstrip-ansi@0.2.2(transitive)