Skip to content

Commit 5978cce

Browse files
committed
Make row splitting code more readable
1 parent 02e45b9 commit 5978cce

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/ascii-data-table.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as R from './functions'
22
import repeat from 'core-js/library/fn/string/repeat'
33

44
const len = (val) => typeof val === 'undefined' ? 0 : ('' + val).length
5-
const padString = (character, width) => !width ? '' : repeat(character, width)
5+
const padString = (character) => (width) => !width ? '' : repeat(character, width)
6+
const spacePad = padString(' ')
67
const stringifyRows = (rows) => {
78
if (!Array.isArray(rows) || !rows.length) return []
89
return rows.map((row) => row.map(JSON.stringify))
@@ -14,7 +15,7 @@ const getThinSeparatorLine = (colWidths) => getSeparatorLine('─', '├', '┼'
1415
const getBottomSeparatorLine = (colWidths) => getSeparatorLine('─', '└', '┴', '┘', colWidths)
1516
const getSeparatorLine = (horChar, leftChar, crossChar, rightChar, colWidths) => {
1617
return leftChar + colWidths.map(function (w) {
17-
return padString(horChar, w)
18+
return padString(horChar)(w)
1819
}).join(crossChar) + rightChar
1920
}
2021

@@ -37,22 +38,22 @@ const rowHeights = (maxWidth, input) => {
3738
})
3839
}
3940

40-
const splitRowsToLines = (maxWidth, heights, widths, input) => {
41+
const rowsToLines = (maxWidth, heights, widths, input) => {
42+
const columnToLinesWidths = columnToLines(widths, maxWidth)
4143
return input.map((row, i) => {
42-
return row.map((col, colIndex) => {
43-
let lines = R.splitEvery(maxWidth, col)
44-
const lastLinesLen = len(R.last(lines))
45-
if (lastLinesLen < widths[colIndex]) {
46-
lines[lines.length - 1] = lines[lines.length - 1] + padString(' ', widths[colIndex] - lastLinesLen)
47-
}
48-
while (lines.length < heights[i]) {
49-
lines = [].concat(...lines, [padString(' ', widths[colIndex])])
50-
}
51-
return lines
52-
})
44+
return row.map(columnToLinesWidths(heights[i]))
5345
})
5446
}
5547

48+
const columnToLines = (widths, maxWidth) => (rowHeight) => (col, colIndex) => {
49+
let lines = R.splitEvery(maxWidth, col)
50+
lines[lines.length - 1] += spacePad(widths[colIndex] - len(R.last(lines)))
51+
while (lines.length < rowHeight) {
52+
lines.push(spacePad(widths[colIndex]))
53+
}
54+
return lines
55+
}
56+
5657
const createLines = (rows) => {
5758
return rows.reduce((lines, row) => {
5859
if (!Array.isArray(row)) {
@@ -63,14 +64,14 @@ const createLines = (rows) => {
6364
}, [])
6465
}
6566

66-
const renderForWidth = (rows, maxColWidth = 30, minColWidth = 3) => {
67+
const main = (rows, maxColWidth = 30, minColWidth = 3) => {
6768
if (!Array.isArray(rows) || !rows.length) {
6869
return ''
6970
}
7071
maxColWidth = parseInt(maxColWidth)
7172
const widths = colWidths(maxColWidth, minColWidth, rows)
7273
const heights = rowHeights(maxColWidth, rows)
73-
const norm = splitRowsToLines(maxColWidth, heights, widths, rows)
74+
const norm = rowsToLines(maxColWidth, heights, widths, rows)
7475
const header = createLines(R.head(norm))
7576
const separated = R.intersperse(getThinSeparatorLine(widths), R.tail(norm))
7677
const lines = createLines(separated)
@@ -85,7 +86,7 @@ const renderForWidth = (rows, maxColWidth = 30, minColWidth = 3) => {
8586

8687
export default {
8788
serializeData: (rows) => stringifyRows(rows),
88-
tableFromSerializedData: (serializedRows, maxColumnWidth = 30) => renderForWidth(serializedRows, maxColumnWidth),
89-
table: (rows, maxColumnWidth = 30) => renderForWidth(stringifyRows(rows), maxColumnWidth),
89+
tableFromSerializedData: (serializedRows, maxColumnWidth = 30) => main(serializedRows, maxColumnWidth),
90+
table: (rows, maxColumnWidth = 30) => main(stringifyRows(rows), maxColumnWidth),
9091
maxColumnWidth: (rows) => R.apply(Math.max, colWidths(0, 0, stringifyRows(rows)))
9192
}

0 commit comments

Comments
 (0)