-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablify.js
More file actions
executable file
·175 lines (149 loc) · 3.96 KB
/
Copy pathtablify.js
File metadata and controls
executable file
·175 lines (149 loc) · 3.96 KB
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
#!/usr/bin/env node
const Parser = require('stream-json/Parser');
const StreamObject = require('stream-json/streamers/StreamObject');
const {Transform, Writable} = require('stream');
const {'default': createStream} = require('table/dist/createStream');
function WriteTableStream(options) {
if (!(this instanceof WriteTableStream)) {
return new WriteTableStream(options);
}
options = Object.assign({objectMode: true}, options);
Writable.call(this, options);
this._tableStream = createStream(options);
}
WriteTableStream.super_ = Writable;
WriteTableStream.prototype = Object.create(Writable.prototype, {
constructor: {
configurable: true,
enumerable: false,
value: WriteTableStream,
writable: true
}
});
WriteTableStream.prototype._write = function(chunk, encoding, next) {
let err = null;
try {
this._tableStream.write(chunk);
} catch (e) {
err = e;
}
next(err);
};
WriteTableStream.prototype._final = function(done) {
process.stdout.write('\n', done);
};
{
const onerror = (message=false, stack=true) => {
return function(err) {
if (message && err.message) {
console.error(err.message);
}
if (stack && err.stack) {
console.error(err.stack);
}
if (!(err instanceof Error)) {
console.error(err);
}
this.end();
};
};
let headerRow = ['WR %', 'W', 'L', 'B', 'Name'];
let rowKeys = ['percentage', 'wins', 'losses', 'battles', 'name'];
let options = {
columnDefault: {
alignment: 'right',
width: 5
},
columnCount: 5,
columns: {
0: {
width: 7
},
4: {
alignment: 'left',
width: 27
}
}
};
process.stdin
.pipe(new Parser())
.once('error', onerror())
.pipe(new StreamObject({
objectFilter({current}) {
if (current && 'battles' in current) {
return current.battles > 0;
}
}
}))
.once('error', onerror())
.pipe(new Transform({
objectMode: true,
transform(chunk, enc, next) {
let err = null;
let row = null;
try {
row = rowify(chunk.value, {
columns: rowKeys,
custom: {
name: winRateColor(chunk.value)(chunk.key),
percentage: percentage(chunk.value)
}
});
} catch (e) {
err = e;
}
if (headerRow) {
if (!err) {
this.push(headerRow);
}
headerRow = null;
}
next(err, row);
}
}))
.once('error', onerror())
.pipe(new WriteTableStream(options))
.once('error', onerror(true, false));
}
/**
* Convert a flat object to an array of values suitable for use with [table]{@link https://npmjs.com/package/table}.
*
* @param {Object} obj source of plain values
* @param {Object} [options]
* @param {string[]} [options.columns] the key names to use. Order is respected. Default is the result of `Object.keys(obj)`
* @param {Object} [options.custom=null] additional values to use. Keys must not overlap with source obj.
* @param {*} [options.default=''] value to use if a column name is not found in either the source obj or additional custom values.
* @returns {Array} values in the same order as the specified columns
*/
function rowify(obj, options) {
const columns = options && options.columns || Object.keys(obj);
const custom = options && options.custom;
const missingValue = options && options['default'] || '';
const row = [];
// add keys if no columns were specified
if (custom && !options.columns) {
columns.push(...Object.keys(custom));
}
for (const column of columns) {
if (obj.hasOwnProperty(column)) {
row.push(obj[column]);
} else if (custom && custom.hasOwnProperty(column)) {
row.push(custom[column]);
} else {
row.push(missingValue);
}
}
return row;
}
function percentage({wins, battles}, decimals=2) {
return (wins / battles * 100).toFixed(decimals) + '%';
}
function winRateColor({wins, battles}) {
const p = wins / battles;
let color = 35;
if (p < 0.36333) { color = 31; } else
if (p < 0.49999) { color = 33; } else
if (p < 0.66666) { color = 32; } else
if (p < 0.74999) { color = 34; }
return s => `\u001b[${color}m${s}\u001b[0m`;
}