From ded3d2beacbe1fd5e47e58d5c51c66093235d11d Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Fri, 9 Oct 2015 07:23:10 +0200 Subject: [PATCH 1/5] Move 0.2.0 to own definition files --- easy-table/easy-table-0.2.0-tests.ts | 15 ++++++++++ easy-table/easy-table-0.2.0.d.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 easy-table/easy-table-0.2.0-tests.ts create mode 100644 easy-table/easy-table-0.2.0.d.ts diff --git a/easy-table/easy-table-0.2.0-tests.ts b/easy-table/easy-table-0.2.0-tests.ts new file mode 100644 index 000000000..732426064 --- /dev/null +++ b/easy-table/easy-table-0.2.0-tests.ts @@ -0,0 +1,15 @@ +/// + +import EasyTable = require('easy-table'); + +var table = new EasyTable(); + +table.cell('aa', 1); +table.cell('bb',1); +table.newRow(); + +table.cell('aa', 1); +table.cell('bb',1); + +table.print(); + diff --git a/easy-table/easy-table-0.2.0.d.ts b/easy-table/easy-table-0.2.0.d.ts new file mode 100644 index 000000000..912b089ce --- /dev/null +++ b/easy-table/easy-table-0.2.0.d.ts @@ -0,0 +1,41 @@ +// Type definitions for easy-table 0.2.0 +// Project: https://github.com/eldargab/easy-table +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +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; + } + + module EasyTable { + function printArray(array: any[], cellPrinter?: CellPrinter, tablePrinter?: Printer): string; + function printObject(object: any, cellPrinter?: CellPrinter, tablePrinter?: Printer): string; + + //printer helpers + function Number(length: number): CellPrinter; + function RightPadder(char: string): CellPrinter; + function LeftPadder(char: string): 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; + } + } + + export = EasyTable; +} From de3902042aee528c2c711e78d798e8664b5d26b5 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Fri, 9 Oct 2015 07:23:46 +0200 Subject: [PATCH 2/5] Add rewritten definitions for 1.0.0 --- easy-table/easy-table-tests.ts | 108 +++++++++++++-- easy-table/easy-table.d.ts | 238 +++++++++++++++++++++++++++++---- 2 files changed, 309 insertions(+), 37 deletions(-) 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; From 7148685570bd72ea373c7b7f6ec75d67509fa4af Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Fri, 9 Oct 2015 07:32:32 +0200 Subject: [PATCH 3/5] Remove comments an new line --- easy-table/easy-table-0.2.0-tests.ts | 1 - easy-table/easy-table-tests.ts | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/easy-table/easy-table-0.2.0-tests.ts b/easy-table/easy-table-0.2.0-tests.ts index 732426064..93a4516bf 100644 --- a/easy-table/easy-table-0.2.0-tests.ts +++ b/easy-table/easy-table-0.2.0-tests.ts @@ -12,4 +12,3 @@ table.cell('aa', 1); table.cell('bb',1); table.print(); - diff --git a/easy-table/easy-table-tests.ts b/easy-table/easy-table-tests.ts index 56fdb7e3b..62bd78008 100644 --- a/easy-table/easy-table-tests.ts +++ b/easy-table/easy-table-tests.ts @@ -1,9 +1,7 @@ /// -//import * as Table from "easy-table"; -//import * as Table from "easy-table"; - import Table = require("easy-table"); + let data = [ { id: 123123, desc: 'Something awesome', price: 1000.00 }, { id: 245452, desc: 'Very interesting book', price: 11.45 }, From a5496bc364b7fed8879dca625127cdab631d70c1 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Mon, 12 Oct 2015 20:21:22 +0200 Subject: [PATCH 4/5] Remove I prefix from interface names --- easy-table/easy-table-tests.ts | 4 ++-- easy-table/easy-table.d.ts | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/easy-table/easy-table-tests.ts b/easy-table/easy-table-tests.ts index 62bd78008..911031d90 100644 --- a/easy-table/easy-table-tests.ts +++ b/easy-table/easy-table-tests.ts @@ -8,7 +8,7 @@ let data = [ { id: 232323, desc: 'Yet another product', price: 555.55 } ]; -interface IData { +interface Data { id: number; desc: string; price: number; @@ -35,7 +35,7 @@ function currency(val: number, width?: number) { } function sample_2() { - Table.print(data, { + Table.print(data, { desc: { name: 'description' }, price: { printer: Table.number(2) } }); diff --git a/easy-table/easy-table.d.ts b/easy-table/easy-table.d.ts index 9995585fe..e06cfdd27 100644 --- a/easy-table/easy-table.d.ts +++ b/easy-table/easy-table.d.ts @@ -108,7 +108,7 @@ declare module "easy-table" * @param {IPrintColumnOptions} [opts] * @returns {String} */ - public printTransposed(opts?: IPrintColumnOptions): string; + public printTransposed(opts?: PrintColumnOptions): string; /** * Sort the table @@ -132,11 +132,11 @@ declare module "easy-table" * @param {Object} [opts] * @returns {Table} `this` */ - public total(col: string, opts?: ITotalOptions): EasyTable; + public total(col: string, opts?: TotalOptions): EasyTable; /** * Predefined helpers for totals */ - public static aggr: IAggregators; + public static aggr: Aggregators; /** * Print the array or object @@ -146,12 +146,12 @@ declare module "easy-table" * @param {Function} [cb] - Table post processing and formating * @returns {String} */ - public static print(obj: T | T[], format?: FormatFunction | IFormatObject, cb?: TablePostProcessing): 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 | IFormatObject, cb?: TablePostProcessing): void; + public static log(obj: T | T[], format?: FormatFunction | FormatObject, cb?: TablePostProcessing): void; /** * Same as `.toString()` but yields the result to `console.log()` */ @@ -164,7 +164,7 @@ declare module "easy-table" type FormatFunction = (obj: T, cell: (name: string, val: any) => void) => void; type TablePostProcessing = (result: EasyTable) => string; - interface IPrintColumnOptions { + interface PrintColumnOptions { /** * Column separation string */ @@ -175,7 +175,7 @@ declare module "easy-table" namePrinter?: CellPrinter; } - interface IAggregators { + interface Aggregators { /** * Create a printer which formats the value with `printer`, * adds the `prefix` to it and right aligns the whole thing @@ -195,7 +195,7 @@ declare module "easy-table" avg: any; } - interface ITotalOptions { + interface TotalOptions { /** * reduce(acc, val, idx, length) function to compute the total value */ @@ -210,11 +210,11 @@ declare module "easy-table" init?: T; } - interface IFormatObject { - [key: string]: IColumnFormat; + interface FormatObject { + [key: string]: ColumnFormat; } - interface IColumnFormat { + interface ColumnFormat { name?: string; printer?: CellPrinter } From 60d9ffa196e8c551c7ebbbdcdcfe4d870cf77f70 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Tue, 13 Oct 2015 07:20:11 +0200 Subject: [PATCH 5/5] Fix parameter types of padders --- easy-table/easy-table.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/easy-table/easy-table.d.ts b/easy-table/easy-table.d.ts index e06cfdd27..60bcb0457 100644 --- a/easy-table/easy-table.d.ts +++ b/easy-table/easy-table.d.ts @@ -23,7 +23,7 @@ declare module "easy-table" * @param {String} ch * @returns {Function} */ - public static leftPadder(ch: number): CellPrinter; + public static leftPadder(ch: string): CellPrinter; public static padLeft: CellPrinter; @@ -33,9 +33,9 @@ declare module "easy-table" * @param {String} ch * @returns {Function} */ - public static rightPadder(ch: number): CellPrinter; + public static rightPadder(ch: string): CellPrinter; - public static padRight: CellPrinter; + // public static padRight: CellPrinter; /** * Create a printer for numbers