Comparing version 0.2.0 to 0.3.0
29
index.js
@@ -5,3 +5,3 @@ const bpad = require('bpad') | ||
const headerStr = header | ||
.map(text => bpad(text, settings.padLength)) | ||
.map((text, i) => bpad(text, settings.padLength[i])) | ||
.join('|') | ||
@@ -16,10 +16,10 @@ | ||
const alignmentStr = alignment | ||
.map(align => { | ||
.map((align, i) => { | ||
switch (align) { | ||
case 'L': | ||
return `:${'-'.repeat(settings.padLength - 1)}` | ||
return `:${'-'.repeat(settings.padLength[i] - 1)}` | ||
case 'C': | ||
return `:${'-'.repeat(settings.padLength - 2)}:` | ||
return `:${'-'.repeat(settings.padLength[i] - 2)}:` | ||
case 'R': | ||
return `${'-'.repeat(settings.padLength - 1)}:` | ||
return `${'-'.repeat(settings.padLength[i] - 1)}:` | ||
} | ||
@@ -36,3 +36,3 @@ }) | ||
const rowStr = row | ||
.map(text => bpad(text, settings.padLength)) | ||
.map((text, i) => bpad(text, settings.padLength[i])) | ||
.join('|') | ||
@@ -52,9 +52,22 @@ | ||
module.exports = function generateTable (table, settings) { | ||
const columns = table.header.length | ||
const longest = [ | ||
...table.header, | ||
...[].concat(...table.rows) | ||
].reduce((acc, str) => str.length > acc ? str.length : acc, 1) | ||
].reduce((acc, str, idx) => { | ||
// need a column index | ||
const i = idx % columns | ||
return str.length > acc[i] | ||
? [ ...acc.slice(0, i), | ||
str.length, | ||
...acc.slice(i + 1) | ||
] | ||
: acc | ||
}, new Array(columns).fill(1)) | ||
const s = Object.assign({}, settings, { | ||
padLength: (settings.padding * 2) + longest | ||
borders: columns < 2 ? true : settings.borders, | ||
padLength: longest.map(columnLongest => (settings.padding * 2) + columnLongest) | ||
}) | ||
@@ -61,0 +74,0 @@ |
const mdtable = require('./') | ||
test('generates a basic table', () => { | ||
const base_table = { | ||
header: ['A', 'B', 'C'], | ||
alignment: ['L', 'L', 'L'], | ||
rows: [ | ||
['1', '2', '3'] | ||
] | ||
} | ||
test('basic table', () => { | ||
const settings = { | ||
borders: true, | ||
padding: 1 | ||
} | ||
const expected = ` | ||
| A | B | C | | ||
|:--|:--|:--| | ||
| 1 | 2 | 3 | | ||
` | ||
expect(mdtable(base_table, settings)).toBe(expected.trim()) | ||
}) | ||
test('table with extra padding', () => { | ||
const settings = { | ||
borders: true, | ||
padding: 3 | ||
} | ||
const expected = ` | ||
| A | B | C | | ||
|:------|:------|:------| | ||
| 1 | 2 | 3 | | ||
` | ||
expect(mdtable(base_table, settings)).toBe(expected.trim()) | ||
}) | ||
test('table without (outer) borders', () => { | ||
const settings = { | ||
borders: false, | ||
padding: 1 | ||
} | ||
const expected =` A | B | C | ||
:--|:--|:-- | ||
1 | 2 | 3 ` | ||
expect(mdtable(base_table, settings)).toBe(expected) | ||
}) | ||
test('table with different alignments', () => { | ||
const table = Object.assign({}, base_table, { | ||
alignment: ['L', 'C', 'R'] | ||
}) | ||
const settings = { | ||
borders: true, | ||
padding: 1 | ||
} | ||
const expected = ` | ||
| A | B | C | | ||
|:--|:-:|--:| | ||
| 1 | 2 | 3 | | ||
` | ||
expect(mdtable(table, settings)).toBe(expected.trim()) | ||
}) | ||
test('table with forced borders (< 2 columns)', () => { | ||
const table = { | ||
header: ['A', 'B', 'C'], | ||
alignment: ['L', 'L', 'L'], | ||
header: ['A'], | ||
alignment: ['L'], | ||
rows: [ | ||
['1', '2', '3'] | ||
['1'] | ||
] | ||
@@ -13,2 +83,21 @@ } | ||
const settings = { | ||
borders: false, | ||
padding: 1 | ||
} | ||
const expected = ` | ||
| A | | ||
|:--| | ||
| 1 | | ||
` | ||
expect(mdtable(table, settings)).toBe(expected.trim()) | ||
}) | ||
test('table with per-column padding', () => { | ||
const table = Object.assign({}, base_table, { | ||
header: ['AA', 'BBB', 'CCCC'] | ||
}) | ||
const settings = { | ||
borders: true, | ||
@@ -19,8 +108,8 @@ padding: 1 | ||
const expected = ` | ||
| A | B | C | | ||
|:--|:--|:--| | ||
| 1 | 2 | 3 | | ||
` | ||
| AA | BBB | CCCC | | ||
|:---|:----|:-----| | ||
| 1 | 2 | 3 | | ||
` | ||
expect(mdtable(table, settings)).toBe(expected.trim()) | ||
}) |
{ | ||
"name": "mdtable", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Generate markdown tables", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
117056
155