feat(table): update type definitions for v5.0 (#43425)

This commit is contained in:
Daniel Perez Alvarez
2020-04-01 14:28:27 -07:00
committed by GitHub
parent ef139d18d3
commit 4fb709457d
2 changed files with 47 additions and 22 deletions
+25 -18
View File
@@ -1,21 +1,13 @@
// Type definitions for table 4.0
// Type definitions for table 5.0
// Project: https://github.com/gajus/table
// Definitions by: Evan Shortiss <https://github.com/evanshortiss>
// mrmlnc <https://github.com/mrmlnc>
// Daniel Perez Alvarez <https://github.com/unindented>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export type BorderType = 'honeywell' | 'norc' | 'ramac' | 'void';
export type TableBorderTemplate = 'honeywell' | 'norc' | 'ramac' | 'void';
export interface ColumnConfig {
alignment?: 'left' | 'center' | 'right';
paddingLeft?: number;
paddingRight?: number;
truncate?: number;
width?: number;
wrapWord?: boolean;
}
export interface JoinStruct {
export interface TableBorder {
topBody?: string;
topJoin?: string;
topLeft?: string;
@@ -36,19 +28,34 @@ export interface JoinStruct {
joinJoin?: string;
}
export interface TableColumns {
alignment?: 'left' | 'center' | 'right';
paddingLeft?: number;
paddingRight?: number;
truncate?: number;
width?: number;
wrapWord?: boolean;
}
export type TableDrawHorizontalLine = (index: number, size: number) => boolean;
export interface TableUserConfig {
border?: TableBorder;
columns?: {
[index: number]: ColumnConfig
[index: number]: TableColumns;
};
drawHorizontalLine?: (index: number, size: number) => boolean;
border?: JoinStruct;
columnDefault?: ColumnConfig;
columnDefault?: TableColumns;
columnCount?: number;
drawHorizontalLine?: TableDrawHorizontalLine;
singleLine?: boolean;
}
export interface TableStream {
write(row: string[]): void;
}
export function table(data: any[], userConfig?: TableUserConfig): string;
export function createStream(userConfig: TableUserConfig): { write: string[] };
export function createStream(userConfig: TableUserConfig): TableStream;
export function getBorderCharacters(templateName: BorderType): JoinStruct;
export function getBorderCharacters(templateName: TableBorderTemplate): TableBorder;
+22 -4
View File
@@ -1,17 +1,35 @@
import * as table from 'table';
const border: table.JoinStruct = table.getBorderCharacters('norc');
const border: table.TableBorder = table.getBorderCharacters('norc');
const data = [
['first name', 'last name'],
['jane', 'doe'],
['john', 'doe']
['john', 'doe'],
];
const config: table.TableUserConfig = {
border,
columnCount: 1,
drawHorizontalLine: () => true
columnDefault: {
width: 50,
},
columnCount: 3,
columns: {
0: {
width: 10,
alignment: 'right',
},
1: {
alignment: 'center',
},
2: {
width: 10,
},
},
drawHorizontalLine: (index, size) => index === 0 || index === 1 || index === size - 1 || index === size,
};
table.table(data, config);
const stream = table.createStream(config);
stream.write(['john', 'john']);