// Type definitions for easy-table // Project: https://github.com/eldargab/easy-table // Definitions by: Niklas Mollenhauer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare class EasyTable { /** * String to separate columns */ public separator: string; /** * Default printer */ public static string(value: any): string; /** * Create a printer which right aligns the content by padding with `ch` on the left * * @param {String} ch * @returns {Function} */ public static leftPadder(ch: string): CellPrinter; public static padLeft: CellPrinter; /** * Create a printer which pads with `ch` on the right * * @param {String} ch * @returns {Function} */ public static rightPadder(ch: string): CellPrinter; // public static padRight: CellPrinter; /** * Create a printer for numbers * * Will do right alignment and optionally fix the number of digits after decimal point * * @param {Number} [digits] - Number of digits for fixpoint notation * @returns {Function} */ public static number(digits?: number): CellPrinter; public constructor(); /** * Push the current row to the table and start a new one * * @returns {Table} `this` */ public newRow(): EasyTable; /** * Write cell in the current row * * @param {String} col - Column name * @param {Any} val - Cell value * @param {Function} [printer] - Printer function to format the value * @returns {Table} `this` */ public cell(col: string, val: T, printer?: CellPrinter): EasyTable; /** * Get list of columns in printing order * * @returns {string[]} */ public columns(): string[]; /** * Format just rows, i.e. print the table without headers and totals * * @returns {String} String representaion of the table */ public print(): string; /** * Format the table * * @returns {String} */ public toString(): string; /** * Push delimeter row to the table (with each cell filled with dashs during printing) * * @param {String[]} [cols] * @returns {Table} `this` */ public pushDelimeter(cols?: string[]): EasyTable; /** * Compute all totals and yield the results to `cb` * * @param {Function} cb - Callback function with signature `(column, value, printer)` */ public forEachTotal(cb: (column: string, value: T, printer: CellPrinter) => void): void; /** * Format the table so that each row represents column and each column represents row * * @param {IPrintColumnOptions} [opts] * @returns {String} */ public printTransposed(opts?: PrintColumnOptions): string; /** * Sort the table * * @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on * @returns {Table} `this` */ public sort(cmp?: string[]): EasyTable; /** * Sort the table * * @param {Function|string[]} [cmp] - Either compare function or a list of columns to sort on * @returns {Table} `this` */ public sort(cmp?: CompareFunction): EasyTable; /** * Add a total for the column * * @param {String} col - column name * @param {Object} [opts] * @returns {Table} `this` */ public total(col: string, opts?: TotalOptions): EasyTable; /** * Predefined helpers for totals */ public static aggr: Aggregators; /** * Print the array or object * * @param {Array|Object} obj - Object to print * @param {Function|Object} [format] - Format options * @param {Function} [cb] - Table post processing and formating * @returns {String} */ public static print(obj: T | T[], format?: FormatFunction | FormatObject, cb?: TablePostProcessing): string; /** * Same as `Table.print()` but yields the result to `console.log()` */ public static log(obj: T | T[], format?: FormatFunction | FormatObject, cb?: TablePostProcessing): void; /** * Same as `.toString()` but yields the result to `console.log()` */ public log(): void; } type CellPrinter = (val: T, width: number) => string; type CompareFunction = (a: T, b: T) => number; type ReduceFunction = (acc: T, val: T, idx: number, length: number) => T; type FormatFunction = (obj: T, cell: (name: string, val: any) => void) => void; type TablePostProcessing = (result: EasyTable) => string; interface PrintColumnOptions { /** * Column separation string */ separator?: string; /** * Printer to format column names */ namePrinter?: CellPrinter; } interface Aggregators { /** * Create a printer which formats the value with `printer`, * adds the `prefix` to it and right aligns the whole thing * * @param {String} prefix * @param {Function} printer * @returns {printer} */ printer(prefix: string, printer: CellPrinter): CellPrinter; /** * Sum reduction */ sum: any; /** * Average reduction */ avg: any; } interface TotalOptions { /** * reduce(acc, val, idx, length) function to compute the total value */ reduce?: ReduceFunction; /** * Printer to format the total cell */ printer?: CellPrinter; /** * Initial value for reduction */ init?: T; } interface FormatObject { [key: string]: ColumnFormat; } interface ColumnFormat { name?: string; printer?: CellPrinter } export = EasyTable;