Skip to content

Commit

Permalink
Continue rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Nam Hoang Le committed Apr 25, 2021
1 parent f494a10 commit 6ad02b3
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 83 deletions.
4 changes: 2 additions & 2 deletions src/alignTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {

export default (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((row) => {
return row.map((cell, index) => {
const column = config.columns[index];
return row.map((cell, cellIndex) => {
const column = config.columns[cellIndex];

if (stringWidth(cell) === column.width) {
return cell;
Expand Down
4 changes: 2 additions & 2 deletions src/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default (rows: Row[]): number[] => {
rows.forEach((row) => {
const cellWidths = calculateCellWidths(row);

cellWidths.forEach((cellWidth, index) => {
columnWidths[index] = Math.max(columnWidths[index], cellWidth);
cellWidths.forEach((cellWidth, cellIndex) => {
columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], cellWidth);
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/calculateRowHeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default (rows: Row[], config: BaseConfig): number[] => {
return rows.map((row) => {
let rowHeight = 1;

row.forEach((cell, index) => {
const cellHeight = calculateCellHeight(cell, config.columns[index].width, config.columns[index].wrapWord);
row.forEach((cell, cellIndex) => {
const cellHeight = calculateCellHeight(cell, config.columns[cellIndex].width, config.columns[cellIndex].wrapWord);

rowHeight = Math.max(rowHeight, cellHeight);
});
Expand Down
18 changes: 9 additions & 9 deletions src/createStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const prepareData = (data: Row[], config: StreamConfig) => {
return rows;
};

const create = (row: Row, columnWidthIndex: number[], config: StreamConfig) => {
const create = (row: Row, columnWidths: number[], config: StreamConfig) => {
const rows = prepareData([row], config);

const body = rows.map((literalRow) => {
Expand All @@ -44,30 +44,30 @@ const create = (row: Row, columnWidthIndex: number[], config: StreamConfig) => {

output = '';

output += drawBorderTop(columnWidthIndex, config);
output += drawBorderTop(columnWidths, config);
output += body;
output += drawBorderBottom(columnWidthIndex, config);
output += drawBorderBottom(columnWidths, config);

output = output.trimEnd();

process.stdout.write(output);
};

const append = (row: Row, columnWidthIndex: number[], config: StreamConfig) => {
const append = (row: Row, columnWidths: number[], config: StreamConfig) => {
const rows = prepareData([row], config);

const body = rows.map((literalRow) => {
return drawRow(literalRow, config);
}).join('');

let output = '';
const bottom = drawBorderBottom(columnWidthIndex, config);
const bottom = drawBorderBottom(columnWidths, config);

if (bottom !== '\n') {
output = '\r\u001B[K';
}

output += drawBorderJoin(columnWidthIndex, config);
output += drawBorderJoin(columnWidths, config);
output += body;
output += bottom;

Expand All @@ -79,7 +79,7 @@ const append = (row: Row, columnWidthIndex: number[], config: StreamConfig) => {
export default (userConfig: StreamUserConfig): WritableStream => {
const config = makeStreamConfig(userConfig);

const columnWidthIndex = Object.values(config.columns).map((column) => {
const columnWidths = Object.values(config.columns).map((column) => {
return column.width + column.paddingLeft + column.paddingRight;
});

Expand All @@ -94,9 +94,9 @@ export default (userConfig: StreamUserConfig): WritableStream => {
if (empty) {
empty = false;

create(row, columnWidthIndex, config);
create(row, columnWidths, config);
} else {
append(row, columnWidthIndex, config);
append(row, columnWidths, config);
}
},
};
Expand Down
16 changes: 8 additions & 8 deletions src/drawBorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Separator = {
readonly join: string,
};

const drawBorder = (columnSizeIndex: number[],
const drawBorder = (columnWidths: number[],
config: {separator: Separator, drawVerticalLine: DrawVerticalLine, }): string => {
const {separator, drawVerticalLine} = config;
const columns = columnSizeIndex.map((size) => {
const columns = columnWidths.map((size) => {
return config.separator.body.repeat(size);
});

Expand All @@ -30,9 +30,9 @@ const drawBorder = (columnSizeIndex: number[],
}) + '\n';
};

const drawBorderTop = (columnSizeIndex: number[],
const drawBorderTop = (columnWidths: number[],
config: {border: TopBorderConfig, drawVerticalLine: DrawVerticalLine, }): string => {
const result = drawBorder(columnSizeIndex, {
const result = drawBorder(columnWidths, {
...config,
separator: {
body: config.border.topBody,
Expand All @@ -49,9 +49,9 @@ const drawBorderTop = (columnSizeIndex: number[],
return result;
};

const drawBorderJoin = (columnSizeIndex: number[],
const drawBorderJoin = (columnWidths: number[],
config: {border: JoinBorderConfig, drawVerticalLine: DrawVerticalLine, }): string => {
return drawBorder(columnSizeIndex, {
return drawBorder(columnWidths, {
...config,
separator: {
body: config.border.joinBody,
Expand All @@ -62,9 +62,9 @@ const drawBorderJoin = (columnSizeIndex: number[],
});
};

const drawBorderBottom = (columnSizeIndex: number[],
const drawBorderBottom = (columnWidths: number[],
config: {border: BottomBorderConfig, drawVerticalLine: DrawVerticalLine, }): string => {
return drawBorder(columnSizeIndex, {
return drawBorder(columnWidths, {
...config,
separator: {
body: config.border.bottomBody,
Expand Down
4 changes: 2 additions & 2 deletions src/drawContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export default function drawContent (contents: string[], separatorConfig: Separa
result.push(startSeparator);
}

contents.forEach((content, index) => {
contents.forEach((content, contentIndex) => {
result.push(content);

// Only append the middle separator if the content is not the last
if (index + 1 < contentSize && drawSeparator(index + 1, contentSize)) {
if (contentIndex + 1 < contentSize && drawSeparator(contentIndex + 1, contentSize)) {
result.push(middleSeparator);
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/makeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ const makeColumns = (rows: Row[],
columnDefault?: ColumnUserConfig): Indexable<ColumnConfig> => {
const columnWidths = calculateColumnWidths(rows);

return rows[0].map((_cell, index) => {
return rows[0].map((_, columnIndex) => {
return {
alignment: 'left',
paddingLeft: 1,
paddingRight: 1,
truncate: Number.POSITIVE_INFINITY,
width: columnWidths[index],
width: columnWidths[columnIndex],
wrapWord: false,
...columnDefault,
...columns?.[index],
...columns?.[columnIndex],
};
});
};
Expand Down
6 changes: 3 additions & 3 deletions src/padTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {

export default (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((cells) => {
return cells.map((value, index1) => {
const column = config.columns[index1];
return cells.map((cell, cellIndex) => {
const column = config.columns[cellIndex];

return ' '.repeat(column.paddingLeft) + value + ' '.repeat(column.paddingRight);
return ' '.repeat(column.paddingLeft) + cell + ' '.repeat(column.paddingRight);
});
});
};
6 changes: 3 additions & 3 deletions src/truncateTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
*/
export default (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((cells) => {
return cells.map((content, index) => {
return truncate(content, {
length: config.columns[index].truncate,
return cells.map((cell, cellIndex) => {
return truncate(cell, {
length: config.columns[cellIndex].truncate,
omission: '…',
});
});
Expand Down
14 changes: 7 additions & 7 deletions test/calculateCellWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import {
} from 'chai';
import calculateCellWidths from '../src/calculateCellWidths';

describe('calculateCellWidthIndex', () => {
describe('calculateCellWidths', () => {
context('all cells have different width', () => {
it('describes each cell contents width', () => {
const cellWidthIndex = calculateCellWidths([
const cellWidths = calculateCellWidths([
'a',
'aaa',
'aaaaaa',
]);

expect(cellWidthIndex[0]).to.equal(1, 'first column');
expect(cellWidthIndex[1]).to.equal(3, 'second column');
expect(cellWidthIndex[2]).to.equal(6, 'third column');
expect(cellWidths[0]).to.equal(1, 'first column');
expect(cellWidths[1]).to.equal(3, 'second column');
expect(cellWidths[2]).to.equal(6, 'third column');
});
});
context('cell contains newline characters', () => {
it('picks the longest line length', () => {
const cellWidthIndex = calculateCellWidths([
const cellWidths = calculateCellWidths([
'aaaa\naa',
]);

expect(cellWidthIndex[0]).to.equal(4);
expect(cellWidths[0]).to.equal(4);
});
});
});
18 changes: 9 additions & 9 deletions test/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import {
expect,
} from 'chai';
import chalk from 'chalk';
import calculateColumnWidth from '../src/calculateColumnWidths';
import calculateColumnWidths from '../src/calculateColumnWidths';

describe('calculateMaximumColumnWidthIndex', () => {
describe('calculateColumnWidths', () => {
it('throws an error when attempting to calculate maximum column value index for an empty data set', () => {
expect(() => {
calculateColumnWidth([]);
calculateColumnWidths([]);
}).to.throw(Error, 'Dataset must have at least one row.');
});
it('calculates the maximum column value index', () => {
const maximumColumnValueIndex = calculateColumnWidth([
const columnWidths = calculateColumnWidths([
[
'',
'a',
Expand All @@ -32,28 +32,28 @@ describe('calculateMaximumColumnWidthIndex', () => {
],
]);

expect(maximumColumnValueIndex).to.deep.equal([0, 1, 10, 5]);
expect(columnWidths).to.deep.equal([0, 1, 10, 5]);
});
context('cell values contain ANSI codes', () => {
it('uses visual width of the string', () => {
const maximumColumnValueIndex = calculateColumnWidth([
const columnWidths = calculateColumnWidths([
[
chalk.red('aaaaa'),
],
]);

expect(maximumColumnValueIndex[0]).to.equal(5);
expect(columnWidths[0]).to.equal(5);
});
});
context('cell values contain fullwidth characters', () => {
it('uses visual width of the string', () => {
const maximumColumnValueIndex = calculateColumnWidth([
const columnWidths = calculateColumnWidths([
[
chalk.red('古'),
],
]);

expect(maximumColumnValueIndex[0]).to.equal(2);
expect(columnWidths[0]).to.equal(2);
});
});
});
14 changes: 7 additions & 7 deletions test/calculateRowHeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import calculateRowHeights from '../src/calculateRowHeights';
import makeConfig from '../src/makeConfig';

describe('calculateRowHeightIndex', () => {
describe('calculateRowHeights', () => {
context('single column', () => {
context('cell content width is lesser than column width', () => {
it('is equal to 1', () => {
Expand All @@ -21,9 +21,9 @@ describe('calculateRowHeightIndex', () => {
},
});

const rowSpanIndex = calculateRowHeights(data, config);
const rowHeights = calculateRowHeights(data, config);

expect(rowSpanIndex[0]).to.equal(1);
expect(rowHeights[0]).to.equal(1);
});
});
context('cell content width is twice the size of the column width', () => {
Expand All @@ -39,9 +39,9 @@ describe('calculateRowHeightIndex', () => {
},
});

const rowSpanIndex = calculateRowHeights(data, config);
const rowHeights = calculateRowHeights(data, config);

expect(rowSpanIndex[0]).to.equal(2);
expect(rowHeights[0]).to.equal(2);
});
});
});
Expand All @@ -62,9 +62,9 @@ describe('calculateRowHeightIndex', () => {
},
});

const rowSpanIndex = calculateRowHeights(data, config);
const rowHeights = calculateRowHeights(data, config);

expect(rowSpanIndex[0]).to.equal(3);
expect(rowHeights[0]).to.equal(3);
});
});
});
Expand Down

0 comments on commit 6ad02b3

Please sign in to comment.