forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-table-tests.ts
103 lines (87 loc) · 2.19 KB
/
easy-table-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
///<reference path="easy-table.d.ts" />
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 }
];
interface Data {
id: number;
desc: string;
price: number;
}
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());
}
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>(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])
}