-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstyles-and-formatting.js
executable file
·181 lines (163 loc) · 4.19 KB
/
styles-and-formatting.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require("../test/example-utils.js").quickInit()
const Table = require("../")
const header = [
{
value: "item",
headerColor: "cyan",
color: "white",
align: "left",
width: "20%"
},
{
value: "description",
width: "40%",
headerColor: "magenta",
color: "yellow"
},
{
value: "peru_price",
alias: "peru",
color: "red",
width: "15%",
formatter: function (value) {
const str = `$${value.toFixed(2)}`
return (value > 5) ? this.style(str, "green", "bold")
: this.style(str, "red", "bold")
}
},
{
value: "us_price",
alias: "usa",
color: "red",
width: "15%",
formatter: function (value) {
const str = `$${value.toFixed(2)}`
return (value > 5) ? this.style(str, "green", "bold")
: this.style(str, "red", "underline")
}
},
{
alias: "vegan",
value: "organic",
align: "right",
width: "10%",
formatter: function (value) {
if (value === "yes") {
value = this.style(value, "bgGreen", "black")
} else {
value = this.style(value, "bgRed", "white")
}
return value
}
}
]
// Example with objects as rows
const rows = [
{
item: "tallarin verde",
description: "Peruvian version of spaghetti with pesto. Includes a thin piece of fried chicken breast",
peru_price: 2.50,
us_price: 15.50,
organic: "no"
},
{
item: "aji de gallina",
description: "Heavy aji cream sauce, rice, and chicken with a halved hard-boiled egg",
peru_price: 1.80,
us_price: 14.50,
organic: "no"
}
]
// Example with arrays as rows
const rows2 = [
["tallarin verde", 2.50, 15.50, "no"],
["aji de gallina", 1.80, 14.50, "no"]
].map((arr, index) => {
arr.splice(1, 0, rows[index].description); return arr
})
const footer = [
"TOTAL",
"",
function (cellValue, columnIndex, rowIndex, rowData) {
const total = rowData.reduce((prev, curr) => {
return prev + curr[2]
}, 0)
.toFixed(2)
return this.style(`$${total}`, "italic")
},
function (cellValue, columnIndex, rowIndex, rowData) {
const total = rowData.reduce((prev, curr) => {
return prev + curr[3]
}, 0)
.toFixed(2)
return this.style(`$${total}`, "italic")
},
function (cellValue, columnIndex, rowIndex, rowData) {
const total = rowData.reduce((prev, curr) => {
return prev + ((curr[4] === "yes") ? 1 : 0)
}, 0)
return `${(total / rowData.length * 100).toFixed(2)}%`
}
]
const options = {
borderStyle: "solid",
borderColor: "green",
paddingBottom: 0,
headerAlign: "center",
headerColor: "green",
align: "center",
color: "white",
width: "80%"
}
const t1 = Table(header, rows, footer, options).render()
console.log(t1)
const t2 = Table(header, rows2, footer, options).render()
console.log(t2)
const header3 = [
{
value: "price",
formatter: function (cellValue, columnIndex, rowIndex, rowData, inputData) {
const row = inputData[rowIndex] // How to get the whole row
let _color
if (!row.enabled) _color = "gray"
if (row.important) _color = "red"
return this.style(cellValue, _color)
}
},
{
value: "item",
color: "underline",
formatter: function (cellValue) {
return (/banana/.test(cellValue)) ? this.resetStyle(cellValue) : cellValue
}
}
]
const rows3 = [
{
price: 1.99,
item: "[32m[37m[41m banana[49m[32m[39m",
important: true,
enabled: true
},
{
price: 2.99,
item: "[32m[37m[41m grapes[49m[32m[39m",
important: false,
enabled: false
}
]
const t3 = Table(header3, rows3, { width: 50, borderStyle: "none", compact: true }).render()
console.log(t3)
const t4 = Table(header3, rows3, { width: 50, paddingTop: 2, paddingBottom: 2 }).render()
console.log(t4)
header3[0].alias = header3[0].value
header3[1].alias = header3[1].value
delete header3[0].value
delete header3[1].value
const t5 = Table(header3, rows3, { width: 50, paddingTop: 2, paddingBottom: 2 }).render()
console.log(t5)
delete header3[0].alias
delete header3[1].alias
// will have a different column width because no header to derive from
const t6 = Table(header3, rows3, { width: 50, paddingTop: 2, paddingBottom: 2 }).render()
console.log(t6)