Comparing version 3.0.1 to 3.0.2
var Factory = require("../src/factory.js") | ||
var Table = function(options) { | ||
var Table = function (options) { | ||
options = options || {} | ||
@@ -12,4 +11,4 @@ options.adapter = "automattic" | ||
var header = [] | ||
if(options.head && options.head instanceof Array) { | ||
options.head.forEach(function(val) { | ||
if (options.head && options.head instanceof Array) { | ||
options.head.forEach(function (val) { | ||
header.push({ | ||
@@ -22,4 +21,4 @@ value: val | ||
// colWidths | ||
if(options.colWidths) { | ||
options.colWidths.forEach(function(val, i) { | ||
if (options.colWidths) { | ||
options.colWidths.forEach(function (val, i) { | ||
header[i].width = val | ||
@@ -30,4 +29,4 @@ }) | ||
// colAligns | ||
if(options.colAligns) { | ||
options.colAligns.forEach(function(val, i) { | ||
if (options.colAligns) { | ||
options.colAligns.forEach(function (val, i) { | ||
header[i].align = val | ||
@@ -42,7 +41,7 @@ header[i].headerAlign = val | ||
// style - padding | ||
if(options.style["padding-left"]) { | ||
if (options.style["padding-left"]) { | ||
options.paddingLeft = options.style["padding-left"] | ||
} | ||
if(options.style["padding-right"]) { | ||
if (options.style["padding-right"]) { | ||
options.paddingRight = options.style["padding-right"] | ||
@@ -52,7 +51,7 @@ } | ||
// style - colors | ||
if(options.style.head && options.style.head instanceof Array) { | ||
if (options.style.head && options.style.head instanceof Array) { | ||
options.headerColor = options.style.head[0] | ||
} | ||
if(options.style.body && options.style.body instanceof Array) { | ||
if (options.style.body && options.style.body instanceof Array) { | ||
options.color = options.style.body[0] | ||
@@ -62,3 +61,3 @@ } | ||
// style - compact | ||
if(options.style.compact) { | ||
if (options.style.compact) { | ||
options.compact = true | ||
@@ -70,3 +69,3 @@ } | ||
// inherited from prototype | ||
let t = Factory(header, [], [], options) | ||
const t = Factory(header, [], [], options) | ||
t.toString = t.render | ||
@@ -73,0 +72,0 @@ return t |
@@ -1,2 +0,2 @@ | ||
let Factory = require("./../src/factory.js") | ||
const Factory = require("./../src/factory.js") | ||
module.exports = Factory |
@@ -41,4 +41,4 @@ #!/usr/bin/env node | ||
let emitError = function(type, detail) { | ||
console.log(`\n${ chalk.bgRed.white(type) }\n\n${ chalk.bold(detail)}`) | ||
const emitError = function (type, detail) { | ||
console.log(`\n${chalk.bgRed.white(type)}\n\n${chalk.bold(detail)}`) | ||
process.exit(1) | ||
@@ -52,6 +52,6 @@ } | ||
let dataFormat = "csv" | ||
switch(true) { | ||
case(typeof yargs.format === "undefined"): | ||
switch (true) { | ||
case (typeof yargs.format === "undefined"): | ||
break | ||
case(yargs.format.toString().match(/json/i) !== null): | ||
case (yargs.format.toString().match(/json/i) !== null): | ||
dataFormat = "json" | ||
@@ -63,7 +63,7 @@ break | ||
// look for individually flagged options-* | ||
let options = {} | ||
Object.keys(yargs).forEach(function(key) { | ||
let keyParts = key.split("-") | ||
if(keyParts[0]==="options") { | ||
options[keyParts[1]]=yargs[key] | ||
const options = {} | ||
Object.keys(yargs).forEach(function (key) { | ||
const keyParts = key.split("-") | ||
if (keyParts[0] === "options") { | ||
options[keyParts[1]] = yargs[key] | ||
} | ||
@@ -74,7 +74,7 @@ }) | ||
let header = [] | ||
if(yargs.header) { | ||
if(!fs.existsSync(path.resolve(yargs.header))) { | ||
if (yargs.header) { | ||
if (!fs.existsSync(path.resolve(yargs.header))) { | ||
emitError( | ||
"Invalid file path", | ||
`Cannot find config file at: ${ yargs.header }.` | ||
`Cannot find config file at: ${yargs.header}.` | ||
) | ||
@@ -87,8 +87,7 @@ } | ||
// because different dataFormats | ||
let runTable = function(header, body) { | ||
const runTable = function (header, body) { | ||
// footer = [], | ||
let Table = require("../src/factory.js") | ||
const Table = require("../src/factory.js") | ||
options.terminalAdapter = true | ||
let t1 = Table(header, body, options) | ||
const t1 = Table(header, body, options) | ||
@@ -99,11 +98,10 @@ // hide cursor | ||
// wipe existing if already rendered | ||
if(alreadyRendered) { | ||
if (alreadyRendered) { | ||
// move cursor up number to the top of the previous print | ||
// before deleting | ||
console.log(`\u001b[${previousHeight+3}A`) | ||
console.log(`\u001b[${previousHeight + 3}A`) | ||
// delete to end of terminal | ||
console.log("\u001b[0J") | ||
} else{ | ||
} else { | ||
alreadyRendered = true | ||
@@ -117,3 +115,2 @@ } | ||
previousHeight = t1.height | ||
} | ||
@@ -123,11 +120,10 @@ | ||
process.stdin.setEncoding("utf8") | ||
process.stdin.on("data", function(chunk) { | ||
process.stdin.on("data", function (chunk) { | ||
// handle dataFormats | ||
switch(true) { | ||
case(dataFormat==="json"): | ||
switch (true) { | ||
case (dataFormat === "json"): | ||
let data | ||
try { | ||
data = JSON.parse(chunk) | ||
} catch(e) { | ||
} catch (e) { | ||
emitError( | ||
@@ -141,5 +137,5 @@ "JSON parse error", | ||
default: | ||
let formatterOptions = {} | ||
Object.keys(yargs).forEach(function(key) { | ||
if(key.slice(0, 4) === "csv-" && typeof(yargs[key]) !== "undefined") { | ||
const formatterOptions = {} | ||
Object.keys(yargs).forEach(function (key) { | ||
if (key.slice(0, 4) === "csv-" && typeof (yargs[key]) !== "undefined") { | ||
formatterOptions[key.slice(4)] = yargs[key] | ||
@@ -149,5 +145,5 @@ } | ||
csv.parse(chunk, formatterOptions, function(err, data) { | ||
csv.parse(chunk, formatterOptions, function (err, data) { | ||
// validate csv | ||
if(typeof data === "undefined") { | ||
if (err || typeof data === "undefined") { | ||
emitError( | ||
@@ -165,3 +161,3 @@ "CSV parse error", | ||
if (process.platform === "win32") { | ||
let rl = require("readline").createInterface({ | ||
const rl = require("readline").createInterface({ | ||
input: process.stdin, | ||
@@ -182,5 +178,5 @@ output: process.stdout | ||
process.on("exit", function() { | ||
process.on("exit", function () { | ||
// show cursor | ||
console.log("\u001b[?25h") | ||
}) |
{ | ||
"name": "tty-table", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Node cli table", | ||
@@ -23,8 +23,9 @@ "main": "src/main.js", | ||
"report-to-coveralls": "npx nyc report --reporter=text-lcov | npx coveralls", | ||
"report-to-cover-io": "npx nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov -t ffe0f46d-c939-4302-b199-0f2de3e8c18a", | ||
"save-tests": "node npm_scripts/save-tests.js", | ||
"report-to-coverio": "npx nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov -t ffe0f46d-c939-4302-b199-0f2de3e8c18a", | ||
"save-tests": "node npm_scripts/run-examples.js save", | ||
"view-examples": "node npm_scripts/run-examples.js view", | ||
"lint": "npx eslint adapters/* src/*", | ||
"lint-fix": "npx eslint adapters/* src/* --fix", | ||
"lint-examples": "npx eslint examples/", | ||
"lint-examples-fix": "npx eslint examples/ --fix", | ||
"lint-fix-examples": "npx eslint examples/ --fix", | ||
"prepublishOnly": "npm run dist", | ||
@@ -76,6 +77,10 @@ "tags": "npx grunt tags", | ||
"eslint": "^6.8.0", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-node": "^11.0.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"glob": "^7.1.4", | ||
"grunt": "^1.0.4", | ||
"grunt-cli": "^1.3.2", | ||
"grunt-contrib-uglify": "^4.0.1", | ||
"grunt-contrib-watch": "^1.1.0", | ||
@@ -87,3 +92,2 @@ "grunt-shell": "^3.0.1", | ||
"nyc": "^15.0.0", | ||
"orgy": "^2.2.1", | ||
"rollup": "^1.31.1" | ||
@@ -90,0 +94,0 @@ }, |
@@ -15,2 +15,9 @@ # tty-table 端子台 | ||
To view all example output: | ||
```sh | ||
$ git clone https://github.com/tecfu/tty-table && cd tty-table && npm i | ||
$ npm run view-examples | ||
``` | ||
### Terminal (Static) | ||
@@ -37,4 +44,4 @@ | ||
- [demo: jsfiddle](https://jsfiddle.net/6hz1a9cs/3/) | ||
- [demo: plinkr](https://plnkr.co/edit/iQn9xn5yCY4NUkXRF87o?p=preview) | ||
- [live demo: jsfiddle](https://jsfiddle.net/6hz1a9cs/3/) | ||
- [live demo: plnkr](https://plnkr.co/edit/iQn9xn5yCY4NUkXRF87o?p=preview) | ||
- [source: examples/browser-example.html](examples/browser-example.html) | ||
@@ -173,3 +180,3 @@ | ||
const options = { | ||
borderStyle: 1, | ||
borderStyle: "solid", | ||
borderColor: "blue", | ||
@@ -239,6 +246,3 @@ headerAlign: "center", | ||
``` | ||
- Because: | ||
`node script.js --color=always` | ||
## Dev Tips | ||
@@ -245,0 +249,0 @@ |
// @TODO split defaults into table and cell settings | ||
const defaults = { | ||
borderCharacters: { | ||
"invisible": [ | ||
{v: " ", l: " ", j: " ", h: " ", r: " "}, | ||
{v: " ", l: " ", j: " ", h: " ", r: " "}, | ||
{v: " ", l: " ", j: " ", h: " ", r: " "} | ||
invisible: [ | ||
{ v: " ", l: " ", j: " ", h: " ", r: " " }, | ||
{ v: " ", l: " ", j: " ", h: " ", r: " " }, | ||
{ v: " ", l: " ", j: " ", h: " ", r: " " } | ||
], | ||
"solid": [ | ||
{v: "│", l: "┌", j: "┬", h: "─", r: "┐"}, | ||
{v: "│", l: "├", j: "┼", h: "─", r: "┤"}, | ||
{v: "│", l: "└", j: "┴", h: "─", r: "┘"} | ||
solid: [ | ||
{ v: "│", l: "┌", j: "┬", h: "─", r: "┐" }, | ||
{ v: "│", l: "├", j: "┼", h: "─", r: "┤" }, | ||
{ v: "│", l: "└", j: "┴", h: "─", r: "┘" } | ||
], | ||
"dashed": [ | ||
{v: "|", l: "+", j: "+", h: "-", r: "+"}, | ||
{v: "|", l: "+", j: "+", h: "-", r: "+"}, | ||
{v: "|", l: "+", j: "+", h: "-", r: "+"} | ||
dashed: [ | ||
{ v: "|", l: "+", j: "+", h: "-", r: "+" }, | ||
{ v: "|", l: "+", j: "+", h: "-", r: "+" }, | ||
{ v: "|", l: "+", j: "+", h: "-", r: "+" } | ||
], | ||
"none": [ | ||
{v: "", l: "", j: "", h: "", r: ""}, | ||
{v: "", l: "", j: "", h: "", r: ""}, | ||
{v: "", l: "", j: "", h: "", r: ""} | ||
none: [ | ||
{ v: "", l: "", j: "", h: "", r: "" }, | ||
{ v: "", l: "", j: "", h: "", r: "" }, | ||
{ v: "", l: "", j: "", h: "", r: "" } | ||
] | ||
@@ -64,9 +64,7 @@ }, | ||
// support deprecated border style values | ||
defaults.borderCharacters["0"] = defaults.borderCharacters["none"] | ||
defaults.borderCharacters["1"] = defaults.borderCharacters["solid"] | ||
defaults.borderCharacters["2"] = defaults.borderCharacters["dashed"] | ||
defaults.borderCharacters["0"] = defaults.borderCharacters.none | ||
defaults.borderCharacters["1"] = defaults.borderCharacters.solid | ||
defaults.borderCharacters["2"] = defaults.borderCharacters.dashed | ||
module.exports = defaults |
@@ -6,7 +6,6 @@ const defaults = require("./defaults.js") | ||
const Factory = function(paramsArr) { | ||
let _configKey = Symbol["config"] | ||
const Factory = function (paramsArr) { | ||
const _configKey = Symbol.config | ||
let header = [] | ||
let body = [] | ||
const body = [] | ||
let footer = [] | ||
@@ -16,6 +15,5 @@ let options = {} | ||
// handle different parameter scenarios | ||
switch(true) { | ||
switch (true) { | ||
// header, rows, footer, and options | ||
case(paramsArr.length === 4): | ||
case (paramsArr.length === 4): | ||
header = paramsArr[0] | ||
@@ -28,3 +26,3 @@ body.push(...paramsArr[1]) // creates new array to store our rows (body) | ||
// header, rows, footer | ||
case(paramsArr.length === 3 && paramsArr[2] instanceof Array): | ||
case (paramsArr.length === 3 && paramsArr[2] instanceof Array): | ||
header = paramsArr[0] | ||
@@ -36,3 +34,3 @@ body.push(...paramsArr[1]) // creates new array to store our rows | ||
// header, rows, options | ||
case(paramsArr.length === 3 && typeof paramsArr[2] === "object"): | ||
case (paramsArr.length === 3 && typeof paramsArr[2] === "object"): | ||
header = paramsArr[0] | ||
@@ -44,3 +42,3 @@ body.push(...paramsArr[1]) // creates new array to store our rows | ||
// header, rows (rows, footer is not an option) | ||
case(paramsArr.length === 2 && paramsArr[1] instanceof Array): | ||
case (paramsArr.length === 2 && paramsArr[1] instanceof Array): | ||
header = paramsArr[0] | ||
@@ -51,3 +49,3 @@ body.push(...paramsArr[1]) // creates new array to store our rows | ||
// rows, options | ||
case(paramsArr.length === 2 && typeof paramsArr[1] === "object"): | ||
case (paramsArr.length === 2 && typeof paramsArr[1] === "object"): | ||
body.push(...paramsArr[0]) // creates new array to store our rows | ||
@@ -58,3 +56,3 @@ options = paramsArr[1] | ||
// rows | ||
case(paramsArr.length === 1 && paramsArr[0] instanceof Array): | ||
case (paramsArr.length === 1 && paramsArr[0] instanceof Array): | ||
body.push(...paramsArr[0]) | ||
@@ -64,4 +62,4 @@ break | ||
// adapter called: i.e. `require('tty-table')('automattic-cli')` | ||
case(paramsArr.length === 1 && typeof paramsArr[0] === "string"): | ||
return require(`../adapters/${ paramsArr[0]}`) | ||
case (paramsArr.length === 1 && typeof paramsArr[0] === "string"): | ||
return require(`../adapters/${paramsArr[0]}`) | ||
@@ -75,3 +73,3 @@ default: | ||
const cloneddefaults = JSON.parse(JSON.stringify(defaults)) | ||
let config = Object.assign({}, cloneddefaults, options) | ||
const config = Object.assign({}, cloneddefaults, options) | ||
@@ -83,9 +81,9 @@ // backfixes for shortened option names | ||
// for truncate true is equivalent to empty string | ||
if(config.truncate === true) config.truncate = "" | ||
if (config.truncate === true) config.truncate = "" | ||
// if borderColor customized, color the border character set | ||
if(config.borderColor) { | ||
config.borderCharacters[config.borderStyle] = | ||
config.borderCharacters[config.borderStyle].map(function(obj) { | ||
Object.keys(obj).forEach(function(key) { | ||
if (config.borderColor) { | ||
config.borderCharacters[config.borderStyle] | ||
= config.borderCharacters[config.borderStyle].map(function (obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
obj[key] = Style.style(obj[key], config.borderColor) | ||
@@ -111,3 +109,3 @@ }) | ||
// variable widths for multiple tables simulateously | ||
if(config.terminalAdapter !== true) { | ||
if (config.terminalAdapter !== true) { | ||
counter++ // fix columnwidths for streams | ||
@@ -118,3 +116,3 @@ } | ||
// create a new object with an Array prototype | ||
let tableObject = Object.create(body) | ||
const tableObject = Object.create(body) | ||
@@ -134,4 +132,4 @@ // save configuration to new object | ||
*/ | ||
tableObject.render = function() { | ||
let output = Render.stringifyData(this[_configKey], this.slice(0)) // get string output | ||
tableObject.render = function () { | ||
const output = Render.stringifyData(this[_configKey], this.slice(0)) // get string output | ||
tableObject.height = this[_configKey].height | ||
@@ -144,4 +142,3 @@ return output | ||
let Table = function() { | ||
const Table = function () { | ||
return new Factory(arguments) | ||
@@ -148,0 +145,0 @@ } |
@@ -5,3 +5,2 @@ const stripAnsi = require("strip-ansi") | ||
const addPadding = (config, width) => { | ||
@@ -11,3 +10,2 @@ return width + config.paddingLeft + config.paddingRight | ||
/** | ||
@@ -22,9 +20,8 @@ * Returns the widest cell give a collection of rows | ||
const getMaxLength = (columnOptions, rows, columnIndex) => { | ||
let iterable | ||
// add a row that contains the header value, so we use that width too | ||
if(typeof columnOptions === "object" && columnOptions.value) { | ||
if (typeof columnOptions === "object" && columnOptions.value) { | ||
iterable = rows.slice() | ||
let z = new Array(iterable[0].length) // create a new empty row | ||
const z = new Array(iterable[0].length) // create a new empty row | ||
z[columnIndex] = columnOptions.value.toString() | ||
@@ -37,12 +34,6 @@ iterable.push(z) | ||
// iterable.forEach( row => { | ||
// if(row[columnIndex] && stripAnsi(row[columnIndex].toString()).length > widest) { | ||
// widest = wcwidth(row[columnIndex].toString()) | ||
// } | ||
// }) | ||
let widest = iterable.reduce((prev, row) => { | ||
if(row[columnIndex]) { | ||
let width = wcwidth(stripAnsi(row[columnIndex].toString())) | ||
return ( width > prev) ? width : prev | ||
const widest = iterable.reduce((prev, row) => { | ||
if (row[columnIndex]) { | ||
const width = wcwidth(stripAnsi(row[columnIndex].toString())) | ||
return (width > prev) ? width : prev | ||
} | ||
@@ -55,3 +46,2 @@ return prev | ||
/** | ||
@@ -73,3 +63,3 @@ * Get total width available to this table instance | ||
if (config.width !== "auto" && /^\d{1,2}%$/.test(config.width)) { | ||
return (config.width.slice(0, -1) * .01) * viewport | ||
return (config.width.slice(0, -1) * 0.01) * viewport | ||
} | ||
@@ -84,6 +74,5 @@ | ||
throw new Error ("Cannot auto discover available table width. Set table `width` option or export the environment variable COLUMNS") | ||
throw new Error("Cannot auto discover available table width. Set table `width` option or export the environment variable COLUMNS") | ||
} | ||
module.exports.getStringLength = string => { | ||
@@ -94,3 +83,2 @@ // stripAnsi(string.replace(/[^\x00-\xff]/g,'XX')).length | ||
module.exports.wrapCellText = ( | ||
@@ -103,3 +91,2 @@ config, | ||
) => { | ||
// ANSI chararacters that demarcate the start/end of a line | ||
@@ -113,3 +100,3 @@ const startAnsiRegexp = /^(\033\[[0-9;]*m)+/ | ||
// store matching ANSI characters | ||
let startMatches = string.match(startAnsiRegexp) || [""] | ||
const startMatches = string.match(startAnsiRegexp) || [""] | ||
@@ -120,3 +107,3 @@ // remove ANSI start-of-line chars | ||
// store matching ANSI characters so can be later re-attached | ||
let endMatches = string.match(endAnsiRegexp) || [""] | ||
const endMatches = string.match(endAnsiRegexp) || [""] | ||
@@ -128,7 +115,7 @@ // remove ANSI end-of-line chars | ||
switch(rowType) { | ||
case("header"): | ||
switch (rowType) { | ||
case ("header"): | ||
alignTgt = "headerAlign" | ||
break | ||
case("body"): | ||
case ("body"): | ||
alignTgt = "align" | ||
@@ -141,3 +128,3 @@ break | ||
// equalize padding for centered lines | ||
if(cellOptions[alignTgt] === "center") { | ||
if (cellOptions[alignTgt] === "center") { | ||
cellOptions.paddingLeft = cellOptions.paddingRight = Math.max( | ||
@@ -158,3 +145,3 @@ cellOptions.paddingRight, | ||
if(typeof config.truncate === "string") { | ||
if (typeof config.truncate === "string") { | ||
string = exports.truncate(string, cellOptions, innerWidth) | ||
@@ -166,3 +153,3 @@ } else { | ||
// format each line | ||
let strArr = string.split("\n").map( line => { | ||
const strArr = string.split("\n").map(line => { | ||
line = line.trim() | ||
@@ -173,10 +160,10 @@ | ||
// alignment | ||
if(lineLength < columnWidth) { | ||
if (lineLength < columnWidth) { | ||
let emptySpace = columnWidth - lineLength | ||
switch(true) { | ||
case(cellOptions[alignTgt] === "center"): | ||
emptySpace -- | ||
let padBoth = Math.floor(emptySpace / 2), | ||
padRemainder = emptySpace % 2 | ||
switch (true) { | ||
case (cellOptions[alignTgt] === "center"): | ||
emptySpace-- | ||
const padBoth = Math.floor(emptySpace / 2) | ||
const padRemainder = emptySpace % 2 | ||
line = Array(padBoth + 1).join(" ") | ||
@@ -187,3 +174,3 @@ + line | ||
case(cellOptions[alignTgt] === "right"): | ||
case (cellOptions[alignTgt] === "right"): | ||
line = Array(emptySpace - cellOptions.paddingRight).join(" ") | ||
@@ -211,7 +198,6 @@ + line | ||
module.exports.truncate = (string, cellOptions, maxWidth) => { | ||
const stringWidth = wcwidth(string) | ||
if(maxWidth < stringWidth) { | ||
if (maxWidth < stringWidth) { | ||
// @TODO give user option to decide if they want to break words on wrapping | ||
@@ -228,5 +214,4 @@ string = smartwrap(string, { | ||
module.exports.wrap = (string, cellOptions, innerWidth) => { | ||
let outstring = smartwrap(string, { | ||
const outstring = smartwrap(string, { | ||
errorChar: cellOptions.defaultErrorValue, | ||
@@ -241,13 +226,8 @@ minWidth: 1, | ||
module.exports.getColumnWidths = (config, rows) => { | ||
const availableWidth = getAvailableWidth(config) | ||
// array of spaces that cant be proportionately reduced for each row | ||
const nonResizableRowSpaces = [] | ||
// iterate over the header if we have it, iterate over the first row | ||
// if we do not (to step through the correct number of columns) | ||
let iterable = (config.table.header[0] && config.table.header[0].length > 0) | ||
const iterable = (config.table.header[0] && config.table.header[0].length > 0) | ||
? config.table.header[0] : rows[0] | ||
@@ -257,7 +237,6 @@ | ||
let result | ||
nonResizableRowSpaces[columnIndex] = 0 // track non resizable space for row | ||
switch(true) { | ||
switch (true) { | ||
// column width is specified in column header | ||
case(typeof column === "object" && (/^\d{1,2}$/.test(column.width))): | ||
case (typeof column === "object" && (/^\d{1,2}$/.test(column.width))): | ||
result = column.width | ||
@@ -267,4 +246,4 @@ break | ||
// column width is a percentage of table width specified in column header | ||
case(typeof column === "object" && (/^\d{1,2}%$/.test(column.width))): | ||
result = (column.width.slice(0, -1) * .01) * availableWidth | ||
case (typeof column === "object" && (/^\d{1,2}%$/.test(column.width))): | ||
result = (column.width.slice(0, -1) * 0.01) * availableWidth | ||
result = addPadding(config, result) | ||
@@ -275,5 +254,5 @@ break | ||
default: | ||
let columnOptions = (config.table.header[0][columnIndex]) | ||
const columnOptions = (config.table.header[0][columnIndex]) | ||
? config.table.header[0][columnIndex] : {} | ||
let measurableRows = (rows.length) ? rows : config.table.header[0] | ||
const measurableRows = (rows.length) ? rows : config.table.header[0] | ||
@@ -289,3 +268,2 @@ result = getMaxLength(columnOptions, measurableRows, columnIndex) | ||
result = result + config.GUTTER | ||
return result | ||
@@ -295,6 +273,6 @@ }) | ||
// calculate sum of all column widths (including marginLeft) | ||
let totalWidth = widths.reduce((prev, current) => prev + current) | ||
const totalWidth = widths.reduce((prev, current) => prev + current) | ||
// if sum of all widths exceeds viewport, resize proportionately to fit | ||
if(totalWidth > availableWidth) { | ||
if (totalWidth > availableWidth) { | ||
let prop = availableWidth / totalWidth | ||
@@ -301,0 +279,0 @@ let relativeWidths |
@@ -1,2 +0,2 @@ | ||
if(require.main === module) { | ||
if (require.main === module) { | ||
// called directly in terminal | ||
@@ -3,0 +3,0 @@ /* istanbul ignore next */ |
const Style = require("./style.js") | ||
const Format = require("./format.js") | ||
/** | ||
@@ -16,3 +15,3 @@ * Converts arrays of data into arrays of cell strings | ||
const borderStyle = config.borderCharacters[config.borderStyle] | ||
let borders = [] | ||
const borders = [] | ||
@@ -26,7 +25,7 @@ // support backwards compatibility cli-table's multiple constructor geometries | ||
// from one rows set to the next, so we save the first set of widths and reuse | ||
if(!global.columnWidths) { | ||
if (!global.columnWidths) { | ||
global.columnWidths = {} | ||
} | ||
if(global.columnWidths[config.tableId]) { | ||
if (global.columnWidths[config.tableId]) { | ||
config.table.columnWidths = global.columnWidths[config.tableId] | ||
@@ -45,3 +44,3 @@ } else { | ||
case (config.showHeader === true): // explicitly true, show | ||
case (!!config.table.header[0].find(obj => obj.value || obj.alias)): // atleast one named column, show | ||
case (!!config.table.header[0].find(obj => obj.value || obj.alias)): // atleast one named column, show header | ||
sections.header = config.table.header.map(row => { | ||
@@ -68,30 +67,31 @@ return exports.buildRow(config, row, "header", null, rows, inputData) | ||
// add borders | ||
// 0=header, 1=body, 2=footer | ||
for (let a=0; a<3; a++) { | ||
borders.push("") | ||
config.table.columnWidths.forEach(function (w, i, arr) { | ||
borders[a] += Array(w).join(borderStyle[a].h) + | ||
((i+1 !== arr.length) ? borderStyle[a].j : borderStyle[a].r) | ||
// apply borders | ||
// 0=top, 1=middle, 2=bottom | ||
for (let a = 0; a < 3; a++) { | ||
// add left border | ||
borders[a] = borderStyle[a].l | ||
// add joined borders for each column | ||
config.table.columnWidths.forEach((columnWidth, index, arr) => { | ||
// Math.max because otherwise columns 1 wide wont have horizontal border | ||
borders[a] += Array(Math.max(columnWidth, 2)).join(borderStyle[a].h) | ||
borders[a] += ((index + 1 < arr.length) ? borderStyle[a].j : "") | ||
}) | ||
borders[a] = borderStyle[a].l + borders[a] | ||
borders[a] = borders[a].split("") | ||
borders[a][borders[a].length1] = borderStyle[a].r | ||
borders[a] = borders[a].join("") | ||
// add right border | ||
borders[a] += borderStyle[a].r | ||
// no trailing space on footer | ||
borders[a] = (a<2) ? `${marginLeft + borders[a] }\n` : marginLeft + borders[a] | ||
borders[a] = (a < 2) ? `${marginLeft + borders[a]}\n` : marginLeft + borders[a] | ||
} | ||
// top horizontal border | ||
let output = "" | ||
output += borders[0] | ||
let output = borders[0] | ||
// for each section (header,body,footer) | ||
Object.keys(sections).forEach((p, i) => { | ||
// for each row in the section | ||
while(sections[p].length) { | ||
while (sections[p].length) { | ||
const row = sections[p].shift() | ||
let row = sections[p].shift() | ||
// if(row.length === 0) {break} | ||
@@ -114,5 +114,5 @@ | ||
// bottom horizontal row border | ||
switch(true) { | ||
switch (true) { | ||
// skip if end of body and no footer | ||
case(sections[p].length === 0 | ||
case (sections[p].length === 0 | ||
&& i === 1 | ||
@@ -123,3 +123,3 @@ && sections.footer.length === 0): | ||
// skip if end of footer | ||
case(sections[p].length === 0 | ||
case (sections[p].length === 0 | ||
&& i === 2): | ||
@@ -129,7 +129,7 @@ break | ||
// skip if compact | ||
case(config.compact && p === "body" && !row.empty): | ||
case (config.compact && p === "body" && !row.empty): | ||
break | ||
// skip if border style is "none" | ||
case(config.borderStyle === "none" && config.compact): | ||
case (config.borderStyle === "none" && config.compact): | ||
break | ||
@@ -146,3 +146,3 @@ | ||
let finalOutput = Array(config.marginTop + 1).join("\n") + output | ||
const finalOutput = Array(config.marginTop + 1).join("\n") + output | ||
@@ -155,3 +155,2 @@ // record the height of the output | ||
module.exports.buildRow = (config, row, rowType, rowIndex, rowData, inputData) => { | ||
@@ -161,3 +160,3 @@ let minRowHeight = 0 | ||
// tag row as empty if empty, used for `compact` option | ||
if(row.length === 0 && config.compact) { | ||
if (row.length === 0 && config.compact) { | ||
row.empty = true | ||
@@ -168,5 +167,5 @@ return row | ||
// force row to have correct number of columns | ||
let difL = config.table.columnWidths.length - row.length | ||
const difL = config.table.columnWidths.length - row.length | ||
if(difL > 0) { | ||
if (difL > 0) { | ||
// add empty element to array | ||
@@ -182,16 +181,15 @@ row = row.concat(Array.apply(null, new Array(difL)).map(() => null)) | ||
// [1,,3] will only iterate 1,3 | ||
let cArrs = [] | ||
let rowLength = row.length | ||
const cArrs = [] | ||
const rowLength = row.length | ||
for(let index=0; index<rowLength; index++) { | ||
for (let index = 0; index < rowLength; index++) { | ||
const c = exports.buildCell(config, row[index], index, rowType, rowIndex, rowData, inputData) | ||
const cellArr = c.cellArr | ||
let c = exports.buildCell(config, row[index], index, rowType, rowIndex, rowData, inputData) | ||
let cellArr = c.cellArr | ||
if(rowType === "header") { | ||
if (rowType === "header") { | ||
config.table.columnInnerWidths.push(c.width) | ||
} | ||
minRowHeight = (minRowHeight < cellArr.length) ? | ||
cellArr.length : minRowHeight | ||
minRowHeight = (minRowHeight < cellArr.length) | ||
? cellArr.length : minRowHeight | ||
@@ -202,15 +200,15 @@ cArrs.push(cellArr) | ||
// adjust minRowHeight to reflect vertical row padding | ||
minRowHeight = (rowType === "header") ? minRowHeight : | ||
minRowHeight + (config.paddingBottom + config.paddingTop) | ||
minRowHeight = (rowType === "header") ? minRowHeight | ||
: minRowHeight + (config.paddingBottom + config.paddingTop) | ||
// convert array of cell arrays to array of lines | ||
let lines = Array.apply(null, {length: minRowHeight}) | ||
const lines = Array.apply(null, { length: minRowHeight }) | ||
.map(Function.call, () => []) | ||
cArrs.forEach(function(cellArr, a) { | ||
let whiteline = Array(config.table.columnWidths[a]).join(" ") | ||
cArrs.forEach(function (cellArr, a) { | ||
const whiteline = Array(config.table.columnWidths[a]).join(" ") | ||
if(rowType ==="body") { | ||
if (rowType === "body") { | ||
// add whitespace for top padding | ||
for(let i=0; i<config.paddingTop; i++) { | ||
for (let i = 0; i < config.paddingTop; i++) { | ||
cellArr.unshift(whiteline) | ||
@@ -220,9 +218,9 @@ } | ||
// add whitespace for bottom padding | ||
for(let i=0; i<config.paddingBottom; i++) { | ||
for (let i = 0; i < config.paddingBottom; i++) { | ||
cellArr.push(whiteline) | ||
} | ||
} | ||
for(let b=0; b<minRowHeight; b++) { | ||
lines[b].push((typeof cellArr[b] !== "undefined") ? | ||
cellArr[b] : whiteline) | ||
for (let b = 0; b < minRowHeight; b++) { | ||
lines[b].push((typeof cellArr[b] !== "undefined") | ||
? cellArr[b] : whiteline) | ||
} | ||
@@ -234,6 +232,5 @@ }) | ||
module.exports.buildCell = (config, cell, columnIndex, rowType, rowIndex, rowData, inputData) => { | ||
let cellValue | ||
let cellOptions = Object.assign( | ||
const cellOptions = Object.assign( | ||
{}, | ||
@@ -245,3 +242,3 @@ config, | ||
if(rowType === "header") { | ||
if (rowType === "header") { | ||
config.table.columns.push(cellOptions) | ||
@@ -251,4 +248,4 @@ cellValue = cellOptions.alias || cellOptions.value || "" | ||
// set cellValue | ||
switch(true) { | ||
case(typeof cell === "undefined" || cell === null): | ||
switch (true) { | ||
case (typeof cell === "undefined" || cell === null): | ||
// replace undefined/null cell values with placeholder | ||
@@ -260,7 +257,7 @@ cellValue = (config.errorOnNull) ? config.defaultErrorValue : config.defaultValue | ||
case(typeof cell === "object" && typeof cell.value !== "undefined"): | ||
case (typeof cell === "object" && typeof cell.value !== "undefined"): | ||
cellValue = cell.value | ||
break | ||
case(typeof cell === "function"): | ||
case (typeof cell === "function"): | ||
cellValue = cell.bind({ style: Style.style })( | ||
@@ -281,3 +278,3 @@ (!cellOptions.isNull) ? cellValue : "", | ||
// run formatter | ||
if(typeof cellOptions.formatter === "function") { | ||
if (typeof cellOptions.formatter === "function") { | ||
cellValue = cellOptions.formatter | ||
@@ -298,3 +295,3 @@ .bind({ style: Style.style })( | ||
// textwrap cellValue | ||
let wrapObj = Format.wrapCellText(config, cellValue, columnIndex, cellOptions, rowType) | ||
const wrapObj = Format.wrapCellText(config, cellValue, columnIndex, cellOptions, rowType) | ||
@@ -308,3 +305,2 @@ // return as array of lines | ||
/** | ||
@@ -317,10 +313,10 @@ * Check for a backwards compatible (cli-table) constructor | ||
// rows passed as an object | ||
if(typeof row === "object" && !(row instanceof Array)) { | ||
let keys = Object.keys(row) | ||
if (typeof row === "object" && !(row instanceof Array)) { | ||
const keys = Object.keys(row) | ||
if(config.adapter === "automattic") { | ||
if (config.adapter === "automattic") { | ||
// detected cross table | ||
let key = keys[0] | ||
const key = keys[0] | ||
if(row[key] instanceof Array) { | ||
if (row[key] instanceof Array) { | ||
type = "automattic-cross" | ||
@@ -343,3 +339,2 @@ } else { | ||
/** | ||
@@ -349,6 +344,5 @@ * Coerce backwards compatible constructor styles | ||
module.exports.coerceConstructorGeometry = (config, rows, constructorType) => { | ||
let output = [] | ||
switch(constructorType) { | ||
case("automattic-cross"): | ||
switch (constructorType) { | ||
case ("automattic-cross"): | ||
// assign header styles to first column | ||
@@ -359,4 +353,4 @@ config.columnSettings[0] = config.columnSettings[0] || {} | ||
output = rows.map(obj => { | ||
let arr = [] | ||
let key = Object.keys(obj)[0] | ||
const arr = [] | ||
const key = Object.keys(obj)[0] | ||
arr.push(key) | ||
@@ -367,3 +361,3 @@ return arr.concat(obj[key]) | ||
case("automattic-vertical"): | ||
case ("automattic-vertical"): | ||
// assign header styles to first column | ||
@@ -373,4 +367,4 @@ config.columnSettings[0] = config.columnSettings[0] || {} | ||
output = rows.map(function(value) { | ||
let key = Object.keys(value)[0] | ||
output = rows.map(function (value) { | ||
const key = Object.keys(value)[0] | ||
return [key, value[key]] | ||
@@ -380,3 +374,3 @@ }) | ||
case("o-horizontal"): | ||
case ("o-horizontal"): | ||
// cell property names are specified in header columns | ||
@@ -394,3 +388,3 @@ if (config.table.header[0].length | ||
case("a-horizontal"): | ||
case ("a-horizontal"): | ||
output = rows | ||
@@ -405,3 +399,2 @@ break | ||
// @TODO For rotating horizontal data into a vertical table | ||
@@ -408,0 +401,0 @@ // assumes all rows are same length |
const chalk = require("chalk") | ||
const stripAnsi = require("strip-ansi") | ||
module.exports.style = (str, ...colors) => { | ||
let out = colors.reduce(function(input, color) { | ||
const out = colors.reduce(function (input, color) { | ||
return chalk[color](input) | ||
@@ -12,11 +11,10 @@ }, str) | ||
module.exports.styleEachChar = (str, ...colors) => { | ||
// strip existing ansi chars so we dont loop them | ||
// @ TODO create a really clever workaround so that you can accrete styles | ||
let chars = [...stripAnsi(str)] | ||
const chars = [...stripAnsi(str)] | ||
// style each character | ||
let out = chars.reduce((prev, current) => { | ||
let coded = colors.reduce((input, color) => { | ||
const out = chars.reduce((prev, current) => { | ||
const coded = colors.reduce((input, color) => { | ||
return chalk[color](input) | ||
@@ -30,3 +28,2 @@ }, current) | ||
module.exports.resetStyle = (str) => { | ||
@@ -36,13 +33,11 @@ return stripAnsi(str) | ||
module.exports.colorizeCell = (str, cellOptions, rowType) => { | ||
let color = false // false will keep terminal default | ||
switch(true) { | ||
case(rowType === "body"): | ||
switch (true) { | ||
case (rowType === "body"): | ||
color = cellOptions.color || color | ||
break | ||
case(rowType === "header"): | ||
case (rowType === "header"): | ||
color = cellOptions.headerColor || color | ||
@@ -49,0 +44,0 @@ break |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
44200
284
28
967