column-layout
Advanced tools
Comparing version 1.2.0 to 1.3.0
'use strict'; | ||
require('core-js/es6/array'); | ||
require('core-js/es6/weak-map'); | ||
require('core-js/es6/map'); | ||
require('core-js/es7/string'); | ||
var Table = require('./table'); | ||
@@ -5,0 +8,0 @@ var Columns = require('./columns'); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -14,2 +14,3 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var Padding = require('./padding'); | ||
var arrayify = require('array-back'); | ||
@@ -31,7 +32,7 @@ var _viewWidth = new WeakMap(); | ||
value: function totalWidth() { | ||
return this.map(function (col) { | ||
return this.length ? this.map(function (col) { | ||
return col.generatedWidth; | ||
}).reduce(function (a, b) { | ||
return a + b; | ||
}); | ||
}) : 0; | ||
} | ||
@@ -71,9 +72,3 @@ }, { | ||
value: function load(columns) { | ||
var _this = this; | ||
if (columns) { | ||
columns.forEach(function (column) { | ||
_this.push(column instanceof Column ? column : new Column(column)); | ||
}); | ||
} | ||
arrayify(columns).forEach(this.add.bind(this)); | ||
} | ||
@@ -83,3 +78,3 @@ }, { | ||
value: function add(column) { | ||
var col = new Column(column); | ||
var col = column instanceof Column ? column : new Column(column); | ||
this.push(col); | ||
@@ -91,3 +86,3 @@ return col; | ||
value: function autoSize() { | ||
var _this2 = this; | ||
var _this = this; | ||
@@ -97,5 +92,16 @@ var viewWidth = _viewWidth.get(this); | ||
this.forEach(function (column) { | ||
return column.generateWidth(); | ||
column.generateWidth(); | ||
column.generateMinWidth(); | ||
}); | ||
this.forEach(function (column) { | ||
if (t.isDefined(column.maxWidth) && column.generatedWidth > column.maxWidth) { | ||
column.generatedWidth = column.maxWidth; | ||
} | ||
if (t.isDefined(column.minWidth) && column.generatedWidth < column.minWidth) { | ||
column.generatedWidth = column.minWidth; | ||
} | ||
}); | ||
var width = { | ||
@@ -106,3 +112,3 @@ total: this.totalWidth(), | ||
totalFixed: this.totalFixedWidth(), | ||
totalResizable: viewWidth - this.totalFixedWidth() | ||
totalResizable: Math.max(viewWidth - this.totalFixedWidth(), 0) | ||
}; | ||
@@ -116,3 +122,3 @@ | ||
(function () { | ||
var resizableColumns = _this2.getResizable(); | ||
var resizableColumns = _this.getResizable(); | ||
resizableColumns.forEach(function (column) { | ||
@@ -122,6 +128,6 @@ column.generatedWidth = Math.floor(width.totalResizable / resizableColumns.length); | ||
grownColumns = _this2.filter(function (column) { | ||
grownColumns = _this.filter(function (column) { | ||
return column.generatedWidth > column.contentWidth; | ||
}); | ||
shrunkenColumns = _this2.filter(function (column) { | ||
shrunkenColumns = _this.filter(function (column) { | ||
return column.generatedWidth < column.contentWidth; | ||
@@ -139,10 +145,6 @@ }); | ||
}); | ||
_this2.forEach(function (column) { | ||
if (t.isDefined(column.maxWidth) && column.generatedWidth > column.maxWidth) { | ||
column.generatedWidth = column.maxWidth; | ||
} | ||
}); | ||
})(); | ||
} | ||
return this; | ||
} | ||
@@ -166,4 +168,6 @@ }, { | ||
if (t.isDefined(column.name)) this.name = column.name; | ||
if (t.isDefined(column.width)) this.width = column.width; | ||
if (t.isDefined(column.maxWidth)) this.maxWidth = column.maxWidth; | ||
if (t.isDefined(column.minWidth)) this.minWidth = column.minWidth; | ||
if (t.isDefined(column.nowrap)) this.nowrap = column.nowrap; | ||
@@ -173,2 +177,3 @@ if (t.isDefined(column['break'])) this['break'] = column['break']; | ||
if (t.isDefined(column.contentWidth)) this.contentWidth = column.contentWidth; | ||
if (t.isDefined(column.minContentWidth)) this.minContentWidth = column.minContentWidth; | ||
this.padding = column.padding || { left: ' ', right: ' ' }; | ||
@@ -194,2 +199,7 @@ this.generatedWidth = null; | ||
}, { | ||
key: 'generateMinWidth', | ||
value: function generateMinWidth() { | ||
this.minWidth = this.minContentWidth + this.padding.length(); | ||
} | ||
}, { | ||
key: 'padding', | ||
@@ -205,3 +215,3 @@ set: function set(padding) { | ||
get: function get() { | ||
return this.generatedWidth - this.padding.length(); | ||
return Math.max(this.generatedWidth - this.padding.length(), 0); | ||
} | ||
@@ -208,0 +218,0 @@ }]); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -14,2 +14,5 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var ansi = require('./ansi'); | ||
var arrayify = require('array-back'); | ||
var wrap = require('wordwrapjs'); | ||
var Cell = require('./cell'); | ||
@@ -19,7 +22,7 @@ var Rows = (function (_Array) { | ||
function Rows(rows) { | ||
function Rows(rows, columns) { | ||
_classCallCheck(this, Rows); | ||
_get(Object.getPrototypeOf(Rows.prototype), 'constructor', this).call(this); | ||
this.load(rows); | ||
this.load(rows, columns); | ||
} | ||
@@ -29,36 +32,32 @@ | ||
key: 'load', | ||
value: function load(rows) { | ||
value: function load(rows, columns) { | ||
var _this = this; | ||
rows.forEach(function (row) { | ||
return _this.push(row); | ||
arrayify(rows).forEach(function (row) { | ||
return _this.push(new Map(objectToIterable(row, columns))); | ||
}); | ||
} | ||
}, { | ||
}], [{ | ||
key: 'getColumns', | ||
value: function getColumns() { | ||
value: function getColumns(rows) { | ||
var columns = new Columns(); | ||
this.forEach(function (row) { | ||
var _loop = function (prop) { | ||
var column = columns.find(function (column) { | ||
return column.name === prop; | ||
}); | ||
arrayify(rows).forEach(function (row) { | ||
for (var columnName in row) { | ||
var column = columns.get(columnName); | ||
if (!column) { | ||
column = columns.add({ name: prop, contentWidth: 0 }); | ||
column = columns.add({ name: columnName, contentWidth: 0, minContentWidth: 0 }); | ||
} | ||
var cellValue = row[prop]; | ||
if (cellValue === undefined) { | ||
cellValue = ''; | ||
} else if (ansi.has(cellValue)) { | ||
var cell = new Cell(row[columnName], column); | ||
var cellValue = cell.value; | ||
if (ansi.has(cellValue)) { | ||
cellValue = ansi.remove(cellValue); | ||
} else { | ||
cellValue = String(cellValue); | ||
} | ||
if (cellValue.length > column.contentWidth) column.contentWidth = cellValue.length; | ||
if (!column.contentWrappable) column.contentWrappable = /\s+/.test(cellValue); | ||
}; | ||
for (var prop in row) { | ||
_loop(prop); | ||
var longestWord = getLongestWord(cellValue); | ||
if (longestWord > column.minContentWidth) { | ||
column.minContentWidth = longestWord; | ||
} | ||
if (!column.contentWrappable) column.contentWrappable = wrap.isWrappable(cellValue); | ||
} | ||
@@ -73,2 +72,15 @@ }); | ||
function getLongestWord(line) { | ||
var words = wrap.getWords(line); | ||
return words.reduce(function (max, word) { | ||
return Math.max(word.length, max); | ||
}, 0); | ||
} | ||
function objectToIterable(row, columns) { | ||
return columns.map(function (column) { | ||
return [column, new Cell(row[column.name], column)]; | ||
}); | ||
} | ||
module.exports = Rows; |
117
es5/table.js
@@ -8,3 +8,2 @@ 'use strict'; | ||
var wrap = require('wordwrapjs'); | ||
var s = require('string-tools'); | ||
var t = require('typical'); | ||
@@ -14,70 +13,74 @@ var os = require('os'); | ||
var ansi = require('./ansi'); | ||
var extend = require('deep-extend'); | ||
var _options = new WeakMap(); | ||
var Table = (function () { | ||
function Table(data, options) { | ||
var _this = this; | ||
_classCallCheck(this, Table); | ||
options = options || {}; | ||
if (!options.padding) options.padding = {}; | ||
options.padding.left = t.isDefined(options.padding.left) ? options.padding.left : ' '; | ||
options.padding.right = t.isDefined(options.padding.right) ? options.padding.right : ' '; | ||
options.viewWidth = t.isDefined(options.viewWidth) ? options.viewWidth : process && process.stdout.columns || 80; | ||
options.columns = options.columns || []; | ||
var defaults = { | ||
padding: { | ||
left: ' ', | ||
right: ' ' | ||
}, | ||
viewWidth: process && process.stdout.columns || 80, | ||
columns: [] | ||
}; | ||
_options.set(this, extend(defaults, options)); | ||
this.rows = new Rows(data); | ||
this.columns = this.rows.getColumns(); | ||
this.load(data); | ||
} | ||
this.columns.viewWidth = options.viewWidth; | ||
this.columns.forEach(function (column) { | ||
if (options.padding) column.padding = options.padding; | ||
if (options.nowrap) column.nowrap = options.nowrap; | ||
if (options['break']) { | ||
column['break'] = options['break']; | ||
column.contentWrappable = true; | ||
} | ||
}); | ||
_createClass(Table, [{ | ||
key: 'load', | ||
value: function load(data) { | ||
var _this = this; | ||
options.columns.forEach(function (optionColumn) { | ||
var column = _this.columns.get(optionColumn.name); | ||
if (column) { | ||
if (optionColumn.padding) { | ||
column.padding.left = optionColumn.padding.left; | ||
column.padding.right = optionColumn.padding.right; | ||
} | ||
if (optionColumn.width) column.width = optionColumn.width; | ||
if (optionColumn.maxWidth) column.maxWidth = optionColumn.maxWidth; | ||
if (optionColumn.nowrap) column.nowrap = optionColumn.nowrap; | ||
if (optionColumn['break']) { | ||
column['break'] = optionColumn['break']; | ||
this.columns = Rows.getColumns(data); | ||
this.rows = new Rows(data, this.columns); | ||
var options = _options.get(this); | ||
this.columns.viewWidth = options.viewWidth; | ||
this.columns.forEach(function (column) { | ||
if (options.padding) column.padding = options.padding; | ||
if (options.nowrap) column.nowrap = options.nowrap; | ||
if (options['break']) { | ||
column['break'] = options['break']; | ||
column.contentWrappable = true; | ||
} | ||
} | ||
}); | ||
}); | ||
this.columns.autoSize(); | ||
} | ||
options.columns.forEach(function (optionColumn) { | ||
var column = _this.columns.get(optionColumn.name); | ||
if (column) { | ||
if (optionColumn.padding) { | ||
column.padding.left = optionColumn.padding.left; | ||
column.padding.right = optionColumn.padding.right; | ||
} | ||
if (optionColumn.width) column.width = optionColumn.width; | ||
if (optionColumn.maxWidth) column.maxWidth = optionColumn.maxWidth; | ||
if (optionColumn.minWidth) column.minWidth = optionColumn.minWidth; | ||
if (optionColumn.nowrap) column.nowrap = optionColumn.nowrap; | ||
if (optionColumn['break']) { | ||
column['break'] = optionColumn['break']; | ||
column.contentWrappable = true; | ||
} | ||
} | ||
}); | ||
_createClass(Table, [{ | ||
this.columns.autoSize(); | ||
return this; | ||
} | ||
}, { | ||
key: 'getWrapped', | ||
value: function getWrapped() { | ||
var _this2 = this; | ||
var lines = []; | ||
this.rows.forEach(function (row) { | ||
this.columns.autoSize(); | ||
return this.rows.map(function (row) { | ||
var line = []; | ||
_this2.columns.forEach(function (column) { | ||
var cell = row[column.name]; | ||
if (!t.isString(cell)) { | ||
if (!t.isDefined(cell)) { | ||
cell = ''; | ||
} else { | ||
cell = String(cell); | ||
} | ||
} | ||
row.forEach(function (cell, column) { | ||
if (column.nowrap) { | ||
line.push(cell.split(/\r\n?|\n/)); | ||
line.push(cell.value.split(/\r\n?|\n/)); | ||
} else { | ||
line.push(wrap.lines(cell, { | ||
line.push(wrap.lines(cell.value, { | ||
width: column.generatedWidth - column.padding.length(), | ||
@@ -89,5 +92,4 @@ ignore: ansi.regexp, | ||
}); | ||
lines.push(line); | ||
return line; | ||
}); | ||
return lines; | ||
} | ||
@@ -119,3 +121,3 @@ }, { | ||
value: function renderLines() { | ||
var _this3 = this; | ||
var _this2 = this; | ||
@@ -125,3 +127,3 @@ var lines = this.getLines(); | ||
return line.reduce(function (prev, cell, index) { | ||
var column = _this3.columns[index]; | ||
var column = _this2.columns[index]; | ||
return prev + padCell(cell, column.padding, column.generatedWidth); | ||
@@ -150,5 +152,6 @@ }, ''); | ||
var ansiLength = cellValue.length - ansi.remove(cellValue).length; | ||
return (padding.left || '') + s.padRight(cellValue || '', width - padding.length() + ansiLength) + (padding.right || ''); | ||
cellValue = cellValue || ''; | ||
return (padding.left || '') + cellValue.padRight(width - padding.length() + ansiLength) + (padding.right || ''); | ||
} | ||
module.exports = Table; |
'use strict' | ||
require('core-js/es6/array') | ||
require('core-js/es6/weak-map') | ||
require('core-js/es6/map') | ||
require('core-js/es7/string'); | ||
var Table = require('./table') | ||
@@ -5,0 +8,0 @@ var Columns = require('./columns') |
'use strict' | ||
var t = require('typical') | ||
var Padding = require('./padding') | ||
var arrayify = require('array-back') | ||
@@ -13,8 +14,16 @@ var _viewWidth = new WeakMap() | ||
/** | ||
* sum of all generatedWidth fields | ||
* @return {number} | ||
*/ | ||
totalWidth () { | ||
return this.map(col => col.generatedWidth).reduce((a, b) => a + b) | ||
return this.length | ||
? this.map(col => col.generatedWidth).reduce((a, b) => a + b) | ||
: 0 | ||
} | ||
totalFixedWidth () { | ||
return this.getFixed().map(col => col.generatedWidth).reduce((a, b) => a + b, 0) | ||
return this.getFixed() | ||
.map(col => col.generatedWidth) | ||
.reduce((a, b) => a + b, 0) | ||
} | ||
@@ -35,11 +44,7 @@ | ||
load (columns) { | ||
if (columns) { | ||
columns.forEach(column => { | ||
this.push(column instanceof Column ? column : new Column(column)) | ||
}) | ||
} | ||
arrayify(columns).forEach(this.add.bind(this)) | ||
} | ||
add (column) { | ||
var col = new Column(column) | ||
var col = column instanceof Column ? column : new Column(column) | ||
this.push(col) | ||
@@ -55,2 +60,3 @@ return col | ||
* sets `generatedWidth` for each column | ||
* @chainable | ||
*/ | ||
@@ -61,4 +67,18 @@ autoSize () { | ||
/* size */ | ||
this.forEach(column => column.generateWidth()) | ||
this.forEach(column => { | ||
column.generateWidth() | ||
column.generateMinWidth() | ||
}) | ||
/* adjust if user set a min or maxWidth */ | ||
this.forEach(column => { | ||
if (t.isDefined(column.maxWidth) && column.generatedWidth > column.maxWidth) { | ||
column.generatedWidth = column.maxWidth | ||
} | ||
if (t.isDefined(column.minWidth) && column.generatedWidth < column.minWidth) { | ||
column.generatedWidth = column.minWidth | ||
} | ||
}) | ||
var width = { | ||
@@ -69,5 +89,5 @@ total: this.totalWidth(), | ||
totalFixed: this.totalFixedWidth(), | ||
totalResizable: viewWidth - this.totalFixedWidth() | ||
totalResizable: Math.max(viewWidth - this.totalFixedWidth(), 0) | ||
} | ||
// console.log(width); | ||
/* adjust if short of space */ | ||
@@ -94,11 +114,9 @@ if (width.diff > 0) { | ||
/* adjust if user set a maxWidth */ | ||
this.forEach(column => { | ||
if (t.isDefined(column.maxWidth) && column.generatedWidth > column.maxWidth) { | ||
column.generatedWidth = column.maxWidth | ||
} | ||
}) | ||
/* need a minWidth to stop breakable columns getting too short */ | ||
/* need a minWidth to stop breakable columns getting too short */ | ||
/* if, after autosizing, we still don't fit within viewWidth then give up */ | ||
} | ||
// console.log(this); | ||
return this | ||
} | ||
@@ -109,7 +127,20 @@ } | ||
// setting any column property which is a factor of the width should trigger autoSize() | ||
/** | ||
* @class | ||
* @classdesc Represents a table column | ||
*/ | ||
class Column { | ||
constructor (column) { | ||
/** | ||
* @type {string} | ||
*/ | ||
if (t.isDefined(column.name)) this.name = column.name | ||
/** | ||
* @type {number} | ||
*/ | ||
if (t.isDefined(column.width)) this.width = column.width | ||
if (t.isDefined(column.maxWidth)) this.maxWidth = column.maxWidth | ||
if (t.isDefined(column.minWidth)) this.minWidth = column.minWidth | ||
if (t.isDefined(column.nowrap)) this.nowrap = column.nowrap | ||
@@ -119,2 +150,3 @@ if (t.isDefined(column.break)) this.break = column.break | ||
if (t.isDefined(column.contentWidth)) this.contentWidth = column.contentWidth | ||
if (t.isDefined(column.minContentWidth)) this.minContentWidth = column.minContentWidth | ||
this.padding = column.padding || { left: ' ', right: ' ' } | ||
@@ -126,2 +158,3 @@ this.generatedWidth = null | ||
_padding.set(this, new Padding(padding)) | ||
} | ||
@@ -133,3 +166,3 @@ get padding () { | ||
get wrappedContentWidth () { | ||
return this.generatedWidth - this.padding.length() | ||
return Math.max(this.generatedWidth - this.padding.length(), 0) | ||
} | ||
@@ -148,2 +181,6 @@ | ||
} | ||
generateMinWidth () { | ||
this.minWidth = this.minContentWidth + this.padding.length() | ||
} | ||
} | ||
@@ -150,0 +187,0 @@ |
'use strict' | ||
var Columns = require('./columns') | ||
var ansi = require('./ansi') | ||
const Columns = require('./columns') | ||
const ansi = require('./ansi') | ||
const arrayify = require('array-back') | ||
const wrap = require('wordwrapjs') | ||
const Cell = require('./cell') | ||
@@ -10,31 +13,38 @@ /** | ||
class Rows extends Array { | ||
constructor (rows) { | ||
constructor (rows, columns) { | ||
super() | ||
this.load(rows) | ||
this.load(rows, columns) | ||
} | ||
load (rows) { | ||
rows.forEach(row => this.push(row)) | ||
load (rows, columns) { | ||
// arrayify(rows).forEach(row => this.push(new Row(row, columns))) | ||
arrayify(rows).forEach(row => this.push(new Map(objectToIterable(row, columns)))) | ||
} | ||
getColumns () { | ||
/** | ||
* returns all distinct columns from input | ||
* @param {object[]} | ||
* @return {module:columns} | ||
*/ | ||
static getColumns (rows) { | ||
var columns = new Columns() | ||
this.forEach(row => { | ||
for (let prop in row) { | ||
let column = columns.find(column => column.name === prop) | ||
arrayify(rows).forEach(row => { | ||
for (let columnName in row) { | ||
let column = columns.get(columnName) | ||
if (!column) { | ||
column = columns.add({ name: prop, contentWidth: 0 }) | ||
column = columns.add({ name: columnName, contentWidth: 0, minContentWidth: 0 }) | ||
} | ||
let cellValue = row[prop] | ||
if (cellValue === undefined) { | ||
cellValue = '' | ||
} else if (ansi.has(cellValue)) { | ||
let cell = new Cell(row[columnName], column) | ||
let cellValue = cell.value | ||
if (ansi.has(cellValue)) { | ||
cellValue = ansi.remove(cellValue) | ||
} else { | ||
cellValue = String(cellValue) | ||
} | ||
if (cellValue.length > column.contentWidth) column.contentWidth = cellValue.length | ||
if (!column.contentWrappable) column.contentWrappable = /\s+/.test(cellValue) | ||
// row[prop] = cellValue | ||
let longestWord = getLongestWord(cellValue) | ||
if (longestWord > column.minContentWidth) { | ||
column.minContentWidth = longestWord | ||
} | ||
if (!column.contentWrappable) column.contentWrappable = wrap.isWrappable(cellValue) | ||
} | ||
@@ -46,2 +56,21 @@ }) | ||
// class Row extends Map { | ||
// constructor (row, columns) { | ||
// super(objectToIterable(row, columns)) | ||
// } | ||
// } | ||
function getLongestWord (line) { | ||
const words = wrap.getWords(line) | ||
return words.reduce((max, word) => { | ||
return Math.max(word.length, max) | ||
}, 0) | ||
} | ||
function objectToIterable (row, columns) { | ||
return columns.map(column => { | ||
return [ column, new Cell(row[column.name], column) ] | ||
}) | ||
} | ||
/** | ||
@@ -48,0 +77,0 @@ * @module rows |
'use strict' | ||
var wrap = require('wordwrapjs') | ||
var s = require('string-tools') | ||
var t = require('typical') | ||
var os = require('os') | ||
var Rows = require('./rows') | ||
var ansi = require('./ansi') | ||
const wrap = require('wordwrapjs') | ||
const t = require('typical') | ||
const os = require('os') | ||
const Rows = require('./rows') | ||
const ansi = require('./ansi') | ||
const extend = require('deep-extend') | ||
const _options = new WeakMap() | ||
/** | ||
@@ -16,18 +17,24 @@ * @class | ||
constructor (data, options) { | ||
// TODO: HANDLE EMPTY data [] | ||
let defaults = { | ||
padding: { | ||
left: ' ', | ||
right: ' ' | ||
}, | ||
viewWidth: (process && process.stdout.columns) || 80, | ||
columns: [] | ||
} | ||
_options.set(this, extend(defaults, options)) | ||
/* defaults */ | ||
options = options || {} | ||
if (!options.padding) options.padding = {} | ||
options.padding.left = t.isDefined(options.padding.left) ? options.padding.left : ' ' | ||
options.padding.right = t.isDefined(options.padding.right) ? options.padding.right : ' ' | ||
options.viewWidth = t.isDefined(options.viewWidth) | ||
? options.viewWidth | ||
: (process && process.stdout.columns) || 80 | ||
options.columns = options.columns || [] | ||
this.load(data) | ||
} | ||
this.rows = new Rows(data) | ||
this.columns = this.rows.getColumns() | ||
/** | ||
* @chainable | ||
*/ | ||
load (data) { | ||
this.columns = Rows.getColumns(data) | ||
this.rows = new Rows(data, this.columns) | ||
let options = _options.get(this) | ||
/* load values from options */ | ||
/* load default column properties from options */ | ||
this.columns.viewWidth = options.viewWidth | ||
@@ -43,3 +50,3 @@ this.columns.forEach(column => { | ||
/* load values from options.columns */ | ||
/* load column properties from options.columns */ | ||
options.columns.forEach(optionColumn => { | ||
@@ -54,2 +61,3 @@ let column = this.columns.get(optionColumn.name) | ||
if (optionColumn.maxWidth) column.maxWidth = optionColumn.maxWidth | ||
if (optionColumn.minWidth) column.minWidth = optionColumn.minWidth | ||
if (optionColumn.nowrap) column.nowrap = optionColumn.nowrap | ||
@@ -64,22 +72,14 @@ if (optionColumn.break) { | ||
this.columns.autoSize() | ||
// console.log(this.columns); | ||
return this | ||
} | ||
getWrapped () { | ||
var lines = [] | ||
this.rows.forEach(row => { | ||
this.columns.autoSize() | ||
return this.rows.map(row => { | ||
let line = [] | ||
this.columns.forEach(column => { | ||
let cell = row[column.name] | ||
if (!t.isString(cell)) { | ||
if (!t.isDefined(cell)) { | ||
cell = '' | ||
} else { | ||
cell = String(cell) | ||
} | ||
} | ||
row.forEach((cell, column) => { | ||
if (column.nowrap) { | ||
line.push(cell.split(/\r\n?|\n/)) | ||
line.push(cell.value.split(/\r\n?|\n/)) | ||
} else { | ||
line.push(wrap.lines(cell, { | ||
line.push(wrap.lines(cell.value, { | ||
width: column.generatedWidth - column.padding.length(), | ||
@@ -91,5 +91,4 @@ ignore: ansi.regexp, | ||
}) | ||
lines.push(line) | ||
return line | ||
}) | ||
return lines | ||
} | ||
@@ -139,4 +138,5 @@ | ||
var ansiLength = cellValue.length - ansi.remove(cellValue).length | ||
cellValue = cellValue || '' | ||
return (padding.left || '') + | ||
s.padRight(cellValue || '', width - padding.length() + ansiLength) + | ||
cellValue.padRight(width - padding.length() + ansiLength) + | ||
(padding.right || '') | ||
@@ -143,0 +143,0 @@ } |
{ | ||
"name": "column-layout", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Pretty-print JSON data in columns.", | ||
@@ -25,3 +25,3 @@ "repository": "https://github.com/75lb/column-layout.git", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo", | ||
"es5": "babel --no-comments lib --out-dir es5", | ||
"es5": "babel --presets es2015 --no-comments lib --out-dir es5", | ||
"cover": "istanbul cover tape -- test/*.js && cat coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf coverage; echo" | ||
@@ -31,9 +31,9 @@ }, | ||
"ansi-escape-sequences": "^2.1.0", | ||
"array-tools": "^2.0.0", | ||
"array-back": "^1.0.2", | ||
"collect-json": "^1", | ||
"command-line-args": "^2", | ||
"core-js": "^1.2.2", | ||
"deep-extend": "~0.4.0", | ||
"feature-detect-es6": "^1.0.0", | ||
"object-tools": "^2", | ||
"string-tools": "^1", | ||
"typical": "^2.2.0", | ||
@@ -43,6 +43,12 @@ "wordwrapjs": "^1.1.1" | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.0.15", | ||
"coveralls": "^2.11.4", | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"tape": "^4.0.0" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"es5" | ||
] | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94345
42
2052
4
+ Addedarray-back@^1.0.2
+ Addeddeep-extend@~0.4.0
- Removedarray-tools@^2.0.0
- Removedstring-tools@^1
- Removedarray-tools@2.0.9(transitive)
- Removedfilter-where@1.0.1(transitive)
- Removedreduce-extract@1.0.0(transitive)
- Removedreduce-flatten@1.0.1(transitive)
- Removedreduce-unique@1.0.0(transitive)
- Removedreduce-without@1.0.1(transitive)
- Removedsort-array@1.1.2(transitive)
- Removedstring-tools@1.0.0(transitive)