Skip to content

Commit

Permalink
refactor: refine variable/file names (#164)
Browse files Browse the repository at this point in the history
* Remove 'index', 'span' words

* Rename in mapData

* Continue rename

Co-authored-by: Nam Hoang Le <nam.hoang.le@mgm-tp.com>
  • Loading branch information
nam-hle and Nam Hoang Le committed Apr 25, 2021
1 parent ea61184 commit 8148dde
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 209 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
3 changes: 3 additions & 0 deletions src/calculateCellHeight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import wrapCell from './wrapCell';

/**
* Calculates height of cell content in regard to its width and word wrapping.
*/
export default (value: string, columnWidth: number, useWrapWord = false): number => {
return wrapCell(value, columnWidth, useWrapWord).length;
};
8 changes: 3 additions & 5 deletions src/calculateCellWidthIndex.ts → src/calculateCellWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import type {
} from './types/internal';

/**
* Calculates width of each cell contents.
* Calculates width of each cell contents in a row.
*/
export default (cells: Cell[]): number[] => {
return cells.map((value) => {
return cells.map((cell) => {
return Math.max(
...value.split('\n').map((line) => {
return stringWidth(line);
}),
...cell.split('\n').map(stringWidth),
);
});
};
25 changes: 25 additions & 0 deletions src/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import calculateCellWidths from './calculateCellWidths';
import type {
Row,
} from './types/internal';

/**
* Produces an array of values that describe the largest value length (width) in every column.
*/
export default (rows: Row[]): number[] => {
if (!rows[0]) {
throw new Error('Dataset must have at least one row.');
}

const columnWidths = new Array(rows[0].length).fill(0);

rows.forEach((row) => {
const cellWidths = calculateCellWidths(row);

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

return columnWidths;
};
27 changes: 0 additions & 27 deletions src/calculateMaximumColumnWidthIndex.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/calculateRowHeightIndex.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/calculateRowHeights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import calculateCellHeight from './calculateCellHeight';
import type {
BaseConfig,
Row,
} from './types/internal';

/**
* Produces an array of values that describe the largest value length (height) in every row.
*/
export default (rows: Row[], config: BaseConfig): number[] => {
return rows.map((row) => {
let rowHeight = 1;

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

rowHeight = Math.max(rowHeight, cellHeight);
});

return rowHeight;
});
};
26 changes: 13 additions & 13 deletions src/createStream.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import alignTableData from './alignTableData';
import calculateRowHeightIndex from './calculateRowHeightIndex';
import calculateRowHeights from './calculateRowHeights';
import {
drawBorderBottom,
drawBorderJoin,
drawBorderTop,
} from './drawBorder';
import drawRow from './drawRow';
import makeStreamConfig from './makeStreamConfig';
import mapDataUsingRowHeightIndex from './mapDataUsingRowHeightIndex';
import mapDataUsingRowHeights from './mapDataUsingRowHeights';
import padTableData from './padTableData';
import stringifyTableData from './stringifyTableData';
import truncateTableData from './truncateTableData';
Expand All @@ -24,16 +24,16 @@ const prepareData = (data: Row[], config: StreamConfig) => {

rows = truncateTableData(rows, config);

const rowHeightIndex = calculateRowHeightIndex(rows, config);
const rowHeights = calculateRowHeights(rows, config);

rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config);
rows = mapDataUsingRowHeights(rows, rowHeights, config);
rows = alignTableData(rows, config);
rows = padTableData(rows, config);

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
10 changes: 5 additions & 5 deletions src/makeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cloneDeep from 'lodash.clonedeep';
import calculateMaximumColumnWidthIndex from './calculateMaximumColumnWidthIndex';
import calculateColumnWidths from './calculateColumnWidths';
import getBorderCharacters from './getBorderCharacters';
import type {
ColumnUserConfig, Indexable,
Expand Down Expand Up @@ -27,18 +27,18 @@ const makeBorder = (border: BorderUserConfig | undefined): BorderConfig => {
const makeColumns = (rows: Row[],
columns?: Indexable<ColumnUserConfig>,
columnDefault?: ColumnUserConfig): Indexable<ColumnConfig> => {
const maximumColumnWidthIndex = calculateMaximumColumnWidthIndex(rows);
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: maximumColumnWidthIndex[index],
width: columnWidths[columnIndex],
wrapWord: false,
...columnDefault,
...columns?.[index],
...columns?.[columnIndex],
};
});
};
Expand Down
32 changes: 0 additions & 32 deletions src/mapDataUsingRowHeightIndex.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/mapDataUsingRowHeights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import flatten from 'lodash.flatten';
import type {
BaseConfig,
Row,
} from './types/internal';
import wrapCell from './wrapCell';

export default (unmappedRows: Row[], rowHeights: number[], config: BaseConfig): Row[] => {
const tableWidth = unmappedRows[0].length;

const mappedRows = unmappedRows.map((unmappedRow, unmappedRowIndex) => {
const outputRowHeight = rowHeights[unmappedRowIndex];
const outputRow: Row[] = Array.from({length: outputRowHeight}, () => {
return new Array(tableWidth).fill('');
});

// rowHeight
// [{row index within rowSaw; index2}]
// [{cell index within a virtual row; index1}]

unmappedRow.forEach((cell, cellIndex) => {
const cellLines = wrapCell(cell, config.columns[cellIndex].width, config.columns[cellIndex].wrapWord);

cellLines.forEach((cellLine, cellLineIndex) => {
outputRow[cellLineIndex][cellIndex] = cellLine;
});
});

return outputRow;
});

return flatten(mappedRows);
};

0 comments on commit 8148dde

Please sign in to comment.