Comparing version 0.2.3 to 0.3.0
{ | ||
"name": "tabacol", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Output a table in string format from data in JSON format", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,2 +13,13 @@ /** | ||
/** | ||
* Pad a string on the left with spaces | ||
* @param {string} str The string to pad | ||
* @param {int} lgt The lenght that the string will have once padded | ||
* @returns {string} The padded string or the untouched input string if it's already long enough | ||
*/ | ||
const padStringLeft = (str, lgt) => { | ||
const r = str.length >= lgt ? 0 : lgt - str.length; | ||
return `${' '.repeat(r)}${str}`; | ||
}; | ||
/** | ||
* Pad a string on both sides with spaces | ||
@@ -49,2 +60,3 @@ * @param {string} str The string to pad | ||
padStringRight, | ||
padStringLeft, | ||
padStringCenter, | ||
@@ -51,0 +63,0 @@ getArray0N, |
const chalk = require('chalk'); | ||
const helper = require('./helper'); | ||
const { | ||
truncateString, | ||
padStringLeft, | ||
padStringRight, | ||
padStringCenter, | ||
} = require('./helper'); | ||
@@ -34,8 +39,31 @@ const getColorApplyFunction = (col) => { | ||
const processCell = (content, md) => { | ||
const fw = md.col.finalWidth; | ||
// truncate the content of the cell if necessary | ||
let cell = helper.truncateString(content, md.col.finalWidth); | ||
// make sure to add space on the right if necessary | ||
cell = helper.padStringRight(cell, md.col.finalWidth); | ||
let cell = truncateString(content, fw); | ||
let textAlign = str => padStringRight(str, fw); | ||
// align the text inside the cell if some metadata about this are provided | ||
if (md.col.textAlign) { | ||
if (md.col.textAlign === 'right') textAlign = str => padStringLeft(str, fw); | ||
else textAlign = str => padStringRight(str, fw); | ||
} | ||
if (md.row) { | ||
if (md.row.textAlign) { | ||
if (md.row.textAlign === 'right') textAlign = str => padStringLeft(str, fw); | ||
else textAlign = str => padStringRight(str, fw); | ||
} | ||
} | ||
if (md.cell) { | ||
if (md.cell.textAlign) { | ||
if (md.cell.textAlign === 'right') textAlign = str => padStringLeft(str, fw); | ||
else textAlign = str => padStringRight(str, fw); | ||
} | ||
} | ||
cell = textAlign(cell); | ||
// add the margins on both size of the cell if there are any | ||
cell = helper.padStringCenter(cell, md.col.spaceLeft, md.col.spaceRight); | ||
cell = padStringCenter(cell, md.col.spaceLeft, md.col.spaceRight); | ||
@@ -58,2 +86,11 @@ let bold = str => str; | ||
// Check cell parameters | ||
if (md.cell) { | ||
if (md.cell.color) applyColor = getColorApplyFunction(md.cell.color); | ||
if (md.cell.backgroundColor) { | ||
applyBackColor = getBackColorApplyFunction(md.cell.backgroundColor); | ||
} | ||
if (md.cell.bold) bold = chalk.bold; | ||
} | ||
return bold(applyBackColor(applyColor(cell))); | ||
@@ -78,6 +115,7 @@ }; | ||
for (let colIndex = 0; colIndex < prd.length; colIndex += 1) { | ||
// We gather all the metadata regarding that particular cell | ||
// we gather all the metadata regarding that particular cell | ||
const cellMd = { | ||
col: pmd.columns[colIndex], | ||
row: pmd.rows.filter(r => r.index === rowIndex)[0], | ||
cell: pmd.cells.filter(c => c.row === rowIndex && c.column === colIndex)[0], | ||
}; | ||
@@ -84,0 +122,0 @@ |
@@ -26,2 +26,22 @@ const chai = require('chai'); | ||
describe('Helper - padStringRight', () => { | ||
it('should return the same string because longer than the wanted length', (done) => { | ||
const res = helper.padStringLeft('test string', 5); | ||
expect(res).equal('test string'); | ||
done(); | ||
}); | ||
it('should return an empty string of the wanted length', (done) => { | ||
const res = helper.padStringLeft('', 5); | ||
expect(res).equal(' '); | ||
done(); | ||
}); | ||
it('should return a padded string with the correct length', (done) => { | ||
const res = helper.padStringLeft('test string', 14); | ||
expect(res).equal(' test string'); | ||
done(); | ||
}); | ||
}); | ||
describe('Helper - padStringCenter', () => { | ||
@@ -28,0 +48,0 @@ it('should return the same string because no spaces', (done) => { |
@@ -20,7 +20,8 @@ const chai = require('chai'); | ||
spaceRight: 2, | ||
type: 'fixedContent', | ||
type: 'left', | ||
backgroundColor: { hex: '#22BB44' }, | ||
textAlign: 'right', | ||
}, | ||
{ spaceLeft: 0, spaceRight: 2, type: 'fixedAbsolute', size: 15 }, | ||
{ spaceLeft: 0, spaceRight: 2, type: 'fixedAbsolute', size: 20 }, | ||
{ spaceLeft: 0, spaceRight: 0, type: 'fixedAbsolute', size: 20 }, | ||
], | ||
@@ -32,2 +33,16 @@ rows: [ | ||
], | ||
cells: [ | ||
{ | ||
row: 0, | ||
column: 0, | ||
textAlign: 'left', | ||
}, | ||
{ | ||
row: 0, | ||
column: 2, | ||
color: { h: 110, s: 100, l: 50 }, | ||
backgroundColor: { hex: '#AABB44' }, | ||
textAlign: 'right', | ||
}, | ||
], | ||
}; | ||
@@ -34,0 +49,0 @@ |
35722
908
17