diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-12-12 11:21:47 +0300 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-12-12 11:21:47 +0300 |
commit | c7ad7279cc879930ec58ccc62fa642e621ecb65c (patch) | |
tree | 1b380572eb21447ec1f9b45d7324eb071e6910db /node_modules/columnify/index.js | |
parent | b6d6acfc02c8887f78067931babab8f7c5180fed (diff) |
columnify@1.3.2
Various improvements and dependency upgrades.
Diffstat (limited to 'node_modules/columnify/index.js')
-rw-r--r-- | node_modules/columnify/index.js | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/node_modules/columnify/index.js b/node_modules/columnify/index.js index c44b8ca8f..8c15c993e 100644 --- a/node_modules/columnify/index.js +++ b/node_modules/columnify/index.js @@ -9,6 +9,14 @@ var splitIntoLines = utils.splitIntoLines var splitLongWords = utils.splitLongWords var truncateString = utils.truncateString +var DEFAULT_HEADING_TRANSFORM = function(key) { + return key.toUpperCase() +} + +var DEFAULT_DATA_TRANSFORM = function(cell, column, index) { + return cell +} + var DEFAULTS = { maxWidth: Infinity, minWidth: 0, @@ -18,12 +26,8 @@ var DEFAULTS = { preserveNewLines: false, paddingChr: ' ', showHeaders: true, - headingTransform: function(key) { - return key.toUpperCase() - }, - dataTransform: function(cell, column, index) { - return cell - } + headingTransform: DEFAULT_HEADING_TRANSFORM, + dataTransform: DEFAULT_DATA_TRANSFORM } module.exports = function(items, options) { @@ -68,9 +72,11 @@ module.exports = function(items, options) { // sanitize column settings columnNames.forEach(function(columnName) { var column = columns[columnName] + column.name = columnName column.maxWidth = Math.ceil(column.maxWidth) column.minWidth = Math.ceil(column.minWidth) column.truncate = !!column.truncate + column.align = column.align || 'left' }) // sanitize data @@ -96,7 +102,18 @@ module.exports = function(items, options) { columnNames.forEach(function(columnName) { var column = columns[columnName] items = items.map(function(item, index) { - item[columnName] = column.dataTransform(item[columnName], column, index) + var col = Object.create(column) + item[columnName] = column.dataTransform(item[columnName], col, index) + + var changedKeys = Object.keys(col) + // disable default heading transform if we wrote to column.name + if (changedKeys.indexOf('name') !== -1) { + if (column.headingTransform !== DEFAULT_HEADING_TRANSFORM) return + column.headingTransform = function(heading) {return heading} + } + changedKeys.forEach(function(key) { + column[key] = col[key] + }) return item }) }) @@ -106,7 +123,7 @@ module.exports = function(items, options) { if(options.showHeaders) { columnNames.forEach(function(columnName) { var column = columns[columnName] - headers[columnName] = column.headingTransform(columnName) + headers[columnName] = column.headingTransform(column.name) }) items.unshift(headers) } @@ -194,8 +211,8 @@ function createRows(items, columns, columnNames, paddingChr) { columnNames.forEach(function(columnName) { var column = columns[columnName] var val = item[columnName][i] || '' // || '' ensures empty columns get padded - 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)) + if (column.align === 'right') row[i].push(padLeft(val, column.width, paddingChr)) + else if (column.align === 'center' || column.align === 'centre') row[i].push(padCenter(val, column.width, paddingChr)) else row[i].push(padRight(val, column.width, paddingChr)) }) } |