Comparing version 2.1.2 to 3.0.1
@@ -31,5 +31,5 @@ 'use strict'; | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorder~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -50,12 +50,12 @@ exports.drawBorder = drawBorder = function (columnSizeIndex, parts) { | ||
* @typedef drawBorderTop~parts | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderTop~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -73,12 +73,12 @@ exports.drawBorderTop = drawBorderTop = function (columnSizeIndex, parts) { | ||
* @typedef drawBorderJoin~parts | ||
* @property {String} joinLeft | ||
* @property {String} joinRight | ||
* @property {String} joinBody | ||
* @property {String} joinJoin | ||
* @property {string} joinLeft | ||
* @property {string} joinRight | ||
* @property {string} joinBody | ||
* @property {string} joinJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderJoin~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -96,12 +96,12 @@ exports.drawBorderJoin = drawBorderJoin = function (columnSizeIndex, parts) { | ||
* @typedef drawBorderBottom~parts | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderBottom~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -121,2 +121,2 @@ exports.drawBorderBottom = drawBorderBottom = function (columnSizeIndex, parts) { | ||
exports.drawBorderBottom = drawBorderBottom; | ||
//# sourceMappingURL=drawBorder.js.map | ||
//# sourceMappingURL=drawBorder.js.map |
@@ -24,2 +24,2 @@ /** | ||
module.exports = exports["default"]; | ||
//# sourceMappingURL=drawRow.js.map | ||
//# sourceMappingURL=drawRow.js.map |
@@ -60,2 +60,2 @@ 'use strict'; | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=drawTable.js.map | ||
//# sourceMappingURL=drawTable.js.map |
@@ -9,6 +9,2 @@ 'use strict'; | ||
var _border = require('./border/'); | ||
var _border2 = _interopRequireDefault(_border); | ||
var _table = require('./table'); | ||
@@ -18,4 +14,8 @@ | ||
exports.border = _border2['default']; | ||
var _getBorderCharacters = require('./getBorderCharacters'); | ||
var _getBorderCharacters2 = _interopRequireDefault(_getBorderCharacters); | ||
exports.getBorderCharacters = _getBorderCharacters2['default']; | ||
exports['default'] = _table2['default']; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
'use strict'; | ||
var _lodashLangCloneDeep2 = require('lodash/lang/cloneDeep'); | ||
var _lodashLangCloneDeep3 = _interopRequireDefault(_lodashLangCloneDeep2); | ||
var _lodashObjectAssign2 = require('lodash/object/assign'); | ||
@@ -19,5 +15,5 @@ | ||
var _lodashObjectMapValues2 = require('lodash/object/mapValues'); | ||
var _lodashLangCloneDeep2 = require('lodash/lang/cloneDeep'); | ||
var _lodashObjectMapValues3 = _interopRequireDefault(_lodashObjectMapValues2); | ||
var _lodashLangCloneDeep3 = _interopRequireDefault(_lodashLangCloneDeep2); | ||
@@ -28,5 +24,5 @@ Object.defineProperty(exports, '__esModule', { | ||
var _border = require('./border/'); | ||
var _getBorderCharacters = require('./getBorderCharacters'); | ||
var _border2 = _interopRequireDefault(_border); | ||
var _getBorderCharacters2 = _interopRequireDefault(_getBorderCharacters); | ||
@@ -37,68 +33,84 @@ var _validateConfig = require('./validateConfig'); | ||
var _calculateMaximumColumnValueIndex = require('./calculateMaximumColumnValueIndex'); | ||
var _calculateMaximumColumnWidthIndex = require('./calculateMaximumColumnWidthIndex'); | ||
var _calculateMaximumColumnValueIndex2 = _interopRequireDefault(_calculateMaximumColumnValueIndex); | ||
var _calculateMaximumColumnWidthIndex2 = _interopRequireDefault(_calculateMaximumColumnWidthIndex); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var makeBorder = undefined, | ||
makeColumns = undefined; | ||
/** | ||
* @param {Array[]} rows | ||
* @param {Object} inputConfig | ||
* Merges user provided border characters with the default border ("honeywell") characters. | ||
* | ||
* @param {Object} border | ||
* @return {Object} | ||
*/ | ||
makeBorder = function () { | ||
var border = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
return (0, _lodashObjectAssign3['default'])({}, (0, _getBorderCharacters2['default'])('honeywell'), border); | ||
}; | ||
exports['default'] = function (rows) { | ||
var inputConfig = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
/** | ||
* Creates a configuration for every column using default | ||
* values for the missing configuration properties. | ||
* | ||
* @param {Array[]} rows | ||
* @param {Object} columns | ||
* @return {Object} | ||
*/ | ||
makeColumns = function (rows) { | ||
var columns = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var config = undefined, | ||
maximumColumnValueIndex = undefined; | ||
var maximumColumnWidthIndex = undefined; | ||
config = (0, _lodashLangCloneDeep3['default'])(inputConfig); | ||
maximumColumnWidthIndex = (0, _calculateMaximumColumnWidthIndex2['default'])(rows); | ||
(0, _validateConfig2['default'])(rows, config); | ||
if (!config.border) { | ||
config.border = {}; | ||
} | ||
config.border = (0, _lodashObjectAssign3['default'])({}, (0, _border2['default'])('honeywell'), config.border); | ||
maximumColumnValueIndex = (0, _calculateMaximumColumnValueIndex2['default'])(rows); | ||
if (!config.column) { | ||
config.column = {}; | ||
} | ||
(0, _lodashUtilityTimes3['default'])(rows[0].length, function (index) { | ||
if ((0, _lodashLangIsUndefined3['default'])(config.column[index])) { | ||
config.column[index] = {}; | ||
if ((0, _lodashLangIsUndefined3['default'])(columns[index])) { | ||
columns[index] = {}; | ||
} | ||
}); | ||
config.column = (0, _lodashObjectMapValues3['default'])(config.column, function (column, index0) { | ||
if ((0, _lodashLangIsUndefined3['default'])(column.minWidth) || maximumColumnValueIndex[index0] > config.column[index0].minWidth) { | ||
column.minWidth = maximumColumnValueIndex[index0]; | ||
if ((0, _lodashLangIsUndefined3['default'])(columns[index].alignment)) { | ||
columns[index].alignment = 'left'; | ||
} | ||
if ((0, _lodashLangIsUndefined3['default'])(column.alignment)) { | ||
column.alignment = 'left'; | ||
if ((0, _lodashLangIsUndefined3['default'])(columns[index].width)) { | ||
columns[index].width = maximumColumnWidthIndex[index]; | ||
} | ||
if ((0, _lodashLangIsUndefined3['default'])(column.maxWidth)) { | ||
column.maxWidth = Infinity; | ||
} else if (column.maxWidth < column.minWidth) { | ||
column.minWidth = column.maxWidth; | ||
if ((0, _lodashLangIsUndefined3['default'])(columns[index].paddingLeft)) { | ||
columns[index].paddingLeft = 1; | ||
} | ||
if ((0, _lodashLangIsUndefined3['default'])(column.paddingLeft)) { | ||
column.paddingLeft = 0; | ||
if ((0, _lodashLangIsUndefined3['default'])(columns[index].paddingRight)) { | ||
columns[index].paddingRight = 1; | ||
} | ||
}); | ||
if ((0, _lodashLangIsUndefined3['default'])(column.paddingRight)) { | ||
column.paddingRight = 0; | ||
} | ||
return columns; | ||
}; | ||
return column; | ||
}); | ||
/** | ||
* Makes a new configuration object out of the userConfig object | ||
* using default values for the missing configuration properties. | ||
* | ||
* @param {Array[]} rows | ||
* @param {Object} userConfig | ||
* @return {Object} | ||
*/ | ||
exports['default'] = function (rows) { | ||
var userConfig = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var config = undefined; | ||
(0, _validateConfig2['default'])(userConfig); | ||
config = (0, _lodashLangCloneDeep3['default'])(userConfig); | ||
config.border = makeBorder(config.border); | ||
config.columns = makeColumns(rows, config.columns); | ||
return config; | ||
@@ -108,2 +120,2 @@ }; | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=makeConfig.js.map | ||
//# sourceMappingURL=makeConfig.js.map |
'use strict'; | ||
var _lodashCollectionMap2 = require('lodash/collection/map'); | ||
var _lodashCollectionMap3 = _interopRequireDefault(_lodashCollectionMap2); | ||
var _lodashStringRepeat2 = require('lodash/string/repeat'); | ||
var _lodashStringRepeat3 = _interopRequireDefault(_lodashStringRepeat2); | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
value: true | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _drawTable = require('./drawTable'); | ||
@@ -19,5 +13,5 @@ | ||
var _calculateColumnSizeIndex = require('./calculateColumnSizeIndex'); | ||
var _calculateCellWidthIndex = require('./calculateCellWidthIndex'); | ||
var _calculateColumnSizeIndex2 = _interopRequireDefault(_calculateColumnSizeIndex); | ||
var _calculateCellWidthIndex2 = _interopRequireDefault(_calculateCellWidthIndex); | ||
@@ -28,9 +22,9 @@ var _makeConfig = require('./makeConfig'); | ||
var _calculateRowSpanIndex = require('./calculateRowSpanIndex'); | ||
var _calculateRowHeightIndex = require('./calculateRowHeightIndex'); | ||
var _calculateRowSpanIndex2 = _interopRequireDefault(_calculateRowSpanIndex); | ||
var _calculateRowHeightIndex2 = _interopRequireDefault(_calculateRowHeightIndex); | ||
var _mapDataUsingRowSpanIndex = require('./mapDataUsingRowSpanIndex'); | ||
var _mapDataUsingRowHeightIndex = require('./mapDataUsingRowHeightIndex'); | ||
var _mapDataUsingRowSpanIndex2 = _interopRequireDefault(_mapDataUsingRowSpanIndex); | ||
var _mapDataUsingRowHeightIndex2 = _interopRequireDefault(_mapDataUsingRowHeightIndex); | ||
@@ -41,12 +35,20 @@ var _stringWidth = require('string-width'); | ||
var _align = require('./align'); | ||
var _alignTableData = require('./alignTableData'); | ||
var _align2 = _interopRequireDefault(_align); | ||
var _alignTableData2 = _interopRequireDefault(_alignTableData); | ||
var _validateData = require('./validateData'); | ||
var _padTableData = require('./padTableData'); | ||
var _validateData2 = _interopRequireDefault(_validateData); | ||
var _padTableData2 = _interopRequireDefault(_padTableData); | ||
var _validateTableData = require('./validateTableData'); | ||
var _validateTableData2 = _interopRequireDefault(_validateTableData); | ||
var _stringifyTableData = require('./stringifyTableData'); | ||
var _stringifyTableData2 = _interopRequireDefault(_stringifyTableData); | ||
/** | ||
* @typedef {String} table~cell | ||
* @typedef {string} table~cell | ||
*/ | ||
@@ -59,27 +61,26 @@ | ||
/** | ||
* @typedef {Object} table~configColumn | ||
* @property {String} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {Number} minWidth Minimum column width (default: 0). | ||
* @property {Number} maxWidth Maximum column width (default: Infinity). | ||
* @property {Number} paddingLeft Cell content padding width left (default: 0). | ||
* @property {Number} paddingRight Cell content padding width right (default: 0). | ||
* @typedef {Object} table~columns | ||
* @property {string} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {number} width Column width (default: auto). | ||
* @property {number} paddingLeft Cell content padding width left (default: 1). | ||
* @property {number} paddingRight Cell content padding width right (default: 1). | ||
*/ | ||
/** | ||
* @typedef {Object} table~configBorder | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} bottomBody | ||
* @property {String} bottomJoin | ||
* @property {String} bottomLeft | ||
* @property {String} bottomRight | ||
* @property {String} bodyLeft | ||
* @property {String} bodyRight | ||
* @property {String} bodyJoin | ||
* @property {String} joinBody | ||
* @property {String} joinLeft | ||
* @property {String} joinRight | ||
* @property {String} joinJoin | ||
* @typedef {Object} table~border | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} bottomBody | ||
* @property {string} bottomJoin | ||
* @property {string} bottomLeft | ||
* @property {string} bottomRight | ||
* @property {string} bodyLeft | ||
* @property {string} bodyRight | ||
* @property {string} bodyJoin | ||
* @property {string} joinBody | ||
* @property {string} joinLeft | ||
* @property {string} joinRight | ||
* @property {string} joinJoin | ||
*/ | ||
@@ -89,4 +90,4 @@ | ||
* @typedef {Object} table~config | ||
* @property {table~configBorder} | ||
* @property {table~configColumn[]} column Column specific configuration. | ||
* @property {table~border} border | ||
* @property {table~columns[]} columns Column specific configuration. | ||
*/ | ||
@@ -97,75 +98,33 @@ | ||
* | ||
* @param {table~row[]} rows | ||
* @param {table~config} config | ||
* @param {table~row[]} data | ||
* @param {table~config} userConfig | ||
* @return {String} | ||
*/ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
exports['default'] = function (data) { | ||
var userConfig = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
exports['default'] = function (rows) { | ||
var config = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var config = undefined, | ||
rowHeightIndex = undefined, | ||
cellWidthIndex = undefined, | ||
rows = undefined; | ||
var derivedConfig = undefined, | ||
safeData = undefined, | ||
rowSpanIndex = undefined, | ||
columnSizeIndex = undefined, | ||
dataMappedUsingRowSpanIndex = undefined, | ||
tableBorder = undefined; | ||
(0, _validateTableData2['default'])(data); | ||
(0, _validateData2['default'])(rows); | ||
rows = (0, _stringifyTableData2['default'])(data); | ||
safeData = (0, _lodashCollectionMap3['default'])(rows, function (columns) { | ||
return (0, _lodashCollectionMap3['default'])(columns, String); | ||
}); | ||
config = (0, _makeConfig2['default'])(rows, userConfig); | ||
// console.log(`safeData`, safeData); | ||
rowHeightIndex = (0, _calculateRowHeightIndex2['default'])(rows, config); | ||
derivedConfig = (0, _makeConfig2['default'])(safeData, config); | ||
rows = (0, _mapDataUsingRowHeightIndex2['default'])(rows, rowHeightIndex, config); | ||
rows = (0, _alignTableData2['default'])(rows, config); | ||
rows = (0, _padTableData2['default'])(rows, config); | ||
// console.log(`derivedConfig`, derivedConfig); | ||
cellWidthIndex = (0, _calculateCellWidthIndex2['default'])(rows[0]); | ||
rowSpanIndex = (0, _calculateRowSpanIndex2['default'])(safeData, derivedConfig); | ||
// console.log(`rowSpanIndex`, rowSpanIndex); | ||
dataMappedUsingRowSpanIndex = (0, _mapDataUsingRowSpanIndex2['default'])(safeData, rowSpanIndex, derivedConfig); | ||
// console.log(`dataMappedUsingRowSpanIndex`, dataMappedUsingRowSpanIndex); | ||
dataMappedUsingRowSpanIndex = (0, _lodashCollectionMap3['default'])(dataMappedUsingRowSpanIndex, function (cells, index0) { | ||
return (0, _lodashCollectionMap3['default'])(cells, function (value, index1) { | ||
var column = undefined; | ||
column = derivedConfig.column[index1]; | ||
// console.log(column); | ||
if ((0, _stringWidth2['default'])(value) === column.maxWidth) { | ||
return value; | ||
} else { | ||
return (0, _align2['default'])(value, column.minWidth, column.alignment); | ||
} | ||
}); | ||
}); | ||
dataMappedUsingRowSpanIndex = (0, _lodashCollectionMap3['default'])(dataMappedUsingRowSpanIndex, function (cells, index0) { | ||
return (0, _lodashCollectionMap3['default'])(cells, function (value, index1) { | ||
var column = undefined; | ||
column = derivedConfig.column[index1]; | ||
return (0, _lodashStringRepeat3['default'])(' ', column.paddingLeft) + value + (0, _lodashStringRepeat3['default'])(' ', column.paddingRight); | ||
}); | ||
}); | ||
//_.times(config.column[index1].paddingLeft, ` `) | ||
columnSizeIndex = (0, _calculateColumnSizeIndex2['default'])(dataMappedUsingRowSpanIndex[0]); | ||
// console.log(`columnSizeIndex`, columnSizeIndex); | ||
return (0, _drawTable2['default'])(dataMappedUsingRowSpanIndex, derivedConfig.border, columnSizeIndex, rowSpanIndex); | ||
return (0, _drawTable2['default'])(rows, config.border, cellWidthIndex, rowHeightIndex); | ||
}; | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=table.js.map | ||
//# sourceMappingURL=table.js.map |
'use strict'; | ||
var _lodashCollectionForEach2 = require('lodash/collection/forEach'); | ||
var _lodashCollectionForEach3 = _interopRequireDefault(_lodashCollectionForEach2); | ||
var _lodashLangIsUndefined2 = require('lodash/lang/isUndefined'); | ||
var _lodashLangIsUndefined3 = _interopRequireDefault(_lodashLangIsUndefined2); | ||
Object.defineProperty(exports, '__esModule', { | ||
@@ -15,7 +7,7 @@ value: true | ||
// To accomodate for the ./dist/ package. | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _srcSchemasConfigJson = require('./../src/schemas/config.json'); | ||
var _schemasConfigJson = require('./schemas/config.json'); | ||
var _srcSchemasConfigJson2 = _interopRequireDefault(_srcSchemasConfigJson); | ||
var _schemasConfigJson2 = _interopRequireDefault(_schemasConfigJson); | ||
@@ -27,15 +19,20 @@ var _tv4 = require('tv4'); | ||
/** | ||
* @param {row[]} rows | ||
* @typedef {string} cell | ||
*/ | ||
/** | ||
* @typedef {cell[]} validateData~column | ||
*/ | ||
/** | ||
* @param {formatData~config} config | ||
* @return {undefined} | ||
* @returns {undefined} | ||
*/ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
exports['default'] = function () { | ||
var config = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
exports['default'] = function (rows) { | ||
var config = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var result = undefined; | ||
result = _tv42['default'].validateResult(config, _srcSchemasConfigJson2['default']); | ||
result = _tv42['default'].validateResult(config, _schemasConfigJson2['default']); | ||
@@ -53,13 +50,5 @@ if (!result.valid) { | ||
} | ||
if (config.column) { | ||
(0, _lodashCollectionForEach3['default'])(config.column, function (column) { | ||
if (!(0, _lodashLangIsUndefined3['default'])(column.minWidth) && !(0, _lodashLangIsUndefined3['default'])(column.maxWidth) && column.minWidth > column.maxWidth) { | ||
throw new Error('Column minWidth cannot be greater than maxWidth.'); | ||
} | ||
}); | ||
} | ||
}; | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=validateConfig.js.map | ||
//# sourceMappingURL=validateConfig.js.map |
@@ -36,4 +36,10 @@ import gulp from 'gulp'; | ||
gulp.task('build-es5', ['clean'], () => { | ||
gulp.task('copy', ['clean'], () => { | ||
return gulp | ||
.src('./src/**/*') | ||
.pipe(gulp.dest('./dist')); | ||
}); | ||
gulp.task('build-es5', ['copy'], () => { | ||
return gulp | ||
.src('./src/**/*.js') | ||
@@ -40,0 +46,0 @@ .pipe(sourcemaps.init()) |
{ | ||
"name": "table", | ||
"version": "2.1.2", | ||
"version": "3.0.1", | ||
"description": "Formats data into a string table.", | ||
@@ -23,14 +23,17 @@ "main": "./dist/index.js", | ||
"scripts": { | ||
"test": "node ./node_modules/.bin/babel-node ./node_modules/.bin/gulp test", | ||
"build": "node ./node_modules/.bin/babel-node ./node_modules/.bin/gulp build", | ||
"watch": "node ./node_modules/.bin/babel-node ./node_modules/.bin/gulp watch" | ||
"gulp": "node ./node_modules/.bin/babel-node ./node_modules/.bin/gulp", | ||
"test": "npm run gulp test", | ||
"build": "npm run gulp build", | ||
"watch": "npm run gulp watch", | ||
"readme": "node ./node_modules/.bin/gitdown ./.README/README.md --output-file ./README.md" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.8.23", | ||
"babel-core": "^5.8.24", | ||
"babel-core": "^5.8.25", | ||
"babel-loader": "^5.3.2", | ||
"babel-plugin-lodash": "^0.2.0", | ||
"canonical": "^1.0.1", | ||
"chai": "^3.2.0", | ||
"chai": "^3.3.0", | ||
"del": "^2.0.2", | ||
"gitdown": "^2.4.0", | ||
"globby": "^3.0.1", | ||
@@ -40,16 +43,15 @@ "gulp": "^3.9.0", | ||
"gulp-mocha": "^2.1.3", | ||
"gulp-sourcemaps": "^1.5.2", | ||
"gulp-sourcemaps": "^1.6.0", | ||
"gulp-util": "^3.0.6" | ||
}, | ||
"dependencies": { | ||
"ansi-slice": "^1.2.0", | ||
"bluebird": "^2.10.2", | ||
"chalk": "^1.1.1", | ||
"ansi-slice": "^1.2.0", | ||
"bluebird": "^2.10.0", | ||
"lodash": "^3.10.1", | ||
"string-length": "^1.0.1", | ||
"string-width": "^1.0.1", | ||
"strip-ansi": "^3.0.0", | ||
"tv4": "^1.2.7", | ||
"xregexp": "slevithan/xregexp#baef6b6429fbd6f9aeb3c2c86a41b5422b023e18" | ||
"xregexp": "^3.0.0" | ||
} | ||
} |
418
README.md
@@ -1,30 +0,32 @@ | ||
# Table | ||
<h1 id="table">Table</h1> | ||
[![Travis build status](http://img.shields.io/travis/gajus/table/master.svg?style=flat)](https://travis-ci.org/gajus/table) | ||
[![NPM version](http://img.shields.io/npm/v/table.svg?style=flat)](https://www.npmjs.com/package/table) | ||
[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-brightgreen.svg?style=flat)](https://github.com/gajus/canonical) | ||
* [Features](#features) | ||
* [Usage](#usage) | ||
* [Custom Border](#custom-border) | ||
* [Predefined Border Templates](#predefined-border-templates) | ||
* [Minimum Column Width](#minimum-column-width) | ||
* [Maximum Column Width](#maximum-column-width) | ||
* [Cell Content Alignment](#cell-content-alignment) | ||
* [Table](#table) | ||
* [Features](#table-features) | ||
* [Usage](#table-usage) | ||
* [Cell Content Alignment](#table-usage-cell-content-alignment) | ||
* [Column Width](#table-usage-column-width) | ||
* [Custom Border](#table-usage-custom-border) | ||
* [Padding Cell Content](#table-usage-padding-cell-content) | ||
* [Predefined Border Templates](#table-usage-predefined-border-templates) | ||
Produces a string that represents array data in a text table. | ||
![Demo of Table displaying a list of missions to Moon](./README/demo.png) | ||
![Demo of table displaying a list of missions to the Moon.](./.README/demo.png) | ||
## Features | ||
<h2 id="table-features">Features</h2> | ||
* Works with strings containing [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) characters. | ||
* Works with strings containing [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code). | ||
* Custom border characters. | ||
* Content alignment. | ||
* Content padding. | ||
* Column `minWidth`. | ||
* Column `maxWidth`. | ||
* Expanding long cell values into multiple rows. | ||
* Configurable border characters. | ||
* Configurable content alignment per column. | ||
* Configurable content padding per column. | ||
* Configurable column width. | ||
* Text wrapping. | ||
## Usage | ||
<h2 id="table-usage">Usage</h2> | ||
@@ -46,3 +48,3 @@ Table data is described using an array (rows) of array (cells). | ||
/** | ||
* @typedef {String} table~cell | ||
* @typedef {string} table~cell | ||
*/ | ||
@@ -55,27 +57,26 @@ | ||
/** | ||
* @typedef {Object} table~configColumn | ||
* @property {String} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {Number} minWidth Minimum column width (default: 0). | ||
* @property {Number} maxWidth Maximum column width (default: Infinity). | ||
* @property {Number} paddingLeft Cell content padding width left (default: 0). | ||
* @property {Number} paddingRight Cell content padding width right (default: 0). | ||
* @typedef {Object} table~columns | ||
* @property {string} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {number} width Column width (default: auto). | ||
* @property {number} paddingLeft Cell content padding width left (default: 1). | ||
* @property {number} paddingRight Cell content padding width right (default: 1). | ||
*/ | ||
/** | ||
* @typedef {Object} table~configBorder | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} bottomBody | ||
* @property {String} bottomJoin | ||
* @property {String} bottomLeft | ||
* @property {String} bottomRight | ||
* @property {String} bodyLeft | ||
* @property {String} bodyRight | ||
* @property {String} bodyJoin | ||
* @property {String} joinBody | ||
* @property {String} joinLeft | ||
* @property {String} joinRight | ||
* @property {String} joinJoin | ||
* @typedef {Object} table~border | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} bottomBody | ||
* @property {string} bottomJoin | ||
* @property {string} bottomLeft | ||
* @property {string} bottomRight | ||
* @property {string} bodyLeft | ||
* @property {string} bodyRight | ||
* @property {string} bodyJoin | ||
* @property {string} joinBody | ||
* @property {string} joinLeft | ||
* @property {string} joinRight | ||
* @property {string} joinJoin | ||
*/ | ||
@@ -85,4 +86,4 @@ | ||
* @typedef {Object} table~config | ||
* @property {table~configBorder} | ||
* @property {table~configColumn[]} column Column specific configuration. | ||
* @property {table~border} border | ||
* @property {table~columns[]} columns Column specific configuration. | ||
*/ | ||
@@ -94,3 +95,3 @@ | ||
* @param {table~row[]} rows | ||
* @param {table~config} config | ||
* @param {table~config} userConfig | ||
* @return {String} | ||
@@ -104,19 +105,22 @@ */ | ||
``` | ||
╔══╤══╤══╗ | ||
║0A│0B│0C║ | ||
╟──┼──┼──╢ | ||
║1A│1B│1C║ | ||
╟──┼──┼──╢ | ||
║2A│2B│2C║ | ||
╚══╧══╧══╝ | ||
╔════╤════╤════╗ | ||
║ 0A │ 0B │ 0C ║ | ||
╟────┼────┼────╢ | ||
║ 1A │ 1B │ 1C ║ | ||
╟────┼────┼────╢ | ||
║ 2A │ 2B │ 2C ║ | ||
╚════╧════╧════╝ | ||
``` | ||
### Custom Border | ||
`border` property describes the characters used to draw the table borders. | ||
<h3 id="table-usage-cell-content-alignment">Cell Content Alignment</h3> | ||
`config.columns[{number}].alignment` property controls content horizontal alignment within a cell. | ||
Valid values are: "left", "right" and "center". | ||
```js | ||
let data, | ||
output, | ||
options; | ||
let config, | ||
data, | ||
output; | ||
@@ -129,26 +133,20 @@ data = [ | ||
options = { | ||
border: { | ||
topBody: `─`, | ||
topJoin: `┬`, | ||
topLeft: `┌`, | ||
topRight: `┐`, | ||
bottomBody: `─`, | ||
bottomJoin: `┴`, | ||
bottomLeft: `└`, | ||
bottomRight: `┘`, | ||
bodyLeft: `│`, | ||
bodyRight: `│`, | ||
bodyJoin: `│`, | ||
joinBody: `─`, | ||
joinLeft: `├`, | ||
joinRight: `┤`, | ||
joinJoin: `┼` | ||
config = { | ||
columns: { | ||
0: { | ||
alignment: 'left', | ||
minWidth: 10 | ||
}, | ||
1: { | ||
alignment: 'center', | ||
minWidth: 10 | ||
}, | ||
2: { | ||
alignment: 'right', | ||
minWidth: 10 | ||
} | ||
} | ||
}; | ||
output = table(data, options); | ||
output = table(data, config); | ||
@@ -159,74 +157,16 @@ console.log(output); | ||
``` | ||
┌──┬──┬──┐ | ||
│0A│0B│0C│ | ||
├──┼──┼──┤ | ||
│1A│1B│1C│ | ||
├──┼──┼──┤ | ||
│2A│2B│2C│ | ||
└──┴──┴──┘ | ||
╔════════════╤════════════╤════════════╗ | ||
║ 0A │ 0B │ 0C ║ | ||
╟────────────┼────────────┼────────────╢ | ||
║ 1A │ 1B │ 1C ║ | ||
╟────────────┼────────────┼────────────╢ | ||
║ 2A │ 2B │ 2C ║ | ||
╚════════════╧════════════╧════════════╝ | ||
``` | ||
#### Predefined Border Templates | ||
<h3 id="table-usage-column-width">Column Width</h3> | ||
You can load one of the predefined border templates using `border` function. | ||
`config.columns[{number}].width` property restrictions column width to a fixed width. | ||
```js | ||
import table from 'table'; | ||
import { | ||
border | ||
} from 'table'; | ||
let data; | ||
data = [ | ||
['0A', '0B', '0C'], | ||
['1A', '1B', '1C'], | ||
['2A', '2B', '2C'] | ||
]; | ||
table(data, { | ||
border: border(`name of the template`) | ||
}); | ||
``` | ||
``` | ||
# honeywell | ||
╔══╤══╤══╗ | ||
║0A│0B│0C║ | ||
╟──┼──┼──╢ | ||
║1A│1B│1C║ | ||
╟──┼──┼──╢ | ||
║2A│2B│2C║ | ||
╚══╧══╧══╝ | ||
# norc | ||
┌──┬──┬──┐ | ||
│0A│0B│0C│ | ||
├──┼──┼──┤ | ||
│1A│1B│1C│ | ||
├──┼──┼──┤ | ||
│2A│2B│2C│ | ||
└──┴──┴──┘ | ||
# ramac (ASCII; for use in terminals that do not support Unicode characters) | ||
+--+--+--+ | ||
|0A|0B|0C| | ||
|--|--|--| | ||
|1A|1B|1C| | ||
|--|--|--| | ||
|2A|2B|2C| | ||
+--+--+--+ | ||
``` | ||
Raise [an issue](https://github.com/gajus/table/issues) if you'd like to contribute a new border template. | ||
### Minimum Column Width | ||
`minWidth` property pads the string to fill the cell. | ||
```js | ||
let data, | ||
@@ -243,5 +183,5 @@ output, | ||
options = { | ||
column: { | ||
columns: { | ||
1: { | ||
minWidth: 10 | ||
width: 10 | ||
} | ||
@@ -257,22 +197,22 @@ } | ||
``` | ||
╔══╤══════════╤══╗ | ||
║0A│0B │0C║ | ||
╟──┼──────────┼──╢ | ||
║1A│1B │1C║ | ||
╟──┼──────────┼──╢ | ||
║2A│2B │2C║ | ||
╚══╧══════════╧══╝ | ||
╔════╤════════════╤════╗ | ||
║ 0A │ 0B │ 0C ║ | ||
╟────┼────────────┼────╢ | ||
║ 1A │ 1B │ 1C ║ | ||
╟────┼────────────┼────╢ | ||
║ 2A │ 2B │ 2C ║ | ||
╚════╧════════════╧════╝ | ||
``` | ||
### Maximum Column Width | ||
<h3 id="table-usage-custom-border">Custom Border</h3> | ||
`maxWidth` property makes the overflowing text break into multiple lines. | ||
`config.border` property describes characters used to draw the table border. | ||
```js | ||
let data, | ||
output, | ||
options; | ||
let config, | ||
data, | ||
output; | ||
data = [ | ||
['0A', 'AAABBBCCC', '0C'], | ||
['0A', '0B', '0C'], | ||
['1A', '1B', '1C'], | ||
@@ -282,11 +222,26 @@ ['2A', '2B', '2C'] | ||
options = { | ||
column: { | ||
1: { | ||
maxWidth: 3 | ||
} | ||
config = { | ||
border: { | ||
topBody: `─`, | ||
topJoin: `┬`, | ||
topLeft: `┌`, | ||
topRight: `┐`, | ||
bottomBody: `─`, | ||
bottomJoin: `┴`, | ||
bottomLeft: `└`, | ||
bottomRight: `┘`, | ||
bodyLeft: `│`, | ||
bodyRight: `│`, | ||
bodyJoin: `│`, | ||
joinBody: `─`, | ||
joinLeft: `├`, | ||
joinRight: `┤`, | ||
joinJoin: `┼` | ||
} | ||
}; | ||
output = table(data, options); | ||
output = table(data, config); | ||
@@ -297,26 +252,22 @@ console.log(output); | ||
``` | ||
╔══╤═══╤══╗ | ||
║0A│AAA│0C║ | ||
║ │BBB│ ║ | ||
║ │CCC│ ║ | ||
╟──┼───┼──╢ | ||
║1A│1B │1C║ | ||
╟──┼───┼──╢ | ||
║2A│2B │2C║ | ||
╚══╧═══╧══╝ | ||
┌────┬────┬────┐ | ||
│ 0A │ 0B │ 0C │ | ||
├────┼────┼────┤ | ||
│ 1A │ 1B │ 1C │ | ||
├────┼────┼────┤ | ||
│ 2A │ 2B │ 2C │ | ||
└────┴────┴────┘ | ||
``` | ||
### Cell Content Alignment | ||
<h3 id="table-usage-padding-cell-content">Padding Cell Content</h3> | ||
`alignment` property controls content horizontal alignment within a cell. | ||
`config.columns[{number}].paddingLeft` and `config.columns[{number}].paddingRight` properties control content padding within a cell. Property value represents a number of whitespaces used to pad the content. | ||
Valid values are: "left", "right" and "center". | ||
```js | ||
let data, | ||
output, | ||
options; | ||
let config, | ||
data, | ||
output; | ||
data = [ | ||
['0A', '0B', '0C'], | ||
['0A', 'AABBCC', '0C'], | ||
['1A', '1B', '1C'], | ||
@@ -326,15 +277,10 @@ ['2A', '2B', '2C'] | ||
options = { | ||
column: { | ||
config = { | ||
columns: { | ||
0: { | ||
alignment: 'left', | ||
minWidth: 10 | ||
paddingLeft: 3 | ||
}, | ||
1: { | ||
alignment: 'center', | ||
minWidth: 10 | ||
}, | ||
2: { | ||
alignment: 'right', | ||
minWidth: 10 | ||
width: 2, | ||
paddingRight: 3 | ||
} | ||
@@ -344,3 +290,3 @@ } | ||
output = table(data, options); | ||
output = table(data, config); | ||
@@ -351,22 +297,27 @@ console.log(output); | ||
``` | ||
╔══════════╤══════════╤══════════╗ | ||
║0A │ 0B │ 0C║ | ||
╟──────────┼──────────┼──────────╢ | ||
║1A │ 1B │ 1C║ | ||
╟──────────┼──────────┼──────────╢ | ||
║2A │ 2B │ 2C║ | ||
╚══════════╧══════════╧══════════╝ | ||
╔══════╤══════╤════╗ | ||
║ 0A │ AA │ 0C ║ | ||
║ │ BB │ ║ | ||
║ │ CC │ ║ | ||
╟──────┼──────┼────╢ | ||
║ 1A │ 1B │ 1C ║ | ||
╟──────┼──────┼────╢ | ||
║ 2A │ 2B │ 2C ║ | ||
╚══════╧══════╧════╝ | ||
``` | ||
### Padding Cell Content | ||
<h3 id="table-usage-predefined-border-templates">Predefined Border Templates</h3> | ||
`paddingLeft` and `paddingRight` properties control content padding within a cell. Property value represents a number of whitespaces using to pad the content. | ||
You can load one of the predefined border templates using `getBorderCharacters` function. | ||
```js | ||
let data, | ||
output, | ||
options; | ||
import table, { | ||
getBorderCharacters | ||
} from 'table'; | ||
let config, | ||
data; | ||
data = [ | ||
['0A', 'AABBCC', '0C'], | ||
['0A', '0B', '0C'], | ||
['1A', '1B', '1C'], | ||
@@ -376,29 +327,42 @@ ['2A', '2B', '2C'] | ||
options = { | ||
column: { | ||
0: { | ||
paddingLeft: 3 | ||
}, | ||
1: { | ||
maxWidth: 2, | ||
paddingRight: 3 | ||
} | ||
} | ||
config = { | ||
border: getBorderCharacters(`name of the template`) | ||
}; | ||
output = table(data, options); | ||
table(data, config); | ||
``` | ||
console.log(output); | ||
``` | ||
# honeywell | ||
╔════╤════╤════╗ | ||
║ 0A │ 0B │ 0C ║ | ||
╟────┼────┼────╢ | ||
║ 1A │ 1B │ 1C ║ | ||
╟────┼────┼────╢ | ||
║ 2A │ 2B │ 2C ║ | ||
╚════╧════╧════╝ | ||
# norc | ||
┌────┬────┬────┐ | ||
│ 0A │ 0B │ 0C │ | ||
├────┼────┼────┤ | ||
│ 1A │ 1B │ 1C │ | ||
├────┼────┼────┤ | ||
│ 2A │ 2B │ 2C │ | ||
└────┴────┴────┘ | ||
# ramac (ASCII; for use in terminals that do not support Unicode characters) | ||
+----+----+----+ | ||
| 0A | 0B | 0C | | ||
|----|----|----| | ||
| 1A | 1B | 1C | | ||
|----|----|----| | ||
| 2A | 2B | 2C | | ||
+----+----+----+ | ||
``` | ||
╔═════╤═════╤══╗ | ||
║ 0A│AA │0C║ | ||
║ │BB │ ║ | ||
║ │CC │ ║ | ||
╟─────┼─────┼──╢ | ||
║ 1A│1B │1C║ | ||
╟─────┼─────┼──╢ | ||
║ 2A│2B │2C║ | ||
╚═════╧═════╧══╝ | ||
``` | ||
Raise [an issue](https://github.com/gajus/table/issues) if you'd like to contribute a new border template. | ||
@@ -17,5 +17,5 @@ import _ from 'lodash'; | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorder~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -36,12 +36,12 @@ drawBorder = (columnSizeIndex, parts) => { | ||
* @typedef drawBorderTop~parts | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderTop~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -59,12 +59,12 @@ drawBorderTop = (columnSizeIndex, parts) => { | ||
* @typedef drawBorderJoin~parts | ||
* @property {String} joinLeft | ||
* @property {String} joinRight | ||
* @property {String} joinBody | ||
* @property {String} joinJoin | ||
* @property {string} joinLeft | ||
* @property {string} joinRight | ||
* @property {string} joinBody | ||
* @property {string} joinJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderJoin~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -82,12 +82,12 @@ drawBorderJoin = (columnSizeIndex, parts) => { | ||
* @typedef drawBorderBottom~parts | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
*/ | ||
/** | ||
* @param {Number[]} columnSizeIndex | ||
* @param {number[]} columnSizeIndex | ||
* @param {drawBorderBottom~parts} parts | ||
* @return {String} | ||
* @return {string} | ||
*/ | ||
@@ -94,0 +94,0 @@ drawBorderBottom = (columnSizeIndex, parts) => { |
import _ from 'lodash'; | ||
import { | ||
@@ -8,3 +7,2 @@ drawBorderTop, | ||
} from './drawBorder'; | ||
import drawRow from './drawRow'; | ||
@@ -11,0 +9,0 @@ |
@@ -1,8 +0,8 @@ | ||
import border from './border/'; | ||
import table from './table'; | ||
import getBorderCharacters from './getBorderCharacters'; | ||
export { | ||
border | ||
getBorderCharacters | ||
}; | ||
export default table; |
import _ from 'lodash'; | ||
import border from './border/'; | ||
import getBorderCharacters from './getBorderCharacters'; | ||
import validateConfig from './validateConfig'; | ||
import calculateMaximumColumnValueIndex from './calculateMaximumColumnValueIndex'; | ||
import calculateMaximumColumnWidthIndex from './calculateMaximumColumnWidthIndex'; | ||
let makeBorder, | ||
makeColumns; | ||
/** | ||
* Merges user provided border characters with the default border ("honeywell") characters. | ||
* | ||
* @param {Object} border | ||
* @return {Object} | ||
*/ | ||
makeBorder = (border = {}) => { | ||
return _.assign({}, getBorderCharacters(`honeywell`), border); | ||
}; | ||
/** | ||
* Creates a configuration for every column using default | ||
* values for the missing configuration properties. | ||
* | ||
* @param {Array[]} rows | ||
* @param {Object} inputConfig | ||
* @param {Object} columns | ||
* @return {Object} | ||
*/ | ||
export default (rows, inputConfig = {}) => { | ||
let config, | ||
maximumColumnValueIndex; | ||
makeColumns = (rows, columns = {}) => { | ||
let maximumColumnWidthIndex; | ||
config = _.cloneDeep(inputConfig); | ||
maximumColumnWidthIndex = calculateMaximumColumnWidthIndex(rows); | ||
validateConfig(rows, config); | ||
if (!config.border) { | ||
config.border = {} | ||
} | ||
config.border = _.assign({}, border(`honeywell`), config.border); | ||
maximumColumnValueIndex = calculateMaximumColumnValueIndex(rows); | ||
if (!config.column) { | ||
config.column = {}; | ||
} | ||
_.times(rows[0].length, (index) => { | ||
if (_.isUndefined(config.column[index])) { | ||
config.column[index] = {}; | ||
if (_.isUndefined(columns[index])) { | ||
columns[index] = {}; | ||
} | ||
}); | ||
config.column = _.mapValues(config.column, (column, index0) => { | ||
if (_.isUndefined(column.minWidth) || maximumColumnValueIndex[index0] > config.column[index0].minWidth) { | ||
column.minWidth = maximumColumnValueIndex[index0]; | ||
if (_.isUndefined(columns[index].alignment)) { | ||
columns[index].alignment = `left`; | ||
} | ||
if (_.isUndefined(column.alignment)) { | ||
column.alignment = `left`; | ||
if (_.isUndefined(columns[index].width)) { | ||
columns[index].width = maximumColumnWidthIndex[index]; | ||
} | ||
if (_.isUndefined(column.maxWidth)) { | ||
column.maxWidth = Infinity; | ||
} else if (column.maxWidth < column.minWidth) { | ||
column.minWidth = column.maxWidth; | ||
if (_.isUndefined(columns[index].paddingLeft)) { | ||
columns[index].paddingLeft = 1; | ||
} | ||
if (_.isUndefined(column.paddingLeft)) { | ||
column.paddingLeft = 0; | ||
if (_.isUndefined(columns[index].paddingRight)) { | ||
columns[index].paddingRight = 1; | ||
} | ||
}); | ||
if (_.isUndefined(column.paddingRight)) { | ||
column.paddingRight = 0; | ||
} | ||
return columns; | ||
}; | ||
return column; | ||
}); | ||
/** | ||
* Makes a new configuration object out of the userConfig object | ||
* using default values for the missing configuration properties. | ||
* | ||
* @param {Array[]} rows | ||
* @param {Object} userConfig | ||
* @return {Object} | ||
*/ | ||
export default (rows, userConfig = {}) => { | ||
let config; | ||
validateConfig(userConfig); | ||
config = _.cloneDeep(userConfig); | ||
config.border = makeBorder(config.border); | ||
config.columns = makeColumns(rows, config.columns); | ||
return config; | ||
}; |
@@ -5,3 +5,41 @@ { | ||
"border": { | ||
"$ref": "#/definitions/borders" | ||
}, | ||
"columns": { | ||
"$ref": "#/definitions/columns" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"definitions": { | ||
"columns": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[0-9]+$": { | ||
"type": "object", | ||
"properties": { | ||
"alignment": { | ||
"type": "string", | ||
"enum": [ | ||
"left", | ||
"right", | ||
"center" | ||
] | ||
}, | ||
"width": { | ||
"type": "number" | ||
}, | ||
"paddingLeft": { | ||
"type": "number" | ||
}, | ||
"paddingRight": { | ||
"type": "number" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
"borders": { | ||
"type": "object", | ||
"properties": { | ||
@@ -56,37 +94,2 @@ "topBody": { | ||
}, | ||
"column": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[0-9]+$": { | ||
"type": "object", | ||
"properties": { | ||
"alignment": { | ||
"type": "string", | ||
"enum": [ | ||
"left", | ||
"right", | ||
"center" | ||
] | ||
}, | ||
"minWidth": { | ||
"type": "number" | ||
}, | ||
"maxWidth": { | ||
"type": "number" | ||
}, | ||
"paddingLeft": { | ||
"type": "number" | ||
}, | ||
"paddingRight": { | ||
"type": "number" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"definitions": { | ||
"border": { | ||
@@ -93,0 +96,0 @@ "type": "string" |
133
src/table.js
import _ from 'lodash'; | ||
import drawTable from './drawTable'; | ||
import calculateColumnSizeIndex from './calculateColumnSizeIndex'; | ||
import calculateCellWidthIndex from './calculateCellWidthIndex'; | ||
import makeConfig from './makeConfig'; | ||
import calculateRowSpanIndex from './calculateRowSpanIndex'; | ||
import mapDataUsingRowSpanIndex from './mapDataUsingRowSpanIndex'; | ||
import calculateRowHeightIndex from './calculateRowHeightIndex'; | ||
import mapDataUsingRowHeightIndex from './mapDataUsingRowHeightIndex'; | ||
import stringWidth from 'string-width'; | ||
import align from './align'; | ||
import validateData from './validateData'; | ||
import alignTableData from './alignTableData'; | ||
import padTableData from './padTableData'; | ||
import validateTableData from './validateTableData'; | ||
import stringifyTableData from './stringifyTableData'; | ||
/** | ||
* @typedef {String} table~cell | ||
* @typedef {string} table~cell | ||
*/ | ||
@@ -20,27 +22,26 @@ | ||
/** | ||
* @typedef {Object} table~configColumn | ||
* @property {String} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {Number} minWidth Minimum column width (default: 0). | ||
* @property {Number} maxWidth Maximum column width (default: Infinity). | ||
* @property {Number} paddingLeft Cell content padding width left (default: 0). | ||
* @property {Number} paddingRight Cell content padding width right (default: 0). | ||
* @typedef {Object} table~columns | ||
* @property {string} alignment Cell content alignment (enum: left, center, right) (default: left). | ||
* @property {number} width Column width (default: auto). | ||
* @property {number} paddingLeft Cell content padding width left (default: 1). | ||
* @property {number} paddingRight Cell content padding width right (default: 1). | ||
*/ | ||
/** | ||
* @typedef {Object} table~configBorder | ||
* @property {String} topBody | ||
* @property {String} topJoin | ||
* @property {String} topLeft | ||
* @property {String} topRight | ||
* @property {String} bottomBody | ||
* @property {String} bottomJoin | ||
* @property {String} bottomLeft | ||
* @property {String} bottomRight | ||
* @property {String} bodyLeft | ||
* @property {String} bodyRight | ||
* @property {String} bodyJoin | ||
* @property {String} joinBody | ||
* @property {String} joinLeft | ||
* @property {String} joinRight | ||
* @property {String} joinJoin | ||
* @typedef {Object} table~border | ||
* @property {string} topBody | ||
* @property {string} topJoin | ||
* @property {string} topLeft | ||
* @property {string} topRight | ||
* @property {string} bottomBody | ||
* @property {string} bottomJoin | ||
* @property {string} bottomLeft | ||
* @property {string} bottomRight | ||
* @property {string} bodyLeft | ||
* @property {string} bodyRight | ||
* @property {string} bodyJoin | ||
* @property {string} joinBody | ||
* @property {string} joinLeft | ||
* @property {string} joinRight | ||
* @property {string} joinJoin | ||
*/ | ||
@@ -50,4 +51,4 @@ | ||
* @typedef {Object} table~config | ||
* @property {table~configBorder} | ||
* @property {table~configColumn[]} column Column specific configuration. | ||
* @property {table~border} border | ||
* @property {table~columns[]} columns Column specific configuration. | ||
*/ | ||
@@ -58,67 +59,27 @@ | ||
* | ||
* @param {table~row[]} rows | ||
* @param {table~config} config | ||
* @param {table~row[]} data | ||
* @param {table~config} userConfig | ||
* @return {String} | ||
*/ | ||
export default (rows, config = {}) => { | ||
let derivedConfig, | ||
safeData, | ||
rowSpanIndex, | ||
columnSizeIndex, | ||
dataMappedUsingRowSpanIndex, | ||
tableBorder; | ||
export default (data, userConfig = {}) => { | ||
let config, | ||
rowHeightIndex, | ||
cellWidthIndex, | ||
rows; | ||
validateData(rows); | ||
validateTableData(data); | ||
safeData = _.map(rows, (columns) => { | ||
return _.map(columns, String); | ||
}); | ||
rows = stringifyTableData(data); | ||
// console.log(`safeData`, safeData); | ||
config = makeConfig(rows, userConfig); | ||
derivedConfig = makeConfig(safeData, config); | ||
rowHeightIndex = calculateRowHeightIndex(rows, config); | ||
// console.log(`derivedConfig`, derivedConfig); | ||
rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config); | ||
rows = alignTableData(rows, config); | ||
rows = padTableData(rows, config); | ||
rowSpanIndex = calculateRowSpanIndex(safeData, derivedConfig); | ||
cellWidthIndex = calculateCellWidthIndex(rows[0]); | ||
// console.log(`rowSpanIndex`, rowSpanIndex); | ||
dataMappedUsingRowSpanIndex = mapDataUsingRowSpanIndex(safeData, rowSpanIndex, derivedConfig); | ||
// console.log(`dataMappedUsingRowSpanIndex`, dataMappedUsingRowSpanIndex); | ||
dataMappedUsingRowSpanIndex = _.map(dataMappedUsingRowSpanIndex, (cells, index0) => { | ||
return _.map(cells, (value, index1) => { | ||
let column; | ||
column = derivedConfig.column[index1]; | ||
// console.log(column); | ||
if (stringWidth(value) === column.maxWidth) { | ||
return value; | ||
} else { | ||
return align(value, column.minWidth, column.alignment); | ||
} | ||
}); | ||
}); | ||
dataMappedUsingRowSpanIndex = _.map(dataMappedUsingRowSpanIndex, (cells, index0) => { | ||
return _.map(cells, (value, index1) => { | ||
let column; | ||
column = derivedConfig.column[index1]; | ||
return _.repeat(` `, column.paddingLeft) + value + _.repeat(` `, column.paddingRight); | ||
}); | ||
}); | ||
//_.times(config.column[index1].paddingLeft, ` `) | ||
columnSizeIndex = calculateColumnSizeIndex(dataMappedUsingRowSpanIndex[0]); | ||
// console.log(`columnSizeIndex`, columnSizeIndex); | ||
return drawTable(dataMappedUsingRowSpanIndex, derivedConfig.border, columnSizeIndex, rowSpanIndex); | ||
return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex); | ||
}; |
import _ from 'lodash'; | ||
import schema from './schemas/config.json'; | ||
import tv4 from 'tv4'; | ||
// To accomodate for the ./dist/ package. | ||
import schema from './../src/schemas/config.json'; | ||
/** | ||
* @typedef {string} cell | ||
*/ | ||
import tv4 from 'tv4'; | ||
/** | ||
* @typedef {cell[]} validateData~column | ||
*/ | ||
/** | ||
* @param {row[]} rows | ||
* @param {formatData~config} config | ||
* @return {undefined} | ||
* @returns {undefined} | ||
*/ | ||
export default (rows, config = {}) => { | ||
export default (config = {}) => { | ||
let result; | ||
@@ -29,10 +33,2 @@ | ||
} | ||
if (config.column) { | ||
_.forEach(config.column, (column) => { | ||
if (!_.isUndefined(column.minWidth) && !_.isUndefined(column.maxWidth) && column.minWidth > column.maxWidth) { | ||
throw new Error(`Column minWidth cannot be greater than maxWidth.`); | ||
} | ||
}); | ||
} | ||
}; |
@@ -7,3 +7,5 @@ import { | ||
drawBorder, | ||
drawBorderTop | ||
drawBorderTop, | ||
drawBorderJoin, | ||
drawBorderBottom | ||
} from './../src/drawBorder'; | ||
@@ -44,1 +46,35 @@ | ||
}); | ||
describe(`drawBorderJoin`, () => { | ||
it(`draws a border using parts`, () => { | ||
let parts; | ||
parts = { | ||
joinBody: `─`, | ||
joinLeft: `╟`, | ||
joinRight: `╢`, | ||
joinJoin: `┼` | ||
}; | ||
expect(drawBorderJoin([1], parts)).to.equal(`╟─╢\n`); | ||
expect(drawBorderJoin([1, 1], parts)).to.equal(`╟─┼─╢\n`); | ||
expect(drawBorderJoin([5, 10], parts)).to.equal(`╟─────┼──────────╢\n`); | ||
}); | ||
}); | ||
describe(`drawBorderBottom`, () => { | ||
it(`draws a border using parts`, () => { | ||
let parts; | ||
parts = { | ||
bottomBody: `═`, | ||
bottomJoin: `╧`, | ||
bottomLeft: `╚`, | ||
bottomRight: `╝` | ||
}; | ||
expect(drawBorderBottom([1], parts)).to.equal(`╚═╝\n`); | ||
expect(drawBorderBottom([1, 1], parts)).to.equal(`╚═╧═╝\n`); | ||
expect(drawBorderBottom([5, 10], parts)).to.equal(`╚═════╧══════════╝\n`); | ||
}); | ||
}); |
@@ -27,57 +27,20 @@ /* eslint-disable max-nested-callbacks */ | ||
context(`column`, () => { | ||
context(`alignment is not provided`, () => { | ||
it(`defaults to 'left'`, () => { | ||
let config; | ||
context(`'alignment'`, () => { | ||
context(`is not provided`, () => { | ||
it(`defaults to 'left'`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
]); | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
]); | ||
expect(config.column[0].alignment).to.equal(`left`); | ||
expect(config.columns[0].alignment).to.equal(`left`); | ||
}); | ||
}); | ||
}); | ||
context(`maxWidth is not provided`, () => { | ||
it(`defaults to Infinity`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
]); | ||
expect(config.column[0].maxWidth).to.equal(Infinity); | ||
}); | ||
}); | ||
context(`paddingLeft is not provided`, () => { | ||
it(`defaults to 0`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
]); | ||
expect(config.column[0].paddingLeft).to.equal(0); | ||
}); | ||
}); | ||
context(`paddingRight is not provided`, () => { | ||
it(`defaults to Infinity`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
]); | ||
expect(config.column[0].paddingRight).to.equal(0); | ||
}); | ||
}); | ||
context(`minWidth is not provided`, () => { | ||
context(`maxWidth is not set`, () => { | ||
it(`uses the maximum value length`, () => { | ||
context(`'width'`, () => { | ||
context(`is not provided`, () => { | ||
it(`defaults to the maximum column width`, () => { | ||
let config; | ||
@@ -91,49 +54,9 @@ | ||
expect(config.column[0].minWidth).to.equal(5); | ||
expect(config.columns[0].width).to.equal(5); | ||
}); | ||
}); | ||
context(`maxWidth is set`, () => { | ||
context(`maxWidth is larger than maximum value length`, () => { | ||
it(`uses the maximum value length`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaa` | ||
] | ||
], { | ||
column: { | ||
0: { | ||
maxWidth: 10 | ||
} | ||
} | ||
}); | ||
expect(config.column[0].minWidth).to.equal(5); | ||
}); | ||
}); | ||
context(`maxWidth is lesser than maximum value length`, () => { | ||
it(`uses the maxWidth value`, () => { | ||
let config; | ||
config = makeConfig([ | ||
[ | ||
`aaaaaaaaaa` | ||
] | ||
], { | ||
column: { | ||
0: { | ||
maxWidth: 5 | ||
} | ||
} | ||
}); | ||
expect(config.column[0].minWidth).to.equal(5); | ||
}); | ||
}); | ||
}); | ||
}); | ||
context(`minWidth is provided`, () => { | ||
context(`minWidth is larger than maximum value length`, () => { | ||
it(`uses the maximum value length`, () => { | ||
context(`'paddingLeft'`, () => { | ||
context(`is not provided`, () => { | ||
it(`defaults to 1`, () => { | ||
let config; | ||
@@ -145,15 +68,11 @@ | ||
] | ||
], { | ||
column: { | ||
0: { | ||
minWidth: 10 | ||
} | ||
} | ||
}); | ||
]); | ||
expect(config.column[0].minWidth).to.deep.equal(10); | ||
expect(config.columns[0].paddingLeft).to.equal(1); | ||
}); | ||
}); | ||
context(`minWidth is smaller than maximum value length`, () => { | ||
it(`uses the maximum value length`, () => { | ||
}); | ||
context(`'paddingRight'`, () => { | ||
context(`is not provided`, () => { | ||
it(`defaults to 1`, () => { | ||
let config; | ||
@@ -165,11 +84,5 @@ | ||
] | ||
], { | ||
column: { | ||
0: { | ||
minWidth: 2 | ||
} | ||
} | ||
}); | ||
]); | ||
expect(config.column[0].minWidth).to.deep.equal(5); | ||
expect(config.columns[0].paddingRight).to.equal(1); | ||
}); | ||
@@ -176,0 +89,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
361857
8
91
2833
0
14
354
+ Addedxregexp@3.2.0(transitive)
- Removedstring-length@^1.0.1
- Removedstring-length@1.0.1(transitive)
Updatedbluebird@^2.10.2
Updatedxregexp@^3.0.0