Skip to content

Commit

Permalink
refactor: restore function names (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
realityking authored Apr 26, 2021
1 parent 2a5796e commit 0553391
Show file tree
Hide file tree
Showing 46 changed files with 212 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/alignString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const alignCenter = (subject: string, width: number): string => {
* Pads a string to the left and/or right to position the subject
* text in a desired alignment within a container.
*/
export default (subject: string, containerWidth: number, alignment: ColumnUserConfig['alignment']): string => {
export const alignString = (subject: string, containerWidth: number, alignment: ColumnUserConfig['alignment']): string => {
const subjectWidth = stringWidth(subject);

if (subjectWidth > containerWidth) {
Expand Down
6 changes: 4 additions & 2 deletions src/alignTableData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import stringWidth from 'string-width';
import alignString from './alignString';
import {
alignString,
} from './alignString';
import type {
BaseConfig,
Row,
} from './types/internal';

export default (rows: Row[], config: BaseConfig): Row[] => {
export const alignTableData = (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((row) => {
return row.map((cell, cellIndex) => {
const column = config.columns[cellIndex];
Expand Down
6 changes: 4 additions & 2 deletions src/calculateCellHeight.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import wrapCell from './wrapCell';
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 => {
export const calculateCellHeight = (value: string, columnWidth: number, useWrapWord = false): number => {
return wrapCell(value, columnWidth, useWrapWord).length;
};
2 changes: 1 addition & 1 deletion src/calculateCellWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
/**
* Calculates width of each cell contents in a row.
*/
export default (cells: Cell[]): number[] => {
export const calculateCellWidths = (cells: Cell[]): number[] => {
return cells.map((cell) => {
return Math.max(
...cell.split('\n').map(stringWidth),
Expand Down
4 changes: 3 additions & 1 deletion src/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import calculateCellWidths from './calculateCellWidths';
import {
calculateCellWidths,
} from './calculateCellWidths';
import type {
Row,
} from './types/internal';
Expand Down
6 changes: 4 additions & 2 deletions src/calculateRowHeights.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import calculateCellHeight from './calculateCellHeight';
import {
calculateCellHeight,
} from './calculateCellHeight';
import type {
BaseConfig,
Row,
Expand All @@ -7,7 +9,7 @@ import type {
/**
* Produces an array of values that describe the largest value length (height) in every row.
*/
export default (rows: Row[], config: BaseConfig): number[] => {
export const calculateRowHeights = (rows: Row[], config: BaseConfig): number[] => {
return rows.map((row) => {
let rowHeight = 1;

Expand Down
34 changes: 25 additions & 9 deletions src/createStream.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import alignTableData from './alignTableData';
import calculateRowHeights from './calculateRowHeights';
import {
alignTableData,
} from './alignTableData';
import {
calculateRowHeights,
} from './calculateRowHeights';
import {
drawBorderBottom,
drawBorderJoin,
drawBorderTop,
} from './drawBorder';
import drawRow from './drawRow';
import makeStreamConfig from './makeStreamConfig';
import mapDataUsingRowHeights from './mapDataUsingRowHeights';
import padTableData from './padTableData';
import stringifyTableData from './stringifyTableData';
import truncateTableData from './truncateTableData';
import {
drawRow,
} from './drawRow';
import {
makeStreamConfig,
} from './makeStreamConfig';
import {
mapDataUsingRowHeights,
} from './mapDataUsingRowHeights';
import {
padTableData,
} from './padTableData';
import {
stringifyTableData,
} from './stringifyTableData';
import {
truncateTableData,
} from './truncateTableData';
import type {
StreamUserConfig,
WritableStream,
Expand Down Expand Up @@ -76,7 +92,7 @@ const append = (row: Row, columnWidths: number[], config: StreamConfig) => {
process.stdout.write(output);
};

export default (userConfig: StreamUserConfig): WritableStream => {
export const createStream = (userConfig: StreamUserConfig): WritableStream => {
const config = makeStreamConfig(userConfig);

const columnWidths = Object.values(config.columns).map((column) => {
Expand Down
4 changes: 3 additions & 1 deletion src/drawBorder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import drawContent from './drawContent';
import {
drawContent,
} from './drawContent';
import type {
DrawVerticalLine,
} from './types/api';
Expand Down
4 changes: 2 additions & 2 deletions src/drawContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SeparatorConfig = {
* Shared function to draw horizontal borders, rows or the entire table
*/

export default function drawContent (contents: string[], separatorConfig: SeparatorConfig): string {
export const drawContent = (contents: string[], separatorConfig: SeparatorConfig): string => {
const {startSeparator, middleSeparator, endSeparator, drawSeparator} = separatorConfig;
const contentSize = contents.length;
const result: string[] = [];
Expand All @@ -32,4 +32,4 @@ export default function drawContent (contents: string[], separatorConfig: Separa
}

return result.join('');
}
};
6 changes: 4 additions & 2 deletions src/drawRow.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import drawContent from './drawContent';
import {
drawContent,
} from './drawContent';
import type {
DrawVerticalLine,
} from './types/api';
import type {
BodyBorderConfig, Row,
} from './types/internal';

export default (row: Row, config: {
export const drawRow = (row: Row, config: {
border: BodyBorderConfig,
drawVerticalLine: DrawVerticalLine,
}): string => {
Expand Down
10 changes: 7 additions & 3 deletions src/drawTable.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {
drawBorderTop, drawBorderJoin, drawBorderBottom,
} from './drawBorder';
import drawContent from './drawContent';
import drawRow from './drawRow';
import {
drawContent,
} from './drawContent';
import {
drawRow,
} from './drawRow';
import type {
TableConfig, Row,
} from './types/internal';
import {
groupBySizes,
} from './utils';

export default (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig): string => {
export const drawTable = (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig): string => {
const {
drawHorizontalLine,
singleLine,
Expand Down
2 changes: 1 addition & 1 deletion src/getBorderCharacters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
BorderConfig,
} from './types/api';

export default (name: string): BorderConfig => {
export const getBorderCharacters = (name: string): BorderConfig => {
if (name === 'honeywell') {
return {
topBody: '═',
Expand Down
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import createStream from './createStream';
import getBorderCharacters from './getBorderCharacters';
import table from './table';
import {
createStream,
} from './createStream';
import {
getBorderCharacters,
} from './getBorderCharacters';
import {
table,
} from './table';

export {
table,
Expand All @@ -9,4 +15,3 @@ export {
};

export * from './types/api';

6 changes: 4 additions & 2 deletions src/makeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import type {
import {
makeBorder,
} from './utils';
import validateConfig from './validateConfig';
import {
validateConfig,
} from './validateConfig';

/**
* Creates a configuration for every column using default
Expand Down Expand Up @@ -40,7 +42,7 @@ const makeColumns = (rows: Row[],
* using default values for the missing configuration properties.
*/

export default (rows: Row[], userConfig: TableUserConfig = {}): TableConfig => {
export const makeConfig = (rows: Row[], userConfig: TableUserConfig = {}): TableConfig => {
validateConfig('config.json', userConfig);

const config = cloneDeep(userConfig);
Expand Down
6 changes: 4 additions & 2 deletions src/makeStreamConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import type {
import {
makeBorder,
} from './utils';
import validateConfig from './validateConfig';
import {
validateConfig,
} from './validateConfig';

/**
* Creates a configuration for every column using default
Expand All @@ -37,7 +39,7 @@ const makeColumns = (columnCount: number,
* Makes a new configuration object out of the userConfig object
* using default values for the missing configuration properties.
*/
export default (userConfig: StreamUserConfig): StreamConfig => {
export const makeStreamConfig = (userConfig: StreamUserConfig): StreamConfig => {
validateConfig('streamConfig.json', userConfig);

const config = cloneDeep(userConfig);
Expand Down
6 changes: 4 additions & 2 deletions src/mapDataUsingRowHeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import type {
BaseConfig,
Row,
} from './types/internal';
import wrapCell from './wrapCell';
import {
wrapCell,
} from './wrapCell';

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

const mappedRows = unmappedRows.map((unmappedRow, unmappedRowIndex) => {
Expand Down
2 changes: 1 addition & 1 deletion src/padTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
Row,
} from './types/internal';

export default (rows: Row[], config: BaseConfig): Row[] => {
export const padTableData = (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((cells) => {
return cells.map((cell, cellIndex) => {
const column = config.columns[cellIndex];
Expand Down
2 changes: 1 addition & 1 deletion src/stringifyTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
normalizeString,
} from './utils';

export default (rows: unknown[][]): Row[] => {
export const stringifyTableData = (rows: unknown[][]): Row[] => {
return rows.map((cells) => {
return cells.map((cell) => {
return normalizeString(String(cell));
Expand Down
42 changes: 31 additions & 11 deletions src/table.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import alignTableData from './alignTableData';
import calculateCellWidths from './calculateCellWidths';
import calculateRowHeights from './calculateRowHeights';
import drawTable from './drawTable';
import makeConfig from './makeConfig';
import mapDataUsingRowHeights from './mapDataUsingRowHeights';
import padTableData from './padTableData';
import stringifyTableData from './stringifyTableData';
import truncateTableData from './truncateTableData';
import {
alignTableData,
} from './alignTableData';
import {
calculateCellWidths,
} from './calculateCellWidths';
import {
calculateRowHeights,
} from './calculateRowHeights';
import {
drawTable,
} from './drawTable';
import {
makeConfig,
} from './makeConfig';
import {
mapDataUsingRowHeights,
} from './mapDataUsingRowHeights';
import {
padTableData,
} from './padTableData';
import {
stringifyTableData,
} from './stringifyTableData';
import {
truncateTableData,
} from './truncateTableData';
import type {
TableUserConfig,
} from './types/api';
import validateTableData from './validateTableData';
import {
validateTableData,
} from './validateTableData';

export default (data: unknown[][], userConfig: TableUserConfig = {}): string => {
export const table = (data: unknown[][], userConfig: TableUserConfig = {}): string => {
validateTableData(data);

let rows = stringifyTableData(data);
Expand Down
2 changes: 1 addition & 1 deletion src/truncateTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
/**
* @todo Make it work with ASCII content.
*/
export default (rows: Row[], config: BaseConfig): Row[] => {
export const truncateTableData = (rows: Row[], config: BaseConfig): Row[] => {
return rows.map((cells) => {
return cells.map((cell, cellIndex) => {
return truncate(cell, {
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import slice from 'slice-ansi';
import stripAnsi from 'strip-ansi';
import getBorderCharacters from './getBorderCharacters';
import {
getBorderCharacters,
} from './getBorderCharacters';
import type {
BorderConfig, BorderUserConfig,
} from './types/api';
Expand Down
2 changes: 1 addition & 1 deletion src/validateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
TableUserConfig,
} from './types/api';

export default (schemaId: 'config.json'|'streamConfig.json', config: TableUserConfig): void => {
export const validateConfig = (schemaId: 'config.json'|'streamConfig.json', config: TableUserConfig): void => {
const validate = validators[schemaId] as ValidateFunction;
if (!validate(config) && validate.errors) {
const errors = validate.errors.map((error: ErrorObject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/validateTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
normalizeString,
} from './utils';

export default (rows: unknown[][]): void => {
export const validateTableData = (rows: unknown[][]): void => {
if (!Array.isArray(rows)) {
throw new TypeError('Table data must be an array.');
}
Expand Down
10 changes: 7 additions & 3 deletions src/wrapCell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {
splitAnsi,
} from './utils';
import wrapString from './wrapString';
import wrapWord from './wrapWord';
import {
wrapString,
} from './wrapString';
import {
wrapWord,
} from './wrapWord';

/**
* Wrap a single cell value into a list of lines
Expand All @@ -11,7 +15,7 @@ import wrapWord from './wrapWord';
* depending on user configuration.
*
*/
export default (cellValue: string, cellWidth: number, useWrapWord: boolean): string[] => {
export const wrapCell = (cellValue: string, cellWidth: number, useWrapWord: boolean): string[] => {
// First split on literal newlines
const cellLines = splitAnsi(cellValue);

Expand Down
Loading

0 comments on commit 0553391

Please sign in to comment.