diff --git a/easy-table/easy-table-tests.ts b/easy-table/easy-table-tests.ts index 732426064..56fdb7e3b 100644 --- a/easy-table/easy-table-tests.ts +++ b/easy-table/easy-table-tests.ts @@ -1,15 +1,105 @@ -/// +/// -import EasyTable = require('easy-table'); +//import * as Table from "easy-table"; +//import * as Table from "easy-table"; -var table = new EasyTable(); +import Table = require("easy-table"); +let data = [ + { id: 123123, desc: 'Something awesome', price: 1000.00 }, + { id: 245452, desc: 'Very interesting book', price: 11.45 }, + { id: 232323, desc: 'Yet another product', price: 555.55 } +]; -table.cell('aa', 1); -table.cell('bb',1); -table.newRow(); +interface IData { + id: number; + desc: string; + price: number; +} -table.cell('aa', 1); -table.cell('bb',1); +function sample_test() { + let t = new Table(); + data.forEach(function(product) { + t.cell('Product Id', product.id); + t.cell('Description', product.desc); + t.cell('Price, USD', product.price, Table.number(2)); + t.newRow(); + }); + console.log(t.toString()); +} -table.print(); +function static_print() { + console.log(Table.print(data)); +} +function currency(val: number, width?: number) { + var str = val.toFixed(2); + return width ? str : Table.padLeft(str, width); +} + +function sample_2() { + Table.print(data, { + desc: { name: 'description' }, + price: { printer: Table.number(2) } + }); +} + +function sample_3() { + Table.print(data, function(item, cell) { + cell('Product id', item.id) + cell('Price, USD', item.price) + }, function(table) { + return table.print() + }) +} + +function sample_4() { + Table.print(data[0]); +} + +function sort_strings() { + let t = new Table(); + t.sort(['Price, USD|des']) // will sort in descending order + t.sort(['Price, USD|asc']) // will sort in ascending order + t.sort(['Price, USD']) // sorts in ascending order by default +} + +function totalling() { + let t = new Table(); + t.total('Price, USD'); + t.total('Price, USD', { + printer: Table.aggr.printer('Avg: ', currency), + reduce: Table.aggr.avg, + init: 0 + }) + + // or alternatively + t.total('Price, USD', { + printer: (val, width) => { + return Table.padLeft('Avg: ' + currency(val), width); + }, + reduce: (acc: number, val: number, idx: number, len: number) => { + acc = acc + val; + return idx + 1 == len ? acc / len : acc; + } + }); +} + +function other_samples() { + var t = new Table(); + + data.forEach(product => { + t.cell('Product Id', product.id) + t.cell('Description', product.desc) + t.cell('Price, USD', product.price, Table.number(2)) + t.newRow() + }) + + t.sort(['Price, USD']) + t.total('Price, USD', { + printer: Table.number(2) + }) + + t.log() + Table.log(data, { price: { printer: Table.number(2) } }) + Table.log(data[0]) +} diff --git a/easy-table/easy-table.d.ts b/easy-table/easy-table.d.ts index 912b089ce..9995585fe 100644 --- a/easy-table/easy-table.d.ts +++ b/easy-table/easy-table.d.ts @@ -1,40 +1,222 @@ -// Type definitions for easy-table 0.2.0 +// Type definitions for easy-table // Project: https://github.com/eldargab/easy-table -// Definitions by: Bart van der Schoor +// Definitions by: Niklas Mollenhauer // Definitions: https://github.com/borisyankov/DefinitelyTyped -declare module "easy-table" { +declare module "easy-table" +{ class EasyTable { - constructor(); - cell(label: string, value: any, printer?: EasyTable.CellPrinter, width?: number):void; - newRow(): void; - toString(): string; - printTransposed(): string; - print(): string; - sort(fields: string): void; - sort(comparer: (a: any, b: any) => number): void; - total(label: string, accumulator: EasyTable.Accumulator, totalPrinter: EasyTable.CellPrinter): void; + /** + * 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: number): CellPrinter; + + public static padLeft: CellPrinter; + + /** + * Create a printer which pads with `ch` on the right + * + * @param {String} ch + * @returns {Function} + */ + public static rightPadder(ch: number): 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?: IPrintColumnOptions): 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?: ITotalOptions): EasyTable; + /** + * Predefined helpers for totals + */ + public static aggr: IAggregators; + + /** + * 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 | IFormatObject, cb?: TablePostProcessing): string; + + /** + * Same as `Table.print()` but yields the result to `console.log()` + */ + public static log(obj: T | T[], format?: FormatFunction | IFormatObject, cb?: TablePostProcessing): void; + /** + * Same as `.toString()` but yields the result to `console.log()` + */ + public log(): void; } - module EasyTable { - function printArray(array: any[], cellPrinter?: CellPrinter, tablePrinter?: Printer): string; - function printObject(object: any, cellPrinter?: CellPrinter, tablePrinter?: Printer): string; + 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; - //printer helpers - function Number(length: number): CellPrinter; - function RightPadder(char: string): CellPrinter; - function LeftPadder(char: string): CellPrinter; + interface IPrintColumnOptions { + /** + * Column separation string + */ + separator?: string; + /** + * Printer to format column names + */ + namePrinter?: CellPrinter; + } - interface CellPrinter extends Function { - (obj: any, cell: (label: string, value: any, width?: number) => void):string; - } - interface Printer extends Function { - (table: EasyTable):string; - } - interface Accumulator extends Function { - (sum: number, val: number, index: number, length: number):number; - } + interface IAggregators { + /** + * 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 ITotalOptions { + /** + * 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 IFormatObject { + [key: string]: IColumnFormat; + } + + interface IColumnFormat { + name?: string; + printer?: CellPrinter } export = EasyTable;