-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
77 lines (58 loc) · 2.38 KB
/
index.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
'use strict';
var _ = require('lodash');
module.exports = function (inputObj, opts) {
// Throw error if `inputObj` does not exist, is an invalid type, or is empty.
if ( typeof inputObj !== 'object' || inputObj === null || Object.keys( inputObj ).length === 0 ) {
throw new Error( 'Whoops! Please ensure that `barHorizontal()` is invoked with a non-empty array or object.' );
}
// Ensure that `opts` is always an object.
opts = (opts && typeof opts === 'object') ? opts : {};
var width = (opts.width && typeof opts.width === 'number') ? opts.width : require('window-size').width;
width = (typeof width === 'number' && width > 0) ? width : 80;
var barChar = opts.ascii ? '=' : require('figures').square;
var maxBarWidth = width - (width > 20 ? 20 : 1);
var totalSum = 0.0;
var percentage = [];
var barWidths = [];
var values = _.values(inputObj);
var keys = _.keys(inputObj);
var printValues = opts.values;
totalSum = _.sum(values);
percentage = _.map(values, (elem) => elem / totalSum * 100);
var maxPercentage = _.max(percentage);
var fixedLabels = [];
var labelsTrue = opts.labels || false;
var maxLabelLength = 0;
if (labelsTrue) {
maxLabelLength = _.max(_.map(keys, e => e.toString().length));
fixedLabels = _.map(inputObj, (value, key) => {
var padLength = maxLabelLength - key.toString().length + 1;
return padLength > 0 ? (key + new Array(padLength).join(' ')) : '';
});
}
maxBarWidth = labelsTrue ? (maxBarWidth - maxLabelLength) : maxBarWidth;
barWidths = _.map(percentage, elem => {
var potentialWidth = Math.ceil(elem / maxPercentage * maxBarWidth);
return (potentialWidth <= 1 ? 0 : potentialWidth);
});
var results = _.map(percentage, function (element, index, array) {
var elementLine = (labelsTrue ? fixedLabels[index] : '');
elementLine += ' : ';
elementLine += new Array(barWidths[index]).join(barChar);
elementLine += ' ';
elementLine += (printValues ? (values[index].toFixed(2)) : (element.toFixed(2) + '%'));
return elementLine;
});
if (_.filter(barWidths, x => x == 0).length > 0 && opts.warnings) {
var message = "This terminal's width is too less!";
if (labelsTrue) {
message += ' You can remove labels, and try again!';
}
results.push(message);
}
if (!opts.noPrint) {
console.log(results.join('\n'));
} else {
return results.join('\n');
}
};